《51单片机LCD12864驱动程序C语言ST7920》由会员分享,可在线阅读,更多相关《51单片机LCD12864驱动程序C语言ST7920(6页珍藏版)》请在金锄头文库上搜索。
1、51单机LCD12864驱动程序C语ST7920main.c#include reg52.h#include#include#include#include#include LCD12864.hvoid main(void)Ini_Lcd();Lcd_WriteStr(0,0,QQ137712826);while(1)lcd12864.h#include#include sbit RS=P2 0;sbit RW=P2 1;sbit EN=P2 2;sbit EN=P2 2;sbit PSB=P2 3;sbit RST=P2 4;#define _NOP()_nop_()/*12864液晶控制管
2、脚*/#define RS_CLR RS=0/*RS置低*/#define RS_SET RS=1/*RS置*/#define RW_CLR RW=0/*RW置低*/#define RW_SET RW=1/*RW置*/#define EN_CLR EN=0/*E置低*/#define EN_SET EN=1/*E置*/#define PSB_CLR PSB=0/*PSB置低,串式*/#define PSB_SET PSB=1/*PSB置,并式*/#define RST_CLR RST=0/*RST置低,会复位*/#define RST_SET RST=1/*RST置*/*12864液晶的数据*
3、/#define DataPort P0/*数据*/#define LcdData P0void Delay_1ms(void);void Delay_Nms(unsigned int n);void LCD_write_com(unsigned char cmd);void LCD_write_data(unsigned char dat);void Ini_Lcd(void);void Lcd_WriteStr(unsigned char x,unsigned char y,unsigned char*Str);unsigned char Lcd_CheckBusy(void);unsig
4、ned char Lcd_ReadData(void);void delay_us(unsigned int t)/*11.0592MHz*/while(t-)_nop_();_nop_();_nop_();void delay_ms(unsigned int t)/*11.0592MHz*/unsigned char i,j;while(t-)_nop_();_nop_();_nop_();i=11;j=190;dowhile(-j);while(-i);void Delay_1ms(void)delay_ms(1);/*显屏命令写函数*/void LCD_write_com(unsigne
5、d char cmd)while(Lcd_CheckBusy();RS_CLR;RW_CLR;EN_SET;DataPort=cmd;EN_CLR;/*显屏数据写函数*/void LCD_write_data(unsigned char dat)while(Lcd_CheckBusy();RS_SET;RW_CLR;EN_SET;DataPort=dat;/*delay_us(1);*/EN_CLR;/*初始化IO程序*/void Port_init_12864(void)delay_ms(50);delay_ms(50);PSB_SET;/*液晶并式*/RST_CLR;delay_ms(10
6、0);RST_SET;/*函数名称:Ini_Lcd*功能:初始化液晶模块*参数:*返回值:*/void Ini_Lcd(void)Port_init_12864();/*液晶控制端设置为输出*/LCD_write_com(0 x30);/*基本指令集*/Delay_1ms();LCD_write_com(0 x02);/*地址归位*/Delay_1ms();LCD_write_com(0 x0c);/*整体显打开,游标关闭*/Delay_1ms();LCD_write_com(0 x01);/*清除显*/Delay_1ms();LCD_write_com(0 x06);/*游标右移*/Dela
7、y_1ms();LCD_write_com(0 x80);/*设定显的起始地址*/*显屏制定起始位置写字符串*/void Lcd_WriteStr(unsigned char x,unsigned char y,unsigned char*Str)if(y 3)|(x 7)return;/*超出范围直接不写*/if(y=0)LCD_write_com(0 x80+x);/*第显*/if(y=1)LCD_write_com(0 x90+x);/*第显*/if(y=2)LCD_write_com(0 x88+x);/*第三显*/if(y=3)LCD_write_com(0 x98+x);/*第四显
8、*/delay_us(1);while(*Str 0)LCD_write_data(*Str);Str+;delay_us(1);unsigned char Lcd_CheckBusy(void)unsigned char Busy;RS_CLR;RW_SET;EN_SET;delay_us(5);Busy=LcdData&0 x80;EN_CLR;return(Busy);/*从LCD中读出数据*/unsigned char Lcd_ReadData(void)unsigned char Temp;while(Lcd_CheckBusy();P0=0XFF;RS_SET;RW_SET;EN_SET;delay_us(10);Temp=LcdData;EN_CLR;return(Temp);