20x4字符液晶的函数
扫描二维码
随时随地手机看文章
使用芯片ATmega8515l,,8M晶振,,在1602的基础上修改过来的
#include
#define lcd_data_port PORTA
#define lcd_data_ddr DDRA
#define lcd_busy_pin PINA
#define lcd_busy_ddr DDRA
#define lcd_control_port PORTC
#define lcd_control_ddr DDRC
#define lcd_RS 0x01
//PORTC0
#define lcd_RW 0x02
//PORTC1
#define lcd_EN 0x04
//PORTC2
#define busy 0x80
//LCD_DB7
/*----------------------------------函数声明----------------------------------*/
//IO口相关
void io_init(void);
//LCD相关
void lcd_init(void);
void lcd_write_command(unsigned char command,unsigned char wait_en);
void lcd_write_data(unsigned char char_data);
void wait_enable(void);
void display_a_char(unsigned char position,unsigned char char_data);
void display_a_string(unsigned char position,unsigned char *ptr);
void display_a_number(unsigned char position,int i);
int ntentimes(unsigned char n);
//全局变量
unsigned char str0[]="Hello,word!";
void main(void)
{
unsigned int i=518;
io_init();
lcd_init();
display_a_char(0,'a');
display_a_string(20,str0);
display_a_number(40,i);
}
//液晶初始化
void lcd_init(void)
{
delay_nms(30);
lcd_data_port=0xff;
lcd_data_ddr=0xff;
lcd_control_port=0x00;
lcd_control_ddr=0x07;
lcd_write_command(0x38,0);//显示模式设置三次(此时不管lcd空闲与否)
delay_nms(1);
lcd_write_command(0x38,0);
delay_nms(1);
lcd_write_command(0x38,0);
delay_nms(1);
lcd_write_command(0x38,1);//显示模式设置(从此之后均需lcd空闲)
lcd_write_command(0x08,1);//显示关闭
lcd_write_command(0x01,1);//显示清屏
lcd_write_command(0x06,1);//显示光标移动设置
lcd_write_command(0x0c,1);//显示开及光标设置
delay_nus(30);
lcd_write_command(0x01,1);//显示清屏
}
//写指令函数: E=高脉冲 RS=0 RW=0//command为指令,wait_en指定是否要检测LCD忙信号
void lcd_write_command(unsigned char command,unsigned char wait_en)
{
if(wait_en)wait_enable();//若wait_en为1,则要检测LCD忙信号,等待其空闲
lcd_control_port&=~lcd_RS;//RS=0
lcd_control_port&=~lcd_RW;//RW=0
lcd_control_port&=~lcd_EN;//E=0,下面给LCD一个高脉冲
lcd_data_ddr=0xff;
lcd_data_port=command;
lcd_control_port|=lcd_EN;//E=1
lcd_data_port=command;
lcd_control_port&=~lcd_EN;//重设E=0
}
//写数据函数: E =高脉冲 RS=1 RW=0
void lcd_write_data(unsigned char char_data)
{
wait_enable();//等待LCD空闲
lcd_control_port|=lcd_RS;//RS=1数据
lcd_control_port&=~lcd_RW;//RW=0写
lcd_control_port&=~lcd_EN;//E=0,下面给LCD一个高脉冲
lcd_data_ddr=0xff;
lcd_data_port=char_data;
lcd_control_port|=lcd_EN;//E=1
lcd_data_port=char_data;
lcd_control_port&=~lcd_EN;//重设E=0
}
//正常读写操作之前必须检测LCD控制器状态:E=1 RS=0 RW=1;DB7: 0 空闲,1 忙。
//检测忙信号,等待LCD空闲函数
void wait_enable(void)
{
lcd_busy_ddr&=~busy;//设置busy口为输入
lcd_control_port&=~lcd_RS;//RS=0
lcd_control_port|=lcd_RW;//RW=1
lcd_control_port&=~lcd_EN;//E=0,下面给LCD一个高脉冲
NOP();
lcd_control_port|=lcd_EN;//E=1
while(lcd_busy_pin&busy);//等待LCD_DB7为0
lcd_control_port&=~lcd_EN;//重设E=0
lcd_busy_ddr|=busy;//设置busy口为输出
}
void display_a_char(unsigned char position,unsigned char char_data)//参数position指定位置0~79,char_data为要显示的字符
{
unsigned char position_tem;
if(position<20) //第一行时,地址为0x80到0x93,输入的数值加上0x80, 就是当前的地址值;
position_tem=position+0x80;
else if(position<40)//第二行的地址为0xc0到0xd3,当为第二行时position>20,此时地址值应为position+0xc0-0x20即position+0xac;
position_tem=position+0xac;
else if(position<60)//第三行的地址为0x94到0xa7,当为第三行时position>40,此时地址值应为position+0x94-0x40即position+0x6c;
position_tem=position+0x6c;
else//第四行的地址为0xd4到0xe7,当为第三行时position>60,此时地址值应为position+0xd4-0x60即position+0x98;
position_tem=position+0x98;
lcd_write_command(position_tem,1);
lcd_write_data(char_data);
}
//指定位置显示任意大小字符串
void display_a_string(unsigned char position,unsigned char *ptr)
{
unsigned char position_tem,i=0,i_tem=0;
lcd_data_port=0xff;
lcd_data_ddr=0xff;
lcd_control_port=0x07;
lcd_control_ddr=0x07;
for(i=0;*(ptr+i)!=0;i++)
i_tem++;
position_tem=position;
for(i=0;i
}
//指定位置显示一个整数
void display_a_number(unsigned char position,int num)
{
int j;
unsigned char i,x;
if(num>=10000)i=5;
else if(num<10000&&num>=1000)i=4;
else if(num<1000&&num>=100)i=3;
else if(num<100&&num>=10)i=2;
else if(num<10&&num>=0)i=1;
else if(num<=-10000)i=15;
else if(num>-10000&&num<=-1000)i=14;
else if(num>-1000&&num<=-100)i=13;
else if(num>-100&&num<=-10)i=12;
else i=11;
if(i<10)
{for(;i>0;i--)
{
j=ntentimes(i-1);
x=num/j;
display_a_char(position,x+0x30);
num=num%j;
position++;
}
}
else
{
i-=10;
display_a_char(position,'-');
position++;
for(;i>0;i--)
{
j=ntentimes(i);
x=num/j;
display_a_char(position,x+0x30);
num=num%j;
position++;
}
}
}
//n的10次方转换
int ntentimes(unsigned char n)
{int m=1;
for(;n>0;n--)
{m*=10;}
return(m);
}
/*------------------------------------延时函数--------------------------------*/
void delay_ns(unsigned int n)
{
unsigned int i,j;
for(i=0;i
delay_1ms();
}
}
//n ms延时函数
void delay_nms(unsigned int n)
{
unsigned int i;
n-=n/1000;
for(;n!=0;n--){for(i=1328;i!=0;i--) ; }
}
//1ms延时函数
void delay_1ms(void)
{
unsigned int i;
for(i=1328;i!=0;i--);
}
//n us延时函数
void delay_nus(int n)
{while(n--);}
/*-----------------------------------端口初始化-------------------------------*/
void io_init(void)
{DDRA=0xff;
PORTA=0x00;
DDRB=0x00;
PORTB=0xff;
DDRC=0x07;
PORTC=0x07;
DDRD=0xff;
PORTD=0xf0;
DDRE=0x07;
PORTE=0x00;
}