-1

I have this code that I want to fiddle with, analyze an hopefully learn m ore about it. It is suppose to make Atmega32 operate an LCD display without the use of proper Libraries.

I'm new to this - I'm trying to figure out through which pins should be the controller connected with (i mean, 4 data pins, en and rs). And I need help with this.

Here comes the code:

#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/delay.h>

void wait_ms(int del) {
for (int i=0; i<del; i++) {
 _delay_ms(1);
  }
}

void LCD_command(int c) {

int d;
d=c;
PORTB = c >> 4; 
PORTD = 0x01;
wait_ms(1);
PORTD = 0x00;
wait_ms(1);
PORTB = d; 
PORTD = 0x01;
wait_ms(1);
PORTD = 0x00;
wait_ms(1);
}

void LCD_char(char c) {

char d;
d=c;
PORTB = (c & 0xf0) >> 4; 
PORTD = 0x03;
wait_ms(1);
PORTD = 0x02;
wait_ms(1);
PORTB = d & 0x0f; 
PORTD = 0x03;
wait_ms(1);
PORTD = 0x02;
wait_ms(1);
}

void LCD_text(char *s) {
while (*s) {
 LCD_char(*s);
 s++;
}

}


void LCD_IR(void) {
PORTD = 0x01;
wait_ms(1);
PORTD = 0x00;
wait_ms(1);
}

void LCD_init(void) {
 wait_ms(20);
 LCD_command(0x03);
 LCD_command(0x03);
 LCD_command(0x03);
 LCD_command(0x02);
 LCD_command(0x28);
 LCD_command(0x01);
 LCD_command(0x06);
 LCD_command(0x0f);

}


int main(void) {

DDRB = 0xFF;
DDRD = 0xFF;

LCD_init();
char s[10]; 

//LCD_char('t');
//char w = "zmienna";



LCD_text(s);



while (1);
}

Once I have this up and running I will most probably be able to figure out where to go with this. I just need this push.

Oh - I'm working with Kamami Board with the LCD already installed on it (just need to connect the pins). Hope you know what i'm talking about.

  • *How do I do this hardware stuff to make this code work?* is not a programming question - it's a hardware question. Software doesn't have pins to connect. Wrong site. – Ken White Feb 05 '16 at 21:01

1 Answers1

0

You didn't give enough information in your question to definitely help you. There are many kamani boards and several with LCD screens. I didn't see an AVR board with an LCD, though. Does this code work on such a board?

Anyway, we can see what this code does by looking at it.

PORTB seems to be the 4-bit data bus. PORTB = (c & 0xf0) >> 4; sends the high bits of c over pins PORTB0:3. Then later, PORTB = c & 0x0f; sends out the low bits.

PORTD pins 0 and 1 seem to be the clock and command bits. (Maybe they are labelled en and rs, but I doubt it.) PORTD0 is toggled between 0 and 1 at the correct times. PORTD1 is 0 for a command, and 1 for data.

You should find the datasheet for the LCD display you are using, and see if the it uses 4-bit data and clock and command pins. Read the number off the device and search on the Internet.

UncleO
  • 8,299
  • 21
  • 29