LCD数字时钟,断电后保存数据到24C02,上电后继续接着走。
代码:
main.c
#include
#include"lcd1602.c"
#include"24c02.c"
#define uint unsigned int
#define uchar unsigned char
***it k1 = P3^0;
***it k2 = P3^1;
***it k3 = P3^2;
***it k4 = P3^3;
uchar count,miao,fen,shi,k1num;
uchar xiqi,ri,yue,nian;
uchar code table[] = " 2012-12-05 Wed ";
uchar code table_week[7][4] = {"Sun ","Mon ","Tus ","Wed ","Thr ","Fri ","Sat "};
void LCD1602_wrt_sfm ( uchar add , uchar dt ) {
uchar sh,ge;
sh = dt/10;
ge = dt%10;
LCD1602_wrt_cmd(0x80+0x40+add);
LCD1602_wrt_dt(0x30+sh);
LCD1602_wrt_dt(0x30+ge);
}
void LCD1602_wrt_date ( uchar add , uchar dt ) {
uchar sh,ge;
sh = dt/10;
ge = dt%10;
LCD1602_wrt_cmd(0x80+add);
LCD1602_wrt_dt(0x30+sh);
LCD1602_wrt_dt(0x30+ge);
}
void LCD1602_wrt_week ( uchar dt ) {
LCD1602_wrt_cmd(0x80+12);
LCD1602_wrt_dt(table_week[dt][0]);
LCD1602_wrt_dt(table_week[dt][1]);
LCD1602_wrt_dt(table_week[dt][2]);
}
void init() {
uchar num;
LCD1602_EN = 0;
miao = 0;
fen = 0;
shi = 0;
count = 0;
k1num = 0;
xiqi = 3;
nian = 12;
yue = 12;
ri = 5;
init_24c02();
miao = read_24c02 (1);
fen = read_24c02 (2);
shi = read_24c02 (3);
if ( (!(miao>=0&&miao<60))||(!(fen>=0&&fen<60))||(!(shi>=0&&shi<24)) ) {
miao = 0;
fen = 0;
shi = 0;
write_24c02 (1,miao);
write_24c02 (2,fen);
write_24c02 (3,shi);
}
xiqi = read_24c02(4);
ri = read_24c02(5);
yue = read_24c02(6);
nian = read_24c02(7);
if ( (!(xiqi>=0&&xiqi<7))||(!(ri>=1&&ri<32))||(!(yue>=1&&shi<13))||(!(nian>=0&&nian<100)) ) {
xiqi = 3;
nian = 12;
yue = 12;
ri = 5;
write_24c02 (4,xiqi);
write_24c02 (5,ri);
write_24c02 (6,yue);
write_24c02 (7,nian);
}
LCD1602_wrt_cmd(0x38);
delay(5);
LCD1602_wrt_cmd(0x38);
delay(5);
LCD1602_wrt_cmd(0x38);
delay(5);
LCD1602_wrt_cmd(0x0c);
delay(5);
LCD1602_wrt_cmd(0x06);
delay(5);
LCD1602_wrt_cmd(0x01);
delay(5);
LCD1602_wrt_cmd(0x80);
delay(5);
for( num=0 ; num<15 ; num++ ) {
LCD1602_wrt_dt( table[num] );
delay(5);
}
LCD1602_wrt_date(3,nian);
LCD1602_wrt_date(6,yue);
LCD1602_wrt_date(9,ri);
LCD1602_wrt_week(xiqi);
LCD1602_wrt_cmd(0x80+0x40+6);
LCD1602_wrt_dt(':');
delay(5);
LCD1602_wrt_cmd(0x80+0x40+9);
LCD1602_wrt_dt(':');
delay(5);
LCD1602_wrt_sfm(10,miao);
LCD1602_wrt_sfm(7,fen);
LCD1602_wrt_sfm(4,shi);
TMOD = 0x01;
TH0 = (65536-49950)/256;
TL0 = (65536-49950)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void keyscan () {
if ( !k1 ) {
delay(10);
if ( !k1 ) {
k1num++;
while ( !k1 ) Bp( 20 );;
if ( 1==k1num ) {
TR0 = 0;
LCD1602_wrt_cmd(0x80+0x40+11);
LCD1602_wrt_cmd(0x0f);
}
if ( 2==k1num ) {
LCD1602_wrt_cmd(0x80+0x40+8);
}
if ( 3==k1num ) {
LCD1602_wrt_cmd(0x80+0x40+5);
}
if ( 4==k1num ) {
LCD1602_wrt_cmd(0x80+14);
}
if ( 5==k1num ) {
LCD1602_wrt_cmd(0x80+10);
}
if ( 6==k1num ) {
LCD1602_wrt_cmd(0x80+7);
}
if ( 7==k1num ) {
LCD1602_wrt_cmd(0x80+4);
}
if ( 8==k1num ) {
k1num = 0;
LCD1602_wrt_cmd(0x0c);
TR0 = 1;
}
}
}
if ( k1num ) {
if ( !k2 ) {
delay(10);
if ( !k2 ) {
while ( !k2 ) Bp( 20 );
if ( 1==k1num ) {
miao++;
if ( 60==miao ) miao=0;
LCD1602_wrt_sfm(10,miao);
write_24c02 (1,miao);
LCD1602_wrt_cmd(0x80+0x40+11);
}
if ( 2==k1num ) {
fen++;
if ( 60==fen ) fen=0;
LCD1602_wrt_sfm(7,fen);
write_24c02 (2,fen);
LCD1602_wrt_cmd(0x80+0x40+8);
}
if ( 3==k1num ) {
shi++;
if ( 24==shi ) shi=0;
LCD1602_wrt_sfm(4,shi);
write_24c02 (3,shi);
LCD1602_wrt_cmd(0x80+0x40+5);
}
if ( 4==k1num ) {
xiqi++;
if ( 7==xiqi ) xiqi = 0;
LCD1602_wrt_week(xiqi);
write_24c02 (4,xiqi);
LCD1602_wrt_cmd(0x80+14);
}
if ( 5==k1num ) {
ri++;
if ( 32==ri ) ri = 1;
LCD1602_wrt_date(9,ri);
write_24c02 (5,ri);
LCD1602_wrt_cmd(0x80+10);
}
if ( 6==k1num ) {
yue++;
if ( 13==yue ) yue = 1;
LCD1602_wrt_date(6,yue);
write_24c02 (6,yue);
LCD1602_wrt_cmd(0x80+7);
}
if ( 7==k1num ) {
nian++;
if ( 100==nian ) nian = 0;
LCD1602_wrt_date(3,nian);
write_24c02 (7,nian);
LCD1602_wrt_cmd(0x80+4);
}
}
}
}
if ( k1num ) {
if ( !k3 ) {
delay(10);
if ( !k3 ) {
while ( !k3 ) Bp( 20 );;
if ( 1==k1num ) {
miao--;
if ( -1==miao ) miao=59;
LCD1602_wrt_sfm(10,miao);
write_24c02 (1,miao);
LCD1602_wrt_cmd(0x80+0x40+11);
}
if ( 2==k1num ) {
fen--;
if ( -1==fen ) fen=59;
LCD1602_wrt_sfm(7,fen);
write_24c02 (2,fen);
LCD1602_wrt_cmd(0x80+0x40+8);
}
if ( 3==k1num ) {
shi--;
if ( -1==shi ) shi=23;
LCD1602_wrt_sfm(4,shi);
write_24c02 (3,shi);
LCD1602_wrt_cmd(0x80+0x40+5);
}
if ( 4==k1num ) {
xiqi--;
if ( -1==xiqi ) xiqi = 6;
LCD1602_wrt_week(xiqi);
write_24c02 (4,xiqi);
LCD1602_wrt_cmd(0x80+14);
}
if ( 5==k1num ) {
ri--;
if ( -1==ri ) ri = 31;
LCD1602_wrt_date(9,ri);
write_24c02 (5,ri);
LCD1602_wrt_cmd(0x80+10);
}
if ( 6==k1num ) {
yue--;
if ( -1==yue ) yue = 12;
LCD1602_wrt_date(6,yue);
write_24c02 (6,yue);
LCD1602_wrt_cmd(0x80+7);
}
if ( 7==k1num ) {
nian--;
if ( -1==nian ) nian = 99;
LCD1602_wrt_date(3,nian);
write_24c02 (7,nian);
LCD1602_wrt_cmd(0x80+4);
}
}
}
}
if ( k1num ) {
if ( !k4 ) {
delay(10);
if ( !k4 ) {
while ( !k4 ) Bp( 20 );
k1num = 0;
LCD1602_wrt_cmd(0x0c);
TR0 = 1;
}
}
}
}
void main () {
init();
while(1) {
keyscan();
}
}
void time0 () interrupt 1 {
TH0 = (65536-49950)/256;
TL0 = (65536-49950)%256;
count++;
if ( 20==count ) {
count = 0;
miao++;
if ( 60==miao ) {
miao = 0;
fen++;
if ( 60==fen ) {
fen = 0;
shi++;
if ( 24==shi ) {
shi = 0;
ri++;
if ( 32==ri ) ri = 0;
xiqi++;
if ( 32==ri ) ri = 0;
LCD1602_wrt_week(xiqi);
write_24c02 (4,xiqi);
LCD1602_wrt_date(9,ri);
write_24c02 (5,ri);
}
LCD1602_wrt_sfm(4,shi);
write_24c02 (3,shi);
}
LCD1602_wrt_sfm(7,fen);
write_24c02 (2,fen);
}
LCD1602_wrt_sfm(10,miao);
write_24c02 (1,miao);
}
}
24C02.c
***it sda = P2^0;
***it scl = P2^1;
void _delay_ () {
; ;
}
void start() { //开始信号
sda = 1;
_delay_();
scl = 1;
_delay_();
sda = 0;
_delay_();
}
void stop() { //停止信号
sda = 0;
_delay_();
scl = 1;
_delay_();
sda = 1;
_delay_();
}
void respons() { //应答
unsigned char i;
scl = 1;
_delay_();
while( (1==sda)&&(i<250) ) i++;
scl = 0;
_delay_();
}
void init_24c02() {
sda = 1;
_delay_();
scl = 1;
_delay_();
}
void write_byte(unsigned char dt){
unsigned char i,temp;
temp = dt;
for ( i=0 ; i<8 ; i++ ) {
temp = temp<<1;
scl=0;
_delay_();
sda = CY;
_delay_();
scl = 1;
_delay_();
}
scl = 0;
_delay_();
sda = 1;
_delay_();
}
unsigned char read_byte() {
unsigned char i,k;
scl=0;
_delay_();
sda=1;
_delay_();
for ( i=0 ; i<8 ; i++ ) {
scl = 1;
_delay_();
k = (k<<1)|sda;
scl = 0;
_delay_();
}
return k;
}
void write_24c02 ( unsigned char i , unsigned char dt ) {
start();
write_byte(0xa0);
respons();
write_byte(i);
respons();
write_byte(dt);
respons();
stop();
}
unsigned char read_24c02 ( unsigned char i ) {
unsigned char dt;
start();
write_byte(0xa0);
respons();
write_byte(i);
respons();
start();
write_byte(0xa1);
respons();
dt = read_byte();
stop();
return dt;
}
LCD1602.c
***it LCD1602_RS = P2^6;
***it LCD1602_RW = P2^5;
***it LCD1602_EN = P2^7;
***it Beep = P1^5;
void delay ( unsigned int z ) { //粗略延时
unsigned int x,y;
for ( x=z ; x>0 ; x-- )
for ( y=110 ; y>0 ; y-- );
}
void Bp ( unsigned int z ) { //蜂鸣器
unsigned int x;
for ( x=z ; x>0 ; x-- ) {
Beep = 0;
delay(1);
Beep = 1;
delay(1);
}
}
void LCD1602_wrt_cmd ( unsigned char cmd ) { //液晶写命令 LCD1602
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_EN = 0;
P0 = cmd;
delay(5);
LCD1602_EN = 1;
delay(5);
LCD1602_EN = 0;
}
void LCD1602_wrt_dt ( unsigned char dt ) { //液晶写数据 LCD1602
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_EN = 0;
P0 = dt;
delay(5);
LCD1602_EN = 1;
delay(5);
LCD1602_EN = 0;
}
|