STM32F2系列外设设置规则
扫描二维码
随时随地手机看文章
外设设置规则:
在配置之前必须添加外设的.c文件,以及相应包含的.h。.h文件在stm32f2xx_conf.h中添加。
1、 时钟设置:包括引脚时钟和外设时钟:
RCC_XXXPeriphClockCmd();
2、 引脚连接:即把引脚和外设功能进行连接;
GPIO_PinAFConfig();
如下功能需要引脚与外设连接。
* @arg GPIO_AF_RTC_50Hz
* @arg GPIO_AF_MCO
* @arg GPIO_AF_TAMPER
* @arg GPIO_AF_SWJ
* @arg GPIO_AF_TRACE
* @arg GPIO_AF_TIM1
* @arg GPIO_AF_TIM2
* @arg GPIO_AF_TIM3
* @arg GPIO_AF_TIM4
* @arg GPIO_AF_TIM5
* @arg GPIO_AF_TIM8
* @arg GPIO_AF_TIM9
* @arg GPIO_AF_TIM10
* @arg GPIO_AF_TIM11
* @arg GPIO_AF_I2C1
* @arg GPIO_AF_I2C2
* @arg GPIO_AF_I2C3
* @arg GPIO_AF_SPI1
* @arg GPIO_AF_SPI2
* @arg GPIO_AF_SPI3
* @arg GPIO_AF_USART1
* @arg GPIO_AF_USART2
* @arg GPIO_AF_USART3
* @arg GPIO_AF_UART4
* @arg GPIO_AF_UART5
* @arg GPIO_AF_USART6
* @arg GPIO_AF_CAN1
* @arg GPIO_AF_CAN2
* @arg GPIO_AF_TIM12
* @arg GPIO_AF_TIM13
* @arg GPIO_AF_TIM14
* @arg GPIO_AF_OTG1_FS
* @arg GPIO_AF_OTG2_HS
* @arg GPIO_AF_ETH
* @arg GPIO_AF_FSMC
* @arg GPIO_AF_OTG2_FS
* @arg GPIO_AF_SDIO
* @arg GPIO_AF_DCMI
* @arg GPIO_AF_EVENTOUT
3、 初始化GPIO配置:
GPIO_Init();
4、 外设初始化:
如UART初始化如下:
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStructure);
ADC初始化如下:(多路模式)
// DMA1_Stream0 channel0 configuration **************************************/
DMA_DeInit(DMA2_Stream0);
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; //定义外设基址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue; //定义寄存器基址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //定义DMA传输模式从外设到内存
DMA_InitStructure.DMA_BufferSize = 4; //定义buffer大小,根据通道数来定
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //DMA寄存器随之增加(单通道不增加)
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //DMA外设数据大小,16位半字
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMA存储器数据大小,16位半字
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //模式为循环模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级高
DMA_InitStructure.DMA_DirectMode = DMA_DirectMode_Disable; //直接传输模式无效 DMA_InitStructure.DMA_FifoThreshold = DMA_FifoThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
//************ DMA2_Stream0 enable ************/
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA流0
//***************** ADCs DeInit ***************/
ADC_DeInit(); //ADC重初始化
//************* ADC Common Init ****************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //ADC独立模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; //ADC采样周期2分频
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //采样周期
ADC_CommonInit(&ADC_CommonInitStructure); //基本初始化
//*************** ADC1 Init ********************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //ADC采样精度
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //采用扫描模式,因为多路
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续扫描模式
ADC_InitStructure.ADC_ExternalTrigConvPolarity = ADC_ExternalTrigConvPolarity_None;//无外部触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 4; //采样通道数
ADC_Init(ADC1, &ADC_InitStructure);
//************ ADC1 regular channel12 configuration ************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_3Cycles);
//********* Enable DMA request after last transfer (Multi-ADC mode) *********/
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
//*********** Enable ADC1 DMA ****************/
ADC_DMACmd(ADC1, ENABLE);
//*************** Enable ADC1 ***************/
ADC_Cmd(ADC1, ENABLE);
//******* Start ADC1 Software Conversion *****/
ADC_SoftwareStartConv(ADC1); //软启动开始采样
5、 中端配置:
以下为串口中断配置:
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
/* Configure one bit for preemption priority */
/*优先级组说明了抢占优先级所用的位数,和子优先级所用的位数在这里是1,7 */
NVIC_PriorityGroupConfig(NVIC_PriorityGro