28335用c语言写adc读取
时间: 2023-05-14 17:06:54 浏览: 234
f28335中ADC模块的学习
3星 · 编辑精心推荐
可以使用以下代码进行 ADC 读取:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "F28335_adc.h"
#define ADC_SOC_NUMBER 8
void adc_init(void)
{
EALLOW;
AdcRegs.ADCCTL1.bit.ADCBGPWD = 1; // Power up bandgap reference
AdcRegs.ADCCTL1.bit.ADCREFSEL = 0; // Select internal bandgap reference
AdcRegs.ADCCTL1.bit.ADCREFPWD = 1; // Power up reference buffer
AdcRegs.ADCCTL1.bit.ADCPWDN = 1; // Power up rest of ADC
AdcRegs.ADCCTL1.bit.ADCENABLE = 1; // Enable ADC
AdcRegs.ADCCTL1.bit.ADCREFBURST = 1; // Enable reference buffer burst mode
AdcRegs.ADCCTL1.bit.ADCINTPULSEPOS = 1; // ADCINT1 trips after AdcResults latch
AdcRegs.ADCSOC0CTL.bit.CHSEL = 0; // SOC0 will convert pin A0
AdcRegs.ADCSOC0CTL.bit.ACQPS = 6; // Sample window is 7 ADCCLK
AdcRegs.INTSEL1N2.bit.INT1SEL = 0; // Connect ADCINT1 to SOC0
AdcRegs.INTSEL1N2.bit.INT1E = 1; // Enable ADCINT1
AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // Clear ADCINT1 flag
AdcRegs.ADCSOCFRC1.all = 0x0001; // Force start SOC0
EDIS;
}
void adc_read(uint16_t *adc_data)
{
int i;
for (i = 0; i < ADC_SOC_NUMBER; i++) {
while (AdcRegs.ADCINTFLG.bit.ADCINT1 == 0); // Wait for ADCINT1
AdcRegs.ADCINTFLGCLR.bit.ADCINT1 = 1; // Clear ADCINT1 flag
adc_data[i] = AdcResult.ADCRESULT0; // Read ADC result
AdcRegs.ADCSOCFRC1.all = (1 << i); // Start next SOC
}
}
int main(void)
{
uint16_t adc_data[ADC_SOC_NUMBER];
adc_init();
while (1) {
adc_read(adc_data);
// Do something with adc_data
}
return 0;
}
阅读全文