Initializing a CFAH0802D-YYH-JP on Atmega32

Status
Not open for further replies.

karpinsn

New member
Hi,

I am trying to initialize my CFAH08 LCD but after power up and initialization I get the top row filled with black boxes and thats it. I have the contrast hooked up to a 10K pot and have adjusted that with no success. I have checked all my wiring with a multimeter and it all checks out. My guess is that there is something wrong with my code, but I have been over it a dozen times. I will check my wiring with an Oscope tomorrow, but if that is not the case then I am at a loss. My code is as follows:

lcd.h
Code:
#ifndef LCD_H
#define LCD_H

#include <avr/io.h>
#include <inttypes.h>
#include "delay.h"

#define LCD_PORT	PORTC
#define LCD_DDR		DDRC

#define LCD_E		1
#define LCD_RS		2
#define LCD_RW		3

void lcd_init(void);
void lcd_port_init(void);
void lcd_cmd_write(uint8_t command);
void lcd_data_write(uint8_t command);
#endif
lcd.c
Code:
#include "lcd.h"

void lcd_init(void)
{
	lcd_port_init();

	delay_ms(50);

	LCD_PORT &= ~((1 << LCD_RS) & (1 << LCD_RW));
	//	Ensure that the address is set
	delay_us(50);

	//	Send 0x30 once
	LCD_PORT = (LCD_PORT & 0x0F) | (0x0b00110000 & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_ms(5);

	//	Send 0x30 twice
	LCD_PORT = (LCD_PORT & 0x0F) | (0x0b00110000 & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_ms(125);

	//	Send 0x30 three times
	LCD_PORT = (LCD_PORT & 0x0F) | (0x0b00110000 & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_ms(5);

	//	Put the LCD in 4 bit mode
	LCD_PORT = (LCD_PORT & 0x0F) | (0x0b00100000 & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_ms(5);


	lcd_cmd_write(0b00101000);
	lcd_cmd_write(0b00001000);
	lcd_cmd_write(0b00000001);
	lcd_cmd_write(0b00000110);

	lcd_cmd_write(0x0F);
	lcd_cmd_write(0x01);

	delay_ms(500);
}

void lcd_port_init(void)
{
	//	Set the control pins to be output
	LCD_DDR |= (1 << LCD_E) & (1 << LCD_RS) & (1 << LCD_RW);

	//	Set the data pins to be output
	LCD_DDR |= (1 << 4) & (1 << 5) & (1 << 6) & (1 << 7);
}

void lcd_cmd_write(uint8_t command)
{
	LCD_PORT &= ~((1 << LCD_RS) & (1 << LCD_RW));
	//	Ensure that the address is set
	delay_us(1);

	//	Send the high nibble of the command
	LCD_PORT = (LCD_PORT & 0x0F) | (command & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_us(1);

	//	Send the low nibble of the command
	LCD_PORT = (LCD_PORT & 0x0F) | ((command << 4) & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_us(1);
}

void lcd_data_write(uint8_t command)
{
	LCD_PORT |= (1 << LCD_RS);
	LCD_PORT &= ~(1 << LCD_RW);
	//	Ensure that the address is set
	delay_us(1);

	//	Send the high nibble of the command
	LCD_PORT = (LCD_PORT & 0x0F) | (command & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_us(1);

	//	Send the low nibble of the command
	LCD_PORT = (LCD_PORT & 0x0F) | ((command << 4) & 0xF0);
	delay_us(1);
	LCD_PORT |= (1 << LCD_E);
	delay_us(1);
	LCD_PORT &= ~(1 << LCD_E);
	delay_us(1);
}
driver.c
Code:
#include "lcd.h"

int main(void)
{
	lcd_init();
	lcd_data_write(0b01010111);

	while(1 == 1);
}
Any insight would be much appreciated.

- Nik
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.
 
Your code looks ok to me. I guess the oscope will really tell what is happening.

Do you have D0~D3 on the display open or grounded? Probably doesn't matter.
 

karpinsn

New member
I had the pins floating, but have since grounded them. This didn't seem to do anything so I will just have to wait till I can get into my schools lab for an Oscope.
 

karpinsn

New member
Well I finally figured it out. In my init function i had the typo of 0x0b00110000 instead of 0b00110000. The GCC compiler that I am using does not detect this as an error, and so it was sending the wrong signals. The other typo was ((1 << LCD_E) & (1 << LCD_RS) & (1 << LCD_RW)); instead of ((1 << LCD_E) | (1 << LCD_RS) | (1 << LCD_RW));.

I guess I shouldn't code so late at night.

Thanks for the help,

- Nik
 
Well I finally figured it out. In my init function i had the typo of 0x0b00110000 instead of 0b00110000. The GCC compiler that I am using does not detect this as an error, and so it was sending the wrong signals. The other typo was ((1 << LCD_E) & (1 << LCD_RS) & (1 << LCD_RW)); instead of ((1 << LCD_E) | (1 << LCD_RS) | (1 << LCD_RW));
Well, I missed those errors, so I guess I wasn't looking closely enough :(.
 
Status
Not open for further replies.
Top