Ranbeer Singh
Full Member level 5
- Joined
- Jul 30, 2015
- Messages
- 259
- Helped
- 22
- Reputation
- 44
- Reaction score
- 22
- Trophy points
- 1,298
- Location
- Faridabad India
- Activity points
- 3,266
#include <p18f2520.h>
#define RS_PIN PORTCbits.RC2 /* PORT for RS */
#define E_PIN PORTCbits.RC3 /* PORT for E */
//--------------------------------------------------------------------------
void LCD_Init(void);
void Str_LCD( const rom char *buffer);
void Chr_LCD(char data);
void Clear_LCD(void);
void Move_Corsor( char Line, char Pos);
//--------------------------------------------------------------------------
void Start_Display(void);
void Time_10ms(void);
void main()
{
TRISA=0xFF;
TRISC=0x00;
PORTC=0x00;
TRISB=0x01;
PORTB=0x00;
T0CON=0x06;
LCD_Init();
while(1)
{
Start_Display();
}
}
void Start_Display()
{
Str_LCD("SPEED- ");
Str_LCD("0000");
Str_LCD(" RPM");
Move_Corsor(2,0);
Str_LCD("Current- ");
Str_LCD("2.2");
Str_LCD(" A");
}
//---------------------------------------------------------------------------------------
void LCD_Init()
{
RS_PIN=0; //LCD instructions (RS) pin no. 4
Chr_LCD(0x28); //LCD clear
Chr_LCD(0x01); //LCD 4 bit, 2line, 5x7
Chr_LCD(0x0C); //LCD diplay-on cursor not blinking
Chr_LCD(0x80); //LCD cursor Pos. begain
RS_PIN=1; //LCD Data (RS)
}
void Str_LCD( const rom char *buffer)
{
while(*buffer) // Write data to LCD up to null
{
Chr_LCD(*buffer); // Write character to LCD
buffer++; // Increment buffer
}
return;
}
void Chr_LCD(char data)
{
TRISC &= 0x0F;
PORTC &= 0x0F;
PORTC |= data&0xF0;
Time_10ms();
Time_10ms();
Time_10ms();
E_PIN=1;
Time_10ms();
Time_10ms();
Time_10ms();
E_PIN=0;
PORTC &= 0x0F;
PORTC |= ((data<<4)&0xF0);
Time_10ms();
Time_10ms();
Time_10ms();
E_PIN=1;
Time_10ms();
Time_10ms();
Time_10ms();
E_PIN=0;
TRISC |= 0xF0;
}
void Clear_LCD(void)
{
RS_PIN=0;
Chr_LCD(0x01);
Chr_LCD(0x80);
RS_PIN=1;
}
void Move_Corsor( char Line, char Pos)
{
RS_PIN=0;
if(Line==1)Chr_LCD(0x80);
if(Line==2)Chr_LCD(0xC0);
while(Pos!=0)
{
Chr_LCD(0x14);
Pos--;
}
RS_PIN=1;
}
//---------------------------------------------------------------------------------------
void Time_10ms()
{
TMR0H=0xFE;
TMR0L=0x7A;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
void Lcd_Write_Char(char a)
{
char temp,y;
temp = a&0x0F;
y = a&0xF0;
RS = 1; // => RS = 1
Lcd_Port(y>>4); //Data transfer
EN = 1;
__delay_us(40);
EN = 0;
Lcd_Port(temp);
EN = 1;
__delay_us(40);
EN = 0;
}
This is a shift right. So the upper nibble become the lower nibble.Lcd_Port(y>>4); //Data transfer
I don't know, but it surely is in the display's datasheet.What nibble should be send first?
char up, down;
up = data & 0xF0;
down = data & 0x0F;
PORTC = up<<4; // up lower nibble send to PORTC upper nibble
EN = 1;
Delay();
EN = 0;
PORTC = down<<4; // Down lower nibble send to PORTC upper nibble
EN = 1;
Delay();
EN = 0;
Sorry...I was writing Is it right?It is write?
PORTC = up; // send to PORTC upper nibble
EN = 1;
Delay();
EN = 0;
PORTC = (down << 4) & 0XF0; // Down lower nibble send to PORTC upper nibble
EN = 1;
Delay();
EN = 0;
Imagine the data byte is 0x69.up = data & 0xF0;
down = data & 0x0F;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 void TxChLCD(char word, char i) //and looked like this { RS=i ; // i=1 wrt data word , i=0 wrt instruction word PORTB=PORTB & 0b00001111; // clear data bus to lcd PORTB=PORTB | (word & 0b11110000); // put high nibble first on the data bus EN=1; _delay(2);EN=0; // tell the lcd to take in the data present on the bus PORTB=PORTB & 0b00001111; // clear data bus to lcd PORTB=PORTB | (word<<4); // put the lower nibble on the bus EN=1; _delay(2);EN=0;// tell the lcd to take in the data present on the bus _delay(140); }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 void TxChLCD(char word, char i) //and looked like this { RS=i ; // i=1 wrt data word , i=0 wrt instruction word PORTB=PORTB & 0b11110000; // clear data bus to lcd PORTB=PORTB | (word >>4); // put high nibble first on the data bus EN=1; _delay(2);EN=0; // tell the lcd to take in the data present on the bus PORTB=PORTB & 0b11110000; // clear data bus to lcd PORTB=PORTB | (word & 0b1111000); // put the lower nibble on the bus EN=1; _delay(2);EN=0;// tell the lcd to take in the data present on the bus _delay(140); }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 void Chr_LCD(char data) { TRISC &= 0x0F; PORTC &= 0x0F; PORTC |= data&0xF0; Time_10ms(); Time_10ms(); Time_10ms(); E_PIN=1; Time_10ms(); Time_10ms(); Time_10ms(); E_PIN=0; PORTC &= 0x0F; PORTC |= ((data<<4)&0xF0); Time_10ms(); Time_10ms(); Time_10ms(); E_PIN=1; Time_10ms(); Time_10ms(); Time_10ms(); E_PIN=0; TRISC |= 0xF0; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 void Chr_LCD(char data) { TRISC &= 0x0F; // this is what you do first after cpu setup so would not use it here PORTC &= 0x0F; PORTC |= (data&0xF0); _delay (1); // ok let's say i'd put here 1 us delay , thus not needed at all E_PIN=1; _delay(3); // 3 us is more then enough for the lcd to reed the bus E_PIN=0; PORTC &= 0x0F; PORTC |= (data<<4); // when you shift 4 positions to the left the lower part fills with 0 by default // x x x x 0 0 0 0 E_PIN=1; _delay(3); E_PIN=0; // why to make the pins input by TRISC |= 0xF0; are you going to disconnect the lcd or what ? }
There are several code examples in this thread that solve this problem, e.g. the OR operation in post #1 and post #13. Using this method you shouldn't have a problem of overwriting port bits inadvertantly.
Most probably this is an 8 bit access, so you can´t send only 4 bits.You have to send only 4 bits to the port
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?