一、数字模拟转换DAC
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cbcb147a86d0535c6f0c59fcd7b50046.png)
二、DAC
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/af40b0078b48fa5aa00641d2a4f0de3c.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/27af4ff4144555338cf52d029d2963f5.png)
三、test、DAC输出->ADC输入
void STM_DAC_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
}
int main(void)
{
USART1_init(115200);
uint16_t adc_val,adc_vol,sum,i;
ADC1_CH5_init();
STM_DAC_init();
DAC_SetChannel1Data(DAC_Align_12b_R, 1800*4095/3300);
ADC_SoftwareStartConv(ADC1);
while(1)
{
sum = 0;
for(i = 0; i<1000; i++)
{
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
adc_val = ADC_GetConversionValue(ADC1);
adc_vol = adc_val*3300/4095;
sum+=adc_vol;
}
adc_vol = (sum/1000);
printf("adc_val = %dmv", adc_val);
printf("adc_vol = %dmv\r\n", adc_vol);
delay_ms(1000);
}
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nkk25ug3-1666001256334)(.\img\16-4.png)]](https://i-blog.csdnimg.cn/blog_migrate/bec4ee7bb9018a02d44a4420f93a9085.png)