Atmega16 electronic clock, internal timer, adjustable alarm.
The simulation schematic diagram is as follows, and
the microcontroller source program is as follows:
#include #include #define key_bz 0b00000111 char smg_zx[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d, 0x7d,0x07,0x7f,0x6f}; // global variables char hour=12; char mine; char sec; char nz_hour=12; char nz_min=1; char mode=0; char set; void delay_ms(unsigned int k) { unsigned int i,j; for(i=0;i for(j=0;j<570;j++); } } // Use timer to implement timing void T0_init(void) // port initialization function { TIFR=0XFF; TCCR0=0X0B; //64FENPIN, CTC MODE TCNT0=0; OCR0=250; TIMSK=0X02; } void port_init(void) // port initialization function { DDRC |= 0b11110000; // PC4 5 OUT PUT PORTC |= 0b00111111; //m103 output only PORTD = 0x00; // Turn off the digital tube when powered on, because the power-on voltage is unstable DDRD = 0xff; DDRB |= 0b11100000; // PC5 6 7OUT PUT } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init();//smg_zx[0]=1; T0_heat(); SEI(); //re-enable interrupts //all peripherals are now initialized } //miao=45; void key (void) { if(!(PINC&0X01)) { delay_ms(2); if(!(PINC&0X01)) { TIMSK=0X00; mode++; mode%=3; set=0; if(mode==0)TIMSK=0X02; while(!(PINC&0X01)); } } if(!(PINC&0X02)) { delay_ms(2); if(!(PINC&0X02)) { if(mode==1) { set++; set%=3; } if(mode==2) { set++; set%=2; } while(!(PINC&0X02)); } } if(!(PINC&0X04)) { delay_ms(2); if(!(PINC&0X04)) { if(mode==1) { if(set==0) { hour++; hour%=24; } if(set==1) { min++; min%=60; } if(set==2) { sec++; sec%=60; } } if(mode==2) { if(set==0) { nz_hour++; nz_hour%=24; } if(set==1) { nz_min++; nz_min%=60; } } while(!(PINC&0X04)); } } } //Main function void main() { /// Variable declaration char zz; //Call to initialize hash //port_init(); init_devices(); // Specific code while(1) { if(mode==0) { PORTD=~smg_zx[hour/10]; // Encode the glyph of 5 PORTB|=1< PORTB&=0B00111111;// PORTD=~smg_zx[hour%10]; // Encode the glyph of 5 PORTB|=1< PORTB&=0B00111111;// PORTD=~smg_zx[min/10]; // Encode the glyph of 5 PORTC|=1< PORTC&=0B00001111;// PORTD=~smg_zx[min%10]; // Encode the glyph of 5 PORTC|=1< delay_ms(2); PORTC&=0B00001111;// PORTD=~smg_zx[sec/10]; // Encode the glyph of 5 PORTC|=1< PORTC&=0B00001111;// PORTD=~smg_zx[sec%10]; // Encode the glyph of 5 PORTC|=1< PORTC&=0B00001111;// } else if(mode==1) { PORTD=~smg_zx[hour/10]; // Encode the glyph of 5 PORTB|=1< PORTB&=0B00111111;// PORTD=~smg_zx[hour%10]; // Encode the glyph of 5 if(set==0) PORTD&=0x7f; //control the decimal light PORTB|=1< PORTB&=0B00111111;// PORTD=~smg_zx[min/10]; // Encode the glyph of 5 PORTC|=1< PORTC&=0B00001111;// PORTD=~smg_zx[min%10]; // Encode the glyph of 5 if(set==1) PORTD&=0x7f; //control the decimal light PORTC|=1< delay_ms(2); PORTC&=0B00001111;// PORTD=~smg_zx[sec/10]; // Encode the glyph of 5 PORTC|=1< PORTC&=0B00001111;// PORTD=~smg_zx[sec%10]; // Encode the glyph of 5 if(set==2) PORTD&=0x7f; //control the decimal light PORTC|=1< PORTC&=0B00001111;// } else if(mode==2) { PORTD=~smg_zx[nz_hour/10]; // Encode the glyph of 5 PORTB|=1< PORTB&=0B00111111;// PORTD=~smg_zx[nz_hour%10]; // Encode the glyph of 5 if(set==0) PORTD&=0x7f; //control the decimal light PORTB|=1< PORTB&=0B00111111;// PORTD=~smg_zx[nz_min/10]; // Encode the glyph of 5 PORTC|=1< PORTC&=0B00001111;// PORTD=~smg_zx[nz_min%10]; // Encode the glyph of 5 if(set==1) PORTD&=0x7f; //control the decimal light PORTC|=1< PORTC&=0B00001111;// } if((hour==nz_hour)&&(min==nz_min))//闹钟 { if(zz++>20) { PORTB^=1< } } else PORTB|=1< key (); } } //T0 interrupt service routine// 2ms enter interrupt #pragma interrupt_handler timer0_ocf_isr:20 void timer0_ocf_isr(void) {static int count=0; if(++count>=500) { count=0; sec++; if(sec>=60) { sec=0; min++; if(min>=60) { min=0; hour++; hour%=24; } } } }
Previous article:PT6311 HT1628 AVR microcontroller detailed routine
Next article:AVR-based ds1302 clock code
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- The topological classification of DCDC can be understood as follows
- HCS12 Series MC9S12DP512MPVE Microcontroller 16Bit Flash 112LQFP
- Annoying technology
- Can a PMOS be connected in series on the communication line as a switch?
- How to achieve positive and negative pulse output using a single battery power supply
- 【GD32E231 DIY】Serial communication
- Allow ubinascii.crc32() function in rp2/stm32
- 【McQueen Trial】McQueen's new ultrasonic drive
- Commonly used algorithms for drones - Kalman filter (I)
- 【AT-START-F403A Review】+RTC Test Verification