定时器:

0.结构体:TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
成员变量:
uint16_t TIM_Prescaler;
uint16_t TIM_CounterMode;
uint16_t TIM_Period;
uint16_t TIM_ClockDivision;
uint8_t TIM_RepetitionCounter; 针对高级定时器
1.时钟使能:定时器挂载在总线之下,所以我们通过对应总线的使能函数来使能定时器
例:RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); APB1与TIM3可变
2.初始化结构体
TIM_TimeBaseInitStructure.TIM_Period = arr; 自动重载计数周期值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; 分频系数
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; 计数方式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 时钟分频因子
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
3.开启定时器(大部分外设功能都要在使能后开启才能使用)
TIM_Cmd(TIM3,ENABLE); TIM3可变

GPIO端口:

0.结构体:GPIO_InitTypeDef GPIO_InitStructure;
成员变量:
uint32_t GPIO_Pin; 对应的PIN脚(GPIO而非GPIOx)
GPIOMode_TypeDef GPIO_Mode; 输出模式、输入模式、复用模式
GPIOSpeed_TypeDef GPIO_Speed; 传输速度
GPIOOType_TypeDef GPIO_OType; 推挽开漏
GPIOPuPd_TypeDef GPIO_PuPd; 上拉电阻,下拉电阻,不设置
1.时钟使能:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); AHB1和GPIOA可变
2.初始化结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 同时设置用|符号
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT/IN/AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP/OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL/UP/DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

CAN通信:

0.准备:由于CAN通信是对GPIO引脚的复用,所以需要调用对应的GPIO结构
GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11| GPIO_Pin_12; 查复用表
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 设为复用
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);
1.复用对应:
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1);
2.使能CAN:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
3.CAN初始化:
CAN_InitTypeDef CAN_InitStructure;

CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=ENABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;

CAN_InitStructure.CAN_Mode= mode;
CAN_InitStructure.CAN_SJW=tsjw;
CAN_InitStructure.CAN_BS1=tbs1;
CAN_InitStructure.CAN_BS2=tbs2;
CAN_InitStructure.CAN_Prescaler=brp;
波特率计算:波特率=时钟频率(通常选APB1,即42MHz)/[(tsjw+tbs1+tbs2)*brp]

CAN_Init(CAN1, &CAN_InitStructure);
4.滤波器初始化:
CAN_FilterInitTypeDef CAN_FilterInitStructure;

CAN_FilterInitStructure.CAN_FilterNumber=0; 过滤器名称
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList/Idmask; 列表/掩码
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
大小:标准帧16bit/32bit均可,扩展帧必须是32bit
CAN_FilterInitStructure.CAN_FilterIdHigh=((0x00010200<<3)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterIdLow=((0x00010200<<3)&0xffff)|CAN_ID_EXT; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=((0x00010300<<3)&0xffff0000)>>16;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=((0x00010300<<3)&0xffff)|CAN_ID_EXT; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0; 过滤到的邮箱 CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); 对于扩展帧: 前三位特殊:ID[0]=0,ID[1]=CAN_RTR_Data=0/Remote=1,ID[2]=CAN_ID_STD=0/EXT=1 第4位-第32位:ID[3-31]存储29位扩展ID FilterIdHigh/Low和FilterMaskIdHigh/Low控制的就是这ID[0-31] 其中High控制ID[16-31],Low控制ID[0-15] 0x00010200/0x00010300等就是需要过滤的29位ID,范围为0x00000000-0x1fffffff 但由于前三位特殊,所以采用跳过,即<<3的方法,此时就变为了0xid000的格式 用&0xffff0000取高十六位的值并>>16右移为0xXXXX,作为高十六位
用&0xffff取低十六位的值作为低十六位
由于29位id没有包含前三位,因而还需要对前三位做额外规定
即对IDLow|CAN_RTR_Data/Remote/CAN_ID_STD/EXT的处理
来过滤出需要的帧类型(数据扩展帧)
这是原理层面的,在实际应用时,我们只需要应用结论即可
即要过滤什么ID,就把这个ID放在原来的0xXXXXXXXX中
并根据所需的帧类型进行补充,那么就能过滤出需要的ID
对于标准帧:
前五位特殊:前三位为EXID,不做其他作用;第四位是IDE,第五位是RTR
第六位和第十六位为标准ID,共11位
因而在实际应用时,即把要过滤的ID<<5
并根据所需的帧类型进行补充,那么就能过滤出需要的ID

中断:

定时器更新中断:
0.中断结构体:
NVIC_InitTypeDef NVIC_InitStructure;
成员变量:
uint8_t NVIC_IRQChannel; 中断对象
uint8_t NVIC_IRQChannelPreemptionPriority; 抢占优先级
uint8_t NVIC_IRQChannelSubPriority; 响应优先级
FunctionalState NVIC_IRQChannelCmd; ENABLE即可
1.初始化中断结构体
NVIC_InitStructure.NVIC_IRQChannel=TIMx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
2.设置定时器TIMx允许TIM_IT_X中断(定时器更新中断)
TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE);
(清除标志位可以选做)
TIM_ClearFlag(TIMx,TIM_FLAG_Update);
TIM_ClearITPendingBit(TIMx,TIM_IT_Update);
3.编写中断处理函数
void TIMx_IRQHandler(void)
{
if(TIM_GetITStatus(TIMx,TIM_IT_Update)==SET)
{
……
}
TIM_ClearITPendingBit(TIMx,TIM_IT_Update);
TIM_ClearFlag(TIMx,TIM_FLAG_Update); 选做
}

