使用STM32F103RC芯片并且使用固件库编写的时钟日历
这种综合型的任务,一定要分任务块进行编写
任务一:串口部分
首先要进行串口初始化配置(这里一般使用串行通讯)
然后进行串口服务函数编写
任务二:定时器中断
首先要进行定时器中断初始化配置
然后进行定时器中断服务函数编写
#include "stm32f10x.h"
#include "stdio.h"
#define KEY0 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)//读KEY0
#define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)//读KEY1
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)//读WK_UP
#define USART_REC_LEN 200
int count = 0;
u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15, 接收完成标志0x0a
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字节数目
u16 USART_RX_STA=0; //接收状态标记
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
else USART_RX_STA|=0x8000; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
}
}
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//等待发送完毕
USART1->DR = (u8) ch; //发送单个字符
return ch;
}
void Delay(unsigned long u)
{
unsigned long i,j;
for(i=0;i<u;i++) for(j=0;j<12000;j++);
}
typedef struct DAT{
unsigned int year;
unsigned int month;
unsigned int day;
}str1;
typedef struct TIME{
unsigned int hour;
unsigned int min;
unsigned int sec;
unsigned int ms;
}str2;
str1 t1;
str2 t2;
//初始化定时器
void TIM3_Init_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;//配置定时器结构体
NVIC_InitTypeDef NVIC_InitStrue;//配置NVIC中断控制器结构体
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3,ENABLE);//使能时钟 定时器时钟
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//系统优先级分组,设置为2;
TIM_TimeBaseInitStrue.TIM_Period =arr;
TIM_TimeBaseInitStrue.TIM_Prescaler=psc;
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up;//计数模式 选向上
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1;//1分频 也就是不分频
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue);
TIM_Cmd (TIM3,ENABLE);//定时器使能 和串口很像
//配置定时器与中断线联系创建
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
NVIC_InitStrue .NVIC_IRQChannel =TIM3_IRQn ;//TIM3中断
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority =0;//先占优先级0级
NVIC_InitStrue.NVIC_IRQChannelSubPriority =3;//从优先级3级
NVIC_InitStrue.NVIC_IRQChannelCmd =ENABLE;//IRQ通道被使能
NVIC_Init (&NVIC_InitStrue);//初始化NVIC寄存器
}
void TIM3_IRQHandler(void)
{
static uint16_t temp;//静态变量 变量保持
if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET) //判断定时器中断标志位是否被打开
{
t2.ms ++;
if(t2.ms==1000){
t2.ms=0;
t2.sec=t2.sec+1;
}
if(t2.sec==60){
t2.min=t2.min+1;
t2.sec=0;
}
if(t2.min==60){
t2.min=t2.min+1;
t2.min=0;
}
if(t2.hour==24){
t2.hour=0;
t1.day =t1.day +1;
}
if(t1.day==31)
{
t1.month =t1.month +1;
}
if(t1.month==12)
{
t1.year =t1.year +1;
}
}
TIM_ClearITPendingBit( TIM3, TIM_IT_Update);//最后清除定时器中断标志位
}
int main(void)
{
TIM3_Init_Init(1000,71);
uart_init(115200);
t1.year= 2022 ;
t1.month= 10;
t1.day=10;
t2.hour=19;
t2.min =15;
t2.sec =888;
while(1)
{
printf("%d %d %d %d %d %d\n",t1.year,t1.month,t1.day ,t2.hour,t2.min,t2.sec);
}
}
这里面还用到了如何使用printf语句在串口助手输出
后面会出详细解说