原创 Cepark电子园 AVR M16 实验之二 LCD1602显示

2010-3-31 14:58 1682 3 3 分类: EDA/ IP/ 设计与制造

/**********************************************************************
* 文件名称: 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 <avr/io.h>
#include <util/delay.h>

#define SETLCD1602RS PORTA |= (1<<PA4) //RS信号置一和清零
#define CLRLCD1602RS PORTA &= ~(1<<PA4)
#define SETLCD1602RW PORTA |= (1<<PA5) //RW信号置一和清零
#define CLRLCD1602RW PORTA &= ~(1<<PA5)
#define SETLCD1602EN PORTA |= (1<<PA6) //EN信号置一和清零
#define CLRLCD1602EN PORTA &= ~(1<<PA6)

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 != '\0')
{
LCD1602WriteOperation(*str,1);
str++;
}
}


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

https://static.assets-stash.eet-china.com/album/old-resources/2010/3/31/70e37b15-1935-4deb-9e67-6f8b9becfd36.zip


PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
3
关闭 站长推荐上一条 /3 下一条