• Stm32_标准库_10_TIM_显示时间&日期


    利用TIM计数耗费1s,启动中断,秒表加一
    时间显示代码:

    #include "stm32f10x.h"    // Device header
    #include "Delay.h"
    #include "OLED.h"
    
    
    uint16_t num = 0;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    /*初始化通用定时器TIM2*/
    void Timer_Init(void){
    	   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//APB1外设开启
    	   
    	   TIM_InternalClockConfig(TIM2);//选择内部时钟
    	
    	   /*初始化时基单元*/
    	   TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    	   TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
    	   TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;//ARR自动重装
    	   TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;//psc预分频器
    	   TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//高级计时器内容直接给零
    	   //记录1s 
    	   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);//刚初始化完就会进中断
    	   
    	   TIM_ClearFlag(TIM2, TIM_FLAG_Update);//消除中断标志位
    	   //使能更新中断
    	   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    	
    	   /*配置中断*/
    	   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择组2
    	   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//定时器2在NVIC内的通道
    	   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    	   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    	   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    		 NVIC_Init(&NVIC_InitStructure);
    		 
    		 TIM_Cmd(TIM2, ENABLE);//启动定时器
    }
    unsigned char time[] = {23, 59, 30};
    unsigned int date[] = {2023, 12, 31};
    char month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    void Show_Time(void){
    	   OLED_ShowNum(1,1,time[0], 2);
    	   OLED_ShowString(1, 3, ":");
    	   OLED_ShowNum(1,4,time[1], 2);
    	   OLED_ShowString(1, 6, ":");
    	   OLED_ShowNum(1,7,time[2], 2);
    }
    void Time_Control(void){
    	       time[2] = time[2] + 1;
            if(time[2] >= 60){
              time[2] = 0;
              time[1] = time[1] + 1;
              if(time[1] >= 60){
                time[1] = 0;
                time[0] = time[0] + 1;
                if(time[0] >= 24){
                  time[0] = 0;
                  date[2] = date[2] + 1;
       	          if(date[2] >= month[date[1]] + 1){
       		        date[2] = 1;
       		        date[1] = date[1] + 1;
       		        if(date[1] >= 13){
       		        	date[1] = 1;
       		        	date[0] = date[0] + 1;
       		        	if(date[0] >= 9999){
       		        		date[0] = 2023;
    					   }
    				   }
    	           }
                }
             }
           }
    				 
    				Show_Time();
    }
    
     
    void TIM2_IRQHandler(void){//定时器2的中断函数,名字固定
    	   if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){
    			  
    			 TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清除标志位
    			 Time_Control();
    		 }
    		  
    }
    
     
    
     
    void month2_Control(void){//判别闰平年 
    	if((date[0] % 4 == 0 && date[0] % 100 != 0 )|| date[0] % 400 == 0) month[2] = 29;
    	else month[2] = 28;
    }
    
     
    
     
    int main(void){
    	 
    	OLED_Init();//初始化OLED
      Timer_Init();
    	while(1){ 
    		 // Show_Time();
    		    
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    在这里插入图片描述
    时间 + 日期代码:

    #include "stm32f10x.h"    // Device header
    #include "Delay.h"
    #include "OLED.h"
    
    
    uint16_t num = 0;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    /*初始化通用定时器TIM2*/
    void Timer_Init(void){
    	   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//APB1外设开启
    	   
    	   TIM_InternalClockConfig(TIM2);//选择内部时钟
    	
    	   /*初始化时基单元*/
    	   TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    	   TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
    	   TIM_TimeBaseInitStructure.TIM_Period = 10000 - 1;//ARR自动重装
    	   TIM_TimeBaseInitStructure.TIM_Prescaler = 7200 - 1;//psc预分频器
    	   TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//高级计时器内容直接给零
    	   //记录1s 
    	   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);//刚初始化完就会进中断
    	   
    	   TIM_ClearFlag(TIM2, TIM_FLAG_Update);//消除中断标志位
    	   //使能更新中断
    	   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    	
    	   /*配置中断*/
    	   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择组2
    	   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//定时器2在NVIC内的通道
    	   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    	   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    	   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    		 NVIC_Init(&NVIC_InitStructure);
    		 
    		 TIM_Cmd(TIM2, ENABLE);//启动定时器
    }
    unsigned char time[] = {22, 59, 30};
    unsigned int date[] = {2023, 12, 31};
    char month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    void Show_Time(void){
    	   OLED_ShowNum(1,1,time[0], 2);
    	   OLED_ShowString(1, 3, ":");
    	   OLED_ShowNum(1,4,time[1], 2);
    	   OLED_ShowString(1, 6, ":");
    	   OLED_ShowNum(1,7,time[2], 2);
    }
    void Show_Date(void){
    	   OLED_ShowNum(2,1,date[0], 4);
    	   OLED_ShowString(2, 5, "/");
    	   OLED_ShowNum(2,6,date[1], 2);
    	   OLED_ShowString(2, 7, "/");
    	   OLED_ShowNum(2,8,date[2], 2);
    }
    void Time_Control(void){
    	       time[2] = time[2] + 1;
            if(time[2] >= 60){
              time[2] = 0;
              time[1] = time[1] + 1;
              if(time[1] >= 60){
                time[1] = 0;
                time[0] = time[0] + 1;
                if(time[0] >= 24){
                  time[0] = 0;
                  date[2] = date[2] + 1;
       	          if(date[2] >= month[date[1]] + 1){
       		        date[2] = 1;
       		        date[1] = date[1] + 1;
       		        if(date[1] >= 13){
       		        	date[1] = 1;
       		        	date[0] = date[0] + 1;
       		        	if(date[0] >= 9999){
       		        		date[0] = 2023;
    					   }
    				   }
    	           }
                }
             }
           }
    				 
    				Show_Time();
    			  Show_Date();
    }
    
     
    void TIM2_IRQHandler(void){//定时器2的中断函数,名字固定
    	   if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){
    			  
    			 TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清除标志位
    			 Time_Control();
    		 }
    		  
    }
    
     
    
     
    void month2_Control(void){//判别闰平年 
    	if((date[0] % 4 == 0 && date[0] % 100 != 0 )|| date[0] % 400 == 0) month[2] = 29;
    	else month[2] = 28;
    }
    
     
    
     
    int main(void){
    	 
    	OLED_Init();//初始化OLED
      Timer_Init();
    	while(1){ 
    		 // Show_Time();
    		    
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    在这里插入图片描述

  • 相关阅读:
    webpack入门总结
    【blood group + transferase】
    Flink笔记整理(一)
    <Android开发> HAL层集成第三方so库
    并发容器介绍(一)
    Vue3-无限滚动的懒加载-本地数据操作版
    病毒攻防原理
    DOPE修饰岩藻多糖 Fucoidan-DOPE 岩藻多糖-二油酰基磷脂酰乙醇胺
    SG-Former实战:使用SG-Former实现图像分类任务(一)
    Mysql常用函数(汇总)
  • 原文地址:https://blog.csdn.net/xyint/article/details/133800394