转自www.iccavr.com
LCD1602显示实验
/*
实验十五:
LCD1602显示实验。
1、LCD1602显示实验。显示欢及网址信息。
2、内部1 M晶振,程序采用单任务方式,软件延时。
3、进行此实验请插上JP1、JP2的所有8个短路块,PC7短路块。
AVR mega16学习板
www.iccavr.com
2007-04-13
*/
#include <iom16v.h>
#include "lcd1602.h"
void main(void) {
unsigned char i;
unsigned char *p;
PORTA = 0xFF; /*打开上拉*/
DDRA = 0x00; /*方向输入*/
PORTB = 0xFF; /*电平设置*/
DDRB = 0xFF; /*方向输出*/
PORTC = 0x7F;
DDRC = 0x80;
PORTD = 0xFF;
DDRD = 0x00;
delay_ms(100);
LCD_init();
while (1) {
i = 1;
p = "www.iccavr.com";
LCD_clear();
LCD_write_str(5,0,"Welcome");
delay_ms(250);
while (*p) {
LCD_write_char(i,1,*p);
i ++;
p ++;
delay_ms(250);
}
delay_ms(250);
}
}
LCD1602驱动
/*
LCD1602液晶屏驱动模块
1、可直接嵌入到项目中使用
2、晶振频率:1M
3、如晶振提高低层驱动延时要作相应修改
AVR_AFA
www.iccavr.com
程序编写过程中参照了peak的4线显示程序
peak:AVR论坛技术版版主
*/
#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)
/*延时函数*/
void delay_us(unsigned int n) {
if (n == 0) {
return ;
}
while (--n);
}
/*延时函数*/
void delay_ms(unsigned char i) {
unsigned char a, b;
for (a = 1; a < i; a++) {
for (b = 1; b; b++) {
;
}
}
}
/*显示屏命令写入函数*/
void LCD_write_com(unsigned char com) {
RS_CLR;
RW_CLR;
EN_SET;
PORTB = com;
delay_us(5);
EN_CLR;
}
/*显示屏命令写入函数*/
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);
}
/*显示屏字符串写入函数*/
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 ++;
}
}
/*显示屏单字符写入函数*/
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);
}
/*显示屏初始化函数*/
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); /*显示开及光标设置*/
}
文章评论(0条评论)
登录后参与讨论