1602是一款简单实用的显示设备。
整个工程下载:http://download.csdn.net/detail/c1194758555/9596486
电路连接:
使用的头文件:
#ifndef _1602_H_
#define _1602_H_
#define SysDate P4
#define RS P55
#define RW P54
#define E P53
void init_1602(void); /*初始化显示屏*/
unsigned char buy(void); /*是否可以读写数据*/
void write_date(char date); /*写数据*/
void write_cmd(unsigned char cmd); /*写命令*/
void setxy(unsigned char x,unsigned char y); /*设置字体显示的位置*/
void write_string(unsigned char x,unsigned char y,char *s); /*显示字符串*/
#endif
1602的初始化,命令,数据的写入,显示。
#include <STC15W4K58S4.h>
#include "1602.h"
/*
***********************************************
* RS:命令数据选择端,低电平指令,高电平数据 *
* RW:读写选择,低电平写,高电平读 *
* E :使能信号,1禁止,0允许 *
***********************************************
*/
unsigned char buy(void) //检查数据高位是否为0,是否可读写
{
unsigned char temp;
SysDate = 0xff;
RS = 0;
RW = 1;
temp = SysDate;
E = 0;
E = 1;
if(temp & 0x80)
return 0;
return 1;
}
void write_cmd(unsigned char cmd) //写命令
{
while(!buy());
RS = 0;
RW = 0;
SysDate = cmd;
E = 0;
E = 1;
}
void write_date(char date) //写数据
{
while(!buy());
RS = 1;
RW = 0;
SysDate = date;
E = 0;
E = 1;
}
void setxy(unsigned char x,unsigned char y) //获得坐标
{
unsigned char ardd;
if(!y)
ardd = x;
else
ardd = 0x40 + x;
write_cmd(ardd | 0x80);
}
void init_1602(void) //初始化屏幕
{
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x06);
write_cmd(0x01);
}
void write_string(unsigned char x,unsigned char y,char *s) //显示数据
{
setxy(x,y);
while(*s)
{
write_date(*s++);
}
}
mian函数:
#include <STC15W4K58S4.h>
#include "1602.h"
int main()
{
P4M0 = 0x00;
P4M1 = 0x00;
init_1602();
while(1)
{
write_string(0,0,"hello word!");
}
}