1.头文件
#include "tiva_adc.h" // ADC
2.引脚
3.初始化
// ADC初始化 // 光敏电阻(PE0)为通道3,存在序列0中,硬件平均为8个点 adc_init(TIVA_ADC1 , Channel_3, Sequence0, GPIOE, Pin0, 8);
// 滑动变阻器为通道2
adc_init(TIVA_ADC1 , Channel_2, Sequence1, GPIOE, Pin1, 8);
//具体函数内容 //channel:0-11
//sequence:0-3
void adc_init(uint32_t adc, uint32_t channel, uint32_t sequence, uint32_t GPIO, uint32_t PIN, int average_num) { //参数转换 uint32_t adc_base; uint32_t adc_gpio; //选择ADC0或ADC1 if(adc == TIVA_ADC0) adc_base = ADC0_BASE; else if(adc == TIVA_ADC1) adc_base = ADC1_BASE; //选择GPIO switch(GPIO) { case GPIOA:{adc_gpio = SYSCTL_PERIPH_GPIOA;break;} case GPIOB:{adc_gpio = SYSCTL_PERIPH_GPIOB;break;} case GPIOC:{adc_gpio = SYSCTL_PERIPH_GPIOC;break;} case GPIOD:{adc_gpio = SYSCTL_PERIPH_GPIOD;break;} case GPIOE:{adc_gpio = SYSCTL_PERIPH_GPIOE;break;} case GPIOF:{adc_gpio = SYSCTL_PERIPH_GPIOF;break;} } SysCtlPeripheralEnable(adc); // 开ADC0/ADC1 GPIOPinTypeADC(GPIO, PIN); // 选择管脚 // 开启ADC // adc:ADC0 或 ADC1 // sequence: 选择序列 // 最后一个参数:优先级 ADCSequenceConfigure(adc_base, sequence, ADC_TRIGGER_PROCESSOR, 0); ADCHardwareOversampleConfigure(adc_base, average_num); // 设置硬件8个数取一次均值 ADCSequenceStepConfigure(adc_base, sequence, 0, channel | ADC_CTL_IE | ADC_CTL_END); // 使能ADCx sequencex 中断 if(adc == TIVA_ADC0) ADCIntRegister(adc_base, sequence, ADC0Handler); else ADCIntRegister(adc_base, sequence, ADC1Handler); ADCIntEnable(adc_base, sequence); if(adc == TIVA_ADC0) // ADC0 { switch(sequence) { case 0:IntEnable(INT_ADC0SS0);break; case 1:IntEnable(INT_ADC0SS1);break; case 2:IntEnable(INT_ADC0SS2);break; case 3:IntEnable(INT_ADC0SS3);break; } } else // ADC1 { switch(sequence) { case 0:IntEnable(INT_ADC1SS0);break; case 1:IntEnable(INT_ADC1SS1);break; case 2:IntEnable(INT_ADC1SS2);break; case 3:IntEnable(INT_ADC1SS3);break; } } ADCSequenceEnable(adc_base, sequence); // 通道配置好之后进行只能 ADCIntClear(adc_base, sequence); // 清除中断标志 }
4.获取采样值
// 获取采样值,0-4095,对应0-3.3V adc_val = adc_get_value(TIVA_ADC1, Sequence0)*3.3/4096; // 具体函数内容 uint16_t adc_get_value(uint32_t adc, uint8_t sequence) { if(adc == TIVA_ADC0) // ADC0 { ADCProcessorTrigger(ADC0_BASE, sequence); switch(sequence) { case Sequence0: return ADC0_sequence0_value; case Sequence1: return ADC0_sequence1_value; case Sequence2: return ADC0_sequence2_value; case Sequence3: return ADC0_sequence3_value; default: return 0; } } else // ADC1 { ADCProcessorTrigger(ADC1_BASE, sequence); switch(sequence) { case Sequence0: return ADC1_sequence0_value; case Sequence1: return ADC1_sequence1_value; case Sequence2: return ADC1_sequence2_value; case Sequence3: return ADC1_sequence3_value; default: return 0; } } }