希望可以幫到你
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
單片機源程序如下:
- #include "stm32f10x.h"
- #include "Delay.h"
- #include "smg.h"
- #include "sys.h"
- #include "timer.h"
- #include "led.h"
- #include "fmq.h"
- #include "key.h"
- #include "stm32f10x_tim.h"
- //定義0~9十個數字的字型碼表
- uint16_t table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x80};//段碼
- //顯示0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -, .
- uint16_t wei[]={0x0fe,0x0fd,0x0fb,0x0f7,0x0ef,0x0df,0x0bf,0x07f}; //位碼;
- uint8_t num[8];
- uint16_t t,i,k; //
- uint16_t L1=LED1OFF;//F1=FMQ1OFF;//不能放在一行定義
- uint16_t F1=FMQ1OFF;
- uint16_t hour,minute,secend; //原有定義時間
- uint16_t hour1,minute1,secend1; //保護修改鬧鈴當前時間,恢復用
- uint16_t hour2,minute2,secend2; //設鬧鈴
- int main(void)
- {
- hour=23,minute=59,secend=50;
- NVIC_Configuration(); //設置中斷分組2:2位搶占優先級,2位響應優先級
- SMG_Init();
- TIM3_Init(100,7199); //10Khz的計數頻率,計數到5000為500ms(仿真1000,實際10000)
- LED_Init(); //7200-1=7199 72M=7200 0000
- FMQ_Init();
- KEY_Init(); //<<左移 &與字節 字 &&位與邏輯里小括號 |字節或字 ||位或邏輯 !=不等于 !邏輯取反
-
- while(1)
- {
- t=KEY_Scan(); //得到鍵值
- if(t) //if(條件)t!=0真
- {
- switch(t)
- {
- case 1:
- hour++;
- break;
- case 2:
- minute++;
- break;
- case 3:
- secend++;
- break;
- case 4:
- TIM_ITConfig(TIM3,TIM_IT_Update,DISABLE); //禁止TIM3中斷
- TIM_Cmd(TIM3,DISABLE); //TIM3停止
- hour1=hour,minute1=minute1,secend1=secend; //把原有時間放在當前時間里
- break;
- case 5:
- if(TIM_GetITStatus(TIM3,TIM_IT_Update)==RESET) //獲取TIM3的更新狀標志位條件成立
- {
- hour2=hour,minute2=minute,secend2=secend; //鬧鈴設定時間放在當前時間里
- hour=hour1,minute=minute1,secend=secend1; //還原當前時間
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM3,ENABLE);
- }
- break;
- }
- }
-
- num[7]=secend%10; // 51%10 51/10 取余1 %取余數 /取整數
- num[6]=secend/10;//秒 /整除(商)
- num[5]=10;
- num[4]=minute%10;
- num[3]=(minute%100)/10;
- num[2]=11;
- num[1]=hour%10;
- num[0]=(hour%100)/10;
- for(i=0;i<8;i++)
- {
- GPIO_Write(GPIOB,wei[i]|(L1&F1));//送段送位短延時
- GPIO_Write(GPIOC,table[num[i]]);
- Delay(23);
-
- GPIO_Write(GPIOB,0x0ff|(L1&F1));//復位延時要適當
- Delay(23); //仿真時可以省略,實際根據需要來確定
- }
- if(secend==60){secend=0;minute++;}
- if(minute==60){minute=0;hour++;}
- if(hour==24){hour =0;}
- if(minute==0)
- {
- if(secend%2==1){LED1=0;L1=LED1ON;}
- else{LED1=1;L1=LED1OFF;}
- FMQ1=0;F1=FMQ1ON;
- }
- else
- { LED1=1;L1=LED1OFF;FMQ1=1;F1=FMQ1OFF;}
- if(minute==minute2&&hour==hour2&&secend<=20) //&& 與
- {
- if(secend%2==1){LED1=0;L1=LED1ON;}
- else{LED1=1;L1=LED1OFF;}
- FMQ1=0;F1=FMQ1ON;
- }
- else
- { LED1=1;L1=LED1OFF;FMQ1=1;F1=FMQ1OFF;}
- }
- }
復制代碼
本人初學,僅供參考,如有錯誤,請大家回帖多多指教,仿真代碼下載:
定時器+8位數碼管數字鐘整點PB口閃爍報警按鍵(查詢掃描).7z
(253.75 KB, 下載次數: 394)
2020-6-6 03:19 上傳
點擊文件名下載附件
|