提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:这里可以添加本文要记录的大概内容:
前面学习了STM32的ADC,掌握了模拟信号如何转化成数字信号,现在学习如何将数字信号转换成模拟信号。
目标:串口输入3.3v->stm32->DAC->3.3V ->ADC ->串口 ->电压
举例: 3.3v -> "3.3v"字符串->解释前面的数字 3.3怎么给DAC
具体:(ADC) PA0 ->(DAC) ?
提示:以下是本篇文章正文内容,下面案例可供参考
示例:DAC是数字模拟转换器(Digital to Analog Converter)的缩写,它是一种将数字信号转换为模拟信号的设备。
DAC数模转换实验工作原理
https://www.bilibili.com/video/BV11Z4y1z7mP/?spm_id_from=333.337.search-card.all.click
查看数据手册
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
GPIO_InitStruct.GPIO_Pin= GPIO_Pin_4; //
GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; //模拟输入
GPIO_Init(GPIOA, &GPIO_InitStruct); //&x
//3.初始化DAC
//DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude=
DAC_InitStruct.DAC_OutputBuffer=DAC_OutputBuffer_Disable;
DAC_InitStruct.DAC_Trigger=DAC_Trigger_Software; //软件触发
DAC_InitStruct.DAC_WaveGeneration=DAC_WaveGeneration_None;
DAC_Init(DAC_Channel_1,&DAC_InitStruct);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R, 2048); // 12位 电压范围 0-4095
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);//软件使能转换
完整初始化参考
void dac_init()
{
GPIO_InitTypeDef GPIO_InitStruct;
DAC_InitTypeDef DAC_InitStruct;
//1.开时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA,ENABLE);
//2. 配置位模拟输入
GPIO_InitStruct.GPIO_Pin= GPIO_Pin_4; //
GPIO_InitStruct.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN; //模拟输入
GPIO_Init(GPIOA, &GPIO_InitStruct); //&x
//3.初始化DAC
//DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude=
DAC_InitStruct.DAC_OutputBuffer=DAC_OutputBuffer_Disable;
DAC_InitStruct.DAC_Trigger=DAC_Trigger_Software; //软件触发
DAC_InitStruct.DAC_WaveGeneration=DAC_WaveGeneration_None;
DAC_Init(DAC_Channel_1,&DAC_InitStruct);
// 4. 使能DAC
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R, 2048); // 12位 电压范围 0-4095
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);//软件使能转换
}
程序框架
void main
{
ADC_init();
uart_init();
DAC_init();
while(1)
{
//adc 读取
//发送串口
}
}
“DAC输出:3.3v” 则输出3.3v
“DAC输出:1.65v” 则输出1.65v
“DAC输出:3300mv” 则输出3300mv
“DAC输出:1650v” 则输出1650mv
串口中断获取
举例
“DAC输出:3.3v” 在字符串种查找 DAC关键字,然后再找到 ':'从这里 获取后面的’3.3’字符,直到’v’停止将获取的字符 3.3 转换成实际数字,
3.3 在转换成数字电压
字符转整型STM32史上最短 字符转整形 简简单单
https://blog.csdn.net/weixin_51102592/article/details/121125622?app_version=6.2.4&code=app_1562916241&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22121125622%22%2C%22source%22%3A%22ganhui13000%22%7D&uLinkId=usr1mkqgl919blen&utm_source=app
#include "string.h"
#include "stdlib.h"
u8 DATA[200]="X12Y34Y56Y78";
int shuju_chuli(const char *shuju,char zifu )
{
char *shu_ju_1=strchr(shuju,zifu)+1; //数据
return atoi(shu_ju_1); //数值
}
int shuju_chuli1(const char *shuju,char zifu )
{
char *shu_ju_1=strchr(shuju,zifu)+1; //数据
char *shu_ju_2=strchr(shu_ju_1,zifu)+1; //数据
return atoi(shu_ju_2); //数值
}
printf("X是:%d\r\n",shuju_chuli((const char*)DATA,'X'));
printf("y是:%d\r\n",shuju_chuli((const char*)DATA,'Y'));
printf("Y1是:%d\r\n",shuju_chuli1((const char*)DATA,'Y'));
DAC的原理,STM32DAC的特点,STM32DAC实操,用ADC和DAC闭环,实现了ADC和DAC 的检测。