|
|
View previous topic :: View next topic |
Author |
Message |
indiansiva555@gmail.com
Joined: 23 Jun 2012 Posts: 22
|
4 bit LCD interfacing problem |
Posted: Sun Nov 25, 2012 3:15 am |
|
|
Code: |
#include <16F877A.h>
#device adc=8
#FUSES NOWDT
#FUSES HS
#FUSES NOPUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES NOBROWNOUT
#FUSES NOLVP
#FUSES NOCPD
#FUSES NOWRT
#use delay(clock=20000000)
#define LCD_ENABLE PIN_D2
#define LCD_RS PIN_D0
#define LCD_RW PIN_D1
#define LCD_DATA_PORTD
void lcd_init();
void lcd_cmd(unsigned char c);
void lcd_data(unsigned char d);
void lcd_cmd(unsigned char c)
{
output_high(LCD_RS);
output_low(LCD_RW);
delay_ms(100);
output_d(c>>4);
output_high(LCD_ENABLE);
delay_ms(100);
output_low(LCD_ENABLE);
output_d(c);
output_high(LCD_ENABLE);
delay_ms(100);
output_low(LCD_ENABLE);
}
void lcd_data(unsigned char d)
{
output_high(LCD_RS);
output_low(LCD_RW);
delay_ms(100);
output_d(d>>4);
output_high(LCD_ENABLE);
delay_ms(100);
output_low(LCD_ENABLE);
output_d(d);
output_high(LCD_ENABLE);
delay_ms(100);
output_low(LCD_ENABLE);
}
void lcd_init()
{
lcd_cmd(0x28);//4bit mode
delay_ms(200);
lcd_cmd(0x28);//4bit mode
delay_ms(200);
lcd_cmd(0x28);//4bit mode
delay_ms(200);
lcd_cmd(0x0F);//display On cursor blinking command
delay_ms(200);
lcd_cmd(0x01);//clear display
delay_us(200);
lcd_cmd(0x06);//auto increment with no shift
delay_us(200);
}
void main()
{
while(true)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();
lcd_init();
lcd_init();
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_data('E');
lcd_data('L');
lcd_data('I');
lcd_data('T');
lcd_data('E');
lcd_cmd(0xC0);
lcd_data('w');
lcd_data('e');
lcd_data('l');
lcd_data('c');
lcd_data('o');
lcd_data('m');
lcd_data('e');
}
}
|
This code has some problem. It does not display anything in the LCD.
The compiler shows no error. There is no problem with the circuit as I used the same and displayed ELITE WELCOME using Arduino kit.
Pls help me... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Sun Nov 25, 2012 3:30 am |
|
|
Far easier, just download and use the flex_lcd driver from the code forum. Change the pin definitions to match what you have (first few line), and you are done....
As written, your code is using all eight bits of port d, which is rather a waste.
It is also missing the critical delay at the start. Most LCD's will not accept data, for several hundred mSec after power on. Typically half a second.
Further comments:
setup_adc(ADC_CLOCK_DIV_2); //not legal at 20MHz.
setup_spi(SPI_SS_DISABLED); //Wrong. needs to be:
setup_spi(FALSE);
The 'ss disabled' one is a classic error from the wizard, and actually enables the SPI, with slave select turned off....
As a further comment, any function that accepts a single character, can be called with CCS, with a constant string. So:
lcd_data("ELITE");
will call lcd_data five times, with the five characters. Much neater....
Then, generally, code sequence should be:
Code: |
void main(void) {
//Initialisation of ports etc.
//initialise LCD once
while (TRUE) {
//main code
}
}
|
You don't want all your initialisation stuff repeated again and again in the loop. Only things that want to repeat....
Even if your LCD code worked, you probably wouldn't see anything, since you write the data to the LCD, and then immediately initialise it again, clearing the screen.....
Remember also it is the high four bits on the LCD that need to be connected for 4bit mode. Triple check this.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|