找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1485|回复: 0
打印 上一主题 下一主题
收起左侧

C51 LCD12864使用方法

[复制链接]
跳转到指定楼层
楼主
ID:366635 发表于 2018-7-11 13:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <STC12C5A60S2.H>
#include <math.h>
#define uint unsigned int
#define uchar unsigned char
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80
#define P0OUT P0
#define P1OUT P1
#define P2OUT P2
#define P3OUT P3
#define P4OUT P4
sbit LCD_EN=P2^7;   //使能
sbit LCD_RW=P2^6; //读写选择
sbit LCD_RS=P2^5;   //数据命令选择
uchar  bdata change;
uchar ch;
uchar code num[]={"0123456789ABCDEF"};
uchar key_t,key,t;
sbit change_0 = change^0;
sbit change_1 = change^1;
sbit change_2 = change^2;
sbit change_3 = change^3;
sbit change_4 = change^4;
sbit change_5 = change^5;
sbit change_6 = change^6;
sbit change_7 = change^7;
sbit P00 = P0^0;
sbit P01 = P0^1;
sbit P02 = P0^2;
sbit P03 = P0^3;
sbit P04 = P0^4;
sbit P05 = P0^5;
sbit P06 = P0^6;
sbit P07 = P0^7;
void changeIO(uchar data1)
{
change=data1;
P00 = change_7;
P01 = change_6;
P02 = change_5;
P03 = change_4;
    P04 = change_3;
P05 = change_2;
P06 = change_1;
P07 = change_0;
}
uchar inv(uchar data1)
{
uchar t;
change=data1;
data1=0;
data1 |= (BIT0&change_7);
t=BIT0&change_6;data1 |= t<<1;
t=BIT0&change_5;data1 |= t<<2;
t=BIT0&change_4;data1 |= t<<3;
    t=BIT0&change_3;data1 |= t<<4;
t=BIT0&change_2;data1 |= t<<5;
t=BIT0&change_1;data1 |= t<<6;
t=BIT0&change_0;data1 |= t<<7;
return data1;
}
void delayms(uint xms)  //1MS延时
{
uint i,j;
for(i=xms;i>0;i--)
  for(j=110;j>0;j--);
}

void LCD_write_com(uchar comm)   //写命令
{
LCD_RS=0;
LCD_RW=0;  
changeIO(comm);
delayms(5);
LCD_EN=0;
LCD_EN=0;
LCD_EN=1;
delayms(5);
}
void LCD_write_data(uchar lcd_data)  //写数据
{
LCD_RS=1;
LCD_RW=0;
changeIO(lcd_data);
delayms(5);
LCD_EN=0;
LCD_EN=0;
LCD_EN=1;
delayms(5);
}
uchar LCD_read_data()  //读数据
{
uchar t;
P0OUT =0xff;
LCD_RS=1;
LCD_RW=1;
delayms(5);
LCD_EN=0;
LCD_EN=0;
LCD_EN=1;
delayms(5);
t=P0OUT;
return inv(t);
}
void LCD_set_addr(uchar x,uchar y)    //x是横坐标(列),y是纵坐标(行), 行从1开始,列从1开始
{
x =(x+1)/2;
switch(y){
case 1:
  LCD_write_com(0x7F+x);
  break;
case 2:
  LCD_write_com(0x8F+x);
  break;
case 3:
  LCD_write_com(0x87+x);
  break;
case 4:
  LCD_write_com(0x97+x);
  break;
default:
  break;
}
}
void LCD_write_char(uchar x,uchar y,char lcd_data)
{
uchar t;
LCD_set_addr(x, y);
if((x%2)==0)
{
  t=LCD_read_data();
  LCD_set_addr(x, y);
  LCD_write_data(t);
}
LCD_write_data(lcd_data);
}
void LCD_write_string(uchar x,uchar y,char* str)
{
uchar LCD_temp;
LCD_set_addr(x,y);
if((x%2)==0)
{
  LCD_temp=LCD_read_data();
  LCD_set_addr(x, y);
  LCD_write_data(LCD_temp);
}
LCD_temp=*str;
while(LCD_temp!=0x00){
  LCD_write_data(LCD_temp);
  LCD_temp=*(++str);
}
}
void LCD_init()
{
delayms(500);
LCD_write_com(0x30);
LCD_write_com(0x0C);
LCD_write_com(0x01);
LCD_write_com(0x02);
LCD_write_com(0x80);
}
void LCD_clear()
{
LCD_write_com(0x01);
delayms(15);
}
void main()
{
LCD_init();
LCD_clear();
LCD_write_char(1,1,'a');
LCD_write_string(1,2,"12345789");
LCD_write_char(4,3,'b');
LCD_write_string(1,4,"12345789");
LCD_write_char(2,4,'b');
while(1);
}



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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