static void MCU_ADC_Init(void)
{
uint32_t reg32;
reg32 = CLK->CLKSEL1;
reg32 &= ~CLK_CLKSEL1_ADC_S_Msk;
reg32 |= 0x00000008; //HCLK
CLK->CLKSEL1 = reg32; //选择时钟源
CLK->CLKDIV |= (4U<<16); //时钟源分频 50 / (4+1) = 10MHZ 最大为16MHz
CLK->APBCLK |= CLK_APBCLK_ADC_EN_Msk; //使能ADC时钟
ADC->ADCR = ((2U<<2) | (1U<<0)); //(ADC_ADCR_ADMD_SINGLE_CYCLE | ADC_ADCR_DIFFEN_SINGLE_END | ADC_ADCR_ADEN_CONVERTER_ENABLE); 单次扫描 ,单端采样,模块上电
ADC->ADCHER = 0x0000007F; //使能低7路ADC通道
ADC->ADSR |= 0x01; //清除ADC采样完成标志位
}
//启动ADC转换
void ADC_START(void)
{
ADC->ADCR |= (1U << 11); // 启动ADC转换
while((ADC->ADSR & 0x00000001) ==0) __nop(); //等待转换完成
ADC->ADSR |= 0x00000001; //清标志位
}
//读取ADC的值
void ADC_Data_Sample(void)
{
ADC_START(); //启动转换
ADC_SampleCache.LE[AdcIndex] = (uint16_t)(ADC->ADDR[0] & 0x00000FFF); //向数组中读取值
ADC_SampleCache.IA[AdcIndex] = (uint16_t)(ADC->ADDR[1] & 0x00000FFF);
ADC_SampleCache.IB[AdcIndex] = (uint16_t)(ADC->ADDR[2] & 0x00000FFF);
ADC_SampleCache.IC[AdcIndex] = (uint16_t)(ADC->ADDR[3] & 0x00000FFF);
ADC_SampleCache.UA[AdcIndex] = (uint16_t)(ADC->ADDR[4] & 0x00000FFF);
ADC_SampleCache.UB[AdcIndex] = (uint16_t)(ADC->ADDR[5] & 0x00000FFF);
ADC_SampleCache.UC[AdcIndex] = (uint16_t)(ADC->ADDR[6] & 0x00000FFF);
}
这样看舒服些 |