找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4393|回复: 8
收起左侧

基于stm32单片机的数字时钟可闹铃程序

  [复制链接]
ID:1023077 发表于 2022-5-14 11:49 | 显示全部楼层 |阅读模式
1.1    设计目的和意义
单片机是一种集成在电路芯片,是采用超大规模集成电路技术把具有数据处理能力的中央处理器CPU随机存储器RAM、只读存储器ROM、多种I/O口和中断系统、定时器/计时器等功能(可能还包括显示驱动电路、脉宽调制电路、模拟多路转换器、A/D转换器等电路)集成到一块硅片上构成的一个小而完善的计算机系统。采用单片机可以完成很多功能,现在很多电子产品都要用到单片机。
因为单片机这门课是一门实践性很强的,单纯学习课本不能掌握这门知识,经过设计,我们的硬件设计能力和编程能力都能得到提升。
本次实验采用的是基于stm32单片机设计的数字时钟,与机械式时钟相比具有更高的准确性和直观性,且无机械装置,具有更长的使用寿命,相比于数字逻辑电路,线路连接大大简化,采用程序设计方法使得用户使用时更加的灵活和简便。

1.2 设计任务和要求            
1、任务
设计一个基于STM32的数字电子时钟,能实现计时和时间的显示等功能。
2、要求
(1)用STM32设计数字电子时钟,需要显示时,分,秒。
(2)能通过按键调整时间。
(3)在上述基础上可以任意发挥。

