找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1057|回复: 2
收起左侧

stc89c52 lcd1602 秒表

[复制链接]
ID:812531 发表于 2021-1-21 15:44 | 显示全部楼层 |阅读模式
本帖最后由 朝朝1 于 2021-1-26 16:15 编辑

#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_data P0
sbit LCD_RS=P1^0;
sbit LCD_RW=P1^1;
sbit LCD_EN=P2^5;
sbit LCD_PSB=P1^3;
sbit dula=P2^0;
sbit wela=P2^1;
sbit cs88=P2^2;
sbit key=P3^4;

void delayms(unsigned int xms)
{
  unsigned i,j;
         for(i=xms;i>0;i--)
            for(j=110;j>0;j--);
}
void cmg88()
{
dula=0;
        P0=0x00;
        dula=0;
        cs88=0x00;
        P0=0x00;
}
#define delayNOP();{_nop_();_nop_();_nop_();_nop_();};
uchar IRDIS[2] ;
uchar IRCOM[4] ;
void delay0 (uchar x); //x*0.14MS
void beep();
void dataconv() ;
void lcd_pos(uchar X,uchar Y);
void delay(int ms)
{
while (ms--)
{
uchar i;
for (i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}}
bit lcd_busy()
{
bit result ;
LCD_RS=0;
LCD_RW=1;
LCD_EN=1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN=0;
return (result) ;
}
void lcd_wcmd(uchar cmd)
{
while(lcd_busy()) ;
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
_nop_();
_nop_();
P0=cmd;
delayNOP ()
LCD_EN=1;
delayNOP ()
LCD_EN=0;
}
void lcd_wdat(uchar dat)
{
while(lcd_busy()) ;
LCD_RS=1;
LCD_RW = 0;
LCD_EN=0;
P0=dat;
delayNOP () ;
LCD_EN=1;
delayNOP ();
LCD_EN=0;
}
void charter(unsigned char y,unsigned char x,unsigned char dat)
{

        if(y==1)
                lcd_wcmd(0x80|x);
        if(y==2)
        lcd_wcmd(0x90|x);
        if(y==3)
                lcd_wcmd((0x80|x)+8);
        if(y==4)
      lcd_wcmd((0x90|x)+8);
                               
lcd_wdat(dat);
}
void gotoxy(unsigned char y, unsigned char x,uchar *str)
{
        if(y==1)
                lcd_wcmd(0x80|x);
        if(y==2)
        lcd_wcmd(0x90|x);
        if(y==3)
                lcd_wcmd((0x80|x)+8);
        if(y==4)
      lcd_wcmd((0x90|x)+8);
                                  while(*str!='\0')
        {
                lcd_wdat(*str);
                str++;
        }
}
/*
void print(uchar *str)
{
        while(*str!='\0')
        {
                lcd_wdat(*str);
                str++;
        }
}
*/
void lcd_init()
{
LCD_PSB=1;
lcd_wcmd(0x34) ;
delay(5) ;
lcd_wcmd(0x30) ;
delay(5) ;
lcd_wcmd(0x0c) ;
delay(5) ;
lcd_wcmd(0x01) ;
delay(5) ;
}
main()
{
       
        unsigned char i;
lcd_init();

        while(1)
        {
                charter(1,0,'0'+3);
        //lcd_wcmd(0x80+7);       
        //lcd_wdat('0'+i);
        //delayms(5);
       
        //if(key==0)
        //{
                //delayms(5);
        //        if(key==0)
//        i=3;
               
        //        while(!key);
//        }
        gotoxy(3,0,"考研路上不孤单");
               
}       
}
void Read_Busy()
{
        uchar busy;
        P0 = 0xff;
        RS = 0;
        RW = 1;
        do
        {
                EN = 1;
                busy = P0;
                EN = 0;
        }while(busy & 0x80);
}
//?LCD1602??????
void Write_Cmd(uchar cmd)
{
        Read_Busy();//???
        RS = 0;
        RW = 0;
        P0 = cmd;
        EN = 1;
        EN = 0;
}
//???????
void Write_Dat(uchar dat)
{
        Read_Busy();
        RS = 1;
        RW = 0;
        P0 = dat;
        EN = 1;
        EN = 0;
}
void charter(unsigned char x,unsigned char y,unsigned char dat)
{
if(y==0)
{
Write_Cmd(0x80+x);
}
else
{
Write_Cmd(0xc0+x);

}
Write_Dat( dat);  
}
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{     
if (y == 0)
        {     
Write_Cmd(0x80 + x);     
        }
else
        {     
Write_Cmd(0xC0 + x);     
        }        
while (*s)
        {     
Write_Dat( *s);     
s ++;     
        }
}
void lcdinit()
{
   Write_Cmd(0x38);  
   Write_Cmd(0x08);    /*????*/
   Write_Cmd(0x01);    /*????*/
   Write_Cmd(0x06);    /*????????*/
   Write_Cmd(0x0C);    /*????????*/
}
void Timer0Init(void)                //50??@11.0592MHz
{
        TMOD = 0x01;                //???????
       TH0=0xdc;               //10ms??
   TL0=0x00;
                ET0=1;
        EA=1;
        TR0 = 0;                //???0????


}
void beep()
{
    unsigned char y;
    for (y=0;y<180;y++)
    {
      BEEP=!BEEP;                //BEEP??
          delayms(70);
    }
    BEEP=1;                      //?????
}

void main()
{
unsigned char num,num1,u;
lcdinit();
Timer0Init();
        LCD_Write_String(0,1,"time:");
        LCD_Write_String(5,0,"welcome");
        LCD_Write_String(5,1,"00:00:00:00");
while(1)
{

if(key==0)
{
        delayms(50);
if(key==0)
num=num+1;
while(!key);
}        

switch(num)
{
        case 1:TR0=1;
        LCD_Write_String(0,0,"begin count 1");
                delayms(5);
        break;
        case 2:TR0=0;
                LCD_Write_String(0,0,"pause count 2");
        delayms(5);
        break;
        case 3:TR0=1;
                LCD_Write_String(0,0,"begin count 3");
        delayms(5);
        break;
        case 4:TR0=0;
                LCD_Write_String(0,0,"pause count 4");
        delayms(5);
        break;                 
}
if(key1==0)
{
        
LCD_Write_String(5,1,"00:00:00:00");
}



}
}



void timer() interrupt 1
{
TH0=0xdc;            
   TL0=0x00;
  a++;
        charter(15,1,'0'+a);        
if(a==10)
{
        
        a=0;
        b++;
        x=b%10;
        
                charter(14,1,'0'+x);
}
if(b==10)
{
        b=0;
c++;
        y=c%10;
        charter(12,1,'0'+y);        
        
}
if(c==10)
{
c=0;

        d++;
        z=d%10;
        charter(11,1,'0'+z);               
}
if(d==6)
{
d=0;
                charter(11,1,'0'+0);        
        e++;
        m=e%10;
        charter(9,1,'0'+m);               
}
if(e==10)
{
e=0;
        f++;
        n=f%10;
                charter(8,1,'0'+n);               
}


}

回复

使用道具 举报

ID:876099 发表于 2021-2-1 02:08 | 显示全部楼层
没有原理图吗
回复

使用道具 举报

ID:812531 发表于 2021-2-2 10:37 | 显示全部楼层

没有  因为是在自己的板子上写的   不需要仿真  所以没有
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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