原创
【046】基于HD44780的1602简单驱动(C51)
2008-10-16 11:42
2776
6
6
分类:
电源/新能源
【046】基于HD44780的1602简单驱动(C51)
[相关] (001)基于HD44780液晶芯片的16×2字符型液晶(1602)简介。
lcd.h:
#ifndef LCD_H #define LCD_H #include <AT89X51.h> #define LCD_DATA_PORT P0 // 液晶BD0~BD7与P0口相连 #define LCD_RS P2_0 // 液晶 RS 引脚与P2.0相接 #define LCD_RW P2_1 // 液晶 R/W 引脚与P2.1相接 #define LCD_E P2_2 // 液晶 E 引脚与P2.2相接 #define LCD_BF P0_7 // 液晶 DB7 引脚与P0.7相接 Busy Flag extern void lcd_command(unsigned char cmd); extern void lcd_goto_xy(unsigned char x, unsigned char y); extern void lcd_putc(unsigned char c); extern void lcd_xy_putc(unsigned char x, unsigned char y, unsigned char c); extern void lcd_puts(unsigned char *s); extern void lcd_xy_puts(unsigned char x, unsigned char y, unsigned char *s); extern void lcd_init(void); #endif //LCD_H
lcd.c:
#include "lcd.h" void lcd_delay_us(unsigned char us) { while(--us) { ; } } void lcd_delay_ms(unsigned char ms) { unsigned char i; while(ms--) { for(i = 0; i < 124; i++) { ; } } } void lcd_e_toggle(void) { LCD_E = 1; lcd_delay_us(3); LCD_E = 0; } void lcd_wait_until_finish(void) { LCD_RW = 1; LCD_RS = 0; LCD_E = 1; while(LCD_BF) { ; } LCD_E = 0; } void lcd_command(unsigned char cmd) { LCD_RW = 0; LCD_RS = 0; LCD_DATA_PORT = cmd; lcd_e_toggle(); lcd_wait_until_finish(); } void lcd_goto_xy(unsigned char x, unsigned char y) { unsigned char p; if (y==0) { p = 0x00 + x; } else { p = 0x40 + x; } lcd_command(p + 0x80); } void lcd_putc(unsigned char c) { LCD_RW = 0; LCD_RS = 1; LCD_DATA_PORT = c; lcd_e_toggle(); lcd_wait_until_finish(); } void lcd_xy_putc(unsigned char x, unsigned char y, unsigned char c) { lcd_goto_xy(x,y); lcd_putc(c); } void lcd_puts(unsigned char *s) { while(*s) { lcd_putc(*s); s++; } } void lcd_xy_puts(unsigned char x, unsigned char y, unsigned char *s) { lcd_goto_xy(x, y); lcd_puts(s); } void lcd_init(void) { lcd_delay_ms(12); lcd_command(0x38); lcd_command(0x06); lcd_command(0x0c); lcd_command(0x01); }
测试:
#include "lcd.h"
void main(void) { while(1) { lcd_init(); lcd_goto_xy(0,0); lcd_putc('a'); lcd_putc(0x62); lcd_xy_putc(3,0,'c'); lcd_goto_xy(0,1); lcd_puts("Hello world!"); lcd_xy_puts(16-3,1,"^_^"); } }
文章评论(0条评论)
登录后参与讨论