1.1 設(shè)計目的和意義 單片機是一種集成在電路芯片,是采用超大規(guī)模集成電路技術(shù)把具有數(shù)據(jù)處理能力的中央處理器CPU隨機存儲器RAM、只讀存儲器ROM、多種I/O口和中斷系統(tǒng)、定時器/計時器等功能(可能還包括顯示驅(qū)動電路、脈寬調(diào)制電路、模擬多路轉(zhuǎn)換器、A/D轉(zhuǎn)換器等電路)集成到一塊硅片上構(gòu)成的一個小而完善的計算機系統(tǒng)。采用單片機可以完成很多功能,現(xiàn)在很多電子產(chǎn)品都要用到單片機。 因為單片機這門課是一門實踐性很強的,單純學(xué)習(xí)課本不能掌握這門知識,經(jīng)過設(shè)計,我們的硬件設(shè)計能力和編程能力都能得到提升。 本次實驗采用的是基于stm32單片機設(shè)計的數(shù)字時鐘,與機械式時鐘相比具有更高的準確性和直觀性,且無機械裝置,具有更長的使用壽命,相比于數(shù)字邏輯電路,線路連接大大簡化,采用程序設(shè)計方法使得用戶使用時更加的靈活和簡便。
1.2 設(shè)計任務(wù)和要求 1、任務(wù) 設(shè)計一個基于STM32的數(shù)字電子時鐘,能實現(xiàn)計時和時間的顯示等功能。 2、要求 (1)用STM32設(shè)計數(shù)字電子時鐘,需要顯示時,分,秒。 (2)能通過按鍵調(diào)整時間。 (3)在上述基礎(chǔ)上可以任意發(fā)揮。 - #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;//時間設(shè)置
- u8 a = 0;
- u16 count;
- u8 countflag;//鬧鐘設(shè)置值
- u8 flag;//響鈴設(shè)置
- // 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);
- //時間設(shè)置
- 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; //進入調(diào)時模式
- 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再次按下
- {
- //退出調(diào)時模式
- 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);
- }
-
- //鬧鐘設(shè)置
- 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; //進入設(shè)置鬧鐘模式
- 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再次按下
- {
- //退出設(shè)置鬧鐘模式 進入時鐘運行模式
- 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)//開始計數(shù)
- {
- 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;//退出
- }}}}}}
復(fù)制代碼 Keil代碼下載:
程序.7z
(313.05 KB, 下載次數(shù): 125)
2022-5-14 19:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|