打印
[51单片机]

C51 控制LCD 显示,,总是显示FFFFFFF,,求做过的民间高手...

[复制链接]
930|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 xiaocainiaoa 于 2016-6-7 16:30 编辑

单片机C51 ,,显示LCD 1602。。时钟芯片DS1302,,,仿真显示是FFFFFFFFFFFFFFF...求大神们诊断一下。。。小弟毕设要跪了。。还有这玩意貌似怎么做定时啊?比如定时明天早上6点干个啥的。。。求指教啊。。。跑的代码如下:

DS.PNG (28.31 KB )

DS.PNG

程序.PNG (9.46 KB )

程序.PNG

相关帖子

沙发
xiaocainiaoa|  楼主 | 2016-6-7 16:21 | 只看该作者
本帖最后由 xiaocainiaoa 于 2016-6-7 16:31 编辑

,,,程序。。是这个。。。
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int

uchar code Mun_to_char[16] = {"0123456789ABCDEF"};
uchar idata Time_data[7]={0x53,0x07,0x21,0x05,0x17,0x02,0x11};
uchar idata table[7]={12,3,1,3,1,0,12};
uchar idata Lcd_line1[16]={"11/05/12  12:00 "};
//uchar data Lcd_line2[16]={"    00:00:00    "};
//uchar data Weeks[8][3]={{"SUN"},{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"},{"SUN"}};
char flage=0;
uchar num;

sbit sclk=P1^5;
sbit io=P1^4;
sbit rst=P1^6;
//sbit leds_en=P3^6;
sbit lcdrs=P0^4;
sbit lcdrw=P0^5;
sbit lcden=P0^6;
sbit setkey=P3^2;
sbit key=P3^3;
sbit up=P3^4;
sbit down=P3^5;

sbit  ACC_0 = ACC^0;
sbit  ACC_7 = ACC^7;

void delay25ms(void)   //误差 -0.000000000004us
{
    unsigned char a,b;
    for(b=85;b>0;b--)
        for(a=134;a>0;a--);
}
void delay1s(void)   //误差 -0.00000000024us
{
    unsigned char a,b,c;
    for(c=95;c>0;c--)
        for(b=26;b>0;b--)
            for(a=185;a>0;a--);
}

void delay(uchar time)
{
uchar i,j;
for(i=time;i>0;i--)
for(j=114;j>0;j--);
}
void write_com(uchar com)
{
lcdrw=0;
lcdrs=0;
P2=com;
lcden=1;
delay(5);
lcden=0;
P2=0xff;
}
void write_data(uchar date)
{
lcdrw=0;
lcdrs=1;
P2=date;
lcden=1;
delay(5);
lcden=0;
P2=0xff;
}
void init_1602()
{
P2=0xff;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
}
void write_datalcd(uchar add,uchar date)
{
uchar ge,shi;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void string(uchar ad, uchar *s)
{ write_com(ad);
   while(*s>0)
    { write_data(*s++);
      delay(10);
    }
}
//1302定义
void write_byte1302(uchar d)
{
uchar i;
ACC=d;
for(i=8;i>0;i--)
{
io=ACC_0;
sclk=1;
sclk=0;
ACC=ACC>>1;
}
}
uchar read_byte1302()
{
uchar i;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
ACC_7=io;
sclk=1;
sclk=0;
}
return ACC;
}

write_1302(uchar ucadd,uchar ucdate)
{
rst=0;
sclk=0;
rst=1;
write_byte1302(ucadd);
write_byte1302(ucdate);
sclk=1;
rst=0;
}
uchar read_1302(uchar addr)
{
uchar date;
rst=0;
sclk=0;
rst=1;
write_byte1302(addr);
date=read_byte1302();
sclk=1;
rst=0;
return date;
}

void init_ds1302(uchar *pClock)
{
char i;
uchar ucAddr = 0x80;
write_1302(0x8e,0x00);
//EA = 0;
/*for(i =0; i<7; i++)
{
write_1302(ucAddr,*pClock);
pClock++;
ucAddr +=2;
} */
write_1302(0x8e,0x80);
//EA = 1;
}

void gettime_1302(uchar ucCurtime[])
{
uchar i;
uchar ucAddr = 0x81;
//EA = 0;
for (i=0; i<7; i++)
{
ucCurtime=read_1302(ucAddr);
ucAddr += 2;
}
//EA = 1;
}

