0

我的 4x20 LCD 显示器出现问题。过去几天我一直在查看大量指南和代码片段,但似乎没有任何帮助。问题是显示器什么也没显示。

当我在我的计算机上编译它并在我的屏幕上打印它时,一切似乎都很好。

如果有人能查看它并查看是否有任何明显的错误,我将不胜感激。

提前致谢。

//  Connection:
//
//  Atmega32    LCD
//  PB0     ->  DB4
//  PB1     ->  DB5
//  PB2     ->  DB6
//  PB3     ->  DB7
//  PB4     ->  RS
//  PB5     ->  R/W
//  PB6     ->  E
//  PB7     ->  

#include <avr/io.h>
#include <util/delay.h>

#define LCDPort PORTB
#define LCDDDR  DDRB
#define enable 6        //Enable = on
#define readWrite 5     //Read = on, Write = off
#define RS 4            //Send command = off, send data = on

void checkBusy(void);
void updateLCD(void);
void sendCommand(unsigned char command);
void sendData(unsigned char character);
void sendInitCommand(unsigned char command);

int main(void)
{
    LCDDDR |= 15;
    LCDDDR |= 1 << enable;                      //Set control lines as output (high)
    LCDDDR  |= 1 << readWrite;
    LCDDDR  |= 1 << RS;                         

    _delay_ms(100);                              //Wait for LCD to boot
    sendInitCommand(0x3);                        //Init function set 1
    _delay_ms(100);
    sendInitCommand(0x3);                        //Init function set 2
    _delay_us(100);
    sendInitCommand(0x3);                        //Init function set 3
    _delay_us(100);
    sendInitCommand(0x2);                        //Function set (set 4 bit mode)
    _delay_us(100);
    sendInitCommand(0x28);                       //Funcion set I=1, N=0, F=0
    //sendInitCommand(0x8);                      
    _delay_us(60);
    sendInitCommand(0x8);                        //On/off control D=0, C=0, B=0
    //sendInitCommand(0x8);
    _delay_us(60);
    sendCommand(0x01);                           //Clear display
    //sendInitCommand(0x1);
    _delay_ms(60);
    sendCommand(0x06);                           //Entry mode set I/D=1, S=0
    //sendInitCommand(0x6);
    _delay_us(60);
    sendCommand(0x0C);                           //On/off control D=1, C=0, B=0
    //sendInitCommand(0xC);
    _delay_us(60);


    sendData(0x41);                             //Display "A"
    sendData(0x42);                             //Display "B"
    sendData(0x43);                             //Display "C"
    sendData(0x44);                             //Display "D"
    sendData(0x45);                             //Display "E"



    while(1) {

    }

    return 0;
}

void checkBusy() {

    LCDDDR &= ~15;                              //Set data DDR lines read (low)

/*  LCDPort |= 1 << readWrite;                  //Forget read for now - use delay instead
    LCDPort &= ~1 << RS;

    while((LCDPort & 15) >= 0x8) {
        updateLCD();
    }
*/
    _delay_ms(50);
    LCDDDR |= 15;                               //Set data lines DDR to write (high)


}

void updateLCD() {

    LCDPort |= 1 << enable;                     //Enable
    asm volatile ("nop");
    asm volatile ("nop");
    LCDPort &= ~1 << enable;                    //Disable
}

void sendCommand(unsigned char command) {

    checkBusy();
    LCDPort &= ~(1 << readWrite | 1 << RS);     //Set R/W and RS low (write command)
    LCDPort |= (command >> 4) & 15;             //Send 4 ms bits
    updateLCD();
    LCDPort &= ~15; 
    LCDPort |= command & 15;                    //Send 4 ls bits
    updateLCD();
    LCDPort &= ~15;                             //Clear data lines

}

void sendData(unsigned char character) {

    checkBusy();
    LCDPort &= ~1 << readWrite;                 //Set R/W low and RS high (write data)
    LCDPort |= 1 << RS;
    LCDPort |= (character >> 4 & 15);           //Send 4 ms bits
    updateLCD();
    LCDPort &= ~1 << readWrite;
    LCDPort &= ~15;
    LCDPort |= 1 << RS;
    LCDPort |= (character & 15);                //Send 4 ls bits
    updateLCD();
    LCDPort &= ~15;                             //Clear data lines
}

void sendInitCommand(unsigned char command) {

    LCDPort &= ~(1 << readWrite | 1 << RS);     //Set R/W and RS low (write command)
    LCDPort |= command & 15;                    
    updateLCD();
    LCDPort &= ~15;                             //Clear data lines
}
4

1 回答 1

2

似乎这是一个兼容 HD44780 的控制器。

请注意,每个 4x20 显示器实际上是一个 2x40,在中间被切割。这可以通过一个显示控制器来实现,但显示需要多个驱动程序。看看这个: HD44780

尽管如此,您仍可以在 4 位模式下连接显示器。

这是一个如何在位模式下连接 4x20 的示例(请注意,缺少 Pin15 (LED+) 和 Pin16 (LED-),这只是背光引脚)。不要忘记将 Pin5 (R/W) 连接到 GND。

HD44780 4 位模式

Peter Fleury 编写了一个非常好的连接 HD44780 4x20 的库:http: //homepage.hispeed.ch/peterfleury/avr-software.html#libs 在“基于 HD44870 的 LCD 的 LCD 库”部分中尝试一下

于 2013-01-05T01:19:02.540 回答