编写读取数据的测试代码
修改main函数如下:
int main(void)
{
/* USER CODE BEGIN 1 */
uint16_t i = 0;
uint16_t adc_value = 0;
float vol = 0.0;
/* USER CODE END 1 */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DAC1_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
printf("DAC Test...\r\n");
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
for(i = 0; i < 4096; i++)
{
HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, i);
HAL_Delay(2);
if(i%1024 == 0)
{
/* 使用ADC采样 */
HAL_ADC_Start(&hadc1); //启动ADC单次转换
HAL_ADC_PollForConversion(&hadc1, 50); //等待ADC转换完成
adc_value = HAL_ADC_GetValue(&hadc1); //读取ADC转换数据
vol = ((double)adc_value/4096)*3.3;
printf("adc_value = %d, vol = %.2fV.\n", adc_value, vol);
}
}
printf("DAC test finish, test again!\r\n");
}
/* USER CODE END 3 */
}
|