skbohra
Newbie level 6
- Joined
- Oct 28, 2012
- Messages
- 14
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,366
I am trying to interface a 16x2 lcd with PIC24F16KA102, without much success. Seems like I am missing something in the code. Here's some code I got from a thread here and modified to work with my muc,
Any help is appreciated.
Code:
#include "p24Fxxxx.h"
#include <stdlib.h>
#include <stdio.h>
#include "delay.h"
// LCD display ports etc
#define LCDdata LATB // data port
#define LCDdataEnable TRISB
#define RS LATAbits.LATA2 // RS bit in LCDstatus
#define RW LATAbits.LATA1 // read/write bit in LCDstatus
#define E LATAbits.LATA0 // enable bit in LCDstatus
#define Eenable TRISAbits.TRISA0
#define ERSenable TRISAbits.TRISA2
#define RWenable TRISAbits.TRISA1
void lcd_delay() { delay_ms(8); } // if LCD does not work make this longer
// Write a nibble to the LCD
// may have to adjust delays to suit the processor and clock
void lcdNibble(int n)
{
int lcd=LCDdata;
lcd_delay();
LCDdata=n;
lcd_delay();
E=1; // take clock E high
lcd_delay();
E=0;
lcd_delay();
}
// Write a Control Command to the LCD
// This is written as two nibbles
void lcdCmd(int c)
{
RS=0; // Take RS pin low for command
lcdNibble(c); // Makeup Lower Nibble
}
// write a data byte to LCD
int lcdPutchar(int d)
{
// printf("%c", d);
RS=1; // Take RS pin high for data
lcdNibble(d); // Makeup Lower Nibble
PORTBbits.RB0 = 0;
return 1;
}
// Initialise the LCD in 4bit Mode
void lcdInit()
{
E=0; // take E low
RS=0; // Take RS pin low for command
RW=0; // Take RS pin low for command
// set RS, RW and E bits as output
Eenable =0;
ERSenable =0;
RWenable =0;
lcdNibble(0x3); // This put the LCD into Soft Reset
lcdNibble(0x3);
lcdNibble(0x3);
lcdNibble(0x2);
lcd_delay();
lcdCmd(0x28); // 2 line, 4 bit mode
// lcdCmd(0x38); // 2 line, 8 bit mode
lcd_delay();
lcdCmd(0x06); // increment cursor after each write
lcd_delay();
lcdCmd(0x01); // clear display
lcd_delay();
lcdCmd(0x02); // home
lcd_delay();
lcdCmd(0x0F); // turn disply on
lcd_delay();
}
_FWDT (FWDTEN_OFF);
_FOSCSEL(FNOSC_FRCPLL);
_FOSC ( POSCFREQ_HS & POSCMOD_HS & OSCIOFNC_OFF );
int main(void){
OSCCON = 0x0000;
CLKDIV = 0x0000;
TRISBbits.TRISB0 =0;
PORTBbits.RB0 = 1;
lcdInit();
lcdPutchar("s");
while(1);
return 0;
}
Any help is appreciated.