I am trying to interface 16x2 LCD display with atmega 32 in 4 bit mode and on display there's random character and black boxes. here below my code
//#define F_CPU 12000000UL #define F_CPU 11592000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include "lcd.h"
#define LCD_DATA PORTD // port D is selected as LCD data port
#define ctrl PORTB // port B is selected as LCD command port
#define LCD_DPRT PORTD // LCD DATA PORT
#define LCD_DDDR DDRD // LCD DATA DDR
#define LCD_DPIN PIND // LCD DATA PIN
#define LCD_CPRT PORTB // LCD COMMAND PORT
#define LCD_CDDR DDRB // LCD COMMAND DDR
#define LCD_CPIN PINB // LCD COMMAND PIN
#define LCD_EN PINB2 // enable signal is connected to port B pin 0, same for given PCB
#define LCD_RW PINB1 // read/write signal is connected to port B pin 1,-----------"----
#define LCD_RS PINB0 // register select signal is connected to port B pin 2, -----"-----
unsigned int adc_value; // Variable to hold ADC result
int main(void)
{
_delay_us(1);
_delay_us(1);
DDRD=0xFF; // Port D is set for output
DDRB=0xFF; // port b is set command port
_delay_us(20); //
//PORTA0=0x00;
/* Replace with your application code */
while (1)
{
//*****************LCD INITIALIZATION**********************//
lcd_initi();
lcd_gotoxy(0,1); // cursor location in 1st row
lcd_print("PCB NO:");
lcd_gotoxy(1,2); // cursor location in 2nd row
lcd_print("Verdict");
}
} // main function end
//***********************************LCD FUNCTION DEFINATION's***************************//
//***************************************LCD COMMAND*************************************//
void lcdCommand (unsigned char cmnd)
{
LCD_DPRT=cmnd; // Send command to data port a
LCD_CPRT |= (1<<LCD_RS); // Register select
LCD_CPRT &= ~(1<<LCD_RW); // Read Select
LCD_CPRT |= (1<<LCD_EN); // Enable
_delay_us(20); // Delay
LCD_CPRT &= ~ (1<<LCD_EN); // Enable
_delay_us(100);
}
//*********************DATA**********************//
void lcdData (unsigned char data )
{
LCD_DPRT= data; // send data to data port
LCD_CPRT |= (1<<LCD_RS);
LCD_CPRT &= ~(1<<LCD_RW);
LCD_CPRT |= (1<<LCD_EN);
_delay_us(1);
LCD_CPRT &= ~(1<<LCD_EN);
_delay_us(100);
}
//*************************Initialize*************************//
void lcd_initi ()
{
LCD_DDDR=0xFF;
LCD_CDDR = 0xFF;
LCD_CPRT &= ~ (1<<LCD_EN); // LCD_EN = 0 //
_delay_us(2000); // wait for initialize
lcdCommand (0x33); // send $ 33 for inti
lcdCommand (0x32); // display on , cursor on
lcdCommand (0x28); // 4 bit mode,D4-D7
lcdCommand (0x0e); // display on.cursor on
lcdCommand (0x01); // clear lcd
_delay_us(2000); // Delay
lcdCommand(0x06); //shift cursor right
}
//**********************print string***************************************************//
void lcd_print(char*str)
{
unsigned char i=0;
while (str [i] != 0)
{
lcdData (str[i]);
i++;
}
}
//************************************CURSOR SHUFFLING***************************************************//
void lcd_gotoxy (unsigned char x, unsigned char y )
{
unsigned char firstcharadr []={0x80,0xC0,0x94,0xD4};
lcdCommand (firstcharadr [y-1] + x-1 ) ;
_delay_us(1);
}
//******************************ADC FUNCTIONS DEFINITION **********************************//
void ADC_init()
{
//int adc_value;
ADMUX=(1<<REFS0); // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // Set by ADPS Prescalar div factor =128 and Enable ADC //
}
void ADC_read()
{
ADCSRA|=(1<<ADSC); // Start continuous conversion
while(!(ADCSRA & (1<<ADSC))); // wait for conversion to complete
adc_value = ADCW; // store adc value
}
//************************************ KEY BUTTON ********************************************//
void button ()
{
DDRA = DDRA & ~( 1<<6) ; //Make pin 6 of port A as a input
//DDRA = DDRA & ~(1<<7) ; // Make pin 5 of port C as a input
int button=0;
while (1) //initialize while loop
{
if(PINA |= (1<<6) ) //if PIN5 of port A6 is high
{
lcd_gotoxy(0,8);
lcd_print( button++);
}
else //otherwise
{
lcd_gotoxy(0,8);
lcd_print(button); // value of PIN7 of port 7 will remain same
}
} // while loop ends
} // end of void function