/************************************************************************/
/****************** 广州信先行电子科技有限公司 ****************/
/****************** WWW.XXXMCU.COM ****************/
/****************** ****************/
/****************** MCU:STC89C52RC ****************/
/***********************************************************************/
#include "AT89X51.h"
#include "intrins.h"
#define LCDPORT P2 //LCD数据线
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^2;
sbit KEY=P2^3;
//*************LCD1602驱动程序***************************/
void lcd_delay(unsigned char delaytime)
{
while(delaytime)
delaytime--;
}
/********1ms延时子程序***********/
void delay_nms(unsigned int n)
{
unsigned int i;
unsigned char j;
for(i=0;i
for(j=0;j<120;j++)
; //空操作
}
void write_lcd_command(unsigned char write_command)
{
lcd_delay(40);
RS=0;
RW=0;
LCDPORT=write_command;
//hc164(write_command);
EN=1;
EN=0;
}
void write_lcd_data(unsigned char write_data)
{
lcd_delay(40);
RS=1;
RW=0;
LCDPORT=write_data;
EN=1;
EN=0;
}
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat) //x表示行,y表示该行的位置(也就是列)
{
//LCD_set_xy( x, y );
switch(x)
{
case 0:
write_lcd_command(0x80+y);
break;
case 1:
write_lcd_command(0xc0+y);
break;
}
write_lcd_data(dat|0x30); //dat或上0x30,就时把数字0123456789转换成ASIIC码
}
void lcd_displaystr(unsigned char x,unsigned char y,unsigned char code *str)
{
//x表示行,y表示该行的位置(也就是列)
if(x)
write_lcd_command(0xc0+y);
else
write_lcd_command(0x80+y);
while (*str)
{
write_lcd_data(*str);
str++;
}
}
void initial_lcd1602(void) //初始化LCD1602
{
unsigned char i;
EN=0;
for(i=200;i>0;i--)
lcd_delay(248);
write_lcd_command(0x38);//设置lcd1602为每个字符5×7显示
for(i=4;i>0;i--)
lcd_delay(248);
write_lcd_command(0x38);
for(i=4;i>0;i--)
lcd_delay(248);
write_lcd_command(0x0f); //开显示,光标显示并闪烁
for(i=4;i>0;i--)
lcd_delay(248);
write_lcd_command(0x06);//光标移动设置,每写一个数,光标自动加一
for(i=4;i>0;i--)
lcd_delay(248);
write_lcd_command(0x01); //清屏
for(i=4;i>0;i--)
lcd_delay(248);
}
//********************lcd1602驱动程序到此完********************//
void main(void)
{
unsigned char mode, i;
initial_lcd1602();
//输出显示
lcd_displaystr(0,0x00," Welcome To here");
lcd_displaystr(1,0x00," www.xxxmcu.com ");
mode=0;
while(1)
{
//按键扫描
KEY=1;
if(KEY==0)
{
delay_nms(6);
if(KEY==0)
{
mode++;
if(mode>=4)
mode=0;
// write_lcd_command(0x01); //清屏
}
while(KEY==0);
switch(mode)
{
case 0x00:
lcd_displaystr(0,0x00," Study by MCS51 ");
lcd_displaystr(1,0x00," Come On Baby! ");
break;
case 0x01:
lcd_displaystr(0,0x00,"Welcome To Visit ");
lcd_displaystr(1,0x00," www.xxxmcu.com ");
break;
case 0x02:
lcd_displaystr(0,0x00,"Your Vest Regard");
lcd_displaystr(1,0x00," XXX Technology ");
break;
}
}
}
}