1

I have written the following code to communicate with an LCD display yet I can not get data to appear on the screen. All I get is white boxes on the display. I am properly controlling the RW, RS and Enable pins to my knowledge. I am using an ATmega2560 and an 1602A v2 LCD. And yes you can program the Atmega2560 in the atmel studio environment. Any advice from anyone???

Please help me here, I am desperate to learn what I am doing wrong. Thank you for everything everyone.

And for the oddly formatted code:

#include <avr/io.h>
#include <stdio.h>
#define F_CPU 16000000L
#include <util/delay.h>
#include "serial.c" //this allows me to use a serial monitor

//Pre-defining Commands
#define LCDClear 0x01 //Clears LCD Display,this is a command
#define LCD8bit 0x38 //Sets up LCD in 8bit mode
#define LCDSeek00 0x80 //Seeks the pointer to the beginning

#define LCD_E  PK1    // PORTK.1
#define LCD_RS PK3    //, RS for text
#define LCD_RW PK2   // PORTK.2, RW  is for commands
// Data and Busy flag are on P.....

void character(unsigned char ch);
void command(unsigned char ch);
void LCDInitialize(void);

int main()
{ DDRA=0xff; //the 8 data pins, setting pin a for output
    DDRK=0xff;  //this port used for Enable, RW, and RS
    PORTK=0xff;
    init_UART();//enables the USB's serial feedback from trouble shooting purposes

    printf("hello");

    _delay_ms(2000);
    LCDInitialize();
    printf("initialized");
    command(0x01);
    character("C");
    character("i");
    character("r");
    character("c");
    character("u");
    character("i");
    character("t");
    character("s");
    character(" ");
    character("T");
    character("o");
    character("d");
    character("a");
    character("y");
    return 0;
}
void character(unsigned char letter)
{ _delay_ms(20);
PORTK &=~(1<<LCD_RW);
PORTK |= ( 1<<LCD_RS|1<<LCD_E); //both RS and Enable must be high to 

send data, enable will need to be taken low  however
DDRA=0xff;
PORTA=letter;
_delay_ms(20);
PORTK &=~(1<<LCD_E);//takes enable low
DDRA = 0;   // release bus

}
void command(unsigned char ch)
{ 
    PORTK &= ~(1<<LCD_RS);  //RS low for Command
    PORTK |= (1<<LCD_E);    //Enable pin high
    DDRA=0xff;
    PORTA = ch;         // put data on Port
    asm volatile ("nop");
    asm volatile ("nop");
    asm volatile ("nop");
    asm volatile ("nop");
    PORTK &= ~(1<<LCD_E);   //Enable Pin Low
    DDRA = 0;                // release bus

}
void LCDInitialize(void)
{ 

command(LCD8bit);
        _delay_ms(1);

command(0b00001111);//I believe this makes cursor blink, but didn't work for me

command(LCDClear);
    command(LCDSeek00);
}
  • 1
    Yes. Copy and paste the code, formatting it properly, rather than posting images of it. – Ignacio Vazquez-Abrams Apr 30 '15 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.