单片机主程序:
#include<reg51.h>
#include<intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define LcdDataPort P1
#define Lcd_Start_Row 0xc0
#define Lcd_Page 0xb8
#define Lcd_Col 0x40
void InitializeLCD();
void OneChar(unsigned char x,unsigned char y,unsigned char num);
void Hanzi(uchar x,uchar y,uchar *hz);
void Write_Byte(uchar x);
uchar Get_Byte();
uchar Read_Data(uchar addr);
void Write_DS1302(uchar addr,uchar dat);
void Set_Time();
void Get_Time();
sbit K1=P0^0;
sbit K2=P0^1;
sbit K3=P0^2;
sbit K4=P0^3;
sbit DI=P3^6;
sbit RW=P3^5;
sbit E=P3^4;
sbit CSA=P3^0;
sbit CSB=P3^1;
sbit SDA=P2^6;
sbit SCLK=P2^5;
sbit RST=P2^7;
bit Reverse_Display=0;
uchar tCount=0;
uchar DateTime[7];
uchar Adjust_Index=-1;
uchar code DATE_TIME_WORDS[]={0x40,0x20,0x10,0x0c,0xe3,0x22,0x22,0x22,0xfe,0x22,0x22,0x22,0x22,0x02,0x00,0x00,0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0xff,0x04,0x04,0x04,0x04,0x04,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0xff,0x11,0x11,0x11,0x11,0x11,0xff,0x00,0x00,0x00,0x00,0x00,0x40,0x20,0x10,0x0c,0x03,0x01,0x01,0x01,0x21,0x41,0x3f,0x00,0x00,0x00,0x00,
0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,
0x00,0x00,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x01,0x00,0x11,0x00,0x1F,0xF8,0x21,0x00,0x41,0x00,0x1F,0xF0,0x01,0x00,0x01,0x00,0x7F,0xFC,0x00,0x00,
0x22,0x00,0x22,0x7C,0x7F,0x44,0x22,0x44,0x22,0x44,0x3E,0x7C,0x22,0x44,0x22,0x44,0x3E,0x44,0x22,0x7C,0x22,0x44,0xFF,0x44,0x04,0x84,0x22,0x84,0x41,0x14,0x82,0x08,
0x00,0x08,0x00,0x08,0x7C,0x08,0x44,0x08,0x45,0xFE,0x44,0x08,0x44,0x08,0x7C,0x08,0x44,0x88,0x44,0x48,0x44,0x48,0x44,0x08,0x7C,0x08,0x44,0x08,0x00,0x28,0x00,0x10,
0x00,0x40,0x04,0x40,0x04,0x20,0x08,0x20,0x10,0x10,0x20,0x08,0x40,0x04,0x9F,0xE2,0x04,0x20,0x04,0x20,0x04,0x20,0x08,0x20,0x08,0x20,0x10,0x20,0x21,0x40,0x40,0x80,
0x08,0x20,0x1C,0x20,0xF0,0x20,0x10,0xA8,0x10,0xA4,0xFC,0xA2,0x11,0x22,0x31,0x20,0x3A,0x24,0x54,0x24,0x54,0x28,0x90,0x08,0x10,0x10,0x10,0x20,0x10,0xC0,0x13,0x00};
uchar code WEEKDAY_WORDS[]={0x00,0x00,0x00,0xfe,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3f,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x3f,0x00,
0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xc0,0x80,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x7F,0xFC,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x48,0x44,0x48,0x3C,0x50,0x04,0x60,0x04,0x40,0x04,0x7F,0xFC,0x40,0x04,0x00,0x00,
0x00,0x00,0x00,0x00,0x7F,0xFC,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x48,0x44,0x48,0x3C,0x50,0x04,0x60,0x04,0x40,0x04,0x7F,0xFC,0x40,0x04,0x00,0x00,
0x02,0x00,0x01,0x00,0x00,0x80,0x00,0x80,0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x04,0x40,0x04,0x20,0x08,0x10,0x08,0x08,0x10,0x08,0x20,0x04,0x40,0x04,0x00,0x00};
uchar code Char_code[10][16]={{0x00,0xe0,0x10,0x08,0x08,0x10,0xe0,0x00,0x00,0x0f,0x10,0x20,0x20,0x10,0x0f,0x00},
{0x00,0x10,0x10,0xf8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3f,0x20,0x20,0x00,0x00},
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0e,0x00},
{0x00,0x00,0xc0,0x20,0x10,0xf8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3f,0x24,0x00},
{0x00,0xf8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0e,0x00},
{0x00,0xe0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0f,0x11,0x20,0x20,0x11,0x0e,0x00},
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3f,0x00,0x00,0x00,0x00},
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1c,0x00},
{0x00,0xe0,0x10,0x08,0x08,0x10,0xe0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0f,0x00}};
bit Lcd_Check_Busy()
{
LcdDataPort=0xff;
RW=1;_nop_();DI=0;
E=1;_nop_();E=0;
return(bit)(LcdDataPort&0x80);
}
void LCD_Write_Command(uchar ch)
{
while(Lcd_Check_Busy());
LcdDataPort=0xff;
RW=0;_nop_();DI=0;
LcdDataPort=ch;
E=1;_nop_();E=0;
}
void Lcd_write_Data(uchar da)
{
while(Lcd_Check_Busy());
LcdDataPort=0xff;
RW=0;_nop_();DI=1;
if(!Reverse_Display)LcdDataPort=da;
else LcdDataPort=~da;
E=1;_nop_();E=0;
}
void InitializeLCD()
{
CSA=1;CSB=1;
LCD_Write_Command(0x3f);
LCD_Write_Command(Lcd_Start_Row);
}
void Disp_Bytes(uchar Page,uchar Col,uchar Bytes,uchar *p)
{
uchar i;
if(Col<64)
{
CSA=1;CSB=0;
LCD_Write_Command(Lcd_Page+Page);
LCD_Write_Command(Lcd_Col+Col);
if(Col+Bytes<64)
for(i=0;i<Bytes;i++)Lcd_write_Data(p[i]);
else
{
for(i=0;i<64-Col;i++)Lcd_write_Data(p[i]);
CSA=0;CSB=1;
LCD_Write_Command(Lcd_Page+Page);
LCD_Write_Command(Lcd_Col);
for(i=64-Col;i<Bytes;i++)Lcd_write_Data(p[i]);
}
}
else
{
CSA=0;CSB=1;
LCD_Write_Command(Lcd_Page+Page);
LCD_Write_Command(Lcd_Col+Col-64);
for(i=0;i<Bytes;i++)Lcd_write_Data(p[i]);
}
}
void OneChar(uchar x,uchar y,uchar num)
{
Disp_Bytes(x,y,8,Char_code[num]);
Disp_Bytes(x+1,y,8,Char_code[num]+8);
}
void Hanzi(uchar x,uchar y,uchar *hz)
{
Disp_Bytes(x,y,16,hz);
Disp_Bytes(x+1,y,16,hz+16);
}
void Write_Byte(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
SDA=x&1;
SCLK=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCLK=0;
x>>=1;
}
}
uchar Get_Byte()
{
uchar i,b,t;
for(i=0;i<8;i++)
{
b>>=1;t=SDA;b|=t<<7;
SCLK=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
SCLK=0;
}
t=b/16*10+b%16;
return t;
}
uchar Read_Data(uchar addr)
{
uchar t;
RST=0;SCLK=0;RST=1;
Write_Byte(addr);
_nop_();_nop_();_nop_();_nop_();_nop_();
t=Get_Byte();
SCLK=1;RST=0;
return t;
}
void Write_DS1302(uchar addr,uchar dat)
{
SCLK=0;RST=1;
Write_Byte(addr);
Write_Byte(dat);
SCLK=1;RST=0;
}
void Set_Time()
{
uchar i;
Write_DS1302(0x8e,0x00);
for(i=0;i<7;i++)
Write_DS1302(0x80+2*i,(DateTime[i]/10<<4|DateTime[i]%10));
Write_DS1302(0x8e,0x80);
}
void Get_Time()
{
uchar i;
for(i=0;i<7;i++)
DateTime[i]=Read_Data(0x81+2*i);
}
void DateTime_Adjust(char x)
{
DateTime[Adjust_Index]=DateTime[Adjust_Index]+x;
}
void T0_Int()interrupt 1
{
uchar i;
TH0=-50000/256;
TL0=-50000%256;
if(++tCount<=20)return;
tCount=0;
Reverse_Display=Adjust_Index==6;
OneChar(0,26,DateTime[6]/10);
OneChar(0,34,DateTime[6]%10);
for(i=4;i>2;i--)
{
Reverse_Display=Adjust_Index==i;
OneChar(0,56+(4-i)*32,DateTime[i]/10);
OneChar(0,64+(4-i)*32,DateTime[i]%10);
}
Reverse_Display=Adjust_Index==5;
Hanzi(2,88,WEEKDAY_WORDS+(DateTime[5]%10-1)*32);
for(i=2;i>0;i--)
{
Reverse_Display=Adjust_Index==i;
OneChar(5,26+(2-i)*32,DateTime[i]/10);
OneChar(5,34+(2-i)*32,DateTime[i]%10);
}
Reverse_Display=0;
OneChar(5,88,DateTime[0]/10);
OneChar(5,96,DateTime[0]%10);
}
void Ex_Int0()interrupt 0
{
if(K1==0)
{
if(Adjust_Index==-1||Adjust_Index==0)Adjust_Index=7;
else Adjust_Index--;
}
else if(K2==0)
DateTime_Adjust(1);
else if(K3==0)
DateTime_Adjust(-1);
else
{
Set_Time();
Adjust_Index=-1;
}
}
main()
{
uchar i;
InitializeLCD();
OneChar(0,10,2);
OneChar(0,18,0);
for(i=0;i<3;i++)
Hanzi(0,42+i*32,DATE_TIME_WORDS+i*32);
Hanzi(2,42,DATE_TIME_WORDS+3*32);
Hanzi(2,58,DATE_TIME_WORDS+4*32);
for(i=0;i<3;i++)
Hanzi(0,42+i*32,DATE_TIME_WORDS+(i+5)*32);
IE=0x83;
IP=0x01;
IT0=0x01;
TH0=-50000/256;
TL0=-50000%256;
TR0=1;
while(1)
if(Adjust_Index==-1)Get_Time();
}
运行报出的警告:
Rebuild started: Project: main
Rebuild target 'Target 1'
assembling STARTUP.A51...
compiling main.c...
linking...
*** WARNING L15: MULTIPLE CALL TO SEGMENT
SEGMENT: ?PR?_ONECHAR?MAIN
CALLER1: ?C_C51STARTUP
CALLER2: ?PR?T0_INT?MAIN
*** WARNING L15: MULTIPLE CALL TO SEGMENT
SEGMENT: ?PR?_DISP_BYTES?MAIN
CALLER1: ?C_C51STARTUP
CALLER2: ?PR?T0_INT?MAIN
*** WARNING L15: MULTIPLE CALL TO SEGMENT
SEGMENT: ?PR?_HANZI?MAIN
CALLER1: ?C_C51STARTUP
CALLER2: ?PR?T0_INT?MAIN
Program Size: data=41.1 xdata=0 code=2022
creating hex file from ".\Objects\main"...
".\Objects\main" - 0 Error(s), 3 Warning(s).
Build Time Elapsed: 00:00:01
|