微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 1602液晶万能驱动程序

1602液晶万能驱动程序

时间:11-29 来源:互联网 点击:
#define mcu_pic
//单片机种类选择  mcu_avr  mcu_51  mcu_pic mcu_msp430/******************************************/#define uchar unsigned char/**************************************************/#ifdef mcu_avr                 //单片机种类  avr#include "iom8535v.h"             //型号头文件#define PORT       PORTC         //端口寄存器#define PORT_OUT  (DDRC=0XFF)       //输出配置#define PORT_IN   (DDRC=0X00)       //输入配置#define PORT_INT  PINC              //端口状态#define rs(x)    (x?(PORTA|=0x01):(PORTA&=0xfe))#define rw(x)    (x?(PORTA|=0x02):(PORTA&=0xfd))#define e(x)     (x?(PORTA|=0x04):(PORTA&=0xfb))#define rs_rw_e_output (DDRA = 0X07)//输出配置#endif/**************************************************//**************************************************/#ifdef mcu_51               //单片机种类  51#include "REG51.h"              //型号头文件#define PORT       P3           //端口寄存器#define PORT_OUT  (P3=0X00)       //输出配置#define PORT_IN   (P3=0XFF)       //输入配置#define PORT_INT  P3              //端口状态#define rs(x)    (x?(P1|=0x20):(P1&=0xDF))#define rw(x)    (x?(P1|=0x40):(P1&=0xBF))#define e(x)     (x?(P1|=0x80):(P1&=0x7F))#define rs_rw_e_output (P1 = 0XE0)//输出配置//#define PORT P3//sbit rs=P2^0;//sbit rw=P2^1;//sbit e=P2^2;#endif/**************************************************//**************************************************/#ifdef mcu_pic                //单片机种类  pic#define _16F877                #include               //型号头文件#define PORT       PORTD        //端口寄存器#define PORT_OUT  (TRISD=0X00)       //输出配置#define PORT_IN   (TRISD=0XFF)       //输入配置#define PORT_INT   PORTD             //端口状态#define rs(x)  (x?(PORTC|=0X01):(PORTC&=0XFE))#define rw(x)  (x?(PORTC|=0X02):(PORTC&=0XFd))#define e(x)   (x?(PORTC|=0X04):(PORTC&=0XFb))#define rs_rw_e_output (TRISC &= 0XF8)//输出配置#endif#ifdef mcu_msp430              #include"msp430x21x1.h"#define PORT       P1OUT       #define PORT_OUT  (P1DIR=0XFF)     #define PORT_IN   (P1DIR=0X00)      #define PORT_INT   P1IN           #define rs(x)  (x?(P2OUT|=0X01):(P2OUT&=0XFE))#define rw(x)  (x?(P2OUT|=0X02):(P2OUT&=0XFd))#define e(x)   (x?(P2OUT|=0X04):(P2OUT&=0XFb))#define rs_rw_e_output (P2DIR = 0X07)#endif/**************************************************//**************************************00000  5*7  -> 0xff*****0** /5*8     0x40*****0**    0x40  ----> I*****0**    0x40  ---->*****0**    0x40*****0**    0x40***00000    0x1f添加自己的自定义字符***********************************/uchar ziku[]={0x04,0x05,0x06,0x07,0x13,0x16,0X15,0x0C,0xff,0xff,0x55,0xff,0x55,0xff,0xff,0xff};/*************************************函数声明区*************************************/uchar lcd_readcom(void);void  check_busy(void);void  lcd_writecom(uchar dat);uchar lcd_readdat(void);void  lcd_writedat(uchar dat);void lcd_gets(const uchar *dat);void  lcd_xy(uchar x,uchar y);void  lcd_init(void);/*******************************读取lcd1602状态  *******************************/uchar lcd_readcom(void){uchar temp;e(0);PORT_IN;rs(0);rw(1);e(1);temp=PORT_INT;e(0);PORT_OUT;return temp;}/***************************************检测 lcd 是否忙碌   阿飞天天忙绿***************************************/void check_busy(void){uchar temp;do{temp=lcd_readcom();}while((temp&0x80)==0x80);}				   /*****************************************向lcd里写命令*****************************************/void lcd_writecom(uchar dat){check_busy();e(0);rs(0);rw(0);e(1);PORT=dat;e(0);}/***************************************读取lcd对应地址数据*****************************************/uchar lcd_readdat(void){uchar temp;check_busy();e(0);PORT_IN;rs(1);rw(1);e(1);temp=PORT_INT;e(0);PORT_OUT;return temp;}/****************************************向lcd里写数据****************************************/void lcd_writedat(uchar dat){check_busy();e(0);rs(1);rw(0);e(1);PORT=dat;e(0);}/****************************************向lcd写字符串*****************************************/void lcd_gets(const uchar *dat){while(*dat!=0){lcd_writedat(*dat);dat++;}}/*****************************************确定要写的位子即x y 坐标******************************************/void lcd_xy(uchar x,uchar y){switch(y){case 0:lcd_writecom(0x80+x);break;case 1:lcd_writecom(0xc0+x);break;}}/****************************************************单行显示才有5*10  其他5*8 MODe(1) 5*8  MODe(0) 5*10****************************************************/void add_custom_word(uchar *dat,uchar len,uchar mode){uchar n,m;for(n=0;n				            

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top