qq_35578129
Kent_小小生
2017-06-07 17:10

关于51单片机的1602液晶结合AT24C02显示时钟程序的相关问题

  • 单片机

我在郭天祥的单片机书上看到关于1602液晶显示时钟的程序,然后我把对照这个模板写,
发现程序出现一个警告, 板子无法正常在1602上显示。 求各位大佬讲解。

//main.c//
#include
#include"IIC.H"
#include
#define uchar unsigned char
uchar count,keynum;
char sec,min,hour;

sbit lcden=P1^2; //液晶使能端
sbit lcdrs=P2^0;
sbit buzz=P0^6;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
uchar code table[]="2017-06-07";
void Delay5ms() //@11.0592MHz
{
unsigned char i, j;

i = 54;
j = 199;
do
{
    while (--j);
} while (--i);

}

void di()
{
P2=0XA0;
buzz=1;
Delay5ms();
P2=0x00;
}
void write_com(unsigned char date)
{
lcdrs=0;
lcden=1;
P0=date;
Delay5ms();

lcden=0;
}

void write_data(unsigned char date)
{
lcdrs=1;
lcden=1;
P0=date;
Delay5ms();
lcden=0;
}
void write_sfm(uchar add, uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+ge);
write_data(0x30+shi);
}
void init1()
{
uchar num;
sec=0;
min=0;
hour=0;
keynum=0;
count=0;
init_24c02();
write_com(0x38); //模式
write_com(0x0c); //开显示,显示光标,不闪烁
write_com(0x06); //指针加1
write_com(0x01); //清屏
for(num=0;num<12;num++)
{
write_data(table[num]);
}
sec=read_add(1);
min=read_add(2);
hour=read_add(3);
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
write_com(0x80+0x40+6);
write_data(':');
Delay5ms();
write_com(0x80+0x40+9);
write_data(':');
Delay5ms();
}
void key_scan()
{

 if(key1)
 {
    TR0=0;
    Delay5ms();
    if(key1)
    {
        while(!key1);
        di();
        keynum++;
    if(keynum==1)
    {
        write_com(0x80+0x40+10);
        write_data(0x0f);

     }
     if(keynum==2)
     {
        write_com(0x80+0x40+7);
    }
    if(keynum==3)
    {
        write_com(0x80+0x40+4);

        }
    if(keynum==4)
    {
        keynum=0;
        write_com(0x0c);
        TR0=1;
    }

}
    if(!keynum)
    {
        if(key2)
        {
            Delay5ms();
         if(key2)
         {
             while(!key3);
            di();
            if(keynum==1)
            {
                sec++;  
                if(sec==60)
                {
                    sec=0;
                }
                write_sfm(10,sec);
                write_com(0x80+0x40+10);
                write_add(1,sec);
            } 
            if(keynum==2)
            {
                min++;
                 if(min==60)
                 {
                    min=0;
                }
            write_sfm(7,min);
            write_com(0x80+0x40+7);
            write_add(2,min);   
            }
            if(keynum==3)
            {
                  hour++;
                  if(hour==60)
                  {
                    hour=0;
                  }
                    write_sfm(4,hour);
                write_com(0x80+0x40+4);
                    write_add(3,hour);
            }
        }
    }    
        if(key3)
        {
            Delay5ms();
            if(key3)
            {
                while(!key3);
                di();
                if(keynum==1)
                {
                    sec--;
                    if(sec==-1)
                    {
                        sec=60;
                    }
                write_sfm(10,sec);
                write_com(0x80+0x40+10);
                write_add(1,sec);
                }
                 if(keynum==2)
                 {
                    min--;
                   if(min==0)
                   {
                        min=60;
                    }
                write_sfm(7,min);
                write_com(0x80+0x40+7);
                write_add(2,min);
                 }
                 if(keynum==3)
                 {
                    hour--;
                    if(hour==0)
                    {
                        hour=60;
                    }
                write_sfm(4,hour);
                write_com(0x80+0x40+4);
                write_add(3,hour);
                }
            }
        }
    }
}

}

void main()
{

init1();
while(1)
{
key_scan();
}
}

void timer0() interrupt 1
{

TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
sec++;
if(sec==60)
{

sec=0;

min++;
if(min==60)
{
min=0;
hour++;
if(hour==60)
{
hour=0;
}
write_sfm(4,hour);
write_add(3,hour);
}
write_sfm(7,min);
write_add(2,min);
}
write_sfm(10,sec);
write_add(1,sec);
}
}
//IIC.c//
#include
#include "IIC.H"
#include
void delay0()
{ ;;}

void Delay5us()

{
unsigned char i;

_nop_();
_nop_();
i = 12;
while (--i);

}

//开始信号
void start()

{
SDA=1;

Delay5us();
SCL=1;
Delay5us();
SDA=0;
Delay5us();
SCL=0;
Delay5us();
}
//停止信号
void stop()

{
SDA=0;
Delay5us();
SCL=1;
Delay5us();
SDA=1;
Delay5us();
}
//主机应答反映
void send_renspons(bit date)

{
SDA=date;
SCL=1;
Delay5us();
SCL=0;
Delay5us();
}
//从机应答信号
bit re_renspons()

{
bit temp;
SDA=1;
Delay5us();
SCL=1;
Delay5us();
if(SDA==1)
temp=0;
else
temp=1;
SCL=0;
Delay5us();
return temp;
}
void init_24c02()
{
SDA=1;
delay0();
SCL=1;
delay0();
}
//写数据_字节
void write_date(unsigned char date)
{ unsigned char i;
for (i=0;i<8;i++)
{
if ((date<<i)&0x80)
SDA=1;
else
SDA=0;
SCL=1;
Delay5us();
SCL=0;
Delay5us();
}

re_renspons();
}
//读数据——字节
unsigned char read_date()

{
unsigned char i,temp;
SDA=1;
Delay5us();
for (i=0;i<8;i++)
{
temp<<=1;
SCL=1;
Delay5us();
if(SDA==1)
temp=temp+1;
SCL=0;
Delay5us();

            }
  return temp;

}
//写一个字节
void write_add(unsigned char addr,unsigned char date)

{
start();
write_date(0xa0);
write_date(addr);
write_date(date);
stop();
}
//读一个字节
unsigned char read_add(unsigned char addr)

{
unsigned char temp;
start();
write_date(0xa0);
write_date(addr);
start();
write_date(0xa1);
temp=read_date();
send_renspons(1);
stop();
return temp;
}
//IIC.H//
#ifndef IIC_H
#define IIC_H

sbit SCL=P2^0;
sbit SDA=P2^1;
void write_add(unsigned char addr,unsigned char date);

unsigned char read_add(unsigned char addr);

void init_24c02();

#endif

图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答