#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit nz=P2^3;//alarm clock
sbit lcdrs=P3^5;
sbit lcdwr =P3^6;
sbit lcden=P3^4;//LCD enable
sbit k1=P3^0;
sbit k2=P3^1;
sbit k3=P3^2;
sbit k4=P3^3;
sbit ajjd=P3^ 7;//Button
sbit clk=P2^1;
sbit io=P2^0;
sbit rst=P2^5;
void naozhongscan();
uchar num0,num1,num2,num3,num4;
void init();
void delay( uchar);
void keyscan();
void didi();
uchar lcd_date_add[7]={0x40+9,0x40+6,0x40+3,0xa,7,0xd,4};
uchar date_write_add[7]={0x80,0x82,0x84,0x86,0x88,0x8a ,0x8c};//miao fen shi ri yue zhou nian
uchar date_read_add[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar date[7]={30,32,16,27, 2,7,11};
uchar date1[7];
uchar naozhong[7]={35,32,16,27,2,7,11};
uchar flag,flag1,flag2;
uchar code table0[]=" - - ";
uchar code table1[]=" : : ";
void lcd_com(uchar);
void lcd_date(uchar);
void lcd_time(uchar,uchar);
void write_ds1302(uchar add,uchar date);
uchar read_ds1302(uchar add);
void read_rtc();
void set_ds1302(uchar add,uchar time);
void display();
void init_ds1302();
void main()
{
wela=1;
P1=0xff;
wela=0;
init ();
init_ds1302();
while(1)
{
if(flag1==1)
naozhongscan();
read_rtc();
if(flag==0)
display();
keyscan();
}
}
void init()
{
flag =0;
flag1=0;
flag2=0;
lcden=0;
lcdwr=0;
num2=0;
num3=0;
num4=0;
lcd_com(0x38); //Display mode setting, 16*2 display, 5*7 dot matrix, 8-bit data interface
lcd_com(0x01); //Clear screen, display, Pointer clear
lcd_com(0x0c); //Display switch and cursor setting, open display, display cursor, cursor flashing
lcd_com(0x06); //Pointer plus one after reading and writing cursor plus one
lcd_com(0x80); //From the first
for(num0=0;num0<16;num0++) lcd_date
(table0[num0]);
lcd_com(0x80+0x40);
for(num0=0;num0<11;num0++)
lcd_date(table1[num0] );
}
void init_ds1302()
{
uchar i,j;
uchar flag3;
flag3=read_ds1302(0x81);
if(flag3&0x80)//If ch=1, initialize
{
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
j=date[i]/10;
date[i]=date[i]%10;
date[i]=date[i]+j*16;
write_ds1302(date_write_add[i],date[i]);
}
write_ds1302(0x8e,0x80);
}
}
void delay(uchar z)
{
uchar x,y;
for(x=110;x>0;x--)
for(y=z;y>0;y--);
}
void didi()
{
nz=0;
delay(50);
nz=1;
delay(100);
nz=0;
delay(50);
nz=1;
}
void lcd_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden= 1;
delay(5);
lcden=0;
}
void lcd_date(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void lcd_time(uchar add,uchar Date)
{
uchar shi,ge;
shi=Date/10;
ge=Date%10;
lcd_com(0x80+add);
lcd_date(0x30+shi) ;
lcd_date(0x30+ge);
}
void keyscan()
{
ajjd=0;
if(num3!=2)
{
if(k1==0)
{
delay(5);
if(k1==0)
{
while(! k1);
num2++;
if(num2==1)
{
flag=1;
lcd_com(0x80+lcd_date_add[0]);
lcd_com(0x0f); //Display switch and cursor setting, turn on display, display cursor, cursor flashing
}
if (num2==2)
{
lcd_com(0x80+lcd_date_add[1]);
}
if(num2==3)
{
lcd_com(0x80+lcd_date_add[2]);
}
if(num2==4)
{
lcd_com(0x80+lcd_date_add[3]);
}
if(num2==5)
{
lcd_com(0x80+ lcd_date_add[4]);
}
if(num2==6)
{
lcd_com(0x80+lcd_date_add[5]);
}
if(num2==7)
{
lcd_com(0x80+lcd_date_add[6]);
}
if(num2== 8)
{
lcd_com(0x0c);//Close the cursor
num2=0;
flag=0;
}
}
}
if(num2!=0)
{
if(k2==0)
{
delay(5);
if(k2==0 )
{
while(!k2);
if(num2==1)
{
date[0]++;
if(date[0]==60)
date[0]=0;
_nop_();
lcd_time(lcd_date_add[0],date[0]);
lcd_com(0x80+lcd_date_add[0]);
set_ds1302(date_write_add[0],date[0]);
}
if(num2==2)
{
date[1]+ +;
if(date[1]==60)
date[1]=0;
_nop_();
lcd_time(lcd_date_add[1],date[1]);
lcd_com(0x80+lcd_date_add[1]);
set_ds1302(date_write_add[ 1],date[1]);
}
if(num2==3)
{
date[2]++;
if(date[2]==24)
date[2]=0;
_nop_();
lcd_time(lcd_date_add[ 2],date[2]);
lcd_com(0x80+lcd_date_add[2]);
set_ds1302(date_write_add[2],date[2]);
}
if(num2==4)
{
date[3]++;
if(date[3]==32)
date [3]=1;
_nop_();
lcd_time(lcd_date_add[3],date[3]);
lcd_com(0x80+lcd_date_add[3]);
set_ds1302(date_write_add[3],date[3]);
}
if(num2 ==5)
{
date[4]++;
if(date[4]==13)
date[4]=1;
_nop_();
lcd_time(lcd_date_add[4],date[4]);
lcd_com(0x80+ lcd_date_add[4]);
set_ds1302(date_write_add[4],date[4]);
}
if(num2==6)
{
date[5]++;
if(date[5]==8)
date[5]=1;
_nop_();
lcd_time(lcd_date_add[5],date[5]);
lcd_com(0x80+lcd_date_add[5]);
set_ds1302(date_write_add[5],date[5]);
}
if(num2==7)
{
date[6]+ +;
if(date[6]==99)
date[6]=1;
_nop_();
lcd_time(lcd_date_add[6],date[6]);
lcd_com(0x80+lcd_date_add[6]);
set_ds1302(date_write_add[ 6],date[6]);
}
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
if(num2==1)
{
date[0]--;
if(date[0]== -1)
date[0]=59;
_nop_();
lcd_time(lcd_date_add[2],date[0]);
lcd_com(0x80+lcd_date_add[0]);
set_ds1302(date_write_add[0],date[0]);
}
if(num2==2)
{
date[1]--;
if(date[1]==-1)
date[1]=59;
_nop_() ;
lcd_time(lcd_date_add[1],date[1]);
lcd_com(0x80+lcd_date_add[1]);
set_ds1302(date_write_add[1],date[1]);
}
if(num2==3)
{
date[2] --;
if(date[2]==-1)
date[2]=23;
_nop_();
lcd_time(lcd_date_add[2],date[2]);
lcd_com(0x80+lcd_date_add[2]);
set_ds1302( date_write_add[2],date[2]);
}
if(num2==4)
{
date[3]--;
if(date[3]==0)
date[3]=31;
_nop_();
lcd_time(lcd_date_add[3],date[3]);
lcd_com (0x80+lcd_date_add[3]);
set_ds1302(date_write_add[3],date[3]);
}
if(num2==5)
{
date[4]--;
if(date[4]==0)
date[ 4]=12;
_nop_();
lcd_time(lcd_date_add[4],date[4]);
lcd_com(0x80+lcd_date_add[4]);
set_ds1302(date_write_add[4],date[4]);
}
if(num2= =6)
{
date[5]--;
if(date[5]==0)
date[5]=7;
_nop_();
lcd_time(lcd_date_add[5],date[5]);
lcd_com(0x80+lcd_date_add[5]);
set_ds1302(date_write_add[5],date[5]);
}
if( num2==7)
{
date[6]--;
if(date[6]==0)
date[6]=99;
_nop_();
lcd_time(lcd_date_add[6],date[6]);
lcd_com(0x80 +lcd_date_add[6]);
set_ds1302(date_write_add[6],date[6]);
}
}
}
}
}[page]
if(k4==0)
{
delay(5);
if(k4==0)
{
while (!k4);
num3++;
}
}
if(num3==1)
{
flag1=1;
lcd_com(0x80+1);
lcd_date(0x41);
}
if(num3==2)
{
ajjd=0;
flag1=0;
if(num4==0)
{
for(num0=0;num0<7;num0++)
lcd_time(lcd_date_add[num0],naozhong[num0]);
lcd_com(0x80+1);
lcd_com(0x0c);//Turn off the cursor
lcd_date(0x42);
}
//lcd_com(0x0f);
flag=1;
if(k1==0)
{
delay(5);
if(k1==0)
{
while(!k1);
num4++;
if(num4==1)
{
lcd_com(0x80+lcd_date_add[0]);
lcd_com(0x0f);//Display switch and cursor setting, turn on display, display cursor, cursor blinking
}
if(num4==2)
{
lcd_com(0x80+lcd_date_add[1]);
}
if(num4==3)
{
lcd_com(0x80+lcd_date_add[2]);
}
if(num4==4)
{
lcd_com(0x80+lcd_date_add[3]);
}
if(num4==5)
{
lcd_com(0x80+lcd_date_add[4]);
}
if(num4==6)
{
lcd_com(0x80+lcd_date_add[5]);
}
if(num 4==7)
{
lcd_com(0x80+lcd_date_add[6]);
}
if(num4==8)
{
lcd_com(0x0c);//Turn off the cursor
num4=0;
flag=1;
}
}
}
if(num4!=0)
{
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
if(num4==1)
{
naozhong[0]++;
if(naozhong[0]==60)
naozhong[0]=0;
_nop_();
lcd_time(lcd_date_add[0],naozhong[0]);
lcd_com(0x80+lcd_date_add[0]);
}
if(num4==2)
{
naozhong[1]++;
if(naozhong[1]==60)
naozhong[1]=0;
_nop_();
lcd_time(lcd_date_add[1],naozhong[1]);
lcd_com(0x80+lcd_date_add[1]) ;
}
if(num4==3)
{
naozhong[2]++;
if(naozhong[2]==24)
naozhong[2]=0;
_nop_();
lcd_time(lcd_date_add[2],naozhong[2]) ;
lcd_com(0x80+lcd_date_add[2]);
}
if(num4==4)
{
naozhong[3]++;
if(naozhong[3]==32)
naozhong[3]=1;
_nop_();
lcd_time(lcd_date_add[3],naozhong[3]);
lcd_com(0x80+lcd_date_add[3]);
}
if(num4==5)
{
naozhong[4]++;
if(naozhong[4]==13)
naozhong [4]=1;
_nop_();
lcd_time(lcd_date_add[4],naozhong[4]);
lcd_com(0x80+lcd_date_add[4]);
}
if(num4==6)
{
naozhong[5]++;
if (naozhong[5]==8)
naozhong[5]=1;
_nop_();
lcd_time(lcd_date_add[5],naozhong[5]);
lcd_com(0x80+lcd_date_add[5]);
}
if(num4==7 )
{
naozhong[6]++;
if(naozhong[6]==99)
naozhong[6]=1;
_nop_();
lcd_time(lcd_date_add[6],naozhong[6]);
lcd_com(0x80+lcd_date_add[6]) ;
}
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
if(num4==1)
{
naozhong[0]--;
if(naozhong[ 0]==-1)
naozhong[0]=59;
_nop_();
lcd_time(lcd_date_add[2],naozhong[0]);
lcd_com(0x80+lcd_date_add[0]);
}
if(num4==2)
{
naozhong[1]--;
if(naozhong[1]==-1)
naozhong[1]=59;
_nop_();
lcd_time(lcd_date_add[1],naozhong[1]);
lcd_com(0x80+lcd_date_add[1]);
}
if(num4==3)
{
naozhong[2]--;
if(naozhong[2]==-1)
naozhong[2]=23;
_nop_();
lcd_time(lcd_date_add[2],naozhong[2]);
lcd_com(0x80+lcd_date_add[2]);
}
if(num4= =4)
{
naozhong[3]--;
if(naozhong[3]==0)
naozhong[3]=31;
_nop_();
lcd_time(lcd_date_add[3],naozhong[3]);
lcd_com(0x80+lcd_date_add[3] );
}
if(num4==5)
{
naozhong[4]--;
if(naozhong[4]==0)
naozhong[4]=12;
_nop_();
lcd_time(lcd_date_add[4],naozhong[4] );
lcd_com(0x80+lcd_date_add[4]);
}
if(num4==6)
{
naozhong[5]--;
if(naozhong[5]==0)
naozhong[5]=7;
_nop_();
lcd_time(lcd_date_add[5],naozhong[5]);
lcd_com(0x80+lcd_date_add[5]);
}
if(num4==7)
{
naozhong[6]--;
if(naozhong[6]==0)
naozhong [6]=99;
_nop_();
lcd_time(lcd_date_add[6],naozhong[6]);
lcd_com(0x80+lcd_date_add[6]);
}
}
}
} }
if
(num3==3)
{
flag=0;
flag1=0;
lcd_com(0x80+1);
lcd_date(20);//Let LCD display nothing
num3=0;
num4=0;
}
}
void write_ds1302(uchar add,uchar date)
{
uchar i,temp;
rst=0;
clk=0;
_nop_();
rst=1;
for(i=0;i<8;i++)
{
clk=0;
temp=add;
io=temp&0x01;
add>>=1;
clk=1;
_nop_();
}
for(i=0; i<8;i++)
{
clk=0;
temp=date;
io=temp&0x01;
date>>=1;
clk=1;
_nop_();
}
rst=0;
}
uchar read_ds1302(uchar add)
{
uchar i,temp ,data1,j;
rst=0;
clk=0;
_nop_();;
rst=1;
for(i=0;i<8;i++)
{
clk=0;
temp=add;
io=temp&0x01;
add>>=1;
clk=1;
_nop_();
}
for(i=0; i<8;i++)
{
data1>>=1;
clk=0;
if(io)
data1=data1|0x80;
clk=1;
}
rst=0;
_nop_();
j=data1%16;
data1=data1/ 16;
data1=data1*10+j;
return(data1);
}
void set_ds1302(uchar add,uchar time)
{
uchar j;
write_ds1302(0x8e,0x00);
j=time/10;
time=time%10;
time= time+j*16;
write_ds1302(add,time);
write_ds1302(0x8e,0x80);
}
void read_rtc()
{
uchar i;
for(i=0;i<7;i++)
{
date[i]=read_ds1302(date_read_add[i]);
}
}
void display()
{
uchar i;
for(i=0;i<7;i++)
{
lcd_time(lcd_date_add[i],date[i]);
}
}
void naozhongscan()
{
uchar i=0;
uchar j=0;
while((j= =0)&&(i<7))
{
j=naozhong[i]^date[i];
i++;
}
if(i==7)
didi();
}
Previous article:Digital tube electronic calendar made by 51 single chip microcomputer
Next article:Microcontroller measuring bicycle speed program - with simulation
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- X-NUCLEO-IKS01A3 sensor test based on STM32F401RE development board 2 LPS22HH pressure sensor
- Buck
- What embedded systems are hidden in smart furniture?
- Electric water heater control panel
- 【FAQ】Enabling secure authentication with TrustFlex secure elements and Microsoft Azure | Microchip Seminar
- USB download cable driver installation.pdf
- Allegro Ali Dog Drawing Board. I haven't drawn on the drawing board for a long time.
- BMW Night Vision (Infrared Thermal Imaging) Camera Disassembly
- stm32 usb cdc communication
- [ATmega4809 Curiosity Nano Review] Unboxing