STM32F1系列之常用外设说明

1、RCC相关:

APB2启动时钟项:

RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,

RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,

RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,

RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,

RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,

RCC_APB2Periph_ALL

APB2启动函数:

RCC_APB2PeriphClockCmd(XX,ENABLE);

APB1启动时钟项:

RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,

RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,

RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,

RCC_APB1Periph_USART2, RCC_APB1Periph_USART3, RCC_APB1Periph_USART4,

RCC_APB1Periph_USART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,

RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,

RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_ALL

APB1启动函数:

RCC_APB2PeriphClockCmd(XX,ENABLE);

AHB启动时钟项:

RCC_AHBPeriph_DMA1

RCC_AHBPeriph_DMA2

RCC_AHBPeriph_SRAM

RCC_AHBPeriph_FLITF

RCC_AHBPeriph_CRC

RCC_AHBPeriph_FSMC

RCC_AHBPeriph_SDIO

AHB启动函数:

RCC_AHBPeriphClockCmd (XX,ENABLE);

2、GPIO相关:

GPIO模式:

GPIO_Mode_AIN:模拟输入                                      GPIO_Mode_IN_FLOATING:浮空输入

GPIO_Mode_IPD:下拉输入                                GPIO_Mode_IPU:上拉输入

GPIO_Mode_Out_OD:开漏输出                    GPIO_Mode_Out_PP:推挽输出

GPIO_Mode_AF_OD:复用开漏输出             GPIO_Mode_AF_PP:复用推挽输出

GPIO速度:

GPIO_Speed_2MHz

GPIO_Speed_10MHz

GPIO_Speed_50MHz

GPIO引脚声明:

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_x;                   x:1~15或者all

GPIO初始化函数:

GPIO_Init(GPIOx, &GPIO_InitStructure);                           x:A~G

注:需要定义GPIO结构体变量,GPIO_InitTypeDef  GPIO_InitStructure;

3、ADC相关

最好需要重新初始化:ADC_DeInit(ADCx);

ADC工作模式选择:

ADC_Mode_Independent:独立工作

ADC_Mode_RegInjecSimult:混合同步+注入同步

ADC_Mode_RegSimult_AlterTrig:混合同步+交替触发

ADC_Mode_InjecSimult_FastInterl:混合同步+快速交替

ADC_Mode_InjecSimult_SlowInterl:混合同步+慢速交替

ADC_Mode_InjecSimult:注入同步

ADC_Mode_RegSimult:规则同步

ADC_Mode_FastInterl:快速交替

ADC_Mode_SlowInterl:慢速交替

ADC_Mode_AlterTrig:交替触发

通过ADC_InitStructure.ADC_Mode = XX进行赋值

ADC扫描使能:

ADC_InitStructure.ADC_ScanConvMode = ENABLE;

ADC连续 /单次模式选择:

ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

ADC转换控制方式:

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//有软件控制转换

ADC数据对齐方式:ADC_DataAlign_Right:右对齐 /  ADC_DataAlign_Right:左对齐

ADC_InitStructure.ADC_DataAlign =XX

ADC转换通道数:

ADC_InitStructure.ADC_NbrOfChannel = X                     // X=1~16

ADC初始化函数:

ADC_Init(ADCx, &ADC_InitStructure);

注:在开始要定义结构体变量     ADC_InitTypeDef   ADC_InitStructure

是否使能ADCx DMA :

ADC_DMACmd(ADCx, ENABLE);

使能ADCx:

ADC_Cmd(ADCx, ENABLE);

初始化ADC1校准寄存器:

ADC_ResetCalibration(ADCx);

检测ADC1校准寄存器初始化是否完成:

while(ADC_GetResetCalibrationStatus(ADCx));

开始校准ADC1:

ADC_StartCalibration(ADCx);

检测是否完成校准:

while(ADC_GetCalibrationStatus(ADCx));

ADC1转换软件启动:

ADC_SoftwareStartConvCmd(ADCx, ENABLE);

4、DMA相关:

复位通道:

DMA_DeInit(DMAy_Channelx);//复位DMAy通道x,y=1时,x=1~7;y=2时,x=1~5

定义外设基地址,全局变量:

DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//地址自己定义

如:#define ADC1_DR_Address    ((u32)0x4001244C)

定义DMA存储器地址,全局变量u32

DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //定义DMA通道存储器地址

如:volatile unsigned  short int  ADC_ConvertedValue[8];

DMA外设方向:

DMA_InitStructure.DMA_DIR = XX;

DMA_DIR_PeripheralSRC:外设为数据传输的来源,DMA_DIR_PeripheralDST:外设为数据传输的目的地

