新聞中心

IAR For AVR -- LCD1602

作者: 時(shí)間:2016-12-03 來源:網(wǎng)絡(luò) 收藏
LCD1602的端口大多一樣,這里不多說,可以找找 長沙太陽人的datasheet。

下面是程序,定義在程序里很明顯,要注意的是背光是用三極管控制的,但是效果不是很好,反而不如直接接。

本文引用地址:http://butianyuan.cn/article/201612/325119.htm

源代碼包括三個(gè)文件:LCD1602.C , main.c ,delay.h

/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/

LCD1602.C

/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/

#include
#include "delay.h"

/*設(shè)置管腳*/
#define RS PORTD_Bit3
#define RW PORTD_Bit4
#define E PORTD_Bit6

/*LCD1602控制指令*/
#define LCD_Set 0x38 // 顯示初始化,16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)接口 ;
#define LCD_Clear 0x01 // 清屏LCD ;
#define LCD_Display1 0x0f // 顯示功能設(shè)置:開顯示,顯示光標(biāo),光標(biāo)閃爍 ;
#define LCD_Display2 0x0c // 顯示功能設(shè)置:開顯示,不顯示光標(biāo),光標(biāo)不閃爍 ;
#define LCD_Mode 0x06 // 設(shè)置光標(biāo)狀態(tài)默認(rèn)0x06,為讀一個(gè)字符光標(biāo)加1 ;

#define LCD_FirstLineAddress_Left 0x80 //設(shè)置初始化數(shù)據(jù)指針,指向 左半屏第一行首位 ;
#define LCD_SecondLineAddress_Left (0x80+0x40) //設(shè)置初始化數(shù)據(jù)指針,指向 左半屏第二行首位 ;

#define LCD_FirstLineAddress_Right (0x80+0x10) //設(shè)置初始化數(shù)據(jù)指針,指向 右半屏第一行首位 ;
#define LCD_SecondLineAddress_Right (0x80+0x40+0x10) //設(shè)置初始化數(shù)據(jù)指針,指向 右半屏第二行首位 ;

#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; //數(shù)據(jù)端定義為輸出

DDRC_Bit7 = 1; //背光控制端
PORTC_Bit7 =0; //由三級(jí)管控制,低電平導(dǎo)通; 始終打開背光
}

/*液晶1602 寫控制指令函數(shù)*/
void Write_Command(unsigned char Command)
{
RS = 0 ; // 進(jìn)行指令的操作
RW = 0 ; // 進(jìn)行寫操作
PORTB = Command ; // 賦值指令
E = 1 ; // 使能端拉高
delay_ms(5); // 等待指令寫完
E = 0 ; // 使能端拉低
}

/*液晶1602 寫數(shù)據(jù)函數(shù)*/
void Write_Data(unsigned char Data)
{
RS = 1 ; // 進(jìn)行數(shù)據(jù)的操作
RW = 0 ; // 進(jìn)行寫操作
PORTB = Data ; // 賦值數(shù)據(jù)
E = 1 ; // 使能端拉高
delay_ms(5); // 等待數(shù)據(jù)寫完
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個(gè)字符
for(i=0;i<16;i++)
{
Write_Data(table1[i]);
delay_ms(50);
}

Write_Command(LCD_SecondLineAddress_Left);delay_ms(1); //第二行顯示16個(gè)字符
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個(gè)字符delay_ms(1);
for(i=0;i<16;i++)
{
Write_Data(table1[i]);
delay_ms(50);
}

Write_Command(LCD_SecondLineAddress_Right); //第二行顯示16個(gè)字符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 ! ";

/*調(diào)用函數(shù)聲明*/
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();

/*主函數(shù)*/

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



關(guān)鍵詞: IARAVRLCD160

評(píng)論


技術(shù)專區(qū)

關(guān)閉