stm32 对正点原子RTC实时时钟例程的一些改编

stm32 对正点原子RTC实时时钟例程的一些改编

开发板为正点原子stm32f103nano。改编内容分别为每秒所有灯翻转、每秒随机开关灯和二进制日期显示。只对main.c文件进行更改,需要移植的复制代码替换main.c文件的代码即可。具体说明见注释。

每秒所有灯翻转

#include "timer.h"
#include "usmart.h"

//共阴数字数组
// 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, .,全灭
u8 smg_num[] = {0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0xee, 0x3e, 0x9c, 0x7a, 0x9e, 0x8e, 0x01, 0x00};

int main(void)
{

    HAL_Init();                    	//初始化HAL库
    Stm32_Clock_Init(RCC_PLL_MUL9); //设置时钟,72M
    delay_init(72);                 //初始化延时函数
    LED_SMG_Init();	        //初始化数码管
    uart_init(115200);	 	//串口初始化为115200
    LED_Init();				//初始化与LED连接的硬件接口
    usmart_dev.init(72);	//初始化USMART
    TIM3_Init(19, 7199);    //数码管2ms定时显示
LED0 = !LED0;
				LED2=!LED2;
				LED4=!LED4; 
				LED6=!LED6;
    while (RTC_Init())		//RTC初始化,一定要初始化成功
    {
        printf("RTC ERROR!\r\n");
        delay_ms(800);
        printf("RTC Trying...\r\n");
    }

    while (1)
    {
    }
}

u8 smg_wei = 0; //数码管位选
u8 num = 0; //数码管数值
u8 time = 0; //时间值


//回调函数,定时器中断服务函数调用
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim == (&TIM3_Handler))
    {
        switch (smg_wei)
        {
            case 0:
                num = smg_num[calendar.hour / 10];
                break;//时

            case 1:
                num = smg_num[calendar.hour % 10];
                break;

            case 2:
            case 5:
                num = 0x02;
                break;

            case 3:
                num = smg_num[calendar.min / 10];
                break; //分

            case 4:
                num = smg_num[calendar.min % 10];
                break;

            case 6:
                num = smg_num[calendar.sec / 10];
                break; //秒

            case 7:
                num = smg_num[calendar.sec % 10];
                break;
        }
 
        if (time != calendar.sec) //LED灯每秒翻转
        {
            time = calendar.sec;
            LED0 = !LED0;
					LED1=!LED1; 
					LED2=!LED2; 
					LED3=!LED3; 
					LED4=!LED4; 
					LED5=!LED5; 
					LED6=!LED6; 
					LED7=!LED7; 
        }

        LED_Write_Data(num, smg_wei); //写数据到数码管
        LED_Refresh();//更新显示
        smg_wei++;
 
        if (smg_wei == 8) smg_wei = 0;
    }
                
}

每秒随机开关灯(与上一个程序相同的内容不重写注释,请参考上一个程序)

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "rtc.h"
#include "smg.h"
#include "timer.h"
#include "usmart.h"
#include<stdlib.h>

// ¹²ÒõÊý×ÖÊý×é
// 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, .,È«Ãð
u8 smg_num[] = {0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0xee, 0x3e, 0x9c, 0x7a, 0x9e, 0x8e, 0x01, 0x00};
int i=0;
int main(void)
{srand(calendar.sec); //把启动时的秒数设为随机数种子
    HAL_Init();                    	//³õʼ»¯HAL¿â
    Stm32_Clock_Init(RCC_PLL_MUL9); //ÉèÖÃʱÖÓ,72M
    delay_init(72);                 //³õʼ»¯ÑÓʱº¯Êý
    LED_SMG_Init();	        //ÊýÂë¹Ü³õʼ»¯
    uart_init(115200);	 	//´®¿Ú³õʼ»¯Îª115200
    LED_Init();				//³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
    usmart_dev.init(72);	//³õʼ»¯USMART
    TIM3_Init(19, 7199);    //ÊýÂë¹Ü2ms¶¨Ê±ÏÔʾ

    while (RTC_Init())		//RTC³õʼ»¯£¬Ò»¶¨Òª³õʼ»¯³É¹¦
    {
        printf("RTC ERROR!\r\n");
        delay_ms(800);
        printf("RTC Trying...\r\n");
    }

    while (1)
    {
    }
}

u8 smg_wei = 0; //ÊýÂë¹Üλѡ
u8 num = 0; //ÊýÂë¹ÜÊýÖµ
u8 time = 0; //ʱ¼äÖµ