CAN接收中断:
0.中断结构体:
NVIC_InitTypeDef NVIC_InitStructure;
1.初始化中断结构体:
NVIC_InitStructure.NVIC_IRQChannel = CANx_RXy_IRQn; 接收CANx_Rxy的中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
2.设置总线CANx允许CAN_IT_FMPx中断(邮箱接收中断)
CAN_ITConfig(CANx, CAN_IT_FMPy, ENABLE);
CAN_ClearFlag(CANx, CAN_FLAG_FMPy);
CAN_ClearITPendingBit(CANx,CAN_IT_FMPy);
3.编写中断处理函数
void CANx_RXy_IRQHandler(void)
{
if(CAN_GetITStatus(CANx,CAN_IT_FMPy)!=RESET)
{
……
}
CAN_ClearFlag(CANx, CAN_FLAG_FMPy);
CAN_ClearITPendingBit(CANx,CAN_IT_FMPy);
}

串口接收中断:
0.中断结构体:
NVIC_InitTypeDef NVIC_InitStructure;
1.初始化中断结构体:
NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
2.设置串口USARTx允许USART_IT_RXNE中断(接收数据中断)
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USARTx, USART_FLAG_RXNE);
USART_ClearITPendingBit(USARTx,USART_IT_RXNE);
3.编写中断服务函数:
void USARTx_IRQHandler(void)
{
OSIntEnter();
if(USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
{
……
}
OSIntExit();
USART_ClearFlag(USARTx, USART_FLAG_RXNE);
USART_ClearITPendingBit(USARTx,USART_IT_RXNE);
}

DMA数据传输完成中断:
void DMA2_Stream7_IRQHandler(void)
{
if(DMA_GetITStatus(DMA2_Stream7,DMA_IT_TCIF7)==SET)
{
……
}
DMA_ClearFlag (DMA2_Stream7,DMA_IT_TCIF7); DMA_ClearITPendingBit(DMA2_Stream7,DMA_IT_TCIF7);
}

串口USART:

0.GPIO复用:
0.1 GPIO结构体
GPIO_InitTypeDef GPIO_InitStructure;
0.2 使能时钟(GPIOx和USARTx)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
0.3 复位串口
USART_DeInit(USARTx);
0.4 复用端口
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
0.5 初始化结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 查表
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);

1.串口配置:
1.1USART结构体
USART_InitTypeDef USART_InitStructure;
1.2 初始化结构体
USART_InitStructure.USART_BaudRate = bound; 波特率,一般设置为 9600;
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);

2.使能串口:
USART_Cmd(USARTx, ENABLE);

DMA快速传输:

0.使能对应的DMA时钟:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMAx,ENABLE);
1.复位对应DMA的对应数据流:
DMA_DeInit(DMA_Streamx);
2.等待被传输的数据流禁止后,即可以配置时再开始初始化:
while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
3.初始化DMA:
DMA_InitTypeDef DMA_InitStructure;

DMA_InitStructure.DMA_Channel = chx; 通道选择:DMA1_Stream0~7/DMA2_Stream0~7
DMA_InitStructure.DMA_PeripheralBaseAddr = par; DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = mar; DMA 存储器 0 地址
DMA_InitStructure.DMA_DIR=DMA_DIR_MemoryToPeripheral; 存储器到外设模式
DMA_InitStructure.DMA_BufferSize = ndtr; 数据传输量
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; 外设是否为增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 内存是否为增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; 外设数据长度
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; 存储器数据长度
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA模式
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; 是否使用FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

DMA_Init(DMA_Streamx, &DMA_InitStructure);
4.把串口设置为DMA模式:(一般DMA借助串口实现)
USART_DMACmd(USARTx,USART_DMAReq_Tx,ENABLE);
5.开启传输:
DMA_Cmd(DMA_Streamx,DISABLE); 先关闭传输
while(DMA_GetCmdStatus(DMA_Streamx)!=DISABLE){} 等待DMA传输可用
DMA_SetCurrDataCounter(DMA_Streamx,ndtr); 设置传输长度ndtr
DMA_Cmd(DMA_Streamx,ENABLE); 开启传输,此时如果源内有数据会被发送到目标
6.传输对应表:

PWM脉宽调制(呼吸灯):

0.需要的结构体:
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; PWM设置结构体
1.使能端口和时钟:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
2.复用端口为时钟:
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM14);
3.GPIO初始化:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 复用
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
4.时钟初始化:
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);
5.调整时钟为PWM:(默认为冻结状态)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 设置定时器为PWM模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); 使能预装载寄存器
TIM_ARRPreloadConfig(TIM14,ENABLE); ARPE 使能
6.使能时钟:
TIM_Cmd(TIM14, ENABLE);