我將之前51單片機的1602的程序移植到atm 16的avr單片機中。昨天是初始化都不行,今天慢慢的調試的已經可以正常初始化,但是就是不顯示給的數據。屏幕一片亮的,就是沒字符。下面我附上代碼,希望幫忙看看。
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
uchar dis1[] = "WELCOME TO";
void delay(uchar z)
{
uchar j;
while(z--)
for(j=110;j>0;j--);
}
void Delayms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
// sbit rs=P2^6;
//sbit rw=P2^5;
// sbit en=P2^7;
// sbit LCD=P0;
void busy()// 測忙
{
//P0=0xff;
PORTC&=~BIT(6);
PORTC|=BIT(5);
PORTC|=BIT(7);
while(PORTA&0X80);//判斷最高位D7是否為1
PORTC&=~BIT(7);
}
//寫命令函數
void LCDWCom(uint com)
{
busy();
PORTC&=~BIT(6);
PORTC&=~BIT(5);
PORTA=com;
PORTC=BIT(7);
Delayms(1);
PORTC&=~BIT(7);
}
void LCDWriteCom(uint com)
{
busy();
PORTC&=~BIT(6);
PORTC&=~BIT(5);
PORTA=com;
// P0=(com&0x0f) << 4;
PORTC|=BIT(7);
Delayms(1);
PORTC&=~BIT(7);
PORTA=(com&0x0f) << 4;
//P0=com;
PORTC|=BIT(7);
Delayms(1);
PORTC&=~BIT(7);
}
//寫數據
void LCDWriteByte(uint dat)
{
busy();
PORTC|=BIT(6);
PORTC&=~BIT(5);
PORTA=dat;
PORTC|=BIT(7);
Delayms(1);
PORTC&=~BIT(7);
PORTA=(dat&0x0f)<<4;
PORTC|=BIT(7);
Delayms(1);
PORTC&=~BIT(7);
}
//初始化
void LCDInit()
{
// LCDWriteCom(0x);
LCDWCom(0x28);
LCDWriteCom(0x28);
LCDWriteCom(0x0c);
LCDWriteCom(0x06);
LCDWriteCom(0x01);
// LCDWriteCom(0x80);
}
void main()
{
uchar i;
DDRA=0xff;
DDRC|=BIT(5)|BIT(6)|BIT(7);
busy();
LCDInit();
LCDWriteCom(0x80); //Y是行
for(i=0;dis1[i]!='\0';i++)
{
LCDWriteByte(dis1[i]);
Delayms(10); //實現字體漸出的效果
}
while(1);
}
|