#include<reg52.h>
#include<stdio.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcd_rs=P2^0;
sbit lcd_rw=P2^1;
sbit lcd_en=P2^2;
#define lcd_data P0
//------------------------------------------------
void check_busy(void); //判忙
void write_command(uchar com); //写指令
void write_data(uchar dat); //写数据
void LCD_init(void); //初始化
void weizhi(uchar line,uchar col); //光标定位--参数 行(1-2)、列(0-15)
char putchar(char c); //重写putchar,用于printf调用
//-----------------------------------------------
void check_busy(void)
{ do //do循环,最少执行一次。
{
lcd_data =0x80; //BF置1,假设 忙
lcd_en=0;
lcd_rs=0;
lcd_rw = 1;
lcd_en = 1; //读状态字
lcd_en = 1; //脉冲展宽 ,手册上对lcd_e宽度有要求。
} while(lcd_data & 0x80); //读端口看BF是1吗,是1 则继续循环。
lcd_en = 0;
}
//----
void write_command(uchar com)
{
check_busy();
lcd_en = 0;
lcd_rs= 0;
lcd_rw = 0;
lcd_data = com;
lcd_en= 1;
lcd_en= 1;
lcd_en= 0;
}
// Write Data
void write_data(uchar dat)
{
check_busy();
lcd_en= 0;
lcd_rs = 1;
lcd_rw = 0;
lcd_data = dat;
lcd_en= 1;
lcd_en= 1;
lcd_en= 0;
}
// Initialize LCD controller
void LCD_init(void)
{ write_command(0x38); // 8-bits, 2 lines, 7x5 dots
write_command(0x0C); // no cursor, no blink, enable display
write_command(0x06); // auto-increment on
write_command(0x01); // clear screen
}
//----
void weizhi(uchar line,uchar col)
{
if(line<2)
{
write_command(0x80+col);
}
else
{
write_command(0xc0+col);
}
}
char putchar(char c) //重写putchar以输出到1602
{
write_data(c);
return(c);
}
void main()
{
uint a=2+3;
LCD_init();
weizhi(1,0);
printf("the sp of printf"); //调用printf 通过putchar来输出字符串
weizhi(2,0);
printf("2+3=%d",a); //printf的优点在于可以格式化输出
while(1)
{
;
}
}
|