STM32F2系列外設(shè)設(shè)置規(guī)則
外設(shè)設(shè)置規(guī)則:
在配置之前必須添加外設(shè)的.c文件,以及相應(yīng)包含的.h。.h文件在stm32f2xx_conf.h中添加。
1、 時鐘設(shè)置:包括引腳時鐘和外設(shè)時鐘:
RCC_XXXPeriphClockCmd();
2、 引腳連接:即把引腳和外設(shè)功能進(jìn)行連接;
GPIO_PinAFConfig();
如下功能需要引腳與外設(shè)連接。
* @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、 外設(shè)初始化:
如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; //定義外設(shè)基址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue; //定義寄存器基址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //定義DMA傳輸模式從外設(shè)到內(nèi)存
DMA_InitStructure.DMA_BufferSize = 4; //定義buffer大小,根據(jù)通道數(shù)來定
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設(shè)地址寄存器不變
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //DMA寄存器隨之增加(單通道不增加)
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //DMA外設(shè)數(shù)據(jù)大小,16位半字
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMA存儲器數(shù)據(jù)大小,16位半字
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //模式為循環(huán)模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA優(yōu)先級高
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; //連續(xù)掃描模式
ADC_InitStructure.ADC_ExternalTrigConvPolarity = ADC_ExternalTrigConvPolarity_None;//無外部觸發(fā)
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //數(shù)據(jù)右對齊
ADC_InitStructure.ADC_NbrOfChannel = 4; //采樣通道數(shù)
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 */
/*優(yōu)先級組說明了搶占優(yōu)先級所用的位數(shù),和子優(yōu)先級所用的位數(shù)在這里是1,7 */
NVIC_PriorityGroupConfig(NVIC_PriorityGro