微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR M16 实验之二 LCD1602显示

AVR M16 实验之二 LCD1602显示

时间:11-25 来源:互联网 点击:
/**********************************************************************
* 文件名称: main.c
* 程序作者: kidcao1987
* 程序版本: V1.0
* 功能描述: 装好LCD1602,用在LCD1602上显示,这里建议再外接上电源
“This a LCD demo!
www.CEPARK.com
Keep moving!!
By:kicao1987”
* 编译器:WinAVR-20090313
* 芯片:ATmega16,外部11.0592MHZ晶振
* 技术支持:http://bbs.cepark.com
**********************************************************************/
#include
#include

#define SETLCD1602RS PORTA |= (1#define CLRLCD1602RS PORTA &= ~(1#define SETLCD1602RW PORTA |= (1#define CLRLCD1602RW PORTA &= ~(1#define SETLCD1602EN PORTA |= (1#define CLRLCD1602EN PORTA &= ~(1
unsigned char const ucString1[]={"This a LCD demo!"};
unsigned char const ucString2[]={" www.CEPARK.com "};
unsigned char const ucString3[]={" Keep moving!! "};
unsigned char const ucString4[]={" By:kicao1987 "};

void LCD1602BusyCheck(void); //忙检测
void LCD1602WriteOperation(unsigned char,unsigned char); //写操作
void LCD1602Init(void); //初始化函数
void LCD1602MoveToPosition(unsigned char,unsigned char); //定位到制定位置
void LCD1602PrintString(unsigned char const * str); //输出一串字符

int main(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0xFF;
DDRB = 0xFF;
LCD1602Init();
while(1)
{
LCD1602MoveToPosition(0,0);
LCD1602PrintString(ucString1);
LCD1602MoveToPosition(1,0);
LCD1602PrintString(ucString2);
_delay_ms(5000); //延时5毫秒
LCD1602MoveToPosition(0,0);
LCD1602PrintString(ucString3);
LCD1602MoveToPosition(1,0); //延时5毫秒
LCD1602PrintString(ucString4);
_delay_ms(5000);
}
}

//忙检测
void LCD1602BusyCheck(void)
{
unsigned char i = 254;
DDRB = 0x00; //设置最高位为输入
CLRLCD1602RS;
SETLCD1602RW;
SETLCD1602EN;
_delay_us(5);
while((i--) && (PINB & 0x80));
DDRB = 0xff;
_delay_us(5);
CLRLCD1602EN;
_delay_us(5);
}

//写操作
void LCD1602WriteOperation(unsigned char Data,unsigned char ComOrData)
{
LCD1602BusyCheck(); //ComOrData=0为命令,1为数据

if(0 == ComOrData)
CLRLCD1602RS;
else if(1 == ComOrData)
SETLCD1602RS;
CLRLCD1602RW;
SETLCD1602EN;
_delay_us(5);
PORTB = Data;
_delay_us(5);
CLRLCD1602EN;
_delay_us(100);
}

void LCD1602Init(void) //初始化
{
LCD1602WriteOperation(0x38,0);
_delay_ms(15);
LCD1602WriteOperation(0x38,0);
_delay_ms(5);
LCD1602WriteOperation(0x38,0);
_delay_ms(5);
LCD1602WriteOperation(0x38,0);
_delay_ms(5);

LCD1602WriteOperation(0x0c,0);
_delay_ms(1);
LCD1602WriteOperation(0x06,0);
_delay_ms(1);
LCD1602WriteOperation(0x01,0);
_delay_ms(1);
}

void LCD1602MoveToPosition(unsigned char ucx,unsigned char ucy)
{
if(0 == ucx) //第一行
{
LCD1602WriteOperation(0x80 + ucy,0); //第Y列
}
if(1 == ucx) //第二行
{
LCD1602WriteOperation(0xC0 + ucy,0); //第Y列
}
}

void LCD1602PrintString(unsigned char const * str)
{
while(*str != )
{
LCD1602WriteOperation(*str,1);
str++;
}
}

视频地址:http://v.youku.com/v_show/id_XMTYxNDg1MDQ0.html

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top