ADC_DMA_TIM
/************************************************************************** * 文件名:ADC.h * * 编写人:离逝的风 * * 更新时间:2018.12.25 * * 说明:此文件属于开源,所有权对任何人开放 * * 如有问题请联系邮箱:1477153217@qq.com * * 使用步骤: * * 1.开启ADCx_EN设置为1 * * 2.设置通道数量ADCx_Lenth * * 3.选择定时器触发并设置相关配置 * * 4.设置各个通道转换周期 * * 4.在主函数中使用ADCx_Init初始化 * * 5.在ADCx_BUFF为转换数据结果 * ***************************************************************************/ //注意:下面使用的是DMA传输数据,不需要CPU参与!只能从0通道开始转换,依次转换 #ifndef __ADC_H #define __ADC_H #include "stm32f10x.h" /**#############################下面为所有ADC通道引脚说明##########################################*/ /*------------------------------------------------------------------------------------------------- ADC1: CH0->PA0 CH1->PA0 CH2->PA2 CH3->PA3 CH4->PA4 CH5->PA5 CH6->PA6 CH7->PA7 CH8->PB0 CH9->PB1 CH10->PC0 CH11->PC1 CH12->PC2 CH13->PC3 CH14->PC4 CH15->PC5 CH16->温度传感器 CH17->内部参考电压 --------------------------------------------------------------------------------------------------- ADC2: CH0->PA0 CH1->PA0 CH2->PA2 CH3->PA3 CH4->PA4 CH5->PA5 CH6->PA6 CH7->PA7 CH8->PB0 CH9->PB1 CH10->PC0 CH11->PC1 CH12->PC2 CH13->PC3 CH14->PC4 CH15->PC5 --------------------------------------------------------------------------------------------------- ADC3: CH0->PA0 CH1->PA0 CH2->PA2 CH3->PA3 CH4->PF6 CH5->PF7 CH6->PF8 CH7->PF9 CH8->PF10 -------- CH10->PC0 CH11->PC1 CH12->PC2 CH13->PC3 -------- -------- --------------------------------------------------------------------------------------------------*/ /**#############################下面为文件配置,根据用户需求修改##################################**/ //需要的功能设置为1,否则设置为0,默认为0 #define ADC1_EN 1 #define ADC3_EN 0 /*##############################下面为ADC传输长度################################################*/ //比如有五个传感器则需要定义长度为5 //取值范围1-16 /*****从0通道开始转换*******/ #define ADC1_Lenth 3 #define ADC3_Lenth 3 /**#############################下面为ADC存储数据缓存区###########################################**/ //所有的转换结果会保存在下面的数组中 extern unsigned int ADC1_BUFF[ADC1_Lenth+1]; extern unsigned int ADC3_BUFF[ADC3_Lenth+1]; /**#############################下面为ADC触发定时器设置###########################################**/ //定时器选择 // 1:定时器1 2:定时器2 4:定时器4 #define ADC1_TRR 1 // 1:定时器1 2:定时器2 3:定时器3 #define ADC3_TRR 2 //定时器预分频值 时钟来自72MHz #define ADC1_PSC 719 #define ADC3_PSC 719 //定时器比较输出值 #define ADC1_CCR 100 #define ADC3_CCR 100 //定时器预装值 #define ADC1_ARR 200 #define ADC3_ARR 200 /*##############################ADC时钟分频定义,需用户选择,默认6分频即72/6=12M####################*/ //备注:取值0-3 该时钟来自72MHz的PLL时钟,经过分频后作为ADC时钟 // 0:2分频 1:4分频 2:6分频 3:8分频 #define ADC_CLK_PRE 2 /*###############################ADC转换周期定义,默认239.5周期#####################################*/ //备注:取值0-7 每个通道的转换周期设置 // 0:1.5周期 1:7.5周期 2:13.5周期 3:28.5周期 4:41.5周期 5:55.5周期 // 6:71.5周期 7:239.5周期 #define ADC1_CH0_SMPR 7 #define ADC1_CH1_SMPR 7 #define ADC1_CH2_SMPR 7 #define ADC1_CH3_SMPR 7 #define ADC1_CH4_SMPR 7 #define ADC1_CH5_SMPR 7 #define ADC1_CH6_SMPR 7 #define ADC1_CH7_SMPR 7 #define ADC1_CH8_SMPR 7 #define ADC1_CH9_SMPR 7 #define ADC1_CH10_SMPR 7 #define ADC1_CH11_SMPR 7 #define ADC1_CH12_SMPR 7 #define ADC1_CH13_SMPR 7 #define ADC1_CH14_SMPR 7 #define ADC1_CH15_SMPR 7 #define ADC3_CH0_SMPR 7 #define ADC3_CH1_SMPR 7 #define ADC3_CH2_SMPR 7 #define ADC3_CH3_SMPR 7 #define ADC3_CH4_SMPR 7 #define ADC3_CH5_SMPR 7 #define ADC3_CH6_SMPR 7 #define ADC3_CH7_SMPR 7 #define ADC3_CH8_SMPR 7 #define ADC3_CH10_SMPR 7 #define ADC3_CH11_SMPR 7 #define ADC3_CH12_SMPR 7 #define ADC3_CH13_SMPR 7 /*##############################下面为ADC接口函数,供用户使用#######################################*/ /**-----------------ADC1---------------------------------*/ #if(ADC1_EN==1) //函数名:ADC1_Init //参数:无 //返回值:无 //功能:ADC1初始化函数 void ADC1_Init(void); #endif /**-----------------ADC3---------------------------------*/ #if(ADC3_EN==1) //函数名:ADC3_Init //参数:无 //返回值:无 //功能:ADC3初始化函数 void ADC3_Init(void); #endif #endif //FILE END
/************************************************************************** * 文件名:ADC.c * * 编写人:离逝的风 * * 更新时间:2018.12.25 * * 说明:此文件属于开源,所有权对任何人开放 * * 如有问题请联系邮箱:1477153217@qq.com * * 使用步骤: * * 1.开启ADCx_EN设置为1 * * 2.设置通道数量ADCx_Lenth * * 3.选择定时器触发并设置相关配置 * * 4.设置各个通道转换周期 * * 4.在主函数中使用ADCx_Init初始化 * * 5.在ADCx_BUFF为转换数据结果 * ***************************************************************************/ #include "ADC.h" unsigned int ADC1_BUFF[ADC1_Lenth+1]={0}; unsigned int ADC3_BUFF[ADC3_Lenth+1]={0}; /*###############################下面为ADC1#################################################*/ #if(ADC1_EN==1) void ADC1_Init(void) { RCC->APB2ENR|=(1<<9)|(1<<0); //开启ADC1与复用功能时钟 RCC->CFGR&=(~(3<<14)); //清除ADC1分频的值 RCC->CFGR|=(ADC_CLK_PRE<<14); //让ADC的预分频为6,得到72/6=12M的ADC时钟 ADC1->SQR1&=(~(15<<20)); //清楚转换通道数 ADC1->SQR1|=((ADC1_Lenth-1)<<20); //设置转换通道个数位ADC1_Lenth #if(ADC1_Lenth==1) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR3|=(0<<0); //规则0 #endif #if(ADC1_Lenth==2) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR3|=(0<<0)|(1<<5); //规则序号 #endif #if(ADC1_Lenth==3) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10); //规则序号 #endif #if(ADC1_Lenth==4) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15); //规则序号 #endif #if(ADC1_Lenth==5) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20); //规则序号 #endif #if(ADC1_Lenth==6) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 #endif #if(ADC1_Lenth==7) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0) #endif #if(ADC1_Lenth==8) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA7设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5); #endif #if(ADC1_Lenth==9) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10); #endif #if(ADC1_Lenth==10) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15); #endif #if(ADC1_Lenth==11) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15)|(10<<20); #endif #if(ADC1_Lenth==12) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15)|(10<<20)|(11<<25); #endif #if(ADC1_Lenth==13) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 GPIOC->CRL&=0XFFFFF0FF;GPIOC->CRL|=0X00000000; //GPIOC2设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15)|(10<<20)|(11<<25); ADC1->SQR1|=(12<<0); #endif #if(ADC1_Lenth==14) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 GPIOC->CRL&=0XFFFFF0FF;GPIOC->CRL|=0X00000000; //GPIOC2设置为模拟输入 GPIOC->CRL&=0XFFFF0FFF;GPIOC->CRL|=0X00000000; //GPIOC3设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15)|(10<<20)|(11<<25); ADC1->SQR1|=(12<<0)|(13<<5); #endif #if(ADC1_Lenth==15) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 GPIOC->CRL&=0XFFFFF0FF;GPIOC->CRL|=0X00000000; //GPIOC2设置为模拟输入 GPIOC->CRL&=0XFFFF0FFF;GPIOC->CRL|=0X00000000; //GPIOC3设置为模拟输入 GPIOC->CRL&=0XFFF0FFFF;GPIOC->CRL|=0X00000000; //GPIOC4设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15)|(10<<20)|(11<<25); ADC1->SQR1|=(12<<0)|(13<<5)|(14<<10); #endif #if(ADC1_Lenth==16) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOA->CRL&=0XFFF0FFFF;GPIOA->CRL|=0X00000000; //GPIOA4设置为模拟输入 GPIOA->CRL&=0XFF0FFFFF;GPIOA->CRL|=0X00000000; //GPIOA5设置为模拟输入 GPIOA->CRL&=0XF0FFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOA6设置为模拟输入 GPIOB->CRL&=0XFFFFFFF0;GPIOB->CRL|=0X00000000; //GPIOB0设置为模拟输入 GPIOB->CRL&=0XFFFFFF0F;GPIOB->CRL|=0X00000000; //GPIOB1设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 GPIOC->CRL&=0XFFFFF0FF;GPIOC->CRL|=0X00000000; //GPIOC2设置为模拟输入 GPIOC->CRL&=0XFFFF0FFF;GPIOC->CRL|=0X00000000; //GPIOC3设置为模拟输入 GPIOC->CRL&=0XFFF0FFFF;GPIOC->CRL|=0X00000000; //GPIOC4设置为模拟输入 GPIOC->CRL&=0XFF0FFF0F;GPIOC->CRL|=0X00000000; //GPIOC5设置为模拟输入 ADC1->SMPR1&=0X00;ADC1->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC1->SMPR2|=(ADC1_CH0_SMPR<<0)|(ADC1_CH1_SMPR<<3)|(ADC1_CH2_SMPR<<6); ADC1->SMPR2|=(ADC1_CH3_SMPR<<9)|(ADC1_CH4_SMPR<<12)|(ADC1_CH5_SMPR<<15); ADC1->SMPR2|=(ADC1_CH6_SMPR<<18)|(ADC1_CH7_SMPR<<21)|(ADC1_CH8_SMPR<<24)|(ADC1_CH9_SMPR<<27); ADC1->SMPR1|=(ADC1_CH10_SMPR<<0)|(ADC1_CH11_SMPR<<3)|(ADC1_CH12_SMPR<<6)|(ADC1_CH13_SMPR<<9); ADC1->SMPR1|=(ADC1_CH14_SMPR<<12)|(ADC1_CH15_SMPR<<15); /**下面为设置转换规则通道**/ ADC1->SQR3&=0X00;ADC1->SQR2&=0X00;ADC1->SQR1&=0X00; ADC1->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC1->SQR2|=(6<<0)|(7<<5)|(8<<10)|(9<<15)|(10<<20)|(11<<25); ADC1->SQR1|=(12<<0)|(13<<5)|(14<<10)|(15<<15); #endif ADC1->CR1&=0X00; //清除CR1位 ADC1->CR1|=(1<<8); //使用扫描模式 ADC1->CR1|=(1<<5); ADC1->CR2&=0X00; //清除CR2位 #if(ADC1_TRR==1) ADC1->CR2|=(0<<17); //选择定时器1事件1触发 #endif #if(ADC1_TRR==2) ADC1->CR2|=(3<<17); //选择定时器2事件2触发 #endif #if(ADC1_TRR==4) ADC1->CR2|=(5<<17); //选择定时器4事件4触发 #endif ADC1->CR2|=(1<<20)|(1<<0); //1<<20非常重要,必须使用外部事件启动转换 ADC1->CR2|=(1<<8); //使用DMA ADC1->CR2|=(0<<1); //单次转换模式 ADC1->CR2|=1<<3; //复位校准(初始化校准) while(ADC1->CR2&(1<<3)); //等待校准初始化完成 ADC1->CR2|=1<<2; //开始校准 while(ADC1->CR2&(1<<2)); //等待校准完成 ADC1->CR2|=1<<22; //开始转换规则通道 /**********************下面位DMA设置*****************************/ RCC->AHBENR|=0x01; //开启DMA时钟 DMA1_Channel1->CCR&=0; DMA1_Channel1->CCR|=(0<<14); //非存储器到存储器 DMA1_Channel1->CCR|=(11<<12); //优先级最高 DMA1_Channel1->CCR|=(2<<10); //缓存区数据宽度32位 说明:地址长度为32,所以设置32位宽度 DMA1_Channel1->CCR|=(2<<8); //外设数据宽度32位 DMA1_Channel1->CCR|=(1<<7); //执行存储区地址增量 DMA1_Channel1->CCR|=(0<<6); //不执行外设地址增量 DMA1_Channel1->CCR|=(1<<5); //循环模式 DMA1_Channel1->CCR|=(0<<4); //传输方向:从外设读 DMA1_Channel1->CNDTR|=ADC1_Lenth; //定义传输数量 DMA1_Channel1->CMAR|=(unsigned long)(ADC1_BUFF); //定义传输外设地址 DMA1_Channel1->CPAR|=(unsigned long)(&ADC1->DR); //定义传输目标地址,即存储区地址 DMA1_Channel1->CCR|=(1<<0); //启动DMA传输 /******************************************************************/ /********************下面为TIM触发设置****************************/ #if(ADC1_TRR==1) //如果选择TIM1 RCC->APB2ENR|=(1<<11); //开启TIM1时钟 TIM1->CR1&=0; //清除寄存器值 TIM1->CR1|=(0<<8); //不使用分频设置,即72MHz TIM1->CR1|=(1<<7); //使能自动重装 TIM1->CR1|=(0<<5); //计数器向上或者向下 TIM1->CR1|=(0<<4); //计数器向上 TIM1->CCMR1&=0; //清除寄存器值 TIM1->CCMR1|=(6<<4); //设置为PWM1模式 TIM1->CCMR1|=(1<<3); //设置为输出 即OC1 TIM1->CCER|=(0<<1); //捕获输出极性高电平有效 TIM1->CCER|=(1<<0); //使能捕获输出1 即OC1 TIM1->PSC&=0x00; //清除预分频值 TIM1->PSC|=ADC1_PSC; //预分频值 TIM1->CCR1&=0X00; //清除比较值 TIM1->CCR1|=ADC1_CCR; //比较值 TIM1->ARR&=0X00; //清除重装初值 TIM1->ARR|=ADC1_ARR; //重装初值 TIM1->BDTR|=(1<<15); //开启事件输出使能 TIM1->CR1|=(1<<0); //启动定时器计数 #endif #if(ADC1_TRR==2) //如果选择TIM2 RCC->APB1ENR|=(1<<0); //开启TIM2时钟 TIM2->CR1&=0; //清除寄存器值 TIM2->CR1|=(0<<8); //不使用分频设置,即72MHz TIM2->CR1|=(1<<7); //使能自动重装 TIM2->CR1|=(0<<5); //计数器向上或者向下 TIM2->CR1|=(0<<4); //计数器向上 TIM2->CCMR1&=0; //清除寄存器值 TIM2->CCMR1|=(6<<12); //设置为PWM1模式 TIM2->CCMR1|=(1<<11); //设置为输出 TIM2->CCER|=(0<<5); //捕获输出极性高电平有效 TIM2->CCER|=(1<<4); //使能捕获输出 OC2 TIM2->PSC&=0x00; //清除预分频值 TIM2->PSC|=ADC1_PSC; //预分频值 TIM2->CCR2&=0X00; //清除比较值 TIM2->CCR2|=ADC1_CCR; //比较值 TIM2->ARR&=0X00; //清除重装初值 TIM2->ARR|=ADC1_ARR; //重装初值 TIM2->CR1|=(1<<0); //启动定时器计数 #endif #if(ADC1_TRR==4) //如果选择TIM4 RCC->APB1ENR|=(1<<2); //开启TIM4时钟 TIM4->CR1&=0; //清除寄存器值 TIM4->CR1|=(0<<8); //不使用分频设置,即72MHz TIM4->CR1|=(1<<7); //使能自动重装 TIM4->CR1|=(0<<5); //计数器向上或者向下 TIM4->CR1|=(0<<4); //计数器向上 TIM4->CCMR2&=0; //清除寄存器值 TIM4->CCMR2|=(6<<12); //设置为PWM1模式 TIM4->CCMR2|=(1<<11); //设置为输出 TIM4->CCER|=(0<<13); //捕获输出极性高电平有效 TIM4->CCER|=(1<<12); //使能捕获输出 OC4 TIM4->PSC&=0x00; //清除预分频值 TIM4->PSC|=ADC1_PSC; //预分频值 TIM4->CCR4&=0X00; //清除比较值 TIM4->CCR4|=ADC1_CCR; //比较值 TIM4->ARR&=0X00; //清除重装初值 TIM4->ARR|=ADC1_ARR; //重装初值 TIM4->CR1|=(1<<0); //启动定时器计数 #endif } #endif /*###############################下面为ADC3#################################################*/ #if(ADC3_EN==1) void ADC3_Init(void) { RCC->APB2ENR|=(1<<15)|(1<<0); //开启ADC3与复用功能时钟 RCC->CFGR&=(~(3<<14)); //清除ADC3分频的值 RCC->CFGR|=(ADC_CLK_PRE<<14); //让ADC的预分频为6,得到72/6=12M的ADC时钟 ADC3->SQR1&=~(0XF<<20); //设置转换通道个数为1 ADC3->SQR1|=((ADC3_Lenth-1)<<20); //设置转换通道个数位ADC3_Lenth #if(ADC3_Lenth==1) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0); //规则序号 #endif #if(ADC3_Lenth==2) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5); //规则序号 #endif #if(ADC3_Lenth==3) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10); //规则序号 #endif #if(ADC3_Lenth==4) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15); //规则序号 #endif #if(ADC3_Lenth==5) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20); //规则序号 #endif #if(ADC3_Lenth==6) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 #endif #if(ADC3_Lenth==7) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0); #endif #if(ADC3_Lenth==8) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 GPIOF->CRH&=0XFFFFFF0F;GPIOF->CRH|=0X00000000; //GPIOF9设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0)|(7<<5); #endif #if(ADC3_Lenth==9) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 GPIOF->CRH&=0XFFFFFF0F;GPIOF->CRH|=0X00000000; //GPIOF9设置为模拟输入 GPIOF->CRH&=0XFFFFF0FF;GPIOF->CRH|=0X00000000; //GPIOF10设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0)|(7<<5)|(8<<10); #endif #if(ADC3_Lenth==10) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 GPIOF->CRH&=0XFFFFFF0F;GPIOF->CRH|=0X00000000; //GPIOF9设置为模拟输入 GPIOF->CRH&=0XFFFFF0FF;GPIOF->CRH|=0X00000000; //GPIOF10设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0)|(7<<5)|(8<<10)|(10<<15); #endif #if(ADC3_Lenth==11) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 GPIOF->CRH&=0XFFFFFF0F;GPIOF->CRH|=0X00000000; //GPIOF9设置为模拟输入 GPIOF->CRH&=0XFFFFF0FF;GPIOF->CRH|=0X00000000; //GPIOF10设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0)|(7<<5)|(8<<10)|(10<<15)|(11<<20); #endif #if(ADC3_Lenth==12) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 GPIOF->CRH&=0XFFFFFF0F;GPIOF->CRH|=0X00000000; //GPIOF9设置为模拟输入 GPIOF->CRH&=0XFFFFF0FF;GPIOF->CRH|=0X00000000; //GPIOF10设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 GPIOC->CRL&=0XFFFFF0FF;GPIOC->CRL|=0X00000000; //GPIOC2设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0)|(7<<5)|(8<<10)|(10<<15)|(11<<20)|(12<<25); #endif #if(ADC3_Lenth==13) GPIOA->CRL&=0XFFFFFFF0;GPIOA->CRL|=0X00000000; //GPIOA0设置为模拟输入 GPIOA->CRL&=0XFFFFFF0F;GPIOA->CRL|=0X00000000; //GPIOA1设置为模拟输入 GPIOA->CRL&=0XFFFFF0FF;GPIOA->CRL|=0X00000000; //GPIOA2设置为模拟输入 GPIOA->CRL&=0XFFFF0FFF;GPIOA->CRL|=0X00000000; //GPIOA3设置为模拟输入 GPIOF->CRL&=0XF0FFFFFF;GPIOF->CRL|=0X00000000; //GPIOF6设置为模拟输入 GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=0X00000000; //GPIOF7设置为模拟输入 GPIOF->CRH&=0XFFFFFFF0;GPIOF->CRH|=0X00000000; //GPIOF8设置为模拟输入 GPIOF->CRH&=0XFFFFFF0F;GPIOF->CRH|=0X00000000; //GPIOF9设置为模拟输入 GPIOF->CRH&=0XFFFFF0FF;GPIOF->CRH|=0X00000000; //GPIOF10设置为模拟输入 GPIOC->CRL&=0XFFFFFFF0;GPIOC->CRL|=0X00000000; //GPIOC0设置为模拟输入 GPIOC->CRL&=0XFFFFFF0F;GPIOC->CRL|=0X00000000; //GPIOC1设置为模拟输入 GPIOC->CRL&=0XFFFFF0FF;GPIOC->CRL|=0X00000000; //GPIOC2设置为模拟输入 GPIOC->CRL&=0XFFFF0FFF;GPIOC->CRL|=0X00000000; //GPIOC3设置为模拟输入 ADC3->SMPR1&=0X00;ADC3->SMPR2&=0X00; //清除寄存器之前的值 /** 下面为设置各个通道周期 **/ ADC3->SMPR2|=(ADC3_CH0_SMPR<<0)|(ADC3_CH1_SMPR<<3)|(ADC3_CH2_SMPR<<6); ADC3->SMPR2|=(ADC3_CH3_SMPR<<9)|(ADC3_CH4_SMPR<<12)|(ADC3_CH5_SMPR<<15); ADC3->SMPR2|=(ADC3_CH6_SMPR<<18)|(ADC3_CH7_SMPR<<21)|(ADC3_CH8_SMPR<<24); ADC3->SMPR1|=(ADC3_CH10_SMPR<<0)|(ADC3_CH11_SMPR<<3)|(ADC3_CH12_SMPR<<6)|(ADC3_CH13_SMPR<<9); /**下面为设置转换规则通道**/ ADC3->SQR3&=0X00;ADC3->SQR2&=0X00;ADC3->SQR1&=0X00; ADC3->SQR3|=(0<<0)|(1<<5)|(2<<10)|(3<<15)|(4<<20)|(5<<25); //规则序号 ADC3->SQR2|=(6<<0)|(7<<5)|(8<<10)|(10<<15)|(11<<20)|(12<<25); ADC3->SQR1|=(13<<0); #endif ADC3->CR1&=0X00; //清除CR1位 ADC3->CR1|=(1<<8); //使用扫描模式 ADC3->CR2&=0X00; //清除CR2位 #if(ADC3_TRR==1) ADC3->CR2|=(2<<17); //选择定时器1事件3触发 #endif #if(ADC3_TRR==2) ADC3->CR2|=(1<<17); //选择定时器2事件3触发 #endif #if(ADC3_TRR==3) ADC3->CR2|=(0<<17); //选择定时器3事件1触发 #endif ADC3->CR2|=(1<<20)|(1<<0); //1<<20非常重要,必须使用外部事件启动转换 ADC3->CR2|=(1<<8); //使用DMA ADC3->CR2|=(0<<1); //单次转换模式 ADC3->CR2|=1<<3; //复位校准(初始化校准) while(ADC3->CR2&(1<<3)); //等待校准初始化完成 ADC3->CR2|=1<<2; //开始校准 while(ADC3->CR2&(1<<2)); //等待校准完成 ADC3->CR2|=1<<22; //开始转换规则通道 /**********************下面位DMA设置*****************************/ RCC->AHBENR|=0x02; //开启DMA时钟 DMA2_Channel5->CCR&=0; DMA2_Channel5->CCR|=(0<<14); //非存储器到存储器 DMA2_Channel5->CCR|=(11<<12); //优先级最高 DMA2_Channel5->CCR|=(2<<10); //缓存区数据宽度32位 说明:地址长度为32,所以设置32位宽度 DMA2_Channel5->CCR|=(2<<8); //外设数据宽度32位 DMA2_Channel5->CCR|=(1<<7); //执行存储区地址增量 DMA2_Channel5->CCR|=(0<<6); //不执行外设地址增量 DMA2_Channel5->CCR|=(1<<5); //循环模式 DMA2_Channel5->CCR|=(0<<4); //传输方向:从外设读 DMA2_Channel5->CNDTR|=ADC3_Lenth; //定义传输数量 DMA2_Channel5->CMAR|=(unsigned long)(ADC3_BUFF); //定义传输外设地址 DMA2_Channel5->CPAR|=(unsigned long)(&ADC3->DR); //定义传输目标地址,即存储区地址 DMA2_Channel5->CCR|=(1<<0); //启动DMA传输 /******************************************************************/ /********************下面为TIM触发设置****************************/ #if(ADC3_TRR==1) //如果选择TIM1 RCC->APB2ENR|=(1<<11); //开启TIM1时钟 TIM1->CR1&=0; //清除寄存器值 TIM1->CR1|=(0<<8); //不使用分频设置,即72MHz TIM1->CR1|=(1<<7); //使能自动重装 TIM1->CR1|=(0<<5); //计数器向上或者向下 TIM1->CR1|=(0<<4); //计数器向上 TIM1->CCMR2&=0; //清除寄存器值 TIM1->CCMR2|=(6<<4); //设置为PWM1模式 TIM1->CCMR2|=(1<<3); //设置为输出 即OC3 TIM1->CCER|=(0<<9); //捕获输出极性高电平有效 TIM1->CCER|=(1<<8); //使能捕获输出1 即OC3 TIM1->PSC&=0x00; //清除预分频值 TIM1->PSC|=ADC3_PSC; //预分频值 TIM1->CCR3&=0X00; //清除比较值 TIM1->CCR3|=ADC3_CCR; //比较值 TIM1->ARR&=0X00; //清除重装初值 TIM1->ARR|=ADC3_ARR; //重装初值 TIM1->BDTR|=(1<<15); //开启事件输出使能 TIM1->CR1|=(1<<0); //启动定时器计数 #endif #if(ADC3_TRR==2) //如果选择TIM2 RCC->APB1ENR|=(1<<0); //开启TIM2时钟 TIM2->CR1&=0; //清除寄存器值 TIM2->CR1|=(0<<8); //不使用分频设置,即72MHz TIM2->CR1|=(1<<7); //使能自动重装 TIM2->CR1|=(0<<5); //计数器向上或者向下 TIM2->CR1|=(0<<4); //计数器向上 TIM2->CCMR2&=0; //清除寄存器值 TIM2->CCMR2|=(6<<4); //设置为PWM1模式 TIM2->CCMR2|=(1<<3); //设置为输出 TIM2->CCER|=(0<<9); //捕获输出极性高电平有效 TIM2->CCER|=(1<<8); //使能捕获输出 OC3 TIM2->PSC&=0x00; //清除预分频值 TIM2->PSC|=ADC3_PSC; //预分频值 TIM2->CCR3&=0X00; //清除比较值 TIM2->CCR3|=ADC3_CCR; //比较值 TIM2->ARR&=0X00; //清除重装初值 TIM2->ARR|=ADC3_ARR; //重装初值 TIM2->CR1|=(1<<0); //启动定时器计数 #endif #if(ADC3_TRR==3) //如果选择TIM3 RCC->APB1ENR|=(1<<1); //开启TIM3时钟 TIM3->CR1&=0; //清除寄存器值 TIM3->CR1|=(0<<8); //不使用分频设置,即72MHz TIM3->CR1|=(1<<7); //使能自动重装 TIM3->CR1|=(0<<5); //计数器向上或者向下 TIM3->CR1|=(0<<4); //计数器向上 TIM3->CCMR1&=0; //清除寄存器值 TIM3->CCMR1|=(6<<4); //设置为PWM1模式 TIM3->CCMR1|=(1<<3); //设置为输出 TIM3->CCER|=(0<<1); //捕获输出极性高电平有效 TIM3->CCER|=(1<<0); //使能捕获输出 OC1 TIM3->PSC&=0x00; //清除预分频值 TIM3->PSC|=ADC3_PSC; //预分频值 TIM3->CCR1&=0X00; //清除比较值 TIM3->CCR1|=ADC3_CCR; //比较值 TIM3->ARR&=0X00; //清除重装初值 TIM3->ARR|=ADC3_ARR; //重装初值 TIM3->CR1|=(1<<0); //启动定时器计数 #endif } #endif //FILE END