51 MCU automatic ringing C language program:

How to realize automatic ringing with 51 single chip microcomputer

#include “reg51.h”

#include “intrins.h” #define uchar unsigned char #define uint unsigned int

uchar code DSY_ CODE[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};// Common cathode section//

uchar DSY_ BUFFER[]={0,0,0xBF,0,0,0xff,0,0};// Time display time minute mode

uchar DSY_ BUFFER1[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; // Mode display segment//

uchar Scan_ BIT;// Bit selection//

uchar DSY_ IDX;// Segment selection//

uchar Key_ State; // Key control//

uchar s,s100,h1 ;

char h,m;

sbit dd=P1^7; // Speaker and LED output ports//

sbit cc=P1^6;

sbit ss=P1^5; // Time and second conversion display key//

void DelayMS(uchar x)

{

uchar i;

while(x–) for(i = 0;i《120;i++);

}

void Increase_ Hour()

{

if (++h》23) h=0;

DSY_ BUFFER[0] =DSY_ CODE[h/10];

DSY_ BUFFER[1] =DSY_ CODE[h];

}

void Decrease_ Hour()

{ h–;

if (h《0) h=23;

DSY_ BUFFER[0] =DSY_ CODE[h/10];

DSY_ BUFFER[1] =DSY_ CODE[h];

}

void Increase_ Minute()

{

if(++m》59)

{

m=0; Increase_ Hour();

}

DSY_ BUFFER[3] =DSY_ CODE[m/10];

DSY_ BUFFER[4] =DSY_ CODE[m];

}

void Decrease_ Minute()

{ m–;

if(m《0) m=59;

DSY_ BUFFER[3] =DSY_ CODE[m/10];

DSY_ BUFFER[4] =DSY_ CODE[m];

}

void Increase_ Hour1()

{

if (++h1》7) h1=1;

DSY_ BUFFER[6] =DSY_ CODE[h1/10];

DSY_ BUFFER[7] =DSY_ CODE[h1];

}

void Increase_ Second()

{

if(++s》59)

{

s=0; Increase_ Minute();

}

DSY_ BUFFER1[0] =DSY_ CODE[s/10];

DSY_ BUFFER1[1] =DSY_ CODE[s];

}

void T0_ Int () interrupt 1 / / the display shows//

{

TH0=(65536-1000)/256;

TL0=(65536-1000)%6;

If (SS = = 1) / / P1.5 key is not pressed, the time is displayed//

{

P3=Scan_ BIT;

P2=~DSY_ BUFFER[DSY_ IDX];

}

Else {/ / press P1.5 to flash//

P3=Scan_ BIT;

P2=~DSY_ BUFFER[DSY_ IDX];

}

Scan_ BIT=_ crol_ (Scan_BIT,1);// Bit display movement//

DSY_ IDX=(DSY_IDX+1)%8; // Segment selection each change//

if(h1==3)

{if (H = = 6 & M = = 0) CC = DD = 0; else CC = DD = 1; / / when the time comes, the alarm clock rings and the LED is on for one minute//

if(h==6&m==15)cc=dd=0; else cc=dd=1;

if(h==7&m==0)cc=dd=0; else cc=dd=1;

if(h==12&m==30)cc=dd=0; else cc=dd=1;

if(h==14&m==0)cc=dd=0; else cc=dd=1;

if(h==16&m==30)cc=dd=0; else cc=dd=1;

if(h==18&m==0)cc=dd=0; else cc=dd=1;

if(h==18&m==45)cc=dd=0; else cc=dd=1;

if(h==19&m==30)cc=dd=0; else cc=dd=1;

if(h==21&m==30)cc=dd=0; else cc=dd=1;

if(h==22&m==0)cc=dd=0; else cc=dd=1; }

else cc=dd=1;

if(h1==1|h1==4|h1==2|h1==5)

{if (H = = 6 & M = = 0) CC = DD = 0; else CC = DD = 1; / / when the time comes, the alarm clock rings and the LED is on for one minute//

if(h==7&m==0)cc=dd=0; else cc=dd=1;

if(h==12&m==30)cc=dd=0; else cc=dd=1;

if(h==14&m==0)cc=dd=0; else cc=dd=1;

if(h==16&m==30)cc=dd=0; else cc=dd=1;

if(h==18&m==0)cc=dd=0; else cc=dd=1;

if(h==18&m==45)cc=dd=0; else cc=dd=1;

if(h==19&m==30)cc=dd=0; else cc=dd=1;

if(h==21&m==30)cc=dd=0; else cc=dd=1;

if(h==22&m==0)cc=dd=0; else cc=dd=1; }

else cc=dd=1;

if(h1==6)

{if (H = = 6 & M = = 0) CC = DD = 0; else CC = DD = 1; / / when the time comes, the alarm clock rings and the LED is on for one minute//

if(h==7&m==0)cc=dd=0; else cc=dd=1;

if(h==12&m==30)cc=dd=0; else cc=dd=1;

if(h==14&m==30)cc=dd=0; else cc=dd=1;

if(h==18&m==0)cc=dd=0; else cc=dd=1;

if(h==21&m==0)cc=dd=0; else cc=dd=1;

if(h==21&m==30)cc=dd=0; else cc=dd=1;

if(h==22&m==30)cc=dd=0; else cc=dd=1; }

else cc=dd=1;

if(h1==7)

{if (H = = 6 & M = = 30) CC = DD = 0; else CC = DD = 1; / / when the time comes, the alarm clock rings and the LED is on for one minute//

if(h==7&m==0)cc=dd=0; else cc=dd=1;

if(h==11&m==30)cc=dd=0; else cc=dd=1;

if(h==12&m==30)cc=dd=0; else cc=dd=1;

if(h==15&m==30)cc=dd=0; else cc=dd=1;

if(h==16&m==30)cc=dd=0; else cc=dd=1;

if(h==18&m==0)cc=dd=0; else cc=dd=1;

if(h==18&m==40)cc=dd=0; else cc=dd=1;

if(h==20&m==0)cc=dd=0; else cc=dd=1;

if(h==20&m==30)cc=dd=0; else cc=dd=1;

if(h==21&m==30)cc=dd=0; else cc=dd=1;

if(h==22&m==0)cc=dd=0; else cc=dd=1; }

else cc=dd=1;

}

void T1_ Int() interrupt 3 / / the time is automatically increased by one second, and the travel time is//

{

TH1=(65536-50000)/256;

TL1=(65536-50000)%6;// Delay 50 ms//

if (s100==20)

{

s100=0;

Increase_ Second();

}

else s100++;

}

void main ()

{

P2=P3=0xFF;

TMOD=0x11;

TH0=(65536-1000)/256;

TL0=(65536-1000)%6;

TH1=0XDC;

TL1=0;

TCON=0x01;

EA=1;

ET0=1;

ET1=1;

h=05,m=55,s=s100,h1=1; // Initial value of time and alarm clock//

DSY_ BUFFER[0]=DSY_ CODE[h/10]; // The initial time value is assigned to the time period code display group//

DSY_ BUFFER[1]=DSY_ CODE[h];

DSY_ BUFFER[3]=DSY_ CODE[m/10];

DSY_ BUFFER[4]=DSY_ CODE[m];

DSY_ BUFFER[6]=DSY_ CODE[h1/10];

DSY_ BUFFER[7]=DSY_ CODE[h1];

DSY_ BUFFER1[0]=DSY_ CODE[h1/10]; // The initial value of the alarm clock is assigned to the alarm segment code display group//

DSY_ BUFFER1[1]=DSY_ CODE[h1];

Scan_ BIT=0xFE; // Assign the initial value of bit selection to binary number 11111110, that is, only the last bit is low level, and then move left one bit at a time, which is 111111011111011 01111110 cyclic movement//

DSY_ IDX=0; // Segment code displays the selection variable of the group//

TR0=TR1=1;

Key_ State=0xFF;

while(1)

{

If (P1 ^ key_state) / / judge whether the key is pressed//

{

DelayMS(10);

if (P1^Key_State)

{

Key_ State=P1; EA=0;

if((Key_State&0x01)==0) Increase_ Hour(); // Hour plus one//

else if ((Key_State&0x02)==0) Decrease_ Hour(); // One hour minus one//

else if ((Key_State&0x04)==0) Increase_ Minute(); // Minute plus one//

else if ((Key_State&0x08)==0) Decrease_ Minute(); // One minute minus one//

Else if ((key_state & 0x10) = = 0) / / press the reset button//

{h=05,m=55,s=0,h1=1;

DSY_ BUFFER[0]=DSY_ CODE[h/10];

DSY_ BUFFER[1]=DSY_ CODE[h];

DSY_ BUFFER[3]=DSY_ CODE[m/10];

DSY_ BUFFER[4]=DSY_ CODE[m];

DSY_ BUFFER[6]=DSY_ CODE[h1/10];

DSY_ BUFFER[7]=DSY_ CODE[h1];

}

else if ((Key_State&0x20)==0)Increase_ Hour1(); // Mode plus one//

EA=1;

}

}

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *