1.1 设计目的和意义 单片机是一种集成在电路芯片,是采用超大规模集成电路技术把具有数据处理能力的中央处理器CPU随机存储器RAM、只读存储器ROM、多种I/O口和中断系统、定时器/计时器等功能(可能还包括显示驱动电路、脉宽调制电路、模拟多路转换器、A/D转换器等电路)集成到一块硅片上构成的一个小而完善的计算机系统。采用单片机可以完成很多功能,现在很多电子产品都要用到单片机。 因为单片机这门课是一门实践性很强的,单纯学习课本不能掌握这门知识,经过设计,我们的硬件设计能力和编程能力都能得到提升。 本次实验采用的是基于stm32单片机设计的数字时钟,与机械式时钟相比具有更高的准确性和直观性,且无机械装置,具有更长的使用寿命,相比于数字逻辑电路,线路连接大大简化,采用程序设计方法使得用户使用时更加的灵活和简便。
1.2 设计任务和要求 1、任务 设计一个基于STM32的数字电子时钟,能实现计时和时间的显示等功能。 2、要求 (1)用STM32设计数字电子时钟,需要显示时,分,秒。 (2)能通过按键调整时间。 (3)在上述基础上可以任意发挥。 - #include "stm32f10x.h"
- #include "Key.h"
- #include "Led.h"
- #include "delay.h"
- #include "smg.h"
- #include "timer.h"
- #include "beep.h"
- int sec,min,hour;//时钟
- u8 h,m,s;//闹钟
- u8 setflag;//时间设置
- u8 a = 0;
- u16 count;
- u8 countflag;//闹钟设置值
- u8 flag;//响铃设置
- // void delay_ms(u32 ms)
- //{
- // u32 i;
- // while(ms--)
- // {
- //
- // for(i=350;i>0;i--);
- // }
- //}
- int main()
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- Timer3_Init(9999,799);
- Timer2_Init(9999,799);//proteus里面的晶振频率是8M;
- LED_Init();
- Key_Init();
- delay_init();
- BEEP_Init();
- Smg_Init();
- hour = 7;
- min = 59;
- sec = 52;
- h = 0;
- m = 0;
- s = 0;
- GPIO_ResetBits(GPIOB,GPIO_Pin_1);//cs
- GPIO_SetBits(GPIOB,GPIO_Pin_2);// 闹铃指示灯不亮LED0
- GPIO_SetBits(GPIOB,GPIO_Pin_3);//未进入闹钟模式
- //GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_SET);
- while(1)
- { GPIO_ResetBits(GPIOB,GPIO_Pin_1);
-
- //实时时间显示
- Smg_DisPlay();
- Display_Data(hour,min,sec);
- //时间设置
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//K0按下
- {
- delay_ms(3); //消抖
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
- setflag = 1; //进入调时模式
- TIM_Cmd(TIM3,DISABLE); //TI3失能
- }
- }
- while(setflag)
- {
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)//k1按下,h++
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)));
- hour++;
- if(hour == 24)
- hour = 0;
- }
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)//k2按下,m++
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)));
- min++;
- if(min == 60)
- min = 0;
- }
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)//k7按下,h--
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)));
- hour--;
- if(hour == -1)
- hour = 23;
- }
- }
-
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)//k8按下,m--
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)));
- min--;
- if(min == -1)
- min = 59;
- }
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//k0再次按下
- {
- //退出调时模式
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
- setflag = 0;
- TIM_Cmd(TIM3,ENABLE);//TI3使能
- break;
- }
- }
- Smg_DisPlay();
- Display_Data(hour,min,sec);
- }
-
- //闹钟设置
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)//k2按下
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)));
- countflag = 1; //进入设置闹钟模式
- if(a == 0)
- {
- a = 1;
- TIM_Cmd(TIM2,DISABLE);
- }
- else
- {
- a = 0;
- countflag = 0;//退出定时模式
- TIM_Cmd(TIM2,ENABLE);//开启定时器停止定时
- count = 0;
- }
- }
- }
- while(countflag)//进入闹钟模式
- { GPIO_ResetBits(GPIOB,GPIO_Pin_3);//蓝灯亮
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)//k1,m++
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)));
- m++;
- if(m == 60)
- m = 0;
- }
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13) == 0)//k0,h++
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
- h++;
- if(h == 24)
- h = 0;
- }
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)//k7按下,h--
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)));
- hour--;
- if(hour == -1)
- hour = 23;
- }
- }
-
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)//k8按下,m--
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)));
- min--;
- if(min == -1)
- min = 59;
- }
- }
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)//k2再次按下
- {
- //退出设置闹钟模式 进入时钟运行模式
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)));
- countflag = 0;
- TIM_Cmd(TIM2,ENABLE);//开启定时器2闹钟定时,即开启了count++,用于判断后面的
- flag = 1;
- break;
- }
- }
-
- Smg_DisPlay();
- Display_Data(h,m,s);
- }
- while(flag)//开始计数
- {
- Smg_DisPlay();
- Display_Data(hour,min,sec);
- if(count == CountTime(h,m))/////////////////////////////......................
- { GPIO_SetBits(GPIOB,GPIO_Pin_4);//开始响
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);//亮红灯
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)//k1按下退出闹铃
- {
- delay_ms(3);
- if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
- {
- while(!(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)));
- GPIO_SetBits(GPIOB,GPIO_Pin_2);//灭 红灯
- GPIO_ResetBits(GPIOB,GPIO_Pin_4);//不响
- TIM_Cmd(TIM2,DISABLE);
- flag = 0;
- break;//退出
- }}}}}}
复制代码 Keil代码下载:
程序.7z
(313.05 KB, 下载次数: 125)
|