MM32F013x的ADC还是蛮好用的,每一个ADC通道都有一个自己的结果寄存器。当启用连续转换模式时,ADC就可以自动更新转换结果到各自的结果寄存器,用户只需要直接读取结果寄存器即可,省去很多麻烦。由于ADC最快转换时间仅为1us,即使10个通道全部转换一次的时间也只有10us,对于一般的控制也是足够用的。还记得当初使用NXP芯片时,只有一个结果寄存器,每次都要修改转换通道,等待转换,而且还要考虑函数 的可重入性问题,甚是麻烦,而且也会浪费cpu的时间资源。灵动微的这点做的还是不错。下面就上代码:
void adc_init(void)
{
//init pin mux
RCC->AHBENR |= RCC_AHBENR_GPIOA; //enable GPIOA clock
RCC->AHBENR |= RCC_AHBENR_GPIOB; //enable GPIOB clock
GPIOA->CRL =0;// A0-A7->AD
GPIOB->CRL &= ~(GPIO_CNF_MODE_MASK << GPIO_CRL_CNF_MODE_0_Pos);//B0->ADIN8
GPIOB->CRL |= GPIO_CNF_MODE_AIN << GPIO_CRL_CNF_MODE_0_Pos;
GPIOB->CRL &= ~(GPIO_CNF_MODE_MASK << GPIO_CRL_CNF_MODE_1_Pos);//B1->ADIN9
GPIOB->CRL |= GPIO_CNF_MODE_AIN << GPIO_CRL_CNF_MODE_1_Pos;
//init adc
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; //enableADC1clock
RCC->APB2RSTR |= RCC_APB2RSTR_ADC1RST; //ADC1reset
RCC->APB2RSTR &= ~(RCC_APB2RSTR_ADC1RST); //reset end
ADC1->ADCFG |= ADCFG_ADCPRE_3; // 3 fractional frequency
ADC1->ADCR |= ADCR_ADMD_CONTINUE;// continue mode
ADC1->ADCR |= ADCR_ALIGN_RIGHT; //right-justified
//enable channel
ADC1->ADCHS |= ADCHS_CHEN0; //enable channel 0
ADC1->ADCHS |= ADCHS_CHEN1; //enable channel 1
ADC1->ADCHS |= ADCHS_CHEN2; //enable channel 2
ADC1->ADCHS |= ADCHS_CHEN3; //enable channel 3
ADC1->ADCHS |= ADCHS_CHEN4; //enable channel 4
ADC1->ADCHS |= ADCHS_CHEN5; //enable channel 5
ADC1->ADCHS |= ADCHS_CHEN6; //enable channel 6
ADC1->ADCHS |= ADCHS_CHEN7; //enable channel 7
ADC1->ADCHS |= ADCHS_CHEN8; //enable channel 8
ADC1->ADCHS |= ADCHS_CHEN9; //enable channel 9
//ADC1->ADCHS |= ADCHS_CHENTS; //enable channel TS
//ADC1->ADCHS |= ADCHS_CHENVS; //enable channel VS
ADC1->ADCFG |= ADCFG_ADEN;//ADC1enable
ADC1->ADCR |= ADCR_ADST;//Start Conversion
}
|