发一个我写的1602模块,通过修改头文件标志可以兼容4线和8线,使用很方便。
**************1602.h******************
/***************************************************************
模块名称 : lcd1602a显示模块
处理器类型 : atmega16l
处理器时钟 : 4.000000 mhz
编译存储模式 : small
版本修订 : ver1.1
编 辑 : 胡云杰
日 期 : 2008年7月23日
说 明 :兼容1602lcd 4线和8线模式
================================================================
支持模块 :
================================================================
用法:
lcd_init();
lcd_write_string(列,行,"字符串");
lcd_write_char(列,行,'字符');
---------------------------------------------------------------
下面是8线方式avr与lcd连接信息
pd3 ->rs
pd6 ->en
pd4 ->rw
pb0 ->d0
pb1 ->d1
pb2 ->d2
pb3 ->d3
pb4 ->d4
pb5 ->d5
pb6 ->d6
pb7 ->d7
下面是4线方式avr与lcd连接信息
pd3 ->rs
pd6 ->en
pd4 ->rw
pb0 ->d0
pb1 ->d1
pb2 ->d2
pb3 ->d3
要使用本驱动,改变下面配置信息即可
--------------------------------------------------------------*/
#ifndef __1602_h
#define __1602_h
//头文件包含区
//--------------------------------------------------------------
#include <iom16v.h>
#include <macros.h>
//-------------------------end----------------------------------------------------------------------
//参数配置区
//--------------------------------------------------------------
//_1602_mode = 1为8线模式 _1602_mode = 0为4线模式
#define _1602_mode 0x01
/*---------------------*
* 常 数 宏 定 义 *
*---------------------*/
#ifndef true
# define true 0x01
#endif
#ifndef false
# define false 0x00
#endif
#ifndef enable
# define enable 0x01
#endif
#ifndef disable
# define disable 0x00
#endif
/*------------------------------------------------------------*/
# define lcd_write 0x00
# define lcd_read 0x01
# define lcd_command 0x00
# define lcd_data 0x01
#if _1602_mode
//8线初始化
# define lcd_cmd_init 0x38
#else
//4线初始化
# define lcd_cmd_init 0x28
#endif
# define lcd_cmd_dispctr 0x0c
# define lcd_cmd_cls 0x01
# define lcd_cmd_enterset 0x06
# define lcd_cmd_iconshow 0x0e
# define lcd_cmd_iconhide 0x0c
# define lcd_rs _pd3
# define lcd_rw _pd4
# define lcd_e _pd6
#if _1602_mode
//8线忙检测定义端口
# define lcd_bf read_pb7
#else
//4线忙检测定义端口
# define lcd_bf read_pb3
#endif
# define lcd_data_port portb
# define lcd_data_ddr ddrb
//程序中未使用,待修订
# define lcd_ctrl_ddr ddrd
#if _1602_mode
/*lcd 8线方式*/
# define lcd_setwritedata lcd_data_ddr |= 0xff;lcd_data_port &= 0x00;
# define lcd_setreaddata lcd_data_ddr &= 0x00;lcd_data_port |= 0xff;
# define lcd_senddata(a) lcd_data_port = a;
#else
# define lcd_setwritedata lcd_data_ddr |= 0x0f;lcd_data_port &= 0xf0;
# define lcd_setreaddata lcd_data_ddr &= 0xf0;lcd_data_port |= 0x0f;
# define lcd_sendhalfcharhigh(a) lcd_data_port &= 0xf0; lcd_data_port |= (a>>4);
# define lcd_sendhalfcharlow(a) lcd_data_port &= 0xf0; lcd_data_port |= (a<<4>>4);
#endif
/*---------------------*
* 动 作 宏 定 义 *
*---------------------*/
# define setreadstate lcd_setreaddata;lcd_rs = lcd_command;lcd_rw = lcd_read;
# define setread lcd_setreaddata;lcd_rw = lcd_read;
# define setwrite lcd_setwritedata;lcd_rw = lcd_write;
# define setcommand lcd_rs = lcd_command;
# define setdata lcd_rs = lcd_data;
# define print(a) lcddisplaystring(a);
# define locate(x,y) lcdsetxy(x-1,y-1);
# define cls lcdwaitforready();lcd_write_command(lcd_cmd_cls);
# define printn(a,b) lcddisplaynum((unsigned long)a,b);
# define showicon lcdwaitforready();lcd_write_command(lcd_cmd_iconshow);
# define hideicon lcdwaitforready();lcd_write_command(lcd_cmd_iconhide);
# define lcd_light ddrc |= 0x80;portc &= 0x7f;
# define lcd_dark ddrc |= 0x80;portc |= 0x80;
/***********************
* 全局变量声明区 *
***********************/
extern const char chr[16];
//-----------------------end---------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
全局函数声明
--------------------------------------------------------------------------------------------------*/
extern void lcd_init(void);
extern void lcdwaitforready(void);
extern void lcd_write_command(unsigned char command) ;
extern void lcd_write_data(unsigned char data);
extern void lcd_set_xy (unsigned char x, unsigned char y);
extern void lcd_write_string(unsigned char x,unsigned char y,unsigned char *s);
extern void lcd_write_char(unsigned char x,unsigned char y,unsigned char data);
extern void lcddisplaystring(unsigned char *string);
extern void lcddisplayword(unsigned char word);
extern void lcddisplaynum(unsigned long num,char bitcount);
//-----------------------end---------------------------------------------------------------
#endif
*********************************************
******************1602.c**********************
/***************************************************************
模块名称 : lcd1602a显示模块
处理器类型 : atmega16l
处理器时钟 : 4.000000 mhz
编译存储模式 : small
版本修订 : ver1.1
编 辑 : 胡云杰
日 期 : 2008年7月23日
说 明 :兼容1602lcd 4线和8线模式
================================================================
用法:
lcd_init();
lcd_write_string(列,行,"字符串");
lcd_write_char(列,行,'字符');
---------------------------------------------------------------
下面是8线方式avr与lcd连接信息
pd3 ->rs
pd6 ->en
pd4 ->rw
pb0 ->d0
pb1 ->d1
pb2 ->d2
pb3 ->d3
pb4 ->d4
pb5 ->d5
pb6 ->d6
pb7 ->d7
下面是4线方式avr与lcd连接信息
pd3 ->rs
pd6 ->en
pd4 ->rw
pb0 ->d0
pb1 ->d1
pb2 ->d2
pb3 ->d3
要使用本驱动,改变lcd1602.h配置信息即可
--------------------------------------------------------------*/
//头文件包含区
//--------------------------------------------------------------
#include "lcd1602.h"
//-------------------------end----------------------------------------------------------------------
/***********************
* 全局变量声明区 *
***********************/
const char chr[16] = {Ɔ',Ƈ',ƈ',Ɖ',Ɗ',Ƌ',ƌ',ƍ',Ǝ',Ə','a','b','c','d','e','f'};
//-------------------------end----------------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
局部函数声明
--------------------------------------------------------------------------------------------------*/
static void delay_1us(void); //1us延时函数
static void delay_nus(unsigned int n); //n us延时函数
static void delay_1ms(void); //1ms延时函数
static void delay_nms(unsigned int n); //n ms延时函数
//-----------------------end---------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
全局函数声明
--------------------------------------------------------------------------------------------------*/
extern void lcd_init(void);
extern void lcdwaitforready(void);
extern void lcd_write_command(unsigned char command) ;
extern void lcd_write_data(unsigned char data);
extern void lcd_set_xy (unsigned char x, unsigned char y);
extern void lcd_write_string(unsigned char x,unsigned char y,unsigned char *s);
extern void lcd_write_char(unsigned char x,unsigned char y,unsigned char data);
extern void lcddisplaystring(unsigned char *string);
extern void lcddisplayword(unsigned char word);
extern void lcddisplaynum(unsigned long num,char bitcount);
//-----------------------end---------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
局部函数
--------------------------------------------------------------------------------------------------*/
static void delay_1us(void) //1us延时函数
{
asm("nop");
}
static void delay_nus(unsigned int n) //n us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
static void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
static void delay_nms(unsigned int n) //n ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
//------------------------------end-----------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
全局函数
--------------------------------------------------------------------------------------------------*/
/********************************************************
* 函数说明:lcd初始化函数 *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcd_init(void) //液晶初始化
{
lcd_data_ddr = 0xff; //lcd端口初始化
lcd_data_port = 0x00;
lcd_ctrl_ddr = 0x78;
lcd_rs = 0x00;
lcd_rw = 0x00;
lcd_e = 0x00;
lcd_light //点亮lcd背光
delay_nms(10);
lcdwaitforready();
lcd_write_command(lcd_cmd_init);
lcdwaitforready();
lcd_write_command(lcd_cmd_dispctr);
lcdwaitforready();
lcd_write_command(lcd_cmd_cls);
delay_nms(10);
lcd_write_command(lcd_cmd_enterset);
}
/********************************************************
* 函数说明: *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcd_write_command(unsigned char command) //写指令
{
setwrite;
setcommand;
#if _1602_mode
lcd_e = enable; //8线方式
lcd_senddata(command);
lcd_e = disable;
#else
lcd_e = enable; //4线方式
lcd_sendhalfcharhigh(command);
lcd_e = disable;
lcd_e = enable;
lcd_sendhalfcharlow(command);
lcd_e = disable;
#endif
setread;
setcommand;
}
/********************************************************
* 函数说明: *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcd_write_data(unsigned char data) //写数据
{
setwrite;
setdata;
#if _1602_mode
lcd_e = enable; //8线方式
lcd_senddata(data);
lcd_e = disable;
#else
lcd_e = enable; //4线方式
lcd_sendhalfcharhigh(data);
lcd_e = disable;
lcd_e = enable;
lcd_sendhalfcharlow(data);
lcd_e = disable;
#endif
setread;
setcommand;
}
/********************************************************
* 函数说明:等待lcd空闲状态函数 *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcdwaitforready(void)
{
//delayus(30);
setread;
setcommand;
lcd_e = enable;
while (lcd_bf == enable); //rw=1,读pd7,为0表示空闲;
lcd_e = disable;
}
/********************************************************
* 函数说明: *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcd_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0)
{
address = 0x80 + x;
}
else
{
address = 0xc0 + x;
}
lcdwaitforready();
lcd_write_command(address);
}
/********************************************************
* 函数说明: *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcd_write_string(unsigned char x,unsigned char y,unsigned char *s) //列x=0~15,行y=0,1
{
lcd_set_xy( x, y ); //写地址
while(*s)
{
lcdwaitforready();
lcd_write_data(*s);
s++;
}
}
/********************************************************
* 函数说明: *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcd_write_char(unsigned char x,unsigned char y,unsigned char data) //列x=0~15,行y=0,1
{
lcd_set_xy( x, y ); //写地址
lcdwaitforready();
lcd_write_data(data);
}
/********************************************************
* 函数说明:lcd字符串显示函数 *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcddisplaystring(unsigned char *string)
{
while(*string)
{
lcdwaitforready();
lcd_write_data(*string);
string++;
}
}
/********************************************************
* 函数说明:lcd字符显示函数 *
* 入口参数: *
* 返回参数: *
********************************************************/
void lcddisplayword(unsigned char word)
{
lcdwaitforready();
lcd_write_data(word);
}
/********************************************************
* 函数说明:数值显示函数(hex 16进制显示) *
* 入口参数:需要显示的数字(无符号长整形) *
* 返回参数: *
********************************************************/
void lcddisplaynum(unsigned long num,char bitcount)
{
char a = 0;
for (a = 8-bitcount ;a<8;a++)
{
lcd_write_data(chr[(num<<(a<<2))>>28]);
}
}
//--------------------------------------end---------------------------------------------------------