用STC89C51做了一个带闹钟的1602万年历,怎么不显示?
电路呢?程序呢?什么都没有怎么帮你。难道又是伸手party?
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
#define GPIO P0
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit buz=P2^4;//闹铃位
sbit dat=P2^7;
sbit clk=P2^6;
sbit rst=P2^5;
sbit set=P1^0;//按键
sbit up=P1^1;
sbit down=P1^2;
sbit fn=P1^3;
uchar station=0;//按键状态
bit flag=0;//50ms标志位
bit flag_c=0;//闹钟启动标志位
bit flag_clock=1;//闹钟开,闹钟关
uchar counter_c=0;//闹钟时长计数器
uchar counter_c1=0;
uchar week=0;
char str[16]=" Gzy 20 - - ";
char str1[16]="c- : : ";
//以下为1602函数
void delay_us(uchar us);//延时微秒
void delay_ms(uchar ms);//延时毫秒
void write_com(uchar c);//写指令
void write_data(uchar dat);//写数据
void show_char(uchar pos,uchar c);//显示单个字符
void show_str(uchar line,uchar *str);//显示字符串
void ini_lcd();//初始化LCD
//以下为DS1302函数
void write_byte(uchar temp);//写
void write_1302(uchar add,uchar dat);//写1302
uchar read_1302(uchar add);//读1302数据
void update_time();//更新时间
void clock();//闹钟启动标志置1
void alarm();//闹铃
void key_scan();//按键扫描
void InitTimer1(void);
void main()
{
ini_lcd();
InitTimer1();
buz=0;
show_str(0,str);
show_str(1,str1);
// while(1);
while(1){
if(flag==1){
flag=0;
clock();
alarm();
}
key_scan();
}
}
void Timer1Interrupt(void) interrupt 3
{
TH1 = 0x3C;
TL1 = 0x0B0;
flag=1;
if((flag_c==1)&&(flag_clock==1)){
counter_c++;
if(counter_c==19)
buz=~buz;
}
}
//以下为函数实体
void InitTimer1(void)
{
TMOD = 0x10;
TH1 = 0x3C;
TL1 = 0x0B0;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void delay_us(uchar us)
{
uchar c_us;
c_us=us>>1;
while(--c_us);
}
void delay_ms(uchar ms)
{
while(--ms){
delay_us(250);
delay_us(250);
delay_us(250);
delay_us(250);
}
}
void write_com(uchar c)
{ rw=0;
delay_ms(1);
rs=0;
delay_ms(1);
GPIO=c;
delay_ms(1);
e=1;
delay_ms(1);
e=0;
}
void write_data(uchar dat)
{ rw=0;
delay_ms(1);
rs=1;
delay_ms(1);
GPIO=dat;
delay_ms(1);
e=1;
delay_ms(1);
e=0;
}
void show_char(uchar pos,uchar c)
{
uchar p;
if(pos>=0x10)
p=pos+0xb0;
else
p=pos+0x80;
write_com(p);
write_data(c);
}
void show_str(uchar line,uchar *str)
{
uchar l,i;
l=line<<4;
for(i=0;i<16;i++)
show_char(l++,*(str+i));
}
void ini_lcd()
{
delay_ms(15);
write_com(0x38);
write_com(0x06);//显示光标移动位置
write_com(0x0c);
write_com(0x01);//显示清屏
}
void write_byte(uchar temp)
{
uchar i;
for(i=0;i<8;i++){
clk=0;
dat=temp&0x01;
temp>>=1;
clk=1;
}
}
void write_1302(uchar add,uchar dat)
{
rst=0;
_nop_();
clk=0;
_nop_();
rst=1;
_nop_();
write_byte(add);
write_byte(dat);
rst=0;
//_nop_();
}
uchar read_1302(uchar add)
{
uchar i,temp=0x00;
rst=0;
//_nop_();
clk=0;
//_nop_();
rst=1;
//_nop_();
write_byte(add);
for(i=0;i<8;i++){
if(dat)
temp|=0x80; //每次传输低字节
//clk=0;
temp>>=1;
clk=1;
clk=0;
}
clk=1;
rst=0;
/*_nop_(); //以下为DS1302复位的稳定时间
rst=0;
clk=0;
_nop_();
clk=1;
_nop_();
dat=0;
_nop_();
dat=1;
_nop_();*/
return (temp);
}
void update_time()
{
uchar temp;
temp=read_1302(0x81);//秒
if(str1[15]!=temp%16+0x30){
str1[15]=temp%16+0x30;
str1[14]=temp/16+0x30;
show_char(0x1f,str1[15]);
show_char(0x1e,str1[14]);
}
temp=read_1302(0x83);//分
if(str1[12]!=temp%16+0x30){
str1[12]=temp%16+0x30;
str1[11]=temp/16+0x30;
show_char(0x1c,str1[12]);
show_char(0x1b,str1[11]);
}
temp=read_1302(0x85);//时
if(str1[9]!=temp%16+0x30){
str1[9]=temp%16+0x30;
str1[8]=temp/16+0x30;
show_char(0x19,str1[9]);
show_char(0x18,str1[8]);
}
temp=read_1302(0x8b);//星期
if(week!=temp){
week=temp;
switch (temp){
case 2:week=2;str[14]='M';str[15]='o';break;
case 3:week=3;str[14]='T';str[15]='u';break;
case 4:week=4;str[14]='W';str[15]='e';break;
case 5:week=5;str[14]='T';str[15]='h';break;
case 6:week=6;str[14]='F';str[15]='r';break;
case 7:week=7;str[14]='S';str[15]='a';break;
case 1:week=7;str[14]='S';str[15]='u';break;
}
show_char(0x0f,str[15]);
show_char(0x0e,str[14]);
}
temp=read_1302(0x8d);//年
if(str[7]!=temp%16+0x30){
str[6]=temp/16+0x30;
str[7]=temp%16+0x30;
show_char(0x06,str[6]);
show_char(0x07,str[7]);
}
temp=read_1302(0x89);//月
if(str[10]!=temp%16+0x30){
str[9]=temp/16+0x30;
str[10]=temp%16+0x30;
show_char(0x09,str[9]);
show_char(0x0a,str[10]);
}
temp=read_1302(0x87);//日
if(str[13]!=temp%16+0x30){
str[12]=temp/16+0x30;
str[13]=temp%16+0x30;
show_char(0x0c,str[12]);
show_char(0x0d,str[13]);
}
if(flag_clock==0){
if(str1[2]!='o'){
str1[2]='o';
str1[3]='f';
str1[4]='f';
str1[5]=' ';
str1[6]=' ';
show_char(0x12,str1[2]);
show_char(0x13,str1[3]);
show_char(0x14,str1[4]);
show_char(0x15,str1[5]);
show_char(0x16,str1[6]);
}
}
else{
temp=read_1302(0xc1);//闹钟时
if(str1[3]!=temp%16+0x30){
str1[2]=temp/16+0x30;
str1[3]=temp%16+0x30;
show_char(0x12,str1[2]);
show_char(0x13,str1[3]);
str1[4]=':';
show_char(0x14,str1[4]);
}
temp=read_1302(0xc3);//闹钟分
if(str1[6]!=temp%16+0x30){
str1[5]=temp/16+0x30;
str1[6]=temp%16+0x30;
show_char(0x15,str1[5]);
show_char(0x16,str1[6]);
}
}
//显示2行
show_str(1,str1);
show_str(0,str);
}
void clock()
{
uchar h,m,temp1,temp2;
h=read_1302(0x85);//时
m=read_1302(0x83);//分
temp1=read_1302(0xc1);//读RAM数据0,存闹钟时数据
temp2=read_1302(0xc3);//读RAM数据2,存闹钟分数据
if((temp1==h)&&(temp2==m))
flag_c=1;
}
void alarm()
{
if(flag_c==1){
if(counter_c>=20)//闹钟响20*50ms=1s
{
counter_c=0;
counter_c1++;
if(counter_c1>=60){//60*1s=60s
counter_c1=0;
flag_c=0;
counter_c=0;
buz=0;
}
}
}
}
void key_scan()
{
uchar temp;
if(set==0){
delay_ms(10);
if(set==0){
while(set==0);
station++;
if(station>11)
station=0;
}
}
if(fn==0){
delay_ms(10);
if(fn==0){
while(fn==0);
station--;
if(station>11)
station=1;
if(station==0)
station=1;
}
}
switch (station){
case 0:
//temp=read_1302(0x81);
//if(temp>>7)
//if((temp&0x80)==0x80)
//write_1302(0x80,temp&0x7f);
//write_com(0x38);
//write_com(0x06);//显示光标移动位置
//write_com(0x0c);
if(str[0]!='G'){
str[0]='G';
str[1]='z';
str[2]='y';
show_char(0x00,str[0]);
show_char(0x01,str[1]);
show_char(0x02,str[2]);
}
update_time();
break;
case 1: //调年
if(str[0]!='S'){
str[0]='S';
str[1]='e';
str[2]='t';
show_char(0x00,str[0]);
show_char(0x01,str[1]);
show_char(0x02,str[2]);
}
temp=read_1302(0x81);
write_1302(0x80,temp|0x80);
write_com(0x0e); //光标显示
write_com(0x80+7);//光标位置
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0x8d);
temp++;
if(temp%16>9)
temp=(temp&0xf0)+16;//个位数清零,十位数进一
if(temp>0x79)
temp=0x79;
write_1302(0x8c,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0x8d);
temp--;
if(temp%16>9)
temp=temp&0xf0+9;
if(temp>79)
temp=0;
write_1302(0x8c,temp);
update_time();
}
}
break;
case 2: //调月
write_com(0x80+10);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0x89);
temp++;
if(temp%16>9)
temp=(temp&0xf0)+16;
if(temp>0x12)
temp=0x12;
write_1302(0x88,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0x89);
temp--;
if(temp%16>9)
temp=temp&0xf0+9;
if(temp>0x12)
temp=0x01;
if(temp==0)
temp=0x01;
write_1302(0x88,temp);
update_time();
}
}
break;
case 3: //调日
write_com(0x80+13);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0x87);
temp++;
if(temp%16>9)
temp=(temp&0xf0)+16;
switch (read_1302(0x89)){
case 0x01:
case 0x03:
case 0x05:
case 0x07:
case 0x08:
case 0x10:
case 0x12:
if(temp>0x31)
temp=0x31;
break;
case 2:
switch (read_1302(0x8d)%4){
case 0:
if(temp>0x29)
temp=0x29;
break;
default :
if(temp>0x28)
temp=0x28;
break;
}
case 0x04:
case 0x06:
case 0x11:
if(temp>0x30)
temp=0x30;
break;
}
write_1302(0x86,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0x87);
temp--;
if(temp%16>9)
temp=temp&0xf0+9;
if(temp>0x31)
temp=0x01;
if(temp==0)
temp=0x01;
write_1302(0x86,temp);
update_time();
}
}
break;
case 4: //调星期
write_com(0x80+15);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0x8b);
temp++;
if(temp>0x07)
temp=0x07;
write_1302(0x8a,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0x8b);
temp--;
if(temp==0)
temp=0x01;
write_1302(0x8a,temp);
update_time();
}
}
break;
case 5: //闹钟开关
write_com(0x80+0x40);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
flag_clock=~flag_clock;
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
flag_clock=~flag_clock;
update_time();
}
}
break;
case 6: //调闹钟时
if(flag_clock==1){
write_com(0x80+0x40+3);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0xc1);
temp++;
if(temp%16>9)
temp=(temp&0xf0)+16;
if(temp>0x23)
temp=0x23;
write_1302(0xc0,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0xc1);
temp--;
if(temp%16>9)
temp=temp&0xf0+9;
if(temp>0x23)
temp=0;
if(temp==0x00)
temp=0x00;
write_1302(0xc0,temp);
update_time();
}
}
}
else station=8;
break;
case 7: //调闹钟分
if(flag_clock==1){
write_com(0x80+0x40+6);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0xc3);
temp++;
if(temp%16>9)
temp=(temp&0xf0)+16;
if(temp>0x59)
temp=0x59;
write_1302(0xc2,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0xc3);
temp--;
if(temp%16>9)
temp=temp&0xf0+9;
if(temp==0x00)
temp=0;
if(temp>0x59)
temp=0x00;
write_1302(0xc2,temp);
update_time();
}
}
}
else station=8;
break;
case 8: //调时
write_com(0x80+0x40+9);
if(up==0){
delay_ms(10);
if(up==0){
while(up==0);
temp=read_1302(0x85);
temp++;
if(temp%16>9)
temp=(temp&0xf0)+16;
if(temp>0x23)
temp=0x23;
write_1302(0x84,temp);
update_time();
}
}
if(down==0){
delay_ms(10);
if(down==0){
while(down==0);
temp=read_1302(0x85);
temp--;
if(temp%16>9)
&n