LCD1602的端口大多相同,这儿不多说,能够找找 长沙太阳人的datasheet。
下面是程序,界说在程序里很明显,要注意的是背光是用三极管操控的,可是作用不是很好,反而不如直接接。
源代码包含三个文件:LCD1602.C , main.c ,delay.h
/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
LCD1602.C
/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
#include
#include “delay.h”
/*设置管脚*/
#define RS PORTD_Bit3
#define RW PORTD_Bit4
#define E PORTD_Bit6
/*LCD1602操控指令*/
#define LCD_Set 0x38 // 显现初始化,16*2显现,5*7点阵,8位数据接口 ;
#define LCD_Clear 0x01 // 清屏LCD ;
#define LCD_Display1 0x0f // 显现功用设置:开显现,显现光标,光标闪耀 ;
#define LCD_Display2 0x0c // 显现功用设置:开显现,不显现光标,光标不闪耀 ;
#define LCD_Mode 0x06 // 设置光标状况默许0x06,为读一个字符光标加1 ;
#define LCD_FirstLineAddress_Left 0x80 //设置初始化数据指针,指向 左半屏榜首行首位 ;
#define LCD_SecondLineAddress_Left (0x80+0x40) //设置初始化数据指针,指向 左半屏第二行首位 ;
#define LCD_FirstLineAddress_Right (0x80+0x10) //设置初始化数据指针,指向 右半屏榜首行首位 ;
#define LCD_SecondLineAddress_Right (0x80+0x40+0x10) //设置初始化数据指针,指向 右半屏第二行首位 ;
#define LCD_Move_Left 0x18 // 整屏左移
#define LCD_Move_Right 0x1c // 整屏右移
/*端口初始化*/
void Port_Init()
{
DDRD_Bit3 = 1; //操控端 RS 界说为输出
DDRD_Bit4 = 1; //操控端 RW 界说为输出
DDRD_Bit6 = 1; //操控端 E 界说为输出
DDRB = 0xFF; //数据端界说为输出
DDRC_Bit7 = 1; //背光操控端
PORTC_Bit7 =0; //由三级管操控,低电平导通; 一直翻开背光
}
/*液晶1602 写操控指令函数*/
void Write_Command(unsigned char Command)
{
RS = 0 ; // 进行指令的操作
RW = 0 ; // 进行写操作
PORTB = Command ; // 赋值指令
E = 1 ; // 使能端拉高
delay_ms(5); // 等候指令写完
E = 0 ; // 使能端拉低
}
/*液晶1602 写数据函数*/
void Write_Data(unsigned char Data)
{
RS = 1 ; // 进行数据的操作
RW = 0 ; // 进行写操作
PORTB = Data ; // 赋值数据
E = 1 ; // 使能端拉高
delay_ms(5); // 等候数据写完
E = 0 ; // 使能端拉低
}
/*液晶1602 运用初始化*/
void LCD1602_Init()
{
E = 0;
Write_Command(LCD_Set);
Write_Command(LCD_Clear);
Write_Command(LCD_Display2);
Write_Command(LCD_Mode);
}
/*液晶1602 清屏*/
void LCD1602_Clear(void)
{
Write_Command(LCD_Clear);
}
/*液晶1602 左半屏显现*/
void LCD1602_Left_Display(unsigned char *table1 , unsigned char *table2)
{
unsigned char i;
Write_Command(LCD_FirstLineAddress_Left);delay_ms(1); //榜首行显现16个字符
for(i=0;i<16;i++)
{
Write_Data(table1[i]);
delay_ms(50);
}
Write_Command(LCD_SecondLineAddress_Left);delay_ms(1); //第二行显现16个字符
for(i=0;i<16;i++)
{
Write_Data(table2[i]);
delay_ms(1);
}
}
/*液晶1602 右半屏显现*/
void LCD1602_Right_Display(unsigned char *table1 , unsigned char *table2)
{
unsigned char i;
Write_Command(LCD_FirstLineAddress_Right); //榜首行显现16个字符delay_ms(1);
for(i=0;i<16;i++)
{
Write_Data(table1[i]);
delay_ms(50);
}
Write_Command(LCD_SecondLineAddress_Right); //第二行显现16个字符delay_ms(1);
for(i=0;i<16;i++)
{
Write_Data(table2[i]);
delay_ms(1);
}
}
/*液晶1602 左移*/
void LCD1602_TurnLeft()
{
unsigned char i;
for(i=0;i<16;i++)
{
Write_Command(LCD_Move_Left);
delay_ms(100);
}
}
/*液晶1602 右移*/
void LCD1602_TurnRight()
{
unsigned char i;
for(i=0;i<16;i++)
{
Write_Command(LCD_Move_Right);
delay_ms(100);
}
}
/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
main.c
/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
#include
#include “delay.h”
unsigned char dou1[]=” I LIKE AVR ! “;
unsigned char dou2[]=”Douzi&Douer AVR!”;
unsigned char dou3[]=” AVR to ARM ! “;
unsigned char dou4[]=” I Will Win ! “;
/*调用函数声明*/
void Port_Init();
void LCD1602_Init();
void LCD1602_Clear(void);
void LCD1602_Left_Display(unsigned char *table1 , unsigned char *table2);
void LCD1602_Right_Display(unsigned char *table1 , unsigned char *table2);
void LCD1602_TurnLeft();
void LCD1602_TurnRight();
/*主函数*/
void main(void)
{
Port_Init();
LCD1602_Init();
while(1)
{
LCD1602_Left_Display(dou1,dou2);
LCD1602_Right_Display(dou3,dou4);
LCD1602_TurnLeft();
delay_s(2);
LCD1602_TurnRight();
delay_s(2);
}
}
/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
delay.h
/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
#ifndef __IAR_DELAY_H
#define __IAR_DELAY_H
#include
#define XTAL 7.3728 //可界说为你所用的晶振频率(单位Mhz)
#define delay_us(x) __delay_cycles ( (unsigned long)(x * XTAL) )
#define delay_ms(x) __delay_cycles ( (unsigned long)(x * XTAL*1000) )
#define delay_s(x) __delay_cycles ( (unsigned long)(x * XTAL*1000000) )
#endif