我下边的代码的目的是从UART得到信号后,ADC采集20个信号,然后将信号返回到UART,其中ADC信号采集部分是参照官方例子(见附件)写的
官方的例子可以运行并得到变化的采样值,但是我的程序得到的值都是0,是哪里没有配置合适吗
#include <stdbool.h> #include <stdint.h> #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "driverlib/adc.h" #include "driverlib/gpio.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/uart.h" #include "utils/uartstdio.h" #include "driverlib/timer.h" uint8_t State = 0; uint32_t adc_count = 0; uint32_t adc_val[20]; uint32_t total_samples = 20; uint32_t adc_v; //! - ADC0 peripheral //! - GPIO Port E peripheral (for ADC0 pins) //! - AIN0 - PE3 //! - AIN1 - PE2 //! - UART0 peripheral //! - GPIO Port A peripheral (for UART0 pins) //! - UART0RX - PA0 //! - UART0TX - PA1 //! - UART1RX - PB0 //! - UART1TX - PB1 void UART0_IntHandler(void) {uint32_t ui32Status;ui32Status = UARTIntStatus(UART0_BASE, true); //get interrupt statusUARTIntClear(UART0_BASE, ui32Status); //clear the asserted interrupts while(UARTCharsAvail(UART0_BASE)) { uint32_t get_char; get_char = UARTCharGetNonBlocking(UART0_BASE); switch(get_char) { case 0x00000001: State = 1; break; case 0x00000002: State = 2; break; case 0x00000003: State = 3; break; case 0x00000004: State = 4; break; default: break; } } } void UART1_IntHandler(void) { uint32_t ui32Status; ui32Status = UARTIntStatus(UART1_BASE, true); //get interrupt status UARTIntClear(UART1_BASE, ui32Status); //clear the asserted interrupts while(UARTCharsAvail(UART1_BASE)) { uint32_t get_char; get_char = UARTCharGetNonBlocking(UART1_BASE); switch(get_char) { case 0x00000001: break; case 0x00000002: break; case 0x00000003: break; default: break; } } } void ADC_IntHandler(void) { ADCIntClear(ADC0_BASE,3); ADCSequenceDataGet(ADC0_BASE, 3,adc_v); adc_val[adc_count++] = adc_v; } void UART0_init(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(0, 115200, 16000000); IntEnable(INT_UART0); //enable the UART interrupt UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); //only enable RX and TX interrupts } void UART1_init(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinConfigure(GPIO_PB0_U1RX); GPIOPinConfigure(GPIO_PB1_U1TX); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1); UARTClockSourceSet(UART1_BASE, UART_CLOCK_PIOSC); GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioConfig(1, 115200, 16000000); IntEnable(INT_UART0); //enable the UART interrupt UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); //only enable RX and TX interrupts } void ADC_init(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); GPIOPinTypeADC(GPIO_PORTE_BASE,GPIO_PIN_3 | GPIO_PIN_2); ADCSequenceConfigure(ADC0_BASE, 3,ADC_TRIGGER_TIMER, 0);ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_D | ADC_CTL_CH0 |ADC_CTL_IE | ADC_CTL_END); //ADCReferenceSet(ADC0_BASE,ADC_REF_INT); ADCIntEnable(ADC0_BASE,3); IntEnable(INT_ADC0SS3); ADCIntClear(ADC0_BASE, 3); ADCIntRegister(ADC0_BASE,3,ADC_IntHandler); // 注册序列3中断 函数 ADCSequenceEnable(ADC0_BASE, 3); } void TIMER0_init(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);//使能TIMER0 TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);//周期性计数模式 //TimerLoadSet(TIMER0_BASE, TIMER_A,SysCtlClockGet() / 12800); TimerLoadSet(TIMER0_BASE, TIMER_A,SysCtlClockGet() / 100); TimerADCEventSet(TIMER0_BASE,TIMER_ADC_TIMEOUT_A);//超时触发采样设置 TimerEnable(TIMER0_BASE, TIMER_A);//TIMER0A开始计数,当计数值等于TimerLoadSet,触发中断 } void store_raw_data(void) { adc_count = 0; TimerControlTrigger(TIMER0_BASE,TIMER_A,true); while(adc_count < total_samples){}; TimerControlTrigger(TIMER0_BASE,TIMER_A,false); } int main(void) {uint32_t pui32ADC0Value[1];SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);//这个函数定义结果,外部输入16MHz,经PLL至400MHz,再2分频(芯片自带)至200MHz,再10分频至20MHz。因此这个设置的结果为主频20MHz。IntMasterEnable();UART0_init();ADC_init();TIMER0_init();UARTprintf("ADC ->\n");UARTprintf(" Type: differential\n");UARTprintf(" Samples: One\n");UARTprintf(" Update Rate: 250ms\n");UARTprintf(" Input Pin: (AIN0/PE3 - AIN1/PE2)\n\n"); while(1){ switch(State) { case 1: UARTprintf("00000001"); State = 0; break; case 2: UARTprintf("00000002"); State = 0; break; case 3: UARTprintf("00000003"); State = 0; break; case 4: store_raw_data(); uint8_t i; for(i=0;i<total_samples;i++) UARTprintf("%4d\r",adc_val[i]); State = 0; break; default: break; } } }
Maka Luo:
void ADC_IntHandler(void) {ADCIntClear(ADC0_BASE,3);ADCSequenceDataGet(ADC0_BASE, 3,adc_v);adc_val[adc_count++] = adc_v; }ADC获取的值为0?
jie yang5:
回复 Maka Luo:
是啊,ADC获取的值 adc_v 为0,得到的数组里是20个0.
HG:
建议你确认一下ADC的中断处理程序是否正确执行?
假设你初始化正确,中断处理也正确,你的代码也不合理
uint32_tadc_v; 这个作为全局变量不合理,又没有正确的判断是否获取的新值。
xyz549040622:
回复 jie yang5:
1.单独测试你ADC的中断,ADC函数是否运行OK。
2.感觉是你state的状态引起的,一个全局变量,又要在主函数中改变,还在中断中改变,我觉得是这里的冲突引起的。
Jie Yang4:
回复 HG:
你好,中断程序是执行了20次的,adc_v这个我放在中断中声明,向下边的样子
void ADC_IntHandler(void){ uint32_t adc_v;
ADCSequenceDataGet(ADC0_BASE, 3,adc_v); adc_val[adc_count++] = adc_v; ADCIntClear(ADC0_BASE,3);}
得到的结果是,第一次发送0x04得到20个36,之后都是1,复位后也是如此。
不知道您有什么好的建议,接收到UART中断之后一次性获取连续的N个ADC数值?谢谢
Jie Yang4:
回复 xyz549040622:
您好,ADC中断是正常执行的,state这样的做法在状态机中应该也比较正常吧。