找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5006|回复: 4
收起左侧

stc89C52单片机LCD显示时间设置问题

[复制链接]
ID:183600 发表于 2017-3-26 19:07 | 显示全部楼层 |阅读模式
本帖最后由 Seanwin 于 2017-3-27 16:38 编辑

1.jpg 2.jpg
设计LCD显示日期时间,现在先设计到时间。但是写出来的程序在设置时间时每按设置键s6和按增加s7各只作接低电平的键s9功能一样出现光标,可以移动,但是不能作“+”功能,“-”功能S8无作用。

各位高手能否看下我写的程序(如下),哪里写错了!!!


#include<reg52.h>
#define UCHAR        unsigned char
#define UINT unsigned int

sbit lcdE=P3^4;
sbit lcdRs=P3^5;
sbit bitSeg=P2^6;
sbit bitPos=P2^7;
sbit keySet=P3^0;
sbit keyInc=P3^1;
sbit keyDec=P3^2;
sbit rd=P3^7;

UINT num,setNum,incNum,count,hh,mm,ss;
UCHAR code date[]="2017.03.24 SUN";
UCHAR code time[]="23:59:55";
UCHAR code timeAddr[]={0,1,3,4,6,7};

void delay(UINT t)
        {
                UINT x,y;
                for(x=0;x<t;x++)
                        for(y=0;y<110;y++);
        }

void write_com(UCHAR com)
        {
                lcdRs=0; //set the instruction signal(low level)
                lcdE=0;        //set the port of lcdE low level
                P0=com;        //send valid data to the port of P0
                delay(5);        //delay time
                lcdE=1;        //the clock latch signal input,and write the data to LCD1602
                delay(5);        //delay time
                lcdE=0; //set the port of lcdE low level
        }

void write_dat(UCHAR dat)
        {
                lcdRs=1; //set the data signal(hight level)
                lcdE=0;        //set the port of lcdE low level
                P0=dat;        //send valid data to the port of P0
                delay(5);        //delay time
                lcdE=1;        //the clock latch signal input,and write the data to LCD1602
                delay(5);        //delay time
                lcdE=0; //set the port of lcdE low level
        }

void write_time(UCHAR addr,UCHAR m)
{
        UCHAR shi,ge;
        shi=m/10;
        ge=m%10;
        write_com(0x80+0x44+addr);
        write_dat(0x30+shi); //address reference character sheet
        write_dat(0x30+ge);        //address reference character sheet
}
//写到红色字段程序时,运行不如意。
void key_scan()
{
        rd=0;
        if(keySet==0)
                {
                        delay(5);
                        if(keySet==0)
                                {
                                        while(!keySet);
                                        setNum++;
                                        if(setNum<7)
                                                {
                                                        TR0=0;
                                                        write_com(0x0f);
                                                        switch(setNum)
                                                                {
                                                                        case 1:write_com(0x80+0x45);
                                                                                                        break;
                                                                        case 2:write_com(0x80+0x48);
                                                                                                        break;
                                                                        case 3:write_com(0x80+0x4b);
                                                                                                        break;
                                                                        case 4:write_com(0x0c);
                                                                                                        setNum=0;
                                                                                                        write_com(0x80+0x45);
                                                                                                        TR0=1;
                                                                                                        break;
                                                                        default:   ;break;
                                                                }
                                                        if(keyInc==0)
                                                        {
                                                                delay(5);
                                                                if(keyInc==0)
                                                                        {        
                                                                                while(!keyInc)
                                                                                switch(setNum)
                                                                                        {
                                                                                                case 1:hh++;
                                                                                                                                if(hh==60)
                                                                                                                                                hh=0;
                                                                                                                                write_time(0,hh);
                                                                                                                                write_com(0x80+0x45);
                                                                                                                                break;
                                                                                                case 2:mm++;
                                                                                                                                if(mm==60)
                                                                                                                                                mm=0;
                                                                                                                                write_time(3,mm);
                                                                                                                                write_com(0x80+0x48);
                                                                                                                                break;
                                                                                                case 3:ss++;
                                                                                                                                if(ss==60)
                                                                                                                                                ss=0;
                                                                                                                                write_time(6,ss);
                                                                                                                                write_com(0x80+0x4b);
                                                                                                                                break;
                                                                                                default:   ;break;
                                                                                        }
                                                                                                
                                                                        }
                                                        }
                                                        if(keyDec==0)
                                                        {
                                                                delay(5);
                                                                if(keyDec==0)
                                                                        {        
                                                                                while(!keyDec)
                                                                                switch(setNum)
                                                                                        {
                                                                                                case 1:hh--;
                                                                                                                                if(hh<0)
                                                                                                                                                hh=0;
                                                                                                                                write_time(0,hh);
                                                                                                                                write_com(0x80+0x45);
                                                                                                                                break;
                                                                                                case 2:mm--;
                                                                                                                                if(mm<0)
                                                                                                                                                mm=0;
                                                                                                                                write_time(3,mm);
                                                                                                                                write_com(0x80+0x48);
                                                                                                                                break;
                                                                                                case 3:ss--;
                                                                                                                                if(ss<0)
                                                                                                                                                ss=0;
                                                                                                                                write_time(6,ss);
                                                                                                                                write_com(0x80+0x4b);
                                                                                                                                break;
                                                                                                default:   ;break;
                                                                                        }
                                                                        }
                                                        }
                                        }
                        }        
                }
}


