原创
ATmega16_App_V1.0开发板范例4:LCD1602八线制驱动实验(ICC)
一、
程序结构
下载 (10.62 KB)
2009-11-3 17:18
二、
仿真效果
下载 (145.5 KB)
2009-11-3 17:18
三、程序源码
1、main.c
- /*******************************************************************************
- Platform : ATmega16_App_V1.0开发板(http://bbs.armavr.com)
- Project : 范例4:LCD1602八线制驱动实验(ICC)
- Clock F : 内部8M
- Software : ICCAVR7.14C
- Author : 林夕依然
- Version : 09.11.03
- Updata : 09.04.28 增加了proteus仿真模型,调试通过。
- 09.08.02 优化程序,不会再出现乱码,清屏指令后加延时
- comments :
- 1、八线制:PB0-PB7为数据线,PD3/PD4/PD6控制LCD1602B的RS,RW,EN。
- 2、模块化编程。
- *******************************************************************************/
- #include <iom16v.h>
- #include "LCD1602.h"
- void main(void)
- {
- Port_init();//端口初始化
- LCD_init(); //LCD初始化
- LCD_clear();//清屏
- LCD_write_str(0,0,"abcdefghijklmnop");
- LCD_write_str(0,1,"ABCDEFGHIJKLMNOP");
- delay_ms(2000);
- LCD_clear();//清屏
- while (1)
- {
- uchar i="0",j=0;
- uchar *p="I love ATmega16!";
- uchar *q="I love China!";
- LCD_write_str(0,0,"09.11.03-15:27");
- LCD_write_str(0,1,"2009-03-27");
- delay_ms(2000);
- LCD_clear();//清屏
- LCD_write_str(0,0,"605987969@qq.com");
- LCD_write_str(0,1,"bbs.armavr.com");
- delay_ms(2000);
- LCD_clear();//清屏
- LCD_write_str(0,0,"ATmega16_App_PCB");
- LCD_write_str(0,1,"linxiyiran");
- delay_ms(2000);
- LCD_clear();//清屏
- while (*p)
- {
- LCD_write_char(i,0,*p);
- i++;
- p++;
- delay_ms(100);
- }
- while (*q)
- {
- LCD_write_char(j,1,*q);
- j++;
- q++;
- delay_ms(100);
- }
- delay_ms(2000);
- LCD_clear();//清屏
- }
- }
2、LCD1602.c
- /*******************************
- Platform : ATmega16_App_V1.0开发板(http://bbs.armavr.com)
- function :功能函数集
- Clock F : 内部8M
- Software : ICCAVR7.14C
- Author : 林夕依然
- Version : 09.11.03
- comments :
- 1、两种方式实现延时
- ********************************/
- #include <iom16v.h>
- #include <macros.h>
- #include "LCD1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- #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)
- //us延时函数
- void delay_us(uint n) //8*0.125=1us
- {
- int i,j;
- for(j=0;j<8;j++)
- {
- for (i=0;i<n;i++)
- NOP();
- }
- }
- //ms延时函数
- void delay_ms(uint i)
- {
- while(i--)
- {
- uint j;
- for(j=1;j<=1332;j++)
- ;
- }
- }
- //端口初始化
- void Port_init(void)
- {
- PORTA=0XFF;
- DDRA =0X00;
- PORTB=0XFF;
- DDRB =0X00;
- PORTC=0X7F;
- DDRC =0X80;
- PORTD=0XFF;
- DDRD =0X00;
- }
- //显示屏初始化函数
- void LCD_init(void)
- {
- DDRB = 0xFF; //I/O口方向设置
- DDRD|=(1<<PD3)|(1<<PD4)|(1<<PD6);
- delay_ms(15); //上电延时一段时间,使供电稳定
- Write_Instruction(0x38); //8bit interface,2line,5*7dots
- delay_ms(5);
- Write_Instruction(0x38);
- delay_ms(5);
- Write_Instruction(0x38);
- Write_Instruction(0x08); //关显示,不显光标,光标不闪烁
- Write_Instruction(0x01); //清屏
- delay_ms(5);
- Write_Instruction(0x04); //写一字符,整屏显示不移动
- //Write_Instruction(0x05); //写一字符,整屏右移
- //Write_Instruction(0x06); //写一字符,整屏显示不移动
- //Write_Instruction(0x07); //写一字符,整屏左移
- delay_ms(5);
- //Write_Instruction(0x0B); //关闭显示(不显示字符,只有背光亮)
- Write_Instruction(0x0C); //开显示,光标、闪烁都关闭
- //Write_Instruction(0x0D); //开显示,不显示光标,但光标闪烁
- //Write_Instruction(0x0E); //开显示,显示光标,但光标不闪烁
- //Write_Instruction(0x0F); //开显示,光标、闪烁均显示
- }
- //控制LCD写时序
- void LCD_en_write(void) //EN端产生一个高电平脉冲,控制LCD写时序
- {
- EN_SET;
- delay_us(20);
- EN_CLR;
- delay_us(20);
- }
- //清屏函数
- void LCD_clear(void)
- {
- Write_Instruction(0x01);
- delay_ms(5);
- }
- //写指令函数
- void Write_Instruction(uchar command)
- {
- RS_CLR;
- RW_CLR;
- EN_SET;
- PORTB=command;
- LCD_en_write();//写入指令数据
- }
- //写数据函数
- void Write_Data(uchar Wdata)
- {
- RS_SET;
- RW_CLR;
- EN_SET;
- PORTB=Wdata;
- LCD_en_write();//写入数据
- }
- //字符显示初始地址设置
- void LCD_SET_XY(uchar X,uchar Y)
- {
- uchar address;
- if(Y==0)
- address=0x80+X;//Y=0,表示在第一行显示,地址基数为0x80
- else
- address=0xc0+X;//Y非0时,表时在第二行显示,地址基数为0XC0
- Write_Instruction(address);//写指令,设置显示初始地址
- }
- //在第X行Y列开始显示,指针*S所指向的字符串
- void LCD_write_str(uchar X,uchar Y,uchar *s)
- {
- LCD_SET_XY(X,Y);//设置初始字符显示地址
- while(*s)//逐次写入显示字符,直到最后一个字符"/0"
- {
- Write_Data(*s);//写入当前字符并显示
- s++;//地址指针加1,指向下一个待写字符
- }
- }
- //在第X行Y列开始显示Wdata所对应的单个字符
- void LCD_write_char(uchar X,uchar Y,uchar Wdata)
- {
- LCD_SET_XY(X,Y);//写地址
- Write_Data(Wdata);//写入当前字符并显示
- }
3、LCD1602.h
- #define uchar unsigned char
- #define uint unsigned int
- void delay_us(uint n);
- void delay_ms(uint i);
- void Port_init(void);
- void LCD_init(void);
- void LCD_en_write(void);
- void LCD_clear(void);
- void Write_Instruction(uchar command);
- void Write_Data(uchar Wdata);
- void LCD_SET_XY(uchar X,uchar Y);
- void LCD_write_str(uchar X,uchar Y,uchar *s);
- void LCD_write_char(uchar X,uchar Y,uchar Wdata);
四、完整项目文件
https://static.assets-stash.eet-china.com/album/old-resources/2009/11/5/22210bff-a72a-443a-9ee3-f7727baca0c4.rar
文章评论(0条评论)
登录后参与讨论