📄 1602 four wire-cvavr.txt
字号:
PC7-PC4 接高四位数据线
VSS 接地 VDD接V+
PC 3接 E
PC2接 RS
R/W接 地
#include <mega128.h>
#include<delay.h>
#define LCD_EN_PORT PORTC
#define LCD_RW_PORT PORTC
#define LCD_RS_PORT PORTC
#define LCD_DATA_PORT PORTC
#define LCD_DATA_DDR DDRC
#define LCD_DATA_PIN PINC
#define LCD_EN 0x08 //portd7 out
#define LCD_RS 0x04 //portc6 out
#define LCD_DATA 0xff //porta4/5/6/7 out
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init (void);
void LCD_en_write (void);
void LCD_write_char (unsigned command,unsigned data);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y, char flash *s);
void LCD_init(void) //液晶初始化
{
delay_ms(5);
LCD_write_char(0x28,0); //4位显示
LCD_write_char(0x0c,0); //显示开
LCD_write_char(0x01,0); //清屏
}
void LCD_write_string(unsigned char X,unsigned char Y, char flash *s)
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_char( 0, *s );
s ++;
}
}
void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else
address = 0xc0 + x;
LCD_write_char( address, 0 );
}
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay_us(1);
LCD_EN_PORT&=~LCD_EN;
}
void LCD_write_char(unsigned command,unsigned data) // 写数据
{
unsigned command_temp,data_temp;
command_temp=command;
data_temp=data;
delay_us(16);
if(command==0)
{
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=data_temp&0xf0; //写高四位
LCD_en_write();
data_temp=data_temp<<4;
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=data_temp&0xf0; //写低四位
LCD_en_write();
}
else
{
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=command_temp&0xf0; //写高四位
LCD_en_write();
command_temp=command_temp<<4;
LCD_DATA_PORT&=0x0f;
LCD_DATA_PORT|=command_temp&0xf0; //写低四位
LCD_en_write();
}
}
void main(void)
{
DDRC|=LCD_DATA; // 数据为输出
DDRC|=LCD_RS|LCD_EN; //置位RS.EN
LCD_init();
while(1) //for循环
{
LCD_write_char(0x01,0);
delay_ms(1);
LCD_write_string(0,0,"Wellcome");
LCD_write_string(0,1,"OURAVR!");
delay_ms(150);
LCD_write_string(0,0,"happy new year!");
LCD_write_string(0,1,"tang:everfriend");
delay_ms(150);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -