/*
LCD1602液晶屏驅動模塊
1、可直接嵌入到項目中使用
2、晶振頻率:1M
3、如晶振提高低層驅動延時要作相應修改
AVR_AFA
程序編寫過程中參照了peak的4線顯示程序
peak:AVR論壇技術版版主
*/
/**********************************************/
/* RS---PD3 RW---PD4 EN--PD6 DATA--PB*/
/*********************************************/
#include <iom16v.h>
#define RS_CLR PORTD &= ~(1 << PD3)
#define RS_SET PORTD |= (1 << PD3)
#define RW_CLR PORTD &= ~(1 << PD4)
#define RW_SET PORTD |= (1 << PD4)
#define EN_CLR PORTD &= ~(1 << PD6)
#define EN_SET PORTD |= (1 << PD6)
/*延時函數(shù)*/
void delay_us(unsigned int n) {
if (n == 0) {
return ;
}
while (--n);
}
/*延時函數(shù)*/
void delay_ms(unsigned char i) {
unsigned char a, b;
for (a = 1; a < i; a++) {
for (b = 1; b; b++) {
;
}
}
}
/*顯示屏命令寫入函數(shù)*/
void LCD_write_com(unsigned char com) {
RS_CLR;
RW_CLR;
EN_SET;
PORTB = com;
delay_us(5);
EN_CLR;
}
/*顯示屏數(shù)據(jù)寫入函數(shù)*/
void LCD_write_data(unsigned char data) {
RS_SET;
RW_CLR;
EN_SET;
PORTB = data;
delay_us(5);
EN_CLR;
}
/*顯示屏清空顯示*/
void LCD_clear(void) {
LCD_write_com(0x01);
delay_ms(5);
}
/*顯示屏字符串寫入函數(shù)*/
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s) {
if (y == 0) {
LCD_write_com(0x80 + x);
}
else {
LCD_write_com(0xC0 + x);
}
while (*s) {
LCD_write_data( *s);
s ++;
}
}
/*顯示屏單字符寫入函數(shù)*/
void LCD_write_char(unsigned char x,unsigned char y,unsigned char data) {
if (y == 0) {
LCD_write_com(0x80 + x);
}
else {
LCD_write_com(0xC0 + x);
}
LCD_write_data( data);
}
/*顯示屏初始化函數(shù)*/
void LCD_init(void) {
DDRB = 0xFF; /*I/O口方向設置*/
DDRD |= (1 << PD3) | (1 << PD4) | (1 << PD6);
LCD_write_com(0x38); /*顯示模式設置*/
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
delay_ms(5);
LCD_write_com(0x38);
LCD_write_com(0x08); /*顯示關閉*/
LCD_write_com(0x01); /*顯示清屏*/
LCD_write_com(0x06); /*顯示光標移動設置*/
delay_ms(5);
LCD_write_com(0x0C); /*顯示開及光標設置*/
}
|