angle11 发表于 2010-7-15 10:53:26

使用PICC自带的LCD4线驱动,怎么都调不出来!

4线驱动,直接就用PICC自带的驱动,可是怎么都不显示,但是光标可以在指定的位置闪烁,所以写命令函数是好的,只是写不了字符,我是在proteus上仿真看结果。我反复搞了很久了,还是不出现字符!!!

/*RA2      LCD-RS
RA3      LCD-EN
PORTB0-3      LCD-DATA 4-7
*/



#include        <pic.h>

#define uchar unsigned char
#define uint unsigned int

static bit LCD_RS        @ ((unsigned)&PORTA*8+2);        // Register select
static bit LCD_EN        @ ((unsigned)&PORTA*8+3);        // Enable

#define        LCD_STROBE        (LCD_EN = 1);DelayMs(1);(LCD_EN=0)


void DelayMs(uint x)
{
        uint y,z;
        for(y=x;y>0;y--)
                for(z=110;z>0;z--);
}
void DelayUs(uint x)
{
        uint y,z;
        for(y=x;y>0;y--)
                for(z=5;z>0;z--);
}
/* write a byte to the LCD in 4 bit mode */

void
lcd_write(unsigned char c)
{
        LCD_EN=0;
        PORTB = (PORTB & 0xF0) |(c >> 4);
        LCD_STROBE;
        PORTB = (PORTB & 0xF0) |(c & 0x0F);
        LCD_STROBE;
        DelayUs(40);
}

/*
*         Clear and home the LCD
*/

void
lcd_clear(void)
{
        LCD_RS = 0;
        lcd_write(0x1);
        DelayMs(2);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
        LCD_RS = 1;        // write characters
        while(*s)
        {
                lcd_write(*s++);
                DelayMs(1);
        }
}

/* write one character to the LCD */

void
lcd_putch(uchar c)
{
        LCD_RS = 1;        // write characters
        LCD_EN=0;
        PORTB = (PORTB & 0xF0) |(c >> 4);
        LCD_STROBE;
        PORTB = (PORTB & 0xF0) |(c & 0x0F);
        LCD_STROBE;
        DelayUs(40);
}


/*
* Go to the specified position
*/

void
lcd_goto(unsigned char pos)
{
        LCD_RS = 0;
        lcd_write(0x80+pos);
}
       
/* initialise the LCD - put into 4 bit mode */

void
lcd_init(void)
{
        LCD_RS = 0;        // write control bytes
        DelayMs(15);        // power on delay
        PORTB = 0x3;        // attention!
        LCD_STROBE;
        DelayMs(5);
        LCD_STROBE;
        DelayUs(100);
        LCD_STROBE;
        DelayMs(5);
        PORTB = 0x2;        // set 4 bit mode
        LCD_STROBE;
        DelayUs(40);
        lcd_write(0x28);        // 4 bit mode, 1/16 duty, 5x8 font
//        lcd_write(0x08);        // display off
        lcd_write(0x0f);        // display on, blink curson on
        lcd_write(0x06);        // entry mode
}
void portinit()
{
        PORTA=0;
        PORTB=0;
        TRISA=0;
        TRISB=0;
}

void main()
{
        portinit();
        lcd_init();
        lcd_clear();
        DelayMs(5);
        lcd_goto(1);
        lcd_puts("hello");
        while(1);
}

帮我看一下吧,怎么回事啊?

angle11 发表于 2010-7-15 13:29:47

我在仿真过程中发现,RA2即LCD——RS这个脚怎么一直都是低电平?
可是在程序里不是有LCD——RS=1 这句话吗?
为什么在实际运行时显示为低电平呢?

在main()里最后一个函数lcd_putch(),在里面不是有LCD_RS=1吗?后面一直都没有LCD——RS这个脚的操作,应该一直就是高电平才对啊?为什么在仿真看到的是低电平了呢?我怀疑就是这里的问题,可是究竟是什么原因呢?
把我搞晕了。。。。。。

angle11 发表于 2010-7-15 15:09:05

是不是仿真不太好啊?
在坛子里下了一个驱动,第一次仿真可以正常显示,第二次开始,死都不显示了。。。
有遇到我这种情况的朋友没有啊,这是怎么回事呢?。。。。

angle11 发表于 2010-7-15 15:37:31

为什么RA口好像不作为LCD——EN 和LCD——RS会仿真不出来,而RC口则能仿真出来呢?
有没有朋友遇到这种情况啊?
是因为RA口初始化有什么技巧吗?
我用的是PICC这个编译器。。。。。。。

angle11 发表于 2010-7-15 17:13:05

我靠!PORTA设成正常的I/O口,原来还要设置ADCON1才行!
晕!
页: [1]
查看完整版本: 使用PICC自带的LCD4线驱动,怎么都调不出来!