烧录软件AVRStudio6.0,编译成功,烧录进去就是没显示,请高手帮我看看哪有问题。硬件连线都检查了,没问题。以下是程序。
///////////////////////////////////////////////////////////////////////////////////////////
#include <avr/io.h>
#include <util/delay.h>
//定义MCU与LCD的接口
#define LCD_DATA_PORT PORTC
#define LCD_DATA_DDR DDRC
#define LCD_DATA_PIN PINC
#define LCD_DATA 0xf0 //portd4/5/6/7 out
#define LCD_EN_DDR DDRA
#define LCD_EN_PORT PORTA
#define LCD_EN (1<<PA7)
#define LCD_RW_DDR DDRA
#define LCD_RW_PORT PORTA
#define LCD_RW (1<<PA6)
#define LCD_RS_DDR DDRA
#define LCD_RS_PORT PORTA
#define LCD_RS (1<<PA5)
#define E_H LCD_EN_PORT|=LCD_EN
#define E_L LCD_EN_PORT&= ~LCD_EN
#define RW_H LCD_RW_PORT|=LCD_RW
#define RW_L LCD_RW_PORT&= ~LCD_RW
#define RS_H LCD_RS_PORT|=LCD_RS
#define RS_L LCD_RS_PORT&= ~LCD_RS
void LCD1602_init(void)
{
LCD_DATA_DDR |= LCD_DATA; //#define LCD_DATA 0xf0 //portd4/5/6/7 out 即是让A的高四位输出,低四位输入
LCD_EN_DDR |= LCD_EN; //#define LCD_EN (1<<PB0) 即是让对应EN的PB0为输出
LCD_RS_DDR |= LCD_RS; //#define LCD_RS (1<<PB2) 即是让对应RS的PB2为输出
LCD_RW_DDR |= LCD_RW; //#define LCD_RW (1<<PB1) 即是让对应RW的PS1为输出
//E_L;
RW_L; //#define RW_L LCD_RW_PORT&= ~LCD_RW 即是选择写入模式
LCD_DATA_PORT = 0x20; //#define LCD_DATA_PORT PORTA 结合上一行,指令6:DL=0(4位总线)、N=0(单行显示)、F=0(5*7)
LCD16xx_en_write(); //执行指令,给EN端一个脉冲
//_delay_us(50);
LCD16xx_write_char(1,0x28); //4bit test
//_delay_ms(5);
LCD16xx_write_char(1,0x0c); //显示开,指令4:D=1(显示开)、C=1(光标开)、B=0(闪烁(光标?)关)
LCD16xx_write_char(1,0x01); //显示清屏,指令1
LCD16xx_write_char(1,0x06); //显示光标移动设置,指令3:I/D=1(光标递增移动)、SH=0(显示不移动)
}
void LCD16xx_en_write(void) //EN端产生一个高电平脉冲,写LCD
{
//_delay_us(50);
E_H; //#define E_H LCD_EN_PORT|=LCD_EN
_delay_us(50); //如果晶振频率太高,则可相应提高延时
E_L; //#define E_L LCD_EN_PORT&= ~LCD_EN
}
void LCD16xx_write_char(unsigned char command,unsigned char data)
{
unsigned char datah,datal;
datah = data; //采用四位总线,先送高四位,再送低四位
datal = data<<4 ;
//LCD16xx_wait_Ready();
_delay_ms(50);
RW_L; //RW=0,选择写入
if (command == 0) //data
RS_H; //RS=1,选择数据寄存器
else //command
RS_L; //RS=0,选择命令寄存器
LCD_DATA_PORT &= 0x0F; //使得单片机数据端口的高四位清零(准备传输),低四位保持(没用到,别影响)
LCD_DATA_PORT |= datah&0xf0; //send high 4bit,使datah的低四位清零,然后与数据端口(高四位已清零)或
LCD16xx_en_write(); //执行指令
LCD_DATA_PORT &= 0x0F;
LCD_DATA_PORT |= datal&0xf0; //send low 4bit
LCD16xx_en_write();
}
void LCD16xx_wait_Ready(void) //等待LCD空闲
{
LCD_DATA_DDR &= ~0x80; //PD7 I/O口方向设置为输入
RW_H; //RW=1,选读出
RS_L; //RS=0,选指令寄存器
E_H; //EN=1,与下面的E_L一起,起执行指令的作用
while (!( LCD_DATA_PIN&0x80 ) == 0); //RW=1,读PD7,为0表示空闲;
E_L; //EN=0,与下面的E_H一起,起执行指令的作用
LCD_DATA_DDR |= LCD_DATA; //恢复数据口的传输方向为,11110000B
}
void LCD16xx_clr(void)
{
LCD16xx_write_char(1,0x01); //指令1
}
void LCD16xx_write_string(unsigned char X,unsigned char Y,unsigned char *s) //调用的时候把数组的首地址
{ //送到指针s所指的内存,即是 unsigned char *s=a;(unsigned char *s=&a[0]),C语言中规定数组名代表数组的首地址
LCD16xx_set_xy( X, Y );
while (*s) //*s为指针s所指内存的内容
{
_delay_us(5);
LCD16xx_write_char( 0, *s ); //*s为指针s所指内存的内容
s++;
}
}
void LCD16xx_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0)
address = 0x80 + x;
else if(y == 1)
address = 0xc0 + x;
else if(y == 2)
address = 0x90 + x;
else
address = 0xd0 + x;
LCD16xx_write_char( 1,address );
}
int main(void)
{
LCD1602_init;
LCD16xx_clr(); //显示清屏
_delay_ms(10);
LCD16xx_write_string(3,0,"Welcome to");
LCD16xx_write_string(1,1,"www.avrarm.com!");
_delay_ms(500);
LCD16xx_clr(); //显示清屏
_delay_ms(20);
while(1);
}
|