//中断键盘设定时间
void  settime (void) interrupt 0
{
EA = 0;
delay1s();
if(setkey == 0)
{
string(0xc0,"settime");
table[0]=(((Time_data[0]/16)*10) + Time_data[0]%16);
table[1]=(((Time_data[1]/16)*10) + Time_data[1]%16);
table[2]=(((Time_data[2]/16)*10) + Time_data[2]%16);
table[3]=(((Time_data[3]/16)*10) + Time_data[3]%16);
table[4]=(((Time_data[4]/16)*10) + Time_data[4]%16);
table[5]=(((Time_data[5]/16)*10) + Time_data[5]%16);
table[6]=(((Time_data[6]/16)*10) + Time_data[6]%16);

flage=1;
}
while(flage)
{
if(key==0)
{
delay25ms();
if(key==0)
{
num++;
while(!key);
write_1302(0x8e,0x00);
write_1302(0x80,80);
write_1302(0x8e,0x80);
//显示光标
switch(num)
{
case 1 :
write_com(0x80+1);
write_com(0x0f);break;
case 2 :
write_com(0x80+4);
break;
case 3 :
write_com(0x80+7);
break;
case 4 :
write_com(0x80+11);
break;
case 5 :
write_com(0x80+14);
break;
case 6 :
num=0;
write_com(0x0c);

Time_data[0]=(table[0]/10)*16+(table[0]%10);
Time_data[1]=(table[1]/10)*16+(table[1]%10);
Time_data[2]=(table[2]/10)*16+(table[2]%10);
Time_data[3]=(table[3]/10)*16+(table[3]%10);
Time_data[4]=(table[4]/10)*16+(table[4]%10);
Time_data[5]=(table[5]/10)*16+(table[5]%10);
Time_data[6]=(table[6]/10)*16+(table[6]%10);

init_ds1302(Time_data);
flage=0;
break;

}
}
}
if(num!=0)
{
if(up==0)
{
delay25ms();
if(up==0)
{
switch(num)
{
//年
case 1:
table[6]++;
if(table[6] < 0)
table[6]=99;
if(table[6] >99)
table[6]=0;
while(!up);
write_datalcd(0,table[6]);
write_com(0x80+1);
break;
//月
case 2 :
table[3]++;
if(table[3]>=13)
table[3]=1;
if(table[3]<=0)
table[3]=12;
while(!up);
write_datalcd(3,table[3]);
write_com(0x80+4);
break;
//日
case 3 :
table[4]++;
if(table[4]>=32)
table[4]=0;
while(!up);
write_datalcd(6,table[4]);
write_com(0x80+7);
break;
//时
case 4 :
table[2]++;
if(table[2]>=24)
table[2]=0;
while(!up);
write_datalcd(10,table[2]);
write_com(0x80+11);
break;
//分
case 5 :
table[1]++;
if(table[1]>=60)
table[1]=0;
while(!up);
write_datalcd(13,table[1]);
write_com(0x80+14);
break;
}
}
}

if(down==0)
{
delay25ms();
if(down==0)
{
switch(num)
{
//年
case 1:
table[6]--;
if(table[6] < 0)
table[6]=99;
if(table[6] >99)
table[6]=0;
while(!down);
write_datalcd(0,table[6]);
write_com(0x80+1);
break;
//月
case 2 :
table[3]--;
if(table[3]>=13)
table[3]=1;
if(table[3]<=0)
table[3]=12;
while(!down);
write_datalcd(3,table[3]);
write_com(0x80+4);
break;
//日
case 3 :
table[4]--;
if(table[4]>=32)
table[4]=31;
while(!down);
write_datalcd(6,table[4]);
write_com(0x80+7);
break;
//时
case 4 :
table[2]--;
if(table[2]>=24)
table[2]=23;
while(!down);
write_datalcd(10,table[2]);
write_com(0x80+11);
break;
//分
case 5 :
table[1]--;
if(table[1]>=60)
table[1]=59;
while(!down);
write_datalcd(13,table[1]);
write_com(0x80+14);
break;
}
}
}
}
}
write_com(0x01);
EA = 1;
}

void main()
{
uchar i;
init_1602();
init_ds1302(Time_data);
P2 = 0xff;
   IT0=1;      
   EX0=1;
   EA=1;
while(1)
{

if(flage==0)
{
gettime_1302(Time_data);

Lcd_line1[0]  = Mun_to_char[Time_data[6]/0x10];
Lcd_line1[1]  = Mun_to_char[Time_data[6]%0x10];

Lcd_line1[3]  = Mun_to_char[Time_data[3]/0x10];
Lcd_line1[4]  = Mun_to_char[Time_data[3]%0x10];
Lcd_line1[6]  = Mun_to_char[Time_data[4]/0x10];
Lcd_line1[7]  = Mun_to_char[Time_data[4]%0x10];
Lcd_line1[10]  = Mun_to_char[Time_data[2]/0x10];
Lcd_line1[11]  = Mun_to_char[Time_data[2]%0x10];
Lcd_line1[13]  = Mun_to_char[Time_data[1]/0x10];
Lcd_line1[14]  = Mun_to_char[Time_data[1]%0x10];

/*for(i=0;i<3;i++)
Lcd_line1[i+13]=Weeks[Time_data[5]&0x07];
Lcd_line2[4]  = Mun_to_char[Time_data[2]/0x10];
Lcd_line2[5]  = Mun_to_char[Time_data[2]%0x10];
Lcd_line2[7]  = Mun_to_char[Time_data[1]/0x10];
Lcd_line2[8]  = Mun_to_char[Time_data[1]%0x10];
Lcd_line2[10] = Mun_to_char[Time_data[0]/0x10];
Lcd_line2[11] = Mun_to_char[Time_data[0]%0x10];        */
for(i=0;i<16;i++)
write_data(Lcd_line1);
write_com(0x80);
//for(i=0;i<16;i++)
//write_data(Lcd_line2);
/*if (!setkey)
{
settime();
} */
}
}
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

6

帖子

0

粉丝