//»Øµ÷º¯Êý£¬¶¨Ê±Æ÷ÖжϷþÎñº¯Êýµ÷ÓÃ
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim == (&TIM3_Handler))
    {
        switch (smg_wei)
        {
            case 0:
                num = smg_num[calendar.hour / 10];
                break;//ʱ

            case 1:
                num = smg_num[calendar.hour % 10];
                break;

            case 2:
            case 5:
                num = 0x02;
                break;

            case 3:
                num = smg_num[calendar.min / 10];
                break; //·Ö

            case 4:
                num = smg_num[calendar.min % 10];
                break;

            case 6:
                num = smg_num[calendar.sec / 10];
                break; //Ãë

            case 7:
                num = smg_num[calendar.sec % 10];
                break;
        }
 
        if (time != calendar.sec) //随机开关灯
        {
            time = calendar.sec;
            i=rand()%256;
					if(i>127)  //通过取余转换进制并点亮相应LED灯
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET); //LED7亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET); //LED7灭
						}
						if(i%128>63)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET); //LED6亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET); //LED6灭
						}
						if(i%64>31)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET); //LED5亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET); //LED5灭
						}
						if(i%32>15)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET); //LED4亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET); //LED4灭
						}
						if(i%16>7)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_RESET); //LED3亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_SET); //LED3灭
						}
						if(i%8>3)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET); //LED2亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET); //LED2灭
						}
						if(i%4>1)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET); //LED1亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); //LED1灭
						}
						if(i%2>0)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); //LED0亮
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); //LED0灭
						}
        }

        LED_Write_Data(num, smg_wei); //дÊý¾Ýµ½ÊýÂë¹Ü
        LED_Refresh();//¸üÐÂÏÔʾ
        smg_wei++;
 
        if (smg_wei == 8) smg_wei = 0;
    }
                
}

二进制日期显示(与上一个程序相同的内容不重写注释,请参考上一个程序)

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "rtc.h"
#include "smg.h"
#include "timer.h"
#include "usmart.h"

// ¹²ÒõÊý×ÖÊý×é
// 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, .,È«Ãð
u8 smg_num[] = {0xfc, 0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0xee, 0x3e, 0x9c, 0x7a, 0x9e, 0x8e, 0x01, 0x00};

int main(void)
{

    HAL_Init();                    	//³õʼ»¯HAL¿â
    Stm32_Clock_Init(RCC_PLL_MUL9); //ÉèÖÃʱÖÓ,72M
    delay_init(72);                 //³õʼ»¯ÑÓʱº¯Êý
    LED_SMG_Init();	        //ÊýÂë¹Ü³õʼ»¯
    uart_init(115200);	 	//´®¿Ú³õʼ»¯Îª115200
    LED_Init();				//³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
    usmart_dev.init(72);	//³õʼ»¯USMART
    TIM3_Init(19, 7199);    //ÊýÂë¹Ü2ms¶¨Ê±ÏÔʾ

    while (RTC_Init())		//RTC³õʼ»¯£¬Ò»¶¨Òª³õʼ»¯³É¹¦
    {
        printf("RTC ERROR!\r\n");
        delay_ms(800);
        printf("RTC Trying...\r\n");
    }

    while (1)
    {
    }
}

u8 smg_wei = 0; //ÊýÂë¹Üλѡ
u8 num = 0; //ÊýÂë¹ÜÊýÖµ
u8 time = 0; //ʱ¼äÖµ


//»Øµ÷º¯Êý£¬¶¨Ê±Æ÷ÖжϷþÎñº¯Êýµ÷ÓÃ
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim == (&TIM3_Handler))
    {
        switch (smg_wei)
        {
            case 0:
                num = smg_num[calendar.hour / 10];
                break;//ʱ

            case 1:
                num = smg_num[calendar.hour % 10];
                break;

            case 2:
            case 5:
                num = 0x02;
                break;

            case 3:
                num = smg_num[calendar.min / 10];
                break; //·Ö

            case 4:
                num = smg_num[calendar.min % 10];
                break;

            case 6:
                num = smg_num[calendar.sec / 10];
                break; //Ãë

            case 7:
                num = smg_num[calendar.sec % 10];
                break;
        }
 
        if (time != calendar.sec) 
        {
            time = calendar.sec;
            int i=calendar.w_date;
					  int m=calendar.w_month;
					  int a=m%8;
          	if(i>15)  //通过取余转换进制并点亮相应LED灯,LED0到LED4显示日期
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET); //LED4ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET); //LED4Ãð
						}
						if(i%16>7)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_RESET); //LED3ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_3, GPIO_PIN_SET); //LED3Ãð
						}
						if(i%8>3)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET); //LED2ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_SET); //LED2Ãð
						}
						if(i%4>1)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_RESET); //LED1ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_1, GPIO_PIN_SET); //LED1Ãð
						}
						if(i%2>0)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET); //LED0ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET); //LED0Ãð
						}	
           	if(a%8>3)  //通过取余转换进制并点亮相应LED灯,LED5到LED7显示月份
						{          //由于LED灯数量不足,显示的是月份除以8的余数
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_RESET); //LED7ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET); //LED7Ãð
						}
						if(a%4>1)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET); //LED6ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET); //LED6Ãð
						}
						if(a%2>0)
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET); //LED5ÁÁ
						}else
						{
							HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET); //LED5Ãð
						}					
        }

        LED_Write_Data(num, smg_wei); //дÊý¾Ýµ½ÊýÂë¹Ü
        LED_Refresh();//¸üÐÂÏÔʾ
        smg_wei++;
 
        if (smg_wei == 8) smg_wei = 0;
    }
                
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路易斯720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值