简体   繁体   中英

LCD follows commands but doesn't display characters

I have programmed an Atmega-16 Microcontroller to interface a LM016L LCD display. The program is as follows:

#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;
}

When I send commands to the LCD like making the cursor to blink, it works perfectly fine. But when the function Send_A_Character() is called to write the characters to the screen, it doesn't work. I cannot make LCD to display anything on the screen. There is no syntax error on the above code. If anyone of you could point out a logical error that is preventing LCD to display anything on the screen, that would be great. I've even tried increasing the delays at each points to 2 seconds but that didn't work.

I spent hours on this code and couldn't make it work. However I managed to write stuff on my LCD as follows:

  1. You need to connect RW of the LCD to GND of the MCu
  2. Modify other connections according to code as well.
  3. Here's the code that worked for me: ( 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; }

If the LCD executes the commands correctly and fails to display characters then you need to check two things: Connection of the RS pin. Recall that for commands, the RS should be low meaning that even with a bad connection commands will still run. You need to confirm that the RS is indeed a high and is connected correctly. I had the same issue and it took me a day to realize that indeed i had interchanged the RS and RW connections to the LCD. The data pins are correct so long as they send the commands to the LCD. Hope that helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM