一、本节内容
把电位器的检测数据,实现数字量到物理量的转换,保留两位小数点,将转换的信息通过串口,假设数字0表示阀门开度0%,4095表示阀门开度100%,发送到电脑
提示:
1. 换行串口输出 ‘\r’ ’\n’ 两个字符即可
2. 0.201214597 如何通过串口输出0.20V,也就是连续输出 ‘0’ ‘.’ ‘2’ ‘0’ ‘V’
3. 字符‘2’计算公式为2+’0’
二、原理讲解
本节先使用syscfg配置ADC,UART串口,然后使用直接生成的初始化代码,再设置两个参数分别保存两个ADC的值,当ADC接收到数据的时候,进行保存,并将结果处理发送到串口上。
三、库函数
部分库函数和寄存器:
学习MSP432M0手册——第五节systick和UART接收
CTL1中的VREF位未设置,模块尚未准备好。
开启ADC转换,输入值:指向外围设备adc12寄存器的指针。
返回选定内存索引的转换结果。
输入值:adc12 指向外设的寄存器覆盖的指针、idx 内存转换索引。
返回值:转换结果
获取优先级最高的待处理ADC12中断。
检查是否存在任何待处理的ADC12中断。中断无需先前已启用。
输入值:adc12 指向外设的寄存器的指针
返回值:优先级最高的待处理ADC12中断之一,DL_ADC12_IIDX
四、软件代码
volatile bool gCheckADC;//确定ADC是否接收到数据
volatile uint16_t adcResult; //ADC保存值1
volatile uint16_t adcResult1; //ADC保存值2
int Tem,a=0,b=0,c=0,d=0,e=0;//将数据处理成百分率
int main(void)
{
// uint16_t adcResult;
SYSCFG_DL_init();//初始化
NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);//开启中断
gCheckADC = false;//ADC接收数据标志位清零
/* Confirm VREF has settled before triggering ADC12 conversion */
while (DL_VREF_CTL1_READY_NOTRDY == DL_VREF_getStatus(VREF))//当VREF未被设置,一直处于准备状态
;
while (1) {
DL_ADC12_startConversion(ADC12_0_INST);//软件开启ADC转换,SC=1
while (false == gCheckADC) {//等待转换完成,标志位为ture,继续运行
__WFE();
}
//保存ADC内存转换结果。
adcResult = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0);
adcResult1 = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_1);
//处理ADC数据成为百分比
Tem=adcResult*10000/4095;
a=Tem/10000;
b=Tem%10000/1000;
c=Tem%1000/100;
d=Tem%100/10;
e=Tem%10;
//发送数据
DL_UART_transmitData(UART0,'0'+a);
delay_cycles(100000);
DL_UART_transmitData(UART0,'0'+b);
delay_cycles(100000);
DL_UART_transmitData(UART0,'0'+c);
delay_cycles(100000);
DL_UART_transmitData(UART0,'.');
delay_cycles(100000);
DL_UART_transmitData(UART0,'0'+d);
delay_cycles(100000);
DL_UART_transmitData(UART0,'0'+e);
delay_cycles(100000);
DL_UART_transmitData(UART0,'%');
delay_cycles(100000);
DL_UART_transmitData(UART0,'\r');
delay_cycles(100000);
DL_UART_transmitData(UART0,'\n');
delay_cycles(5000000);
//点灯
if (adcResult > 0x7ff) {
DL_GPIO_clearPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
} else {
DL_GPIO_setPins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
}
//标志位清零
gCheckADC = false;
//启用ADC12转换功能
DL_ADC12_enableConversions(ADC12_0_INST);
}
}
//ADC12中断处理函数,设置标志位为ture
void ADC12_0_INST_IRQHandler(void)
{
//获取优先级最高的待处理ADC12中断。
switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) {
// case DL_ADC12_IIDX_MEM0_RESULT_LOADED:
case DL_ADC12_IIDX_MEM1_RESULT_LOADED:
gCheckADC = true;
break;
default:
break;
}
}
本节先使用syscfg配置ADC,UART串口,然后使用直接生成的初始化代码,再设置两个参数分别保存两个ADC的值,当ADC接收到数据的时候,进行保存,并将结果处理发送到串口上。
注意:需要提前设置ADC的VREF