#include "stm32f10x_lib.h"
#include "usart.h"
#include "delay.h"
#include "sys_config.h"
#include <math.h> //Keil library
#define uchar unsigned char
#define uint unsigned int
//通用定时器中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器1
void Timerx_Init(u16 arr,u16 psc)
{
RCC->APB2ENR|=1<<11;//TIM1时钟使能 1<<3 = 8
TIM1->ARR=arr; //设定计数器自动重装值//刚好1ms
TIM1->PSC=psc; //预分频器7200,得到10Khz的计数时钟
//这两个东西要同时设置才可以使用中断
TIM1->DIER|=1<<0; //允许更新中断
TIM1->DIER|=1<<6; //允许触发中断
TIM1->CR1|=0x01; //使能定时器1
MY_NVIC_Init(1,3,TIM1_UP_IRQChannel,2);//抢占1,子优先级3,组2 //是TIM1_UP_IRQChannel不是通用定时器的TIM1_IRQChannel
//MY_NVIC_Init(0,3,TIM1_BRK_IRQChannel,2);//抢占1,子优先级3,组2
//MY_NVIC_Init(2,3,TIM1_TRG_COM_IRQChannel,2);//抢占1,子优先级3,组2
//MY_NVIC_Init(3,3,TIM1_CC_IRQChannel,2);//抢占1,子优先级3,组2
}
int main(void)
{
Stm32_Clock_Init(3); //配置RCC 24m
Delayms(10);
GPIO_Configuration(); //配置GPIO
USART1_Configuration(); //配置串口1
Timerx_Init(10000,2399);
while(1)
{
// USART1_SendData('X');
// USART1_SendData(0X0D); //换行
// USART1_SendData(0X0A); //回车
Delayms(5); //延时
}
}
//定时器1中断服务程序
void TIM1_UP_IRQHandler(void)
{
if(TIM1->SR&0X0001)//溢出中断
{
USART1_SendData('*'); //每过1秒,就发送一个字符
}
TIM1->SR&=~(1<<0);//清除中断标志位
}