|
4#
楼主 |
发表于 2012-7-11 20:02:32
|
只看该作者
本帖最后由 joyrus 于 2012-7-11 20:56 编辑
函数库如下:
/******************************************************
* 本函数库为LCD1602驱动函数
* 本函数库在HITECH C环境下可以直接使用
* 如果在其他C编译器环境下使用需要根据具体的编译器做调整
* 本人联系方法:沈毅;joyrus@163.net,www.joyrus.com,QQ:15953321
*
*******************************************************/
#include <pic.h>
#include <htc.h>
#ifndef _XTAL_FREQ
//本函数库使用了HITECH C提供的标准函数__delay_us/ms需要设定下面的晶振选项
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif
#define LCDCON_EN RB5 //PORTB5为LCD使能控制
#define LCDCON_RW RB4 //PORTB4为LCD读写控制
#define LCDCON_RS RB3 //PORTB3为LCD指令/数据选择控制
#define LCDPORT PORTD //PORTD为LCD的8位总线LCDPORT
#define LCDPORT_READ TRISD=0xFF //TRISD为LCD的8位总线方向控制LCDTRIS
#define LCDPORT_WRITE TRISD=0x00
#define LCDCONPORT_TRIS TRISB //TRISB为LCD的3位控制线方向控制LCDCONTRIS
//#define LCDP0 RD0
//#define LCDP1 RD1
//#define LCDP2 RD2
//#define LCDP3 RD3
//#define LCDP4 RD4
//#define LCDP5 RD5
//#define LCDP6 RD6
//#define LCDP7 RD7
#define WBF 1 //定义符号1
#define LCD_READ 1
#define LCD_DATA 1
#define NBF 0 //定义符号0
#define LCD_WRITE 0
#define LCD_COMD 0
//本函数库使用了HITECH C提供的标准函数__delay_us/ms
//也可以使用下面自定义的延时函数
void us_delay(unsigned char time); //微秒延时
void ms_delay(unsigned char time); //毫秒延时
//读LCD数据,返回BF标志,和内存指针
unsigned char get_lcd(void);
//LCD初始化
void init_lcd(void);
//发送数据d/地址a/指令c到LCD
//DorC选择数据或者指令
//BF是否检测LCD忙
//不检测LCD忙仅在初始化LCD时使用!
//本函数一次写入一个字符!
void put_lcd(unsigned char ADC, unsigned char DorC, unsigned char BF);
//向LCD发送字符串,真正要显示的数据
//x列,y行字符坐标,*strpoint字符串数组地址
//本函数一次写入一串字符!
void put_lcd_string(unsigned char x, unsigned char y, unsigned char *strpoint);
#define delay_8us us_delay(1) //延时8us
#define delay_60us us_delay(8) //延时8us
#define delay_2ms ms_delay(2) //延时2ms
#define lcd_busy (get_lcd() & 0x80) //读并传递LCD忙BF标志
//利用宏和本函数库定义需要的各种LCD指令
#define set_lcd_init (put_lcd(0x38, LCD_COMD, WBF)) //lCD外部使用初始化
#define set_lcd_off (put_lcd(0x08, LCD_COMD, WBF)) //LCD显示关闭
#define set_lcd_clear (put_lcd(0x01, LCD_COMD, WBF)) //LCD清屏
#define set_lcd_cursor (put_lcd(0x06, LCD_COMD, WBF)) //LCD光标显示设定
#define set_lcd_on (put_lcd(0x0C, LCD_COMD, WBF)) //LCD显示打开
#define set_lcd_position (put_lcd(0x80, LCD_COMD, WBF)) //LCD首字符定位
//#define lcd_init ((put_lcd(0x38, LCD_COMD, NBF)) //LCD初始化函数内部使用
//#define set_lcd_curmove //LCD光标画面位移方式设定
///////////////函数///////////////////
void put_lcd_string(unsigned char x, unsigned char y, unsigned char *strpoint)
{
unsigned char position;
if(y)
position=0xC0+x; //第二行
else
position=0x80+x; //第一行
put_lcd(position, LCD_COMD, WBF); //写入首字符定位指令
//逐个写字符串
while (*strpoint)
{
put_lcd(*strpoint, LCD_DATA, WBF);
strpoint++; //指向字符串中的下一字符
}
}
unsigned char get_lcd(void)
{
LCDCON_RS=LCD_COMD;
LCDCON_RW=LCD_READ;
LCDCON_EN=LCD_WRITE;
__delay_us(8); //产生写到读的脉冲
//delay_8us;
LCDCON_EN=LCD_READ;
LCDPORT_READ;
return LCDPORT;
}
void put_lcd(unsigned char ADC, unsigned char DorC, unsigned char BF)
{
if(BF){while(lcd_busy);} //等待LCD空闲
LCDPORT_WRITE;
LCDPORT=ADC;
LCDCON_RS=DorC;
LCDCON_RW=LCD_WRITE;
LCDCON_EN=LCD_READ; //产生从读到写的脉冲
__delay_us(60);
//delay_60us;
LCDCON_EN=LCD_WRITE;
}
void init_lcd(void)
{
LCDPORT_WRITE;
LCDPORT=0x00;
//lcd_init;
put_lcd(0x38, LCD_COMD, NBF); //初始化指令3次,不检测BF忙标志
__delay_ms(2);
//delay_2ms;
//lcd_init;
put_lcd(0x38, LCD_COMD, NBF);
__delay_ms(2);
//delay_2ms;
//lcd_init;
put_lcd(0x38, LCD_COMD, NBF);
__delay_ms(2);
//delay_2ms;
set_lcd_init;
set_lcd_off;
set_lcd_clear;
set_lcd_cursor;
set_lcd_on;
set_lcd_position;
}
void us_delay(unsigned char time)
{
while(time)
{
time--;
}
}
void ms_delay(unsigned char time)
{
unsigned char i=100;
unsigned char j=200;
while(time)
{
time--;
while(i)
{
i--;
while(j){j--;}
}
}
} |
|