登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] c51----- 1602屏幕左移

[复制链接]
发表于 2023-1-21 08:30:27 | 显示全部楼层 |阅读模式
学习笔记
学习科目: c51
学习安排: 学会屏幕移动
开始时间: 2023-01-21
结束时间: 2023-01-21
#include<STC12C5A60S2.h>
#define LCD1602_DB P0     
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P1^2;
void InitLcd1602();
void LcdshowStr(unsigned char x , unsignedchar y , unsigned char *str,unsigned char len);
void LcdSetCursor(unsigned char x ,unsigned char y);
void LcdWaitReady();
void LcdWritecmd(unsigned char cmd);
void LcdSetCursor(unsigned char x ,unsigned char y);
void delay(int ms);
bit flag500ms=0;
void ConfigTimer0(unsigned int ms);
       unsignedchar T0RH=0;
       unsignedchar T0RL=0;
       unsignedchar code str1[] = "wangyu";
       unsignedchar code str2[] = "karyl!";
void main()
{      
       unsignedchar i =0;
       unsignedchar index = 0 ;
       unsignedchar pdata bufMove1[16+sizeof(str1)+16];
       unsignedchar pdata bufMove2[16+sizeof(str2)+16];
       EA=1;
       ConfigTimer0(10);
       InitLcd1602();
       for(i=0;i<16;i++)
       {
              bufMove1='';
              bufMove2='';
       }
       for(i=0;i<sizeof(str1)-1;i++)
       {
              bufMove1[16+i]=str1;
              bufMove2[16+i]=str2;
       }
       for(i=(16+sizeof(str1)-1);i<sizeof(bufMove1);i++)
       {
              bufMove1='';
              bufMove2='';
       }
       while(1)
       {
            if(flag500ms)
               {
               flag500ms=0;
                      LcdshowStr(0,0,bufMove1+index,16);
                   LcdshowStr(0,1,bufMove2+index,16);
               index++;
                      if(index>=(16+sizeof(str1)-1))
                      {
                      index=0;                                                      
                      }
               }           
       }
}
void ConfigTimer0(unsigned int ms)
{
       unsignedlong tmp;      
       tmp=32000000/12;
       tmp=(tmp*ms)/1000;
       tmp=65536-tmp;
       tmp=tmp+12;
       T0RH=(unsignedchar)(tmp>>8);
       T0RL=(unsignedchar)tmp;
       TMOD=0x01;
       TH0=T0RH;
       TL0=T0RL;
       ET0=1;
       TR0=1;
}
void LcdWriteDat(unsigned char dat)
{
       LcdWaitReady();
       LCD1602_RS=1;
   LCD1602_RW=0;
       LCD1602_DB= dat;
       delay(1);
       LCD1602_E=1;
       delay(1);
       LCD1602_E=0;
}
void LcdWaitReady()  
{
    unsignedchar sta;
       LCD1602_RS=0;
   LCD1602_RW = 1;
       LCD1602_DB= 0xFF;
       do{
            LCD1602_E=1;
               sta=LCD1602_DB;
               LCD1602_E=0;     
        }while(sta&0x80);
}
void LcdWritecmd(unsigned char cmd)  
{
   LcdWaitReady();
       LCD1602_RS=0;
   LCD1602_RW=0;
       LCD1602_DB= cmd;
       delay(1);
       LCD1602_E=1;
       delay(1);
       LCD1602_E=0;
}
void LcdSetCursor(unsigned char x ,unsigned char y)
{
    unsigned char addr;
        if(y==0)
             addr=0x00 + x;
        else
                addr=0x40 + x ;
        LcdWritecmd(addr+0x80);
}
void LcdShowStr(unsigned char x, unsignedchar y ,unsigned char*str,unsigned char len)
{
   LcdSetCursor(x,y);
       while(len--)
       {
            LcdWriteDat(*str++);
       }
}
void InitLcd1602()  
{
   LcdWritecmd(0x38);
    LcdWritecmd(0x0c);
       LcdWritecmd(0x06);
       LcdWritecmd(0x01);
}
void delay(int ms)
{
       ms*=180;
       while(ms)
       {
              ms--;
       }
}
void InterruptTimer0() interrupt 1
{
static unsigned char tmr500ms=0;
       TH0=T0RH;
       TL0=T0RL;
       tmr500ms++;
       if(tmr500ms>=50)
       {
           tmr500ms=0;
              flag500ms=1;
      
       }
}
//代码之前写的 可能有错误 欢迎大佬批评指正
//祝各位新年快乐

好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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