主控为ST7920的12864.由于手头上Mega8 I/O口不够,只好用PC0-PC3接DB0~3口,PD0~3接DB4~7口。PSB写高电平,用并行输入。
12864一直没反应,求路过的高手帮忙看看,指点迷津。以下是程序:
#include <avr/io.h>
#include "lcd12864.h"
#define uchar unsigned char
#define uint unsigned int
#define DB0 PC0
#define DB1 PC1
#define DB2 PC2
#define DB3 PC3
#define DB4 PD0
#define DB5 PD1
#define DB6 PD2
#define DB7 PD3
#define LCD_RS PD7
#define LCD_RW PD6
#define LCD_EN PD5
#define LCD_RET PC6//接Mega8 RESET口,低电平有效
#define SET_DATA() (1<<LCD_RS) //写数据
#define SET_INC() (~(1<<LCD_RS)) //写命令
#define SET_READ() (1<<LCD_RW) //读
#define SET_WRITE() (~(1<<LCD_RW)) //写
#define SET_EN() (1<<LCD_EN) //使能
#define CLR_EN() (~(1<<LCD_EN)) //空闲
uchar LCD_DataPortRead(void)
{
uchar a;
a=((PINC&0x0f)|((PIND&0x0f)<<4));
return a;
}
void LCD_DataPort(uchar Data)
{
if(Data&0x80) PORTD|=(1<<PD3);
if(Data&0x40) PORTD|=(1<<PD2);
if(Data&0x20) PORTD|=(1<<PD1);
if(Data&0x10) PORTD|=(1<<PD0);
if(Data&0x08) PORTC|=(1<<PC3);
if(Data&0x04) PORTC|=(1<<PC2);
if(Data&0x02) PORTC|=(1<<PC1);
if(Data&0x01) PORTC|=(1<<PC0);
}
void LCD_CheckBusy(void)
{
uint temp,Timeout=0;
SET_INC();
SET_READ();
CLR_EN();
SET_EN();
Delay(5);
temp=LCD_DataPortRead();
CLR_EN();
while((temp&0x80)&&(++Timeout!=0));
}
void LCD_SendCmd(uchar Cmd)
{
LCD_CheckBusy();
SET_INC();
SET_WRITE();
SET_EN();
LCD_DataPort(Cmd);
_delay_us(100);
CLR_EN();
}
void LCD_SendData(uchar Data)
{
LCD_CheckBusy();
SET_DATA();
SET_WRITE();
SET_EN();
LCD_DataPort(Data);
Delay(5);
CLR_EN();
}
void Delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void LCD_Init(void)
{
Delay(50);
LCD_SendCmd(0x30); //基本指令集
Delay(5);
LCD_SendCmd(0x30);
Delay(5);
LCD_SendCmd(0x0c); //开显示
Delay(5);
LCD_SendCmd(0x01); //清屏
Delay(5);
LCD_SendCmd(0x06); //光标右移
}
void LCD_SetAddress(uchar x,uchar y)
{
uchar Address;
switch(y)
{
case 0: Address=0x80+x;break;
case 1: Address=0x90+x;break;
case 2: Address=0x88+x;break;
case 3: Address=0x98+x;break;
default:
break;
}
LCD_SendCmd(Address);
}
void LCD_PutString(uchar x,uchar y,uchar *pData)
{
LCD_SetAddress(x,y);
while(*pData!='\0')
{
LCD_SendData(*pData++);
}
}
主程序如下:
#include <avr/io.h>
#include <util/delay.h>
#include "lcd12864.h"
#define uchar unsigned char
#define uint unsigned int
void Port_Init(void)
{
DDRB = 0xEC;
PORTB =0xF3;
DDRC = 0x0F; //PC0~3设置输出,无上拉
PORTC =0xF0;
DDRD = 0x0F;
PORTD =0xF0;
}
void main(void)
{
Port_Init();
PORTB|=(1<<PB5);
LCD_Init();
while(1)
{
LCD_PutString(2,0,"12864");
Delay(500);
LCD_PutString(2,1,"OK");
LCD_PutString(0,2,"LCD12864ST7920");
LCD_PutString(0,3,"★○◇◆※☆■△");
}
} |