/****************************************************** ***************************
Reference address:51 MCU 4-bit digital tube electronic clock source program
Title: Timer interrupt 24-hour clock program Effect: Digital tube displays 24-hour clock (with time adjustment function) Note: Use 12M crystal oscillator, key1=P3^7; key2=P3^6; key3=P3^5; key4=P3^4; Digital tube position selection P1.0~P1.3, segment selection P0 *************************************************** ****************************/ #include#define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt; uchar second; uchar minute; uchar hour; uchar code table[]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9 sbit smg_q = P1^0; //digital tube thousands digit pin sbit smg_b = P1^1; sbit smg_s = P1^2; sbit smg_g = P1^3; sbit key1=P3^7; //button definition sbit key2=P3^6; sbit key3=P3^5; sbit key4=P3^4; void keyscan(); //Function declaration void display(unsigned char hour, unsigned char minute); void delay(unsigned int timer); void init(); /****************************************************** ********** Main function *************************************************** *********/ void main() { init(); while(1) { if(tt==20) { tt=0; second++; if(second==60) { second=0; minute++; if(minute==60) { minute=0; hour++; if(hour==24) hour=0; } } } keyscan(); display(hour,minute); delay(1); } } /****************************************************** ********** Time Adjustment *************************************************** *********/ void keyscan() { if(key1==0) //hour plus 1 { TR0=0; hour++; if(hour==24) hour=0; delay(200) ; TR0=1; } if(key2==0) // hours minus { TR0=0; hour--; if(hour<0) hour=23; delay(200) ; TR0=1; } if(key3==0) // add minutes { TR0=0; minute++; if(minute==60) minute=0; delay(200) ; TR0=1; } if(key4==0) // minutes minus { TR0=0; minute--; if(minute<0) minute=59; delay(200) ; TR0=1; } } /****************************************************** ********** initialization *************************************************** *********/ void init() { tt=0; position=0; second=0; minute=0; hour=0; smg_q=1; smg_b=1; smg_s=1; smg_g=1; key1=1; key2=1; TMOD=0X01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; } /****************************************************** ********** Timer TO interrupt *************************************************** *********/ void t0() interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt++; } /****************************************************** ********** show *************************************************** *********/ void display(unsigned char hour, unsigned char minute) { P0=0XFF; switch(position) { case0: smg_g=1; smg_q=0; P0=table[hour/10]; break; case1: smg_q=1; smg_b=0; P0=table[hour%10]; if(tt>=10) P0&=0x7f; //The decimal point flashes for half a second break; case2: smg_b=1; smg_s=0; P0=table[minute/10]; break; case3: smg_s=1; smg_g=0; P0=table[minute%10]; break; } position++; if(position>3) position=0; } /****************************************************** ********** Delay *************************************************** *********/ void delay(unsigned int time) { unsignedint x,y; for(x=time;x>0;x--) for(y=110;y>0;y--); }
Previous article:51 MCU timer timing mode
Next article:Single chip electronic keyboard assembly program
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
Guess you like
- [AutoChips AC7801x motor demo board review] + unboxing and GPIO lighting
- About the master: zhang2018yi
- Qorvo has a few things to tell you about WiFi 6
- What kind of switch is this? Where can I buy one?
- Chapter 3: Use of GPIO
- Software Installation
- If you don’t lose weight in spring, you will regret it in summer. Let me share some tips on how to lose weight and keep in shape!
- [Embedded Linux] RK3399 Android7.1 CPU is overheating
- 【Chuanglong TL570x-EVM】Button control LED light
- TI Automotive SPD-SmartGlass Driver Reference Design