1、使能定时器时钟: RCC_APB1PeriphClockCmd();
例如:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //开定时器3时钟
2、设置定时器的基本参数(计数方式、计数周期、分频系数等)
注:①计数方式有TIM_CounterMode_Up(向上计数)、TIM_CounterMode_Down(向下计数)等方式;
②分频系数(TIM_Prescaler)可以为1~65535之间的任意数;
③计数初值的计算:(计数周期 + 1) * (分频系数 + 1) / (计时器频率),定时器频率一般情况下都是默认的从AHB二分频之后再倍频得到的,所以说还是72M;
比如说,要定时100ms,即可写为:计数周期 = 999,分频系数 = 7199 ,即为(999 + 1) * (7199 + 1) / (72,000,000),因为
分频系数 / 计数器频率 |
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down; //向下计数
TIM_TimeBaseInitStruct.TIM_Period = 4999;
TIM_TimeBaseInitStruct.TIM_Prescaler = 7199;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
3、定时器中断配置:
/*定时器中断参数设置*/
TIM3->SR &= 0xFFFE; //清除update中断标志位,否则会出现刚配置完中断就进入中断服务函数的问题
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //配置为更新中断
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
4、使能定时器:
TIM_Cmd(TIM3,ENABLE);
5、编写中断服务函数:
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_FLAG_Update) != RESET)
{
TIM3->SR &= ~(0x01 << 0); //清除中断标志位
flag_1s ++;
flag_500ms ++;
}
if(flag_500ms >= 1)
{
flag_500ms = 0;
PBout(5) = !PBout(5);
}
if(flag_1s >= 2)
{
flag_1s = 0;
PDout(12) = !PDout(12);
}
}
附上测试源程序:
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "delay.h"
unsigned char time_count = 0;
unsigned char flag_500ms = 0;
unsigned char flag_1s = 0;
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_FLAG_Update) != RESET)
{
TIM3->SR &= ~(0x01 << 0); //清除中断标志位
flag_1s ++;
flag_500ms ++;
}
if(flag_500ms >= 1)
{
flag_500ms = 0;
PBout(5) = !PBout(5);
}
if(flag_1s >= 2)
{
flag_1s = 0;
PDout(12) = !PDout(12);
}
}
int main(void)
{
unsigned char clear = clear;
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组设置
delay_init();
/*LED指示灯初始化,用来标志全部初始化完成*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOD,&GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_SetBits(GPIOD,GPIO_Pin_12);
/*串口端口初始化*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //串口1时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA时钟使能
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
/*串口参数初始化*/
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStruct);
//USART1->SR = USART1->SR;
USART_Cmd(USART1,ENABLE);
/*定时器3基本参数初始化*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //开定时器3时钟
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down; //向下计数
TIM_TimeBaseInitStruct.TIM_Period = 4999;
TIM_TimeBaseInitStruct.TIM_Prescaler = 7199;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
/*定时器中断参数设置*/
TIM3->SR &= 0xFFFE; //清除update中断标志位,否则会出现刚配置完中断就进入中断服务函数的问题
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //配置为更新中断
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
/*初始化完成*/
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
GPIO_ResetBits(GPIOD,GPIO_Pin_12);
TIM_Cmd(TIM3,ENABLE);
while(1)
{
}
}
就先更新到这里,困了,有机会再更,今天上班的时候刚好对串口又加深了理解,有时间就更新一下。
有兴趣可以一起讨(chui)论(niu)啊!
qq:578294688
邮箱:578294688@qq.com