LCD遵循命令但不显示字符

时间:2015-01-05 13:31:17

标签: c microcontroller electronics lcd

我已经编程了一台Atmega-16微控制器来连接LM016L LCD显示器。该计划如下:

#include <avr/io.h>
#include <util/delay.h>
#define MrLCDsCrib PORTB
#define DataDir_MrLCDsCrib DDRB
#define MrLCDsControl PORTD
#define DataDir_MrLCDsControl DDRD
#define LightSwitch 5
#define ReadWrite 7
#define BiPolarMood 2

void Check_IF_MrLCD_isBusy(void);
void Peek_A_Boo(void);
void Send_A_Command(unsigned char command);
void Send_A_Character(unsigned char character);

int main(void) {
    DataDir_MrLCDsControl |= 1 << LightSwitch | 1 << ReadWrite | 1 << BiPolarMood;
    _delay_ms(15);

    Send_A_Command(0x01); //Clear Screen 
    _delay_ms(20);
    Send_A_Command(0x38); //8-bit mode
    _delay_ms(20);
    Send_A_Command(0b00001110); // cursor on
    _delay_ms(20);
    Send_A_Command(0x0F); // cursor blinking
    _delay_ms(20);

    Send_A_Character(0x4E); //N
    _delay_ms(20);
    Send_A_Character(0x65); //e
    _delay_ms(20);
    Send_A_Character(0x77); //w
    _delay_ms(20);


    while (1) {}
}

void Check_IF_MrLCD_isBusy() {
    DataDir_MrLCDsCrib = 0;
    MrLCDsControl |= 1 << ReadWrite;
    MrLCDsControl &= ~1 << BiPolarMood;

    while (MrLCDsCrib >= 0x80) {
        Peek_A_Boo();
    }
    DataDir_MrLCDsCrib = 0xFF;
}

//Peek_A_Boo() writes the data to or from the LCD that are waiting to be written 
void Peek_A_Boo() {
    MrLCDsControl |= 1 << LightSwitch;
    asm volatile("nop");
    asm volatile("nop");
    MrLCDsControl &= ~1 << LightSwitch;
}

void Send_A_Command(unsigned char command) {
    Check_IF_MrLCD_isBusy();
    MrLCDsCrib = command;
    MrLCDsControl &= ~ ((1 << ReadWrite) | (1 << BiPolarMood));
    Peek_A_Boo();
    MrLCDsCrib = 0;
}

void Send_A_Character(unsigned char character) {
    Check_IF_MrLCD_isBusy();
    MrLCDsCrib = character;
    MrLCDsControl &= ~ (1 << ReadWrite);
    MrLCDsControl |= 1 << BiPolarMood;
    Peek_A_Boo();
    MrLCDsCrib = 0;
}

当我向LCD发送命令,如使光标闪烁时,它完全正常。但是当调用函数Send_A_Character()将字符写入屏幕时,它不起作用。我不能让LCD在屏幕上显示任何内容。上面的代码没有语法错误。如果你们中的任何人都指出了一个阻止LCD在屏幕上显示任何内容的逻辑错误,那就太好了。我甚至尝试将每个点的延迟增加到2秒,但这不起作用。

2 个答案:

答案 0 :(得分:1)

我在这段代码上花费了数小时而无法使其正常工作。 但是我设法在我的液晶显示器上写了如下内容:

  1. 您需要将LCD的RW连接到MCu的GND
  2. 也根据代码修改其他连接。
  3. 以下代码对我有用: (http://circuitdigest.com/microcontroller-projects/lcd-interfacing-with-atmega32-avr

    // Code for LCD Interfacing with ATmega32 AVR microcontroller
    #include <avr/io.h>
    #define F_CPU 1000000UL
    #include <util/delay.h>
    #define RS 6
    #define E  5
    void send_a_command (unsigned char command);
    void send_a_character(unsigned char character);
    int main(void)
    {
        DDRA = 0xFF;
        DDRD = 0xFF;
        _delay_ms(50);
        send_a_command(0x01);// sending all clear command
        send_a_command(0x38);// 16*2 line LCD
        send_a_command(0x0E);// screen and cursor ON
    
         send_a_character (0x44); // ASCII(American Standard Code for Information Interchange) code for 'D'
         send_a_character (0x49); // ASCII(American Standard Code for Information Interchange) code for 'I'
         send_a_character (0x4C); // ASCII(American Standard Code for Information Interchange) code for 'L'
         send_a_character (0x49); // ASCII(American Standard Code for Information Interchange) code for 'I'
         send_a_character (0x50); // ASCII(American Standard Code for Information Interchange) code for 'P'
    
    }
    void send_a_command (unsigned char command)
    {
        PORTA=command;
        PORTD&= ~(1<<RS);
        PORTD|= (1<<E);
        _delay_ms(50);
        PORTD&= ~(1<<E);
        PORTA =0;
    }
    void send_a_character (unsigned char character)
    {
        PORTA=character;
        PORTD|= (1<<RS);
        PORTD|= (1<<E);
        _delay_ms(50);
        PORTD&= ~(1<<E);
        PORTA =0;
    }
    

答案 1 :(得分:0)

如果LCD正确执行命令并且无法显示字符,则需要检查两件事:连接RS引脚。回想一下,对于命令,RS应该是低的,这意味着即使连接错误,命令仍然会运行。您需要确认RS确实是高并且连接正确。我遇到了同样的问题,我花了一天的时间才意识到我确实已经将RS和RW的连接互换到LCD上。数据引脚是正确的,只要它们将命令发送到LCD即可。希望有所帮助:)