码迷,mamicode.com
首页 > 其他好文 > 详细

基于51单片机的12864驱动

时间:2019-04-05 09:25:05      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:www   htm   51单片机   main   for   else   方式   nim   ret   

/**************dis_12864.h***************/

#include <reg52.h>


#ifndef __DIS_12864_H__
#define __DIS_12864_H__

#define uchar unsigned char
#define uint ?unsigned int

/*12864port定义*/
#define LCD_data ?P0 ? ? ? ? ? ? //数据口
sbit LCD_RS ?= ?P1^0; ? ? ? ? ? ?//寄存器选择输入?
sbit LCD_RW ?= ?P1^1; ? ? ? ? ? ?//液晶读/写控制
sbit LCD_EN ?= ?P2^5; ? ? ? ? ? ?//液晶使能控制

sbit LCD_PSB = ?P1^2; ? ? ? ? ? ?//串/并方式控制


/*函数声明*/
void delay(int ms);
void lcd_init();?
void beep();
void ?dataconv();
void lcd_pos(uchar X,uchar Y); ?//确定显示位置
void zifu_dis (uchar X,uchar Y,uchar *dis);

#endif




/**************dis_12864.c***************/

#include <dis_12864.h>

/*******************************************************************/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
// ?延时函数 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
/*******************************************************************/
void delay(int ms)
{
? ? while(ms--)
{
? ? ? uchar i;
?for(i=0;i<250;i++) ?
? {
? ?; ; ; ;
? }
}
}
/*******************************************************************/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
/*检查LCD忙状态 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*/
/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。

? ? ?*/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
/*******************************************************************/?
bit lcd_busy()
?{ ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? bit result;
? ? LCD_RS = 0;
? ? LCD_RW = 1;
? ? LCD_EN = 1;
? ? ?delay(1);
? ? result = (bit)(LCD_data&0x80);
? ? LCD_EN = 0;
? ? return(result);?
?}
/*******************************************************************/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
/*写指令数据到LCD ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*/
/*RS=L。RW=L,E=高脉冲,D0-D7=指令码。

? ? ? ? ? ? ? ? ? ? ? ? ? ? */ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{ ? ? ? ? ? ? ? ? ? ? ? ? ?
? ?while(lcd_busy());
? ? LCD_RS = 0;
? ? LCD_RW = 0;
? ? LCD_EN = 0;
? ? delay(1);
? ? LCD_data = cmd;
? ?delay(1);
? ? LCD_EN = 1;
? ?delay(1);
? ? LCD_EN = 0; ?
}
/*******************************************************************/
/*写显示数据到LCD ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*/
/*RS=H,RW=L。E=高脉冲,D0-D7=数据。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
/*******************************************************************/
void lcd_wdat(uchar dat)
{ ? ? ? ? ? ? ? ? ? ? ? ? ?
? ?while(lcd_busy());
? ? LCD_RS = 1;
? ? LCD_RW = 0;
? ? LCD_EN = 0;
? ? LCD_data = dat;
? ?delay(1);
? ? LCD_EN = 1;
? delay(1);
? ? LCD_EN = 0;?
}
/*******************************************************************/
/* ?LCD初始化设定 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*/
/*******************************************************************/
void lcd_init()
{?


? ? LCD_PSB = 1; ? ? ? ? //并口方式
? ??
? ? lcd_wcmd(0x34); ? ? ?//扩充指令操作
? ? delay(5);
? ? lcd_wcmd(0x30); ? ? ?//基本指令操作
? ? delay(5);
? ? lcd_wcmd(0x0C); ? ? ?//显示开。关光标
? ? delay(5);
? ? lcd_wcmd(0x01); ? ? ?//清除LCD的显示内容
? ? delay(5);
}


/*********************************************************/
/* 设定显示位置 ? ? ? X:行数 ? ? ? ? ? ? ? ?Y:列数 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
/*********************************************************/
void lcd_pos(uchar X,uchar Y)
{ ? ? ? ? ? ? ? ? ? ? ? ? ?
? ?uchar ?pos;
? ?if (X==0)
? ? ?{X=0x80;}
? ?else if (X==1)
? ? ?{X=0x90;}
? ?else if (X==2)
? ? ?{X=0x88;}
? ?else if (X==3)
? ? ?{X=0x98;}
? ?pos = X+Y ; ?
? ?lcd_wcmd(pos); ? ? //显示地址
}

/*********************************************************/
/* 在设定位置显示字符(串) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
/*********************************************************/
void zifu_dis (uchar X,uchar Y,uchar *dis)
{
? ? uchar i;
lcd_pos(X,Y); ?
? ? ?i = 0;
? ? while(dis[i] != ‘\0‘)
? ? ?{ ? ? ? ? ? ? ? ? ? ? ? ? //显示字符
? ? ? ?lcd_wdat(dis[i]);
? ? ? ?i++;
? ? ?}
}



/**************main.c****測试***********/

#include <reg52.h>
#include <intrins.h>
#include <dis_12864.h>

uchar code dis1[] = {"gz_go博客"};
uchar code dis2[] = {"www.csdn.net"};
uchar code dis3[] = {"不断学习 !"};
uchar code dis4[] = {"不断思考!

"};

void ?main(void)
?{
? ? ? ?delay(10); ? ? ? ? ? ? ? ? //延时
? ? ? ?lcd_init(); ? ? ? ? ? ? ? ?//初始化LCD ? ? ?
zifu_dis(0,0,"csdnCSDN");//字符串返回的的本质还是它在内存中的起始地址 ??
zifu_dis(1,0,dis2); ?
zifu_dis(2,0,dis3); ?
zifu_dis(3,0,dis4); ? ? ?
? ? ?while(1);
}


基于51单片机的12864驱动

标签:www   htm   51单片机   main   for   else   方式   nim   ret   

原文地址:https://www.cnblogs.com/ldxsuanfa/p/10657899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!