DMA传输数量寄存器值,即缓存大小,单位由MemoryDataSize或PeripheralDataSize决定:

DMA_InitStructure.DMA_BufferSize = x;

//定义DMA缓冲区大小x,如8通道ADC,则x就为8

DMA外设地址寄存器变不变:

DMA_InitStructure.DMA_PeripheralInc = x;

DMA_PeripheralInc_Disable:外设地址寄存器不变 DMA_PeripheralInc_Enable:外设地址寄存器递增

DMA内存地址寄存器变不变:

DMA_InitStructure.DMA_MemoryInc = X;

DMA_MemoryInc_Disable:内存地址寄存器不变 DMA_MemoryInc_Enable:内存地址寄存器递增

外设数据宽度:

DMA_InitStructure.DMA_PeripheralDataSize = X;

DMA_PeripheralDataSize_Byte              数据宽度为8位

DMA_PeripheralDataSize_HalfWord        数据宽度为16位

DMA_PeripheralDataSize_Word                    数据宽度为32位

存储器数据宽度:

DMA_InitStructure.DMA_MemoryDataSize = X;

DMA_MemoryDataSize_Byte                           数据宽度为8位

DMA_MemoryDataSize_HalfWord           数据宽度为16位

DMA_MemoryDataSize_Word              数据宽度为32位

模式选择:

DMA_InitStructure.DMA_Mode =x;

DMA_Mode_Circular: 循环模式

DMA_Mode_Normal:正常模式

通道软件优先级设置:

DMA_InitStructure.DMA_Priority =x;

DMA_Priority_VeryHigh                    最高优先级

DMA_Priority_High                       高优先级

DMA_Priority_Medium                   中优先级

DMA_Priority_Low                       低优先级

DMA内存到内存的传输使能:

DMA_InitStructure.DMA_M2M = x

DMA_M2M_Enable:使能

DMA_M2M_Disable:未使能

DMA初始化函数:

DMA_Init(DMAy_Channelx ,  &DMA_InitStructure);

DMA通道使能函数:

DMA_Cmd(DMAy_Channelx ,  ENABLE); //使能DMA通道1

注:在开始要定义结构体变量     DMA_InitTypeDef   DMA_InitStructure

5、NVIC相关:

优先级组设定:

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_x);

NVIC_PriorityGroup_0:          0 bits for pre-emption priority

4 bits for subpriority

NVIC_PriorityGroup_1:           1 bits for pre-emption priority

3 bits for subpriority

NVIC_PriorityGroup_2:           2 bits for pre-emption priority

2 bits for subpriority

NVIC_PriorityGroup_3:           3 bits for pre-emption priority

1 bits for subpriority

NVIC_PriorityGroup_4:           4 bits for pre-emption priority

0 bits for subpriority

设置中断:

NVIC_InitStructure.NVIC_IRQChannel =x;

ADC1_2_IRQn =  18,                        /*!< ADC1 and ADC2 global Interrupt*/

USB_HP_CAN1_TX_IRQn = 19,       /*!< USB Device High Priority or CAN1 TX Interrupts  */

USB_LP_CAN1_RX0_IRQn = 20,     /*!< USB Device Low Priority or CAN1 RX0 Interrupts */

CAN1_RX1_IRQn = 21,                /*!< CAN1 RX1 Interrupt*/

CAN1_SCE_IRQn = 22,                       /*!<CAN1 SCE Interrupt*/

EXTI9_5_IRQn = 23,                     /*!< External Line[9:5] Interrupts*/

TIM1_BRK_IRQn = 24,                 /*!< TIM1 Break Interrupt*/

TIM1_UP_IRQn = 25,                   /*!< TIM1 Update Interrupt*/

TIM1_TRG_COM_IRQn = 26,                   /*!< TIM1 Trigger and Commutation Interrupt*/

TIM1_CC_IRQn = 27,                   /*!< TIM1 Capture Compare Interrupt*/

TIM2_IRQn = 28,                           /*!< TIM2 global Interrupt */

TIM3_IRQn = 29,                           /*!< TIM3 global Interrupt */

TIM4_IRQn = 30,                           /*!< TIM4 global Interrupt*/

I2C1_EV_IRQn = 31,                     /*!< I2C1 Event Interrupt*/

I2C1_ER_IRQn = 32,                     /*!< I2C1 Error Interrupt*/

I2C2_EV_IRQn  = 33,                 /*!< I2C2 Event Interrupt */

I2C2_ER_IRQn = 34,                     /*!< I2C2 Error Interrupt */

SPI1_IRQn = 35,                             /*!< SPI1 global Interrupt*/

SPI2_IRQn = 36,                             /*!< SPI2 global Interrupt*/

