|
看了淘宝超值宝贝推介后,早上跑去买了4块,要多了也没有用。
回来写了程序
测试成功!
上图上程序
/****************************2004.h************************************************************/
#define uchar unsigned char
#define uint unsigned int
#define RS 0
#define RW 1
#define EN 2
void s_ms(uint ms)
{
for(;ms>1;ms--);
}
//查忙
void busy(void)
{
uchar temp;
s_ms(500);
PORTC&=~(1<<RS); //RS=0
s_ms(500);
PORTC|=(1<<RW); //RW=1
s_ms(500);
while(temp)
{
PORTC|=(1<<EN); //EN=1
s_ms(500);
DDRA=0x00; //A口变输入
PORTA=0xff; //上拉使能
s_ms(500);
temp = PINA&0x80; //读取A口
s_ms(500);
DDRA=0xff;
PORTA=0xff; //A口变输出
s_ms(500);
PORTC&=~(1<<EN); //EN=0
s_ms(500);
}
}
//写指令
void writecom(uchar com)
{
busy();
s_ms(500);
PORTC&=~(1<<RS); //RS=0
s_ms(500);
PORTC&=~(1<<RW); //RW=0
s_ms(500);
PORTC|=(1<<EN); //EN=1
s_ms(500);
PORTA = com; //输出指令
s_ms(500);
PORTC&=~(1<<EN); //EN=0
s_ms(500);
}
//1602初始化
void LcdInit(void)
{
writecom(0x38);
s_ms(1000);
writecom(0x01);
s_ms(10000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
writecom(0x02);
s_ms(1000);
writecom(0x06);
s_ms(1000);
writecom(0x0c);
s_ms(1000);
writecom(0x38);
s_ms(1000);
}
//写数据
void writedata(uchar data)
{
busy();
s_ms(500);
PORTC|=(1<<RS); //RS=1
s_ms(500);
PORTC&=~(1<<RW); //RW=0
s_ms(500);
PORTC|=(1<<EN); //EN=1
s_ms(500);
PORTA = data; //输出数据
s_ms(500);
PORTC&=~(1<<EN); //EN=0
s_ms(500);
}
//读数据
uchar readdata(void)
{
uchar temp;
busy();
s_ms(500);
PORTC|=(1<<RS); //RS=1
s_ms(500);
PORTC|=(1<<RW); //RW=1
s_ms(500);
PORTC|=(1<<EN); //EN=1
s_ms(500);
DDRA=0x00; //A端口变输入
s_ms(500);
temp = PINA; //读A端口
s_ms(500);
DDRA=0xff; //A端口变输出
s_ms(500);
PORTC&=~(1<<EN); //EN=0
s_ms(500);
return temp;
}
//=================================================
// 描述: 写LCD内部CGRAM函数
// 入口: ‘num’要写的数据个数
// ‘pbuffer’要写的数据的首地址
// 出口: 无
//================================================
void WriteCGRAM(uint num, const uint *pBuffer)
{
uint i,t;
writecom(0x40);
PORTC|=(1<<RS);
PORTC&=~(1<<RW);
for(i=num;i!=0;i--)
{
t = *pBuffer;
PORTC|=(1<<EN);
PORTA = t;
PORTC&=~(1<<EN);
pBuffer++;
}
}
//================================================================
// 描述:在任意位置写任意多个字符
// 入口:’row‘要写的字符所在的行,只能为1或2;
// ‘col’要写的字符所在的列,只能为0---15
// ‘num’要写字符的个数
// ‘pbuffer’要写字符的首地址
//==================================================================
void WriteChar(uchar row,uchar col,uint num,uchar *pBuffer)
{
uchar i,t;
switch (row)
{
case 1 : row = 0x80 + col; break;
case 2 : row = 0xC0 + col; break;
case 3 : row = 0x94 + col; break;
case 4 : row = 0xD4 + col; break;
default: break;
}
writecom(row);
PORTC|=(1<<RS);
s_ms(500);
PORTC&=~(1<<RW);
s_ms(500);
for(i=num;i!=0;i--)
{
t = *pBuffer;
s_ms(500);
PORTA = t;
s_ms(500);
PORTC|=(1<<EN);
s_ms(500);
PORTC&=~(1<<EN);
s_ms(500);
pBuffer++;
}
}
/************************************main.c***************************************************/
#include <iom128v.h>
#include "2004.h"
#define uchar unsigned char
#define uint unsigned int
uchar a[]={"www.ourdev.cn"};
uchar b[]={"I Love AVR!"};
uchar c[]={"shop57085242.taobao.com"};
uchar d[]={"Airwolf 09/11/22"};
main()
{
DDRA=0xff;
PORTA=0xff;
DDRC=0xff;
PORTC=0xff;
LcdInit();
WriteChar(1,0,13,a);
WriteChar(2,0,11,b);
WriteChar(3,0,20,c);
WriteChar(4,0,16,d);
}
![](http://cache.amobbs.com/bbs_upload782111/files_22/ourdev_506109.jpg)
(原文件名:调试成功.jpg) |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|