#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<pic1687x.h> //型號頭文件
#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<len;n++)
{
if(mode)
{
lcd_writecom(0x40+8*n);
for(m=0;m<8;m++)
{
lcd_writedat(*dat);
dat++;
}
}
else
{
lcd_writecom(0x40+10*n);
for(m=0;m<10;m++)
{
lcd_writedat(*dat);
dat++;
}
}
}
}
/********************************************
初始化lcd
********************************************/
void lcd_init(void)
{
PORT_OUT;
rs_rw_e_output;
lcd_writecom(0x01) ;//清屏
lcd_writecom(0x03) ;
lcd_writecom(0x3c) ;
lcd_writecom(0x40) ;
lcd_writecom(0x0c) ;
add_custom_word(ziku,2,1); //初始化自定義字符
}
/**********************************************
主程序 實現顯示
***********************************************/
main()
{
const unsigned char love[]="I LOVE YOU";
const unsigned char time[]="afei time:12:00";
lcd_init();
lcd_xy(1,0);
lcd_gets(love);
lcd_xy(0,1);
lcd_gets(time);
lcd_xy(0,0);
lcd_writedat(0);
lcd_xy(15,0);
lcd_writedat(1);
while(1);
}