https://static.assets-stash.eet-china.com/album/old-resources/2008/10/28/270c3efc-7f15-41fa-922d-9a1cb3601bd8.rar
//LCD1602头文件
/*--------------------------------------------------------------*/
//File: LCD1602_8A.H
//Date: 08-8-9 //08-8-8 //08-8-27 //08-8-26
//Time: 20:10
//Modi: 08-8-10 9:51
//Note: LCD1602 The Best Driver for you!
// Welcome to use. Have a good day!
/*--------------------------------------------------------------*/
//防止被重复定义
#ifndef __LCD1602_8A_H__
#define __LCD1602_8A_H__
//所需头文件
#include <intrins.h>
/*--------------------------------------------------------------*/
//LCD1602与单片机接口定义
sfr LCD_IO = 0xA0; //P0-0x80,P1-0x90,P2-0xA0,P3-0xB0;
sbit LCD_RS = P3^2; //LCD数据/命令选择端(H/L)
sbit LCD_RW = P3^3; //LCD 读/写 选择端(H/L)
sbit LCD_EN = P3^4; //LCD使能控制端
sbit LCD_BZ = LCD_IO^7; //LCD忙标志位
/*--------------------------------------------------------------*/
//函数声明
void LCD_busy (void); //检测LCD是否忙
void LCD_init (void); //LCD初始化
void LCD_clr (void); //LCD清屏
void LCD_cmd (unsigned char cmd); //写入指令
void LCD_dat (unsigned char dat); //写入数据
void LCD_pos (unsigned char x, unsigned char y); //显示定位
void LCD_printc(unsigned char x, unsigned char y, unsigned char c); //定位输出字符
void LCD_prints(unsigned char x, unsigned char y, unsigned char *s); //定位输出字符串
/*--------------------------------------------------------------*/
//检测LCD忙状态
void LCD_busy(void)
{
do
{
LCD_EN = 0;
LCD_RS = 0; //指令
LCD_RW = 1; //读出
LCD_IO = 0xff;
LCD_EN = 1;
_nop_(); //高电平读出 延迟时间1us
}while(LCD_BZ); //LCD_BZ=1表示忙,LCD_BZ=0表示空闲
LCD_EN = 0;
}
/*--------------------------------------------------------------*/
//写入指令
void LCD_cmd(unsigned char cmd)
{
LCD_busy(); //检测忙
LCD_RS = 0; //指令
LCD_RW = 0; //写入
LCD_EN = 1;
LCD_IO = cmd; //传指令
LCD_EN = 0; //下降沿有效
}
/*--------------------------------------------------------------*/
//写入数据
void LCD_dat(unsigned char dat)
{
LCD_busy(); //检测忙
LCD_RS = 1; //数据
LCD_RW = 0; //写入
LCD_EN = 1;
LCD_IO = dat; //传数据
LCD_EN = 0; //下降沿有效
}
/*--------------------------------------------------------------*/
//显示定位
void LCD_pos(unsigned char x, unsigned char y)
{
if(y) LCD_cmd(x|0xc0);//y=1第二行显示,y=0第一行显示 0<=x<16
else LCD_cmd(x|0x80);//数据指针=80+地址码(00H~27H,40H~67H)
}
/*--------------------------------------------------------------*/
//LCD清屏
void LCD_clr(void)
{
LCD_cmd(0x01);
}
/*--------------------------------------------------------------*/
//定位输出字符
void LCD_printc(unsigned char x, unsigned char y, unsigned char c)
{
LCD_pos(x,y);
LCD_dat(c);
}
/*--------------------------------------------------------------*/
//定位输出字符串
void LCD_prints(unsigned char x, unsigned char y, unsigned char *s)
{
LCD_pos(x,y);
while(*s)
{
LCD_dat(*s);
s++;
}
}
/*--------------------------------------------------------------*/
//LCD初始化
void LCD_init(void)
{
LCD_cmd(0x38); //设置LCD为16X2显示,5X7点阵,八位数据接口
LCD_cmd(0x06); //LCD显示光标移动设置(光标地址指针加1,整屏显示不移动)
LCD_cmd(0x0c); //LCD开显示及光标设置(光标不闪烁,不显示"_")
LCD_cmd(0x01); //清屏
}
/*--------------------------------------------------------------*/
#endif
//主函数
#include <AT89X52.H>
#include "LCD1602_8A.H"
//延时500ms 函数定义
void delay500ms(void)
{
unsigned char i,j,k;
for(i=15;i>0;i--)
for(j=66;j>0;j--)
for(k=251;k>0;k--);
}
void main(void)
{
unsigned char code tab[]={" Hello! Welcome you to our shcool! Have a good time!"};
unsigned char *s=tab;
LCD_init();
while(1)
{
LCD_clr();
LCD_prints(5,0,"!*^ ^*!");
LCD_printc(8,0,'_');
LCD_prints(0,1,s++);
if(*s=='\0') s="tab";
delay500ms();
}
}
文章评论(0条评论)
登录后参与讨论