//set timer 50ms interrupt once
void timer() interrupt 1
{
        TH0=0x4c; //{2^16-50000/[12*(1/11.0592)]}/256;
        TL0=0x00;//{2^16-50000/[12*(1/11.0592)]}%256;
        count++;
        if(count==20) //count to 20 seconds
                {
                        count=0;
                        ss++;
                        if(ss==60)
                                {
                                        ss=0;
                                        mm++;
                                        if(mm==60)
                                                {
                                                        mm=0;
                                                        hh++;
                                                        if(hh==24)
                                                                {
                                                                        hh=0;
                                                                }
                                                        write_time(0,hh);
                                                }
                                        write_time(3,mm);
                                }
                        write_time(6,ss);
                }
}

void init()
        {
                bitSeg=0;        //close the nixie tube
                bitPos=0;        //close the nixie tube
                TCON=0x31;        //set T/C interrupt                0011 0001
                IE=0x82;        //set interrupt control                1000 0010               
                TMOD=0x01; //set timer mode                0000 0001
                TH0=0x4c;
                TL0=0x00;
                hh=23;
                mm=59;
                ss=55;

                lcdE=0;
                write_com(0x01); //clear display
                write_com(0x38); //set display mode
                write_com(0x0c);        //set display on and cursor appear
                write_com(0x06); //increase address by one,shift cursor to the right

                write_com(0x80+0x01); //set the address of date
                for(num=0;num<14;num++) //initialization date
                        {
                                write_dat(date[num]);
                                delay(5);
                        }
                write_com(0x80+0x44); //set the address of time
                for(num=0;num<8;num++)        //initialise time
                        {
                                write_dat(time[num]);
                                delay(5);
                        }
        }

void main()
        {
                init();
                while(1)  //if there is no cycle, it will be initialized all the time
                {
                        key_scan();
                }
        }


回复

使用道具 举报

ID:111634 发表于 2017-3-26 22:30 | 显示全部楼层
本帖最后由 zl2168 于 2017-3-26 22:31 编辑

具有校正功能的时钟1302(LCD1602显示)


Proteus仿真,确认有效。
实例91 带校正时钟1302(LCD1602显示).rar (52.74 KB, 下载次数: 44)
回复

使用道具 举报

ID:151348 发表于 2017-3-27 10:03 | 显示全部楼层
要是有仿真文件就好了,只看看程序不好找问题
回复

使用道具 举报

ID:183600 发表于 2017-3-27 16:36 | 显示全部楼层
zl2168 发表于 2017-3-26 22:30
具有校正功能的时钟1302(LCD1602显示)

void key_scan()程序这样写有问题吗?
回复

使用道具 举报

ID:111634 发表于 2017-3-27 19:51 | 显示全部楼层
Seanwin 发表于 2017-3-27 16:36
void key_scan()程序这样写有问题吗?

先Proteus仿真,确认有效。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表