27,381
社区成员
发帖
与我相关
我的任务
分享
// ************************************************
// *** LCD_4BIT Driver V1.1 ***
// *** LCD.C ***
// ************************************************
#define LCD_C
#include "includes.h"
// *** Set port for LCD Data Bus 8 bit mode *** //
#define LCD_WD_PORT PORTB //Bit0-3
#define LCD_RD_PORT PINB //Bit0-3
#define LCD_RW_PORT DDRB //Bit0-3
#define SET_LCD_IN DDRB&=0XF0
#define SET_LCD_OUT DDRB|=0X0F
/****************************************************************/
#define LCD_RS (PORTD_Bit5) //引脚定义
#define LCD_RW (PORTD_Bit6)
#define LCD_EN (PORTB_Bit4)
#define LCD_SET_EN() (LCD_EN=1) //置位与清零
#define LCD_SET_RS() (LCD_RS=1)
#define LCD_SET_RW() (LCD_RW=1)
#define LCD_CLEAR_EN() (LCD_EN=0)
#define LCD_CLEAR_RS() (LCD_RS=0)
#define LCD_CLEAR_RW() (LCD_RW=0)
// *******常量数据***********************
#define LCD_ON 0x0C
#define LCD_CURS_ON 0x0D
#define LCD_OFF 0x08
#define LCD_HOME 0x02
#define LCD_CLEAR 0x01
#define LCD_NEW_LINE 0xC0
#define LCD_FUNCTION_SET 0x28
#define LCD_MODE_SET 0x06
// ***********LED初始化**********************
void LCD_INIT(void)
{
SET_LCD_OUT; // LCD port output
LCD_WD_PORT = 0x03; // Load high-data to port
LCD_SET_EN(); // Write data to LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // Disable LCD
delay_us(40);
LCD_CLEAR_RW() ; // Set LCD to write
LCD_CLEAR_RS(); // Set LCD to command
LCD_SET_EN(); // Write data to LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // Disable LCD
delay_us(40);
LCD_SET_EN(); // Write data to LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // Disable LCD
delay_us(40);
LCD_WD_PORT = 0x02;
LCD_SET_EN(); // Write data to LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // Disable LCD
delay_us(40);
}
// *************************************** //
// 下面是LCD空闲测试程序 //
// *************************************** //
void LCD_Busy ( void )
{
SET_LCD_IN; //设置端口为输入
unsigned char tmp;
do //进入读LCD忙标志循环,直到LCD忙标志结束
{
LCD_SET_RW(); // 设置LCD为读模式
LCD_CLEAR_RS(); // 设置RS=0
LCD_SET_EN();
//读LCD忙标志(RS=0 RW=1 E=1时候,内部BF传送到D7,BF=0时LCD空闲)
delay_us(3); //延时等待数据
tmp=LCD_RD_PORT; //输入数据转TMP暂存.
LCD_CLEAR_EN(); //这里下面的有点看迷糊,反正外国人资料是这样写的
LCD_SET_EN(); //不是这样写函数就不能正常
asm("nop");
asm("nop");
LCD_CLEAR_EN();
} while(tmp & 0X08); //BF为0才退出循环等待//while(high & 0x80);
delay_us(20);
}
// ************************** //
// *** 写控制指令CMD到LCD *** //
// ************************** //
void LCD_WriteControl (unsigned char CMD)
{
LCD_Busy(); // 测试LCD空闲
SET_LCD_OUT; // LCD 端口定义为输出
LCD_WD_PORT=(CMD >>4); // 输出高四位数据到LCD数据端口
LCD_CLEAR_RW(); // 设置LCD为写模式
LCD_CLEAR_RS(); // 设置LCD为命令模式
LCD_SET_EN(); // 激活片选,写命令数据到LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // 关闭LCD片选
LCD_WD_PORT=(CMD&0x0f); // 输出低四位数据到LCD数据端口
LCD_CLEAR_RW(); // 设置LCD为写模式
LCD_CLEAR_RS(); // 设置LCD为命令模式
LCD_SET_EN(); // 激活片选,写命令数据到LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // 关闭LCD片选
}
// **************************** //
// ***八位数据分2次写入 LCD *** //
// **************************** //
void LCD_WriteData (unsigned char Data)
{
LCD_Busy(); // 测试LCD空闲
SET_LCD_OUT; // LCD 端口定义为输出
LCD_WD_PORT=(Data >>4); // 输出高四位数据到LCD数据端口
LCD_CLEAR_RW() ; // 设置LCD为写模式
LCD_SET_RS(); // 设置LCD为数据模式
LCD_SET_EN(); // 激活片选,写数据到LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // 关闭LCD片选
LCD_WD_PORT=(Data&0x0f); // 出低四位数据到LCD数据端口
LCD_CLEAR_RW() ; // 设置LCD为写模式
LCD_SET_RS(); // 设置LCD为数据模式
LCD_SET_EN(); // 激活片选,写数据到LCD
asm("nop");
asm("nop");
asm("nop");
asm("nop");
LCD_CLEAR_EN(); // 关闭LCD片选
}
// ********************************* //
// *** Initialize the LCD driver *** //
// ********************************* //
void Init_LCD(void)
{
LCD_INIT();
LCD_WriteControl (LCD_FUNCTION_SET);
LCD_WriteControl (LCD_OFF);
LCD_WriteControl (LCD_CLEAR);
LCD_WriteControl (LCD_MODE_SET);
LCD_WriteControl (LCD_ON);
LCD_WriteControl (LCD_HOME);
//LCD_WriteControl (0x90);
}
// ************************************************ //
// *** Clear the LCD screen (also homes cursor) *** //
// ************************************************ //
void LCD_Clear(void)
{
LCD_WriteControl(0x01);
}
// *********************************************** //
// *** Position the LCD cursor at row 1, col 1 *** //
// *********************************************** //
void LCD_Home(void)
{
LCD_WriteControl(0x02);
}
// ****************************************************************** //
// *** Display a single character, at the current cursor location *** //
// ****************************************************************** //
void LCD_DisplayCharacter (char Char)
{
LCD_WriteData (Char);
}
// ********************************************************************* //
// *** Display a string at the specified row and column, using FLASH *** //
// ********************************************************************* //
void LCD_DisplayString_F (char row, char column , unsigned char __flash *string)
{
LCD_Cursor (row, column);
while (*string)
{
LCD_DisplayCharacter (*string++);
}
}
// ******************************************************************* //
// *** Display a string at the specified row and column, using RAM *** //
// ******************************************************************* //
void LCD_DisplayString (char row, char column , unsigned char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_DisplayCharacter (*string++);
}
// *************************************************** //
// *** Position the LCD cursor at "row", "column". *** //
// *************************************************** //
void LCD_Cursor (char row, char column)
{
switch (row) {
case 1: LCD_WriteControl (0x80 + column - 1); break;
case 2: LCD_WriteControl (0xc0 + column - 1); break;
case 3: LCD_WriteControl (0x94 + column - 1); break;
case 4: LCD_WriteControl (0xd4 + column - 1); break;
default: break;
}
}
// ************************** //
// *** Turn the cursor on *** //
// ************************** //
void LCD_Cursor_On (void)
{
LCD_WriteControl (LCD_CURS_ON);
}
// *************************** //
// *** Turn the cursor off *** //
// *************************** //
void LCD_Cursor_Off (void)
{
LCD_WriteControl (LCD_ON);
}
// ******************** //
// *** Turn Off LCD *** //
// ******************** //
void LCD_Display_Off (void)
{
LCD_WriteControl(LCD_OFF);
}
// ******************* //
// *** Turn On LCD *** //
// ******************* //
void LCD_Display_On (void)
{
LCD_WriteControl(LCD_ON);
}