附录Ⅲ 部分程序清单
MAIN.C:
  1. #include "stm32f10x.h"
  2. #include "Key.h"
  3. #include "Led.h"
  4. #include "delay.h"
  5. #include "smg.h"
  6. #include "timer.h"
  7. #include "beep.h"

  8. int  sec,min,hour;//时钟
  9. u8 h,m,s;//闹钟
  10. u8 setflag;//时间设置
  11. u8 a = 0;
  12. u16 count;
  13. u8 countflag;//闹钟设置值
  14. u8 flag;//响铃设置

  15. // void delay_ms(u32 ms)               
  16. //{
  17. //        u32 i;
  18. //        while(ms--)
  19. //        {
  20. //       
  21. //        for(i=350;i>0;i--);
  22. //        }
  23. //}
  24. int main()
  25. {
  26.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  27.         Timer3_Init(9999,799);
  28.         Timer2_Init(9999,799);//proteus里面的晶振频率是8M;
  29.         LED_Init();
  30.         Key_Init();
  31.         delay_init();
  32.         BEEP_Init();
  33.         Smg_Init();
  34.         hour = 7;
  35.         min = 59;
  36.         sec = 52;       
  37.         h = 0;
  38.         m = 0;
  39.         s = 0;
  40.         GPIO_ResetBits(GPIOB,GPIO_Pin_1);//cs
  41.                 GPIO_SetBits(GPIOB,GPIO_Pin_2);// 闹铃指示灯不亮LED0
  42.         GPIO_SetBits(GPIOB,GPIO_Pin_3);//未进入闹钟模式
  43.         //GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_SET);
  44.         while(1)
  45.         {                GPIO_ResetBits(GPIOB,GPIO_Pin_1);
  46.                
  47.                 //实时时间显示
  48.                 Smg_DisPlay();
  49.                 Display_Data(hour,min,sec);

  50.                 //时间设置
  51.                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//K0按下
  52.                 {
  53.                         delay_ms(3);                                     //消抖
  54.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)
  55.                         {
  56.                                 while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
  57.                                 setflag = 1;               //进入调时模式
  58.                                 TIM_Cmd(TIM3,DISABLE);     //TI3失能
  59.                         }
  60.                 }
  61.                 while(setflag)
  62.                 {
  63.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)//k1按下,h++
  64.                         {
  65.                                 delay_ms(3);
  66.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
  67.                                 {
  68.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)));
  69.                                         hour++;
  70.                                         if(hour == 24)
  71.                                                 hour = 0;
  72.                                 }
  73.                         }
  74.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)//k2按下,m++
  75.                         {
  76.                                 delay_ms(3);
  77.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
  78.                                 {
  79.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)));
  80.                                         min++;
  81.                                         if(min == 60)
  82.                                                 min = 0;
  83.                                 }
  84.                         }
  85.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)//k7按下,h--
  86.                         {
  87.                                 delay_ms(3);
  88.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)
  89.                                 {
  90.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)));
  91.                                         hour--;
  92.                                         if(hour == -1)
  93.                                                 hour = 23;
  94.                                 }
  95.                         }
  96.                        
  97.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)//k8按下,m--
  98.                         {
  99.                                 delay_ms(3);
  100.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)
  101.                                 {
  102.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)));
  103.                                         min--;
  104.                                         if(min == -1)
  105.                                                 min = 59;
  106.                                 }
  107.                         }
  108.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//k0再次按下
  109.                         {
  110.                                 //退出调时模式
  111.                                 delay_ms(3);
  112.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)
  113.                                 {
  114.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
  115.                                         setflag = 0;
  116.                                         TIM_Cmd(TIM3,ENABLE);//TI3使能
  117.                                         break;
  118.                                 }
  119.                         }
  120.                         Smg_DisPlay();
  121.                         Display_Data(hour,min,sec);
  122.                 }
  123.        
  124.                 //闹钟设置
  125.                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)//k2按下
  126.                 {
  127.                         delay_ms(3);
  128.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
  129.                         {
  130.                                 while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)));
  131.                                 countflag = 1;                           //进入设置闹钟模式
  132.                                 if(a == 0)
  133.                                 {
  134.                                         a = 1;
  135.                                         TIM_Cmd(TIM2,DISABLE);
  136.                                 }
  137.                                 else
  138.                                 {
  139.                                         a = 0;
  140.                                         countflag = 0;//退出定时模式
  141.                                         TIM_Cmd(TIM2,ENABLE);//开启定时器停止定时
  142.                                         count = 0;
  143.                                 }
  144.                         }
  145.                 }
  146.                 while(countflag)//进入闹钟模式
  147.                 {    GPIO_ResetBits(GPIOB,GPIO_Pin_3);//蓝灯亮
  148.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)//k1,m++
  149.                         {
  150.                                 delay_ms(3);
  151.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
  152.                                 {
  153.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)));
  154.                                         m++;
  155.                                         if(m == 60)
  156.                                                 m = 0;
  157.                                 }
  158.                         }
  159.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//k0,h++
  160.                         {
  161.                                 delay_ms(3);
  162.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==0)
  163.                                 {
  164.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
  165.                                         h++;
  166.                                         if(h == 24)
  167.                                                 h = 0;
  168.                                 }
  169.                         }
  170.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)//k7按下,h--
  171.                         {
  172.                                 delay_ms(3);
  173.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)
  174.                                 {
  175.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)));
  176.                                         hour--;
  177.                                         if(hour == -1)
  178.                                                 hour = 23;
  179.                                 }
  180.                         }
  181.                        
  182.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)//k8按下,m--
  183.                         {
  184.                                 delay_ms(3);
  185.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)
  186.                                 {
  187.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)));
  188.                                         min--;
  189.                                         if(min == -1)
  190.                                                 min = 59;
  191.                                 }
  192.                         }
  193.                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)//k2再次按下
  194.                   {
  195.                                 //退出设置闹钟模式 进入时钟运行模式
  196.                                 delay_ms(3);
  197.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
  198.                                 {
  199.                                         while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)));
  200.                                         countflag = 0;
  201.                                         TIM_Cmd(TIM2,ENABLE);//开启定时器2闹钟定时,即开启了count++,用于判断后面的
  202.                                         flag = 1;
  203.                                         break;
  204.                                 }
  205.                   }
  206.                        
  207.                         Smg_DisPlay();
  208.                         Display_Data(h,m,s);
  209.                 }
  210.                                 while(flag)//开始计数
  211.                 {
  212.                         Smg_DisPlay();
  213.                         Display_Data(hour,min,sec);               
  214.                         if(count == CountTime(h,m))/////////////////////////////......................
  215.                         {   GPIO_SetBits(GPIOB,GPIO_Pin_4);//开始响
  216.                                 GPIO_ResetBits(GPIOB,GPIO_Pin_2);//亮红灯
  217.                                 if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)//k1按下退出闹铃
  218.                                 {
  219.                                         delay_ms(3);
  220.                                         if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
  221.                                         {
  222.                                                 while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)));
  223.                                                 GPIO_SetBits(GPIOB,GPIO_Pin_2);//灭 红灯
  224.                                                 GPIO_ResetBits(GPIOB,GPIO_Pin_4);//不响
  225.                                                 TIM_Cmd(TIM2,DISABLE);
  226.                                                 flag = 0;
  227.                                                 break;//退出
  228. }}}}}}
复制代码
Keil代码下载: 程序.7z (313.05 KB, 下载次数: 120)

评分

参与人数 1黑币 +10 收起 理由
admin + 10

查看全部评分

回复

使用道具 举报

ID:1023077 发表于 2022-5-14 11:51 | 显示全部楼层
Proteus仿真图只能用8.11版本打开
回复

使用道具 举报

ID:1047685 发表于 2022-10-14 16:31 | 显示全部楼层
Proteus仿真图只能用8.11版本打开
回复

使用道具 举报

ID:608154 发表于 2022-10-23 09:42 来自手机 | 显示全部楼层
这是库函数版本,有寄存器版本吗
回复

使用道具 举报

ID:1054345 发表于 2022-11-28 11:43 | 显示全部楼层
有STM32CubeIDE_1.8.0用MP157A芯片写的程序不
回复

使用道具 举报

ID:1072425 发表于 2023-4-19 11:00 来自手机 | 显示全部楼层
请问芯片用的是哪个
回复

使用道具 举报

ID:736661 发表于 2023-7-14 08:21 | 显示全部楼层
请教下CountTime(h,m)这个函数怎么能学习一下
回复

使用道具 举报

ID:736661 发表于 2023-7-14 08:22 | 显示全部楼层
请教一下 这个CountTime(h,m)怎么能学习一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表