#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 1000000
#include <xc.h>
#define LCD_PORTB PORTB
#define RS RB0
#define EN RB1
void Strobe (void)
{
EN = 1;
__delay_us(1);
EN = 0;
}
void LCD_cmd( unsigned char cmd)
{
RS = 0;
__delay_ms(1);
LCD_PORTB = (cmd & 0xf0) ;
Strobe();
LCD_PORTB = (cmd << 4 );
Strobe();
}
void LCD_data( unsigned char data)
{
RS = 1;
__delay_ms(1);
LCD_PORTB |= (data & 0xf0);
Strobe();
LCD_PORTB |= (data << 4 );
Strobe();
}
void LCD_init()
{
__delay_ms(20);
LCD_cmd(0x38);
__delay_ms(4);
LCD_cmd(0x38);
__delay_ms(10);
// LCD_cmd(0x38); // Power on Initilization
LCD_cmd(0x28); /* 4bit mode */
LCD_cmd(0x28); /* Initialization of 16X2 LCD in 4bit mode */
LCD_cmd(0x0C); /* Display ON Cursor OFF */
LCD_cmd(0x06); /* Auto Increment cursor */
}
void string(const char *ptr)
{
while (*ptr)
{
LCD_data(*ptr++);
}
}
void main()
{
TRISB =0;
TRISD =0;
LCD_init();
LCD_cmd(0x80);
//string("HELLO WORLD");
// LINE2;
string("IT IS WORKING:-)");
while(1);
return ;
}
Since in the actual code - right after both PORTB data loads - there is a Strobe() command handling the EN control pin, at the end seems like being not an issue at all, once data is submitted to the LCD just after issued this command.There is still a potential problem with writing to PORTB. When you use:
you reset the lower four bits of PORTB which drives EN and RS low.Code:PORTB = (cmd & 0xf0) ; PORTB = (cmd << 4 )&0xf0;
Notice that the first commands are send as 4-bit instructions, with one rather than two strobes.
Code C - [expand] 1 2 3 4 5 6 7 8 void LCD_data( unsigned char data) { PORTB = 0x00; PORTB = (data & 0xf0) | 1; Strobe(); PORTB = (data << 4 ) | 1; Strobe(); }
I changed as said in data sheet(Below code ) but not working .I think where will be the problem . I want to rectify it .Didn't you note you are issuing an 8bit mode configuration command to the LCD whereas the actual connections are for 4 bits only ?
Code C - [expand] 1 LCD_cmd(0x38);
void LCD_init()
{
__delay_ms(40);
LCD_cmd(0x30);
__delay_ms(5);
LCD_cmd(0x30);
__delay_us(100);
LCD_cmd(0x30); /* 4bit mode */
LCD_cmd(0x28);
LCD_cmd(0x01);
LCD_cmd(0x06);
}
Code C - [expand] 1 LCD_cmd(0x30); /* 4bit mode */
Code C - [expand] 1 LCD_cmd(0x28);
My i get a sample code with the use of a variable to remove RMW error.As you are using the simulator, then you might be fighting a bug in the simulator code. That is why I always recommend using real hardware.
Also that old chip doesn't have LAT registers so you may encounter RMW issues when you move to the real hardware. This has been (sort of) touched on elsewhere in this thread about handling the 'E' and 'R/W' signals in the same port as the 4 data lines.
I would suggest that you create an 8-bit variable within your code where you can set and clear the bits as you need and then write the whole value to the PORT register.
Susan
// control bit placements in low half of PORTB
#define RS 0
#define EN 2
// generic write to PORTB with strobe
void LCD_Write(char data)
{
unsigned char temp = data;
PORTB = temp;
temp |= (1 << EN);
PORTB = temp;
__delay(1);
temp &= ~(1 << EN);
PORTB = temp;
__delay(1);
}
// write 8 data bits to LCD in 4-bit mode
void LCD_Data(char data)
{
LCD_Write((data & 0xF0) | (1 << RS));
LCD_Write((data << 4) | (1 << RS));
}
// write a 4 bit command to the LCD
void LCD_Cmd4(char cmd)
{
LCD_Write((cmd << 4) &= ~(1<< RS));
}
// write a 8 bit command to the LCD
void LCD_Cmd8(char cmd)
{
LCD_Write((cmd &= 0xF0) &= ~(1 << RS));
LCD_Write((cmd << 4) &= ~(1 << RS);
}
void LCD_Cmd8(char cmd)
{
LCD_Write((cmd &= 0xF0) &= ~(1 << RS));
LCD_Write((cmd << 4) &= ~(1 << RS);
}
LCD_Write((cmd & 0xF0) | ~(1<<RS));
LCD_Write((cmd << 4) | ~(1<<RS));
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?