Based on digital electronic clock SCM

table of Contents

I. Introduction --------------------------------------- 2
Second, the hardware principle analysis - ------------------------ 2
Third, the program design --------------------- ----------- 4
IV program code -------------------------------- 4
five , 10 simulation results -------------------------------
six references ----------- -------------------- 10
VII learning experience ------------------------- ------ 11

I. Introduction
<1> using the knowledge to design a single-chip digital electronic clock learned
<2> digital electronic clock functional requirements:
(1) Automatic timing function;
(2) can display the measured time, showing good results;
(3) when the correction function, can be calibrated time
<3> design requirements:
"hour, minute, second" (1) by the second main signal generator circuit, a counter, a decoder and a display, the calibration circuit and the like.
(2) second signal generator with a quartz crystal oscillator is generally applied to achieve a frequency divider.
(3) The decoding circuit when the division, the output state of the second counter to send seven decoder decodes, through six seven segment LED display is displayed.
(4) When the correction circuit is used to hours, minutes, seconds display numbers proofread.

Second, the hardware principle analysis
1, the clock signal portion
microcontroller XTAL1, XTAL2 termination external clock circuit (clock circuit reference books), EA termination 5V power supply, so that the microcontroller reads the chip program.

2, the key switch section
begins simulation, press the switch K1, a clock pause, then press the switch K2 first, cut to the transfer position, the press K3, K4 achieve bit addition and subtraction; to tune cut by two points K1 bits, press K4, K4 achieve sub-bit subtraction; K1 by adjusting three seconds to cut position, according to K4, K4 achieve second bit subtraction; press K5, determining a current operation, and then press K1, restart the clock.


3, the digital display section (common cathode)
used is a six seven-segment LED common cathode


4, common cathode digital encoding

Third, the program design

 

Fourth, the program code

#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit set=P1^0;
sbit save=P1^5;
sbit rselect=P1^1;
sbit lselect=P1^2;
sbit add=P1^3;
sbit reduce=P1^4;

uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar disp_buf[6];
uchar disp_bit=0;
uint hour,min,sec;
uint select_num=0;
uint hour_adj,min_adj,sec_adj;
uchar count;
uchar key_num=0;
void key_scan()
{
if(!set)
{
hour_adj=hour;
min_adj=min;
sec_adj=sec;
key_num++;
}
while(!set);
if(key_num%2==1)
{
if(rselect==0)
{
select_num++;
if(select_num==4)
select_num=1;
}
while(!rselect);
if(lselect==0)
{
select_num--;
if(select_num<=0)
select_num=3;
}
while(!lselect);
}
if(!add&&(key_num%2))
{
switch(select_num)
{
case 1 :
{
hour_adj++;
if(hour_adj==24)
hour_adj=0;
break;
}
case 2 :
{
min_adj++;
if(min_adj==60)
min_adj=0;
break;
}
case 3 :
{
sec_adj++;
if(sec_adj==60)
sec_adj=0;
break;
}
default:break;
}
while(!add);
}
if(!reduce&&(key_num%2))
{
switch(select_num)
{
case 1 :
{
hour_adj--;
if(hour_adj<=0)
hour_adj=23;
break;
}
case 2 :
{
min_adj--;
if(min_adj<=0)
min_adj=59;
break;
}
case 3 :
{
sec_adj--;
if(sec_adj<=0)
sec_adj=59;
break;
}
default:break;
}
while(!reduce);
}
if(!save&&(key_num%2))
{
select_num=0;
hour=hour_adj;
min=min_adj;
sec=sec_adj;
while(!save);
}
}

void main()
{
TMOD=0x11;
TH0=0xf7;
TL0=0x00;
TH1=0x4c;
TH0=0x00;
ET0=1;
ET1=1;
EA=1;
TR0=1;
TR1=1;
PT1=1;
hour=23;
min=59;
sec=59;
count=0;
while(1)
{
key_scan();
if(key_num%2)
{
switch(select_num)
{
case 1 :
{
if(count<=10)
{
disp_buf[0]=hour_adj/10;
disp_buf[1]=hour_adj%10;
}
else
{
disp_buf[0]=0x40;
disp_buf[1]=0x40;
}
disp_buf[2]=min_adj/10;
disp_buf[3]=min_adj%10;
disp_buf[4]=sec_adj/10;
disp_buf[5]=sec_adj%10;
break;
}
case 2 :
{
if(count<=10)
{
disp_buf[2]=min_adj/10;
disp_buf[3]=min_adj%10;
}
else
{
disp_buf[2]=0x40;
disp_buf[3]=0x40;
}
disp_buf[0]=hour_adj/10;
disp_buf[1]=hour_adj%10;
disp_buf[4]=sec_adj/10;
disp_buf[5]=sec_adj%10;
break;
}
case 3 :
{
if(count<=10)
{
disp_buf[4]=sec_adj/10;
disp_buf[5]=sec_adj%10;
}
else
{
disp_buf[4]=0x40;
disp_buf[5]=0x40;
}
disp_buf[0]=hour_adj/10;
disp_buf[1]=hour_adj%10;
disp_buf[2]=min_adj/10;
disp_buf[3]=min_adj%10;
break;
}
default : break;
}
}
if(key_num%2==0)
{
disp_buf[0]=hour/10;
disp_buf[1]=hour%10;
disp_buf[2]=min/10;
disp_buf[3]=min%10;
disp_buf[4]=sec/10;
disp_buf[5]=sec%10;

}
}
}

void timer0() interrupt 1
{
TH0=0xf7;
TL0=0x00;
P2=~(0x01<<disp_bit);
P0=tab[disp_buf[disp_bit]];
disp_bit++;
if(disp_bit==6) disp_bit=0;
}

void timer1() interrupt 3
{
TH1=0x4c;
TL1=0x00;
if(++count==20)
{
count=0;
if(++sec==60)
{
sec=0;
if(++min==60)
{
min=0;
if(++hour==24)
{
hour=0;
}
}

}
}
}

 

 

Fifth, the simulation results


Six, Reference
1. Internet search
2. SCM teaching
3. Groups
Seven, learning experience
through the course of this single-chip design, not only deepened my understanding of the theory of knowledge course microcontroller, and by combining theory with practice, so I really fully understand the function of the microcontroller. At the beginning of programming, with no idea of the book by the routine provided, slowly clarified the thinking, a basic understanding of the procedure that generally requires several parts, to determine the basic programming ideas. Throughout the course design process also encountered many problems, but the principle of the encounter and solve problems, and to find information by asking teachers, students approach, basically solved the problems encountered. Throughout the course design process learned a lot by studying the theory can not learn, really enhances their ability.
Course design day time, though hard but huge harvest.

Guess you like

Origin www.cnblogs.com/jtd666/p/12499243.html