1602 c语言驱动程序,[共享]发一个LCD1602驱动程序(四位数据线)

我借鉴网上资料修改的LCD1602的C模块,支持8线和4线方式,可以在其他程序中直接调用,ICC的

LCD1602.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

#include

#include "AVR_Macro.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

#ifndef HIGH

# define HIGH    0x01

#endif

#ifndef LOW

# define LOW     0x00

#endif

#ifndef PI

# define PI      3.1415926535897932f

#endif

# define K_D2R   (PI/180.0)

# define K_R2D   (180.0/PI)

/*------------------------------------------------------------*/

/*

# 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_Data_Port           PORTB

# define LCD_Data_PIN       PINB

# define LCD_Data_DDR            DDRB

//程序中未使用,待修订

# define LCD_CTRL_DDR                DDRD

# define LCD_CTRL_Port      PORTD

# define LCD_RS_SET          (LCD_CTRL_Port|=BIT(3))

# define LCD_RS_CLS          (LCD_CTRL_Port&=(~BIT(3)))

# define LCD_RW_SET          (LCD_CTRL_Port|=BIT(4))

# define LCD_RW_CLS          (LCD_CTRL_Port&=(~BIT(4)))

# define LCD_E_SET           (LCD_CTRL_Port|=BIT(6))

# define LCD_E_CLS           (LCD_CTRL_Port&=(~BIT(6)))

#if _1602_MODE

//8线忙检测定义端口

# define LCD_BF             PINB&BIT(7)

#else

//4线忙检测定义端口

# define LCD_BF             PINB&BIT(3)

#endif

#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_CLS;LCD_RW_SET;

# define SetRead            LCD_SetReadData;LCD_RW_SET;

# define SetWrite           LCD_SetWriteData;LCD_RW_CLS;

# define SetCommand         LCD_RS_CLS;

# define SetData            LCD_RS_SET;

# 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);

//如果LCD背光不由软件控制可以取消下面的语句(初始化函数内的也要修改)

# 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

LCD1602.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] = {'0','1','2','3','4','5','6','7','8','9','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

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

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<>28]);

}

}

//--------------------------------------End---------------------------------------------------------

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值