学习MSP432M0手册——第六节ADC采样

本文介绍了如何使用MSP432M0微控制器通过ADC转换电位器信号,将模拟数据转换为数字并精确到两位小数,然后通过UART发送至电脑。涉及ADC配置、初始化、中断处理以及数据处理和串口通信的详细步骤。
摘要由CSDN通过智能技术生成

一、本节内容

把电位器的检测数据,实现数字量到物理量的转换,保留两位小数点,将转换的信息通过串口,假设数字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手册——第一节GPIO输出功能

学习MSP432M0手册——第二节GPIO输入和输出

学习MSP432M0手册——第三节GPIO中断

学习MSP432M0手册——第四节SDK和UART

学习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

MSP432 低功耗高性能并存10.1 Digital I/O Introduction The digital I/O features include: • Independently programmable individual I/Os • Any combination of input or output • Individually configurable interrupts for ports (available for certain ports only) • Independent input and output data registers • Individually configurable pullup or pulldown resistors • Wake-up capability from ultra-low power modes (available for certain ports only) • Individually configurable high drive I/Os (available for certain I/Os only) Devices within the family may have up to eleven digital I/O ports implemented (P1 to P10 and PJ). Most ports contain eight I/O lines; however, some ports may contain less (see the device-specific data sheet for ports available). Each I/O line is individually configurable for input or output direction, and each can be individually read or written. Each I/O line is individually configurable for pullup or pulldown resistors. Certain ports have interrupt and wake-up capability from ultra-low power modes (see device specific data sheet for ports with interrupt and wake-up capability). Each interrupt can be individually enabled and configured to provide an interrupt on a rising or falling edge of an input signal. All interrupts are fed into an encoded Interrupt Vector register, allowing the application to determine which sub-pin of a port has generated the event. Individual ports can be accessed as byte-wide ports or can be combined into half-word-wide ports. Port pairs P1 and P2, P3 and P4, P5 and P6, P7 and P8, and so on, are associated with the names PA, PB, PC, PD, and so on, respectively. All port registers are handled in this manner with this naming convention. The main exception are the interrupt vector registers, for example, interrupts for ports P1 and P2 must be handled through P1IV and P2IV, PAIV does not exist. When writing to port PA with half-word operations, all 16 bits are written to the port. When writing to the lower byte of port PA using byte operations, the upper byte remains unchanged. Similarly, writing to the upper byte of port PA using byte instructions leaves the lower byte unchanged. When writing to a port that contains less than the maximum number of bits possible, the unused bits are don't care. Ports PB, PC, PD, PE, and PF behave similarly.
要设置MSP432的定时器触发ADC采样频率,您需要使用MSP432标准库中的定时器模块和ADC模块。 首先,您需要初始化定时器和ADC模块。以下是一个示例代码片段,演示如何初始化定时器和ADC: ```c #include <ti/devices/msp432p4xx/driverlib/driverlib.h> // 定时器回调函数 void timerCallback(Timer_Handle handle) { // 在这里触发ADC采样 ADC14_toggleConversionTrigger(); } int main(void) { // 初始化MSP432驱动库 MAP_WDT_A_holdTimer(); // 初始化定时器 Timer_Handle timerHandle; Timer_Params timerParams; Timer_Params_init(&timerParams); timerParams.periodUnits = Timer_PERIOD_HZ; timerParams.period = 10000; // 设置定时器周期为10ms timerParams.timerMode = Timer_CONTINUOUS_CALLBACK; timerParams.timerCallback = timerCallback; timerHandle = Timer_open(CONFIG_TIMER_0, &timerParams); Timer_start(timerHandle); // 初始化ADC MAP_ADC14_enableModule(); MAP_ADC14_initModule(ADC_CLOCKSOURCE_SMCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_1, 0); MAP_ADC14_configureSingleSampleMode(ADC_MEM0, true); MAP_ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS, ADC_INPUT_A0, false); // 开始连续转换 MAP_ADC14_enableConversion(); MAP_ADC14_enableInterrupt(ADC_INT0); MAP_Interrupt_enableInterrupt(INT_ADC14); MAP_Interrupt_enableMaster(); // 启动定时器 Timer_start(timerHandle); // 进入低功耗模式 while (1) { PCM_gotoLPM0(); } } ``` 在上述代码中,定时器的周期设置为10ms,您可以根据需要进行调整。在定时器的回调函数中,我们触发ADC采样,这里使用了`ADC14_toggleConversionTrigger()`函数。您可以根据实际需求更改ADC通道和其他配置。 请注意,上述代码仅提供了一个基本的框架,您可能需要根据您的具体应用进行更多的配置和调整。希望这对您有所帮助!如果您有任何其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值