1.定时器定时串口打印
main.c
#include "stm32f4xx.h" // Device header
#include "serial.h"
#include "delay.h"
#include "tim.h"
extern uint16_t count;
int main(void)
{
Serial_Init();
TIM_Init();
printf("123success");
while (1)
{
printf("count=%d\r\n",count);
if((count%10)==1){
printf("interrupt\r\n");
}
if(count>1000){
count=0;
}
}
}
void TIM3_IRQHandler(){
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET){
count++;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
}
TIM.c
#include "stm32f4xx.h" // Device header
#define PSC 1680-1
uint16_t count=0;
void TIM_Init(){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitTypeDef TimeBaseInitStructure;
TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TimeBaseInitStructure.TIM_Period=1000-1;
TimeBaseInitStructure.TIM_Prescaler=PSC;
TIM_TimeBaseInit(TIM3,&TimeBaseInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//深入理解
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3,ENABLE);
}