added dac support
This commit is contained in:
77
Src/main.c
77
Src/main.c
@ -103,6 +103,8 @@
|
||||
#define DS1809_FLAG_UC 0x0001u
|
||||
#define DS1809_FLAG_DC 0x0002u
|
||||
#define DS1809_PULSE_MS_DEFAULT 2u
|
||||
#define STM32_DAC_FLAG_ENABLE 0x0001u
|
||||
#define STM32_DAC_CODE_MAX 4095u
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@ -224,7 +226,9 @@ static void SPI2_SetMode(uint32_t polarity, uint32_t phase);
|
||||
static void AD9833_WriteWord(uint16_t word);
|
||||
static void AD9833_Apply(uint8_t enable, uint8_t triangle, uint32_t freq_word);
|
||||
static void DS1809_Pulse(uint8_t uc, uint8_t dc, uint16_t count, uint16_t pulse_ms);
|
||||
uint8_t CheckChecksum(uint16_t *pbuff);
|
||||
static void PA4_DAC_Init(void);
|
||||
static void PA4_DAC_Set(uint16_t dac_code, uint8_t enable);
|
||||
uint8_t CheckChecksum(uint16_t *pbuff);
|
||||
uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len);
|
||||
//int SD_Init(void);
|
||||
int SD_SAVE(uint16_t *pbuff);
|
||||
@ -291,6 +295,7 @@ int main(void)
|
||||
MX_TIM11_Init();
|
||||
MX_TIM4_Init();
|
||||
MX_TIM1_Init();
|
||||
PA4_DAC_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
Init_params();
|
||||
//HAL_TIM_Base_Start(&htim11);
|
||||
@ -629,7 +634,22 @@ int main(void)
|
||||
UART_transmission_request = MESS_01;
|
||||
CPU_state = CPU_state_old;
|
||||
break;
|
||||
case DECODE_TASK:
|
||||
case STM32_DAC_CMD://13 - Set STM32 internal DAC (PA4)
|
||||
if (CalculateChecksum(COMMAND, STM32_DAC_CMD_WORDS - 1) == COMMAND[STM32_DAC_CMD_WORDS - 1])
|
||||
{
|
||||
uint16_t flags = COMMAND[0];
|
||||
uint16_t dac_code = (uint16_t)(COMMAND[1] & 0x0FFFu);
|
||||
uint8_t enable = (flags & STM32_DAC_FLAG_ENABLE) ? 1u : 0u;
|
||||
PA4_DAC_Set(dac_code, enable);
|
||||
}
|
||||
else
|
||||
{
|
||||
State_Data[0] |= UART_DECODE_ERR;
|
||||
}
|
||||
UART_transmission_request = MESS_01;
|
||||
CPU_state = CPU_state_old;
|
||||
break;
|
||||
case DECODE_TASK:
|
||||
if (CheckChecksum(COMMAND))
|
||||
{
|
||||
Decode_task(COMMAND, &LD1_curr_setup, &LD2_curr_setup, &Curr_setup);
|
||||
@ -2140,10 +2160,8 @@ static void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
HAL_GPIO_Init(EN_5V1_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : TECEN1_Pin TECEN2_Pin REF2_ON_Pin DAC_TEC2_CS_Pin
|
||||
DAC_LD2_CS_Pin */
|
||||
GPIO_InitStruct.Pin = TECEN1_Pin|TECEN2_Pin|REF2_ON_Pin|DAC_TEC2_CS_Pin
|
||||
|DAC_LD2_CS_Pin;
|
||||
/*Configure GPIO pins : TECEN1_Pin TECEN2_Pin REF2_ON_Pin DAC_LD2_CS_Pin */
|
||||
GPIO_InitStruct.Pin = TECEN1_Pin|TECEN2_Pin|REF2_ON_Pin|DAC_LD2_CS_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
@ -2155,8 +2173,8 @@ static void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : ADC_MPD1_CS_Pin ADC_ThrLD1_CS_Pin */
|
||||
GPIO_InitStruct.Pin = ADC_MPD1_CS_Pin|ADC_ThrLD1_CS_Pin;
|
||||
/*Configure GPIO pins : ADC_MPD1_CS_Pin ADC_ThrLD1_CS_Pin DAC_TEC2_CS_Pin */
|
||||
GPIO_InitStruct.Pin = ADC_MPD1_CS_Pin|ADC_ThrLD1_CS_Pin|DAC_TEC2_CS_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
@ -2329,9 +2347,10 @@ static void Init_params(void)
|
||||
HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_SET);//End operation with LDAC1
|
||||
HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_SET);//End operation with LDAC2
|
||||
HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_SET);//End operation with TEC1 DAC
|
||||
HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_SET);//End operation with TEC2 DAC
|
||||
HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_SET);//End operation with LDAC2
|
||||
HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_SET);//End operation with TEC1 DAC
|
||||
HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_SET);//End operation with TEC2 DAC
|
||||
PA4_DAC_Set(0u, 0u);
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
//test = 11;
|
||||
@ -2696,6 +2715,42 @@ static void AD9833_Apply(uint8_t enable, uint8_t triangle, uint32_t freq_word)
|
||||
AD9833_WriteWord(control);
|
||||
}
|
||||
|
||||
static void PA4_DAC_Init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
__HAL_RCC_DAC_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_4;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
// Keep channel disabled until a dedicated serial command enables it.
|
||||
DAC->CR &= ~(DAC_CR_EN1 | DAC_CR_TEN1 | DAC_CR_DMAEN1);
|
||||
DAC->DHR12R1 = 0u;
|
||||
}
|
||||
|
||||
static void PA4_DAC_Set(uint16_t dac_code, uint8_t enable)
|
||||
{
|
||||
if (dac_code > STM32_DAC_CODE_MAX)
|
||||
{
|
||||
dac_code = STM32_DAC_CODE_MAX;
|
||||
}
|
||||
|
||||
DAC->DHR12R1 = dac_code;
|
||||
|
||||
if (enable)
|
||||
{
|
||||
DAC->CR |= DAC_CR_EN1;
|
||||
}
|
||||
else
|
||||
{
|
||||
DAC->CR &= ~DAC_CR_EN1;
|
||||
}
|
||||
}
|
||||
|
||||
static void DS1809_Pulse(uint8_t uc, uint8_t dc, uint16_t count, uint16_t pulse_ms)
|
||||
{
|
||||
for (uint16_t i = 0; i < count; i++)
|
||||
|
||||
Reference in New Issue
Block a user