下面是程序,定义在程序里很明显,要注意的是背光是用三极管控制的,但是效果不是很好,反而不如直接接。
源代码包括三个文件: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
上一篇:利用AVR的输入捕捉进行红外解码
下一篇:IAR 下 AVR 的外部中断操作
推荐阅读最新更新时间:2024-03-16 15:13