#include <mega16.h>
#include <delay.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define RS PORTD.0
#define RW PORTD.1
#define EN PORTD.2
#define lcd_data PORTA
#define busy 0x80
void busy_check() // 检查忙标志
{
uchar val;
lcd_data=0xff;
RS=0;
RW=1;
EN=1;
PORTA=0xff; //改变端口为输入,使能上拉电阻
DDRA=0x00;
val=PINA;
val=PINA;
while(val&busy)
{
val=PINA;
}
DDRA=0xff;
EN=0;
}
void LCD_1602_write(uchar com,uchar busyc) //写命令,busyc==1,检查忙标志
{
if(busyc==1) busy_check();
RS=0;
RW=0;
lcd_data=com;
EN=0;
delay_us(140);
EN=1;
delay_us(140);
EN=0;
}
void LCD_1602_data(uchar dat) //写数据
{
busy_check();
RS=1;
RW=0;
lcd_data=dat;
EN=0;
delay_us(140);
EN=1;
delay_us(140);
EN=0;
}
void LCD_1602_char(uchar hang,uchar lie,uchar sign) //写字符
{
uchar a;
if(hang==1) a=0x80;
if(hang==2) a=0xc0;
a=a+lie-1;
LCD_1602_write(a,1);
LCD_1602_data(sign);
}
void LCD_1602_string(uchar hang,uchar lie,uchar flash *p) //写字符串
{
uchar a;
if(hang==1) a=0x80;
if(hang==2) a=0xc0;
a=a+lie-1;
while(1)
{
if(*p==' ') break;
LCD_1602_write(*p,1);
p++;
}
}
void LCD_1602_init() //初始化
{
delay_ms(15);
LCD_1602_write(0x38,0);
delay_ms(5);
LCD_1602_write(0x38,0);
delay_ms(5);
LCD_1602_write(0x38,0);
delay_ms(5);
LCD_1602_write(0x38,1);
LCD_1602_write(0x08,1);
LCD_1602_write(0x01,1);
LCD_1602_write(0x06,1);
LCD_1602_write(0x0c,1);
}
void main()
{
PORTA=0xff;
DDRA=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0xff;
LCD_1602_init();
LCD_1602_string(1,1,"distance:000.0") ;
LCD_1602_char(2,1,'y') ;
while(1);
}
这个1602我已经调了好几天了,麻烦大家看看,谢谢啊 !!!
此帖出自
小平头技术问答
一周热门 更多>