📄 lcd._c
字号:
//编译器:ICC-AVR
//日期: 2007-4-13 20:29:57
//目标芯片 : ATmega16
//时钟: 8.0000Mhz
//版本:VISION 2.0
/*
LCD引脚定义
1---GND
2---VCC
3---VO
4---RS
5---RW
6---EN
7到14--D0-D7
15--背景灯+
16--背景灯-
下面是AVR与LCD连接信息
PA1 ->RS
PA0 ->EN
GND ->RW
PA4 ->D4
PA5 ->D5
PA6 ->D6
PA7 ->D7
*/
//#include <iom16v.h>
//#include <macros.h>
#include "main.h"
#define uchar unsigned char
#define uint unsigned int
#define LCD_EN_PORT PORTA //数据寄存器,以下2个要设为同一个口
#define LCD_EN_DDR DDRA //数据方向寄存器
#define LCD_RS_PORT PORTA //以下2个要设为同一个口
#define LCD_RS_DDR DDRA
#define LCD_DATA_PORT PORTA //以下3个要设为同一个口
#define LCD_DATA_DDR DDRA //一定要用高4位
#define LCD_DATA_PIN PINA //端口输入引脚
#define LCD_RS (1<<PA1) //0x04 portA1 out
#define LCD_EN (1<<PA0) //0x08 portA0 out
#define LCD_DATA ((1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7)) //0xf0 portA4/5/6/7 out
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(uchar command);
void LCD_write_data(uchar data);
void LCD_set_xy (uchar x, uchar y);
void delay(uint n) //N ms延时函数
{
uint i;
while(n--)
for (i=0;i<125;i++);
}
void LCD_init(void) //液晶初始化
{
LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出
LCD_EN_DDR|=LCD_EN; //设置EN方向为输出
LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
LCD_write_command(0x28);
LCD_en_write();
delay(40);
LCD_write_command(0x28); //4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delay(2);
}
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay(1);
LCD_EN_PORT&=~LCD_EN;
}
void LCD_write_command(uchar command) //写指令
{
delay(16);
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写高四位
LCD_en_write();
command=command<<4; //低四位移到高四位
LCD_DATA_PORT&=0x0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写低四位
LCD_en_write();
}
void LCD_write_data(uchar data) //写数据
{
delay(16);
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data&0xf0; //写高四位
LCD_en_write();
data=data<<4; //低四位移到高四位
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data&0xf0; //写低四位
LCD_en_write();
}
void LCD_set_xy( uchar x, uchar y ) //写地址函数
{
uchar address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command( address);
}
void LCD_write_string(uchar X,uchar Y,uchar *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
void LCD_write_char(uchar X,uchar Y,uchar data) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
LCD_write_data( data);
}
void init_devices(void)
{
CLI(); //disable all interrupts
LCD_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; // r interrupt sources
SEI(); //re-enable interrupts
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -