代码如下:/*
* @Author: your name
* @Date: 2021-01-07 20:26:04
* @LastEditTime: 2021-01-07 22:17:08
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \projc:\Users\zj\Desktop\first_proj\main.c
*/
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_tim.h"
#include "misc.h"
#include "stdlib.h"
extern void delay_us(u16 time);
extern void delay_ms(u16 time);
void delay_us(u16 time)
{
u16 i=0;
while(time--)
{
i=10;
while(i--);
}
}
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--);
}
}
/*
1us : (1 + 1)*(35 + 1) / 72
1s: (1999 + 1)*(3499 + 1) / 72
*/
void timer2_init(void)
{
TIM_TimeBaseInitTypeDef timer2;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//打开定时器时钟
//timer2 参数配置
timer2.TIM_Period = 19999;//自动重装载值 ARR 0-65535
timer2.TIM_Prescaler = 3499;//定时器分频 PSC 0-65535
timer2.TIM_ClockDivision = TIM_CKD_DIV1;//定时器时钟分频 72M Hz
timer2.TIM_CounterMode = TIM_CounterMode_Up;//计数模式
TIM_TimeBaseInit(TIM2,&timer2);//定时器参数初始化
TIM_Cmd(TIM2,ENABLE);//定时器使能
}
void timer2_nvic(void)
{
NVIC_InitTypeDef timer2;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//0-3 0-3,每个中断的优先级由8位表示,实际起作用的只有4位(包含抢占和子优先级),这里主要是将优先级寄存器的4位分给抢占和子优先级,配置它们分别占多少位
timer2.NVIC_IRQChannel = TIM2_IRQn;
timer2.NVIC_IRQChannelSubPriority = 0;
timer2.NVIC_IRQChannelPreemptionPriority = 1;
timer2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&timer2);
}
int main(void)
{
uart1_init();
delay_ms(100);
timer2_init();
delay_ms(100);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//溢出中断
delay_ms(100);
timer2_nvic();
uart1_send("init ok\r\n");
while(1)
{
}
}
void TIM2_IRQHandler(void)
{
if(SET == TIM_GetITStatus(TIM2,TIM_IT_Update))
{
uart1_send("timer-irq-2\r\n");
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//手动清除中断标志
}
}
|