USART1_IRQn = 37,                      /*!< USART1 global Interrupt*/

USART2_IRQn = 38,                      /*!< USART2 global Interrupt*/

USART3_IRQn  = 39,                  /*!< USART3 global Interrupt*/

EXTI15_10_IRQn = 40,                 /*!< External Line[15:10] Interrupts*/

RTCAlarm_IRQn = 41,                   /*!< RTC Alarm through EXTI Line Interrupt*/

USBWakeUp_IRQn = 42, /*!< USB Device WakeUp from suspend through EXTI Line Interrupt */

TIM8_BRK_IRQn = 43,                 /*!< TIM8 Break Interrupt */

TIM8_UP_IRQn = 44,                   /*!< TIM8 Update Interrupt\*/

TIM8_TRG_COM_IRQn = 45,        /*!< TIM8 Trigger and Commutation Interrupt

TIM8_CC_IRQn = 46,                   /*!< TIM8 Capture Compare Interrupt*/

ADC3_IRQn = 47,                          /*!< ADC3 global Interrupt*/

FSMC_IRQn = 48,                          /*!< FSMC global Interrupt */

SDIO_IRQn = 49,                             /*!< SDIO global Interrupt\*/

TIM5_IRQn = 50,                           /*!< TIM5 global Interrupt*/

SPI3_IRQn = 51,                             /*!< SPI3 global Interrupt*/

UART4_IRQn = 52,                                 /*!< UART4 global Interrupt */

UART5_IRQn = 53,                                 /*!< UART5 global Interrupt */

TIM6_IRQn = 54,                           /*!< TIM6 global Interrupt */

TIM7_IRQn = 55,                           /*!< TIM7 global Interrupt */

DMA2_Channel1_IRQn = 56,           /*!< DMA2 Channel 1 global Interrupt*/

DMA2_Channel2_IRQn = 57,          /*!< DMA2 Channel 2 global Interrupt */

DMA2_Channel3_IRQn = 58,          /*!< DMA2 Channel 3 global Interrupt*/

DMA2_Channel4_5_IRQn = 59,     /*!< DMA2 Channel 4 and Channel 5 global Interrupt*/

设置抢占优先级:

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =X;       //抢占优先级 X

设置子优先级为:

NVIC_InitStructure.NVIC_IRQChannelSubPriority =X;                      //抢占优先级 X

使能中断:

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                    //使能

NVIC初始化函数:

NVIC_Init(&NVIC_InitStructure);

注:在开始时需要定义结构体变量NVIC_InitTypeDef NVIC _InitStructure;

6、USART相关:

USART波特率选择:

USART_InitStructure.USART_BaudRate =XX;                    //波特率为XX bps

数据位位数选择:

USART_InitStructure.USART_WordLength = USART_WordLength_Xb;

//X为8或者9,即8位或者9位数据位

停止位位数选择:

USART_InitStructure.USART_StopBits = USART_StopBits_1;                   //停止位1位

#define USART_StopBits_1                     ((uint16_t)0x0000)

#define USART_StopBits_0_5                   ((uint16_t)0x1000)

#define USART_StopBits_2                     ((uint16_t)0x2000)

#define USART_StopBits_1_5                   ((uint16_t)0x3000)

有无校验位选择:

USART_InitStructure.USART_Parity = USART_Parity_No;                //无校验位

#define USART_Parity_No                      ((uint16_t)0x0000)

#define USART_Parity_Even                    ((uint16_t)0x0400)

#define USART_Parity_Odd                     ((uint16_t)0x0600)

有无硬件流控选择:

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

//无硬件流控

#define USART_HardwareFlowControl_None       ((uint16_t)0x0000)

#define USART_HardwareFlowControl_RTS        ((uint16_t)0x0100)

#define USART_HardwareFlowControl_CTS        ((uint16_t)0x0200)

#define USART_HardwareFlowControl_RTS_CTS    ((uint16_t)0x0300)

模式选择:

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式

#define USART_Mode_Rx                        ((uint16_t)0x0004)

#define USART_Mode_Tx                        ((uint16_t)0x0008)

配置串口参数函数:

USART_Init(USARTx, &USART_InitStructure);

USARTx 可选:USART1, USART2, USART3, UART4 or UART5

注:在开始时需要定义结构体变量USART_InitTypeDef   USART_InitStruct;

使能接收中断:

USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);

使能发送缓冲空中断:

USART_ITConfig(USARTx, USART_IT_TXE, ENABLE);

使能发送完成中断:

USART_ITConfig(USARTx, USART_IT_TC, ENABLE)

使能USART:

USART_Cmd(USARTx, ENABLE);

(0)

相关推荐