Brandon Strickland
Brandon Strickland

Reputation: 1

Initialising LCD display with PIC16F1829 in C?

I am trying to initialise my LCD screen using my PIC16F1829. I have followed the flow chart given with the LCD, but with no avail. Please see my code below and let me know if you know where I am going wrong? I will optimise it at a later date with send_command() functions, etc . . . At the moment I just want to make sure it will initialise.

//config bits that are part-specific for the PIC16F1829
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF);

/*Function Prototypes*/
void init_disp(void);

/*Define outputs*/
#define _XTAL_FREQ 500000
#define RS LATCbits.LATC0 
#define EN LATCbits.LATC1 
#define RW LATCbits.LATC2
#define DB1 LATCbits.LATC4
#define DB2 LATCbits.LATC3
#define DB3 LATCbits.LATC6
#define DB4 LATCbits.LATC7
#define TRIG ((EN=1),(EN=0))

void main(void) {

  TRISC=0; //set PORTC to output 
  EN = 0;
 __delay_ms(50); 

 init_disp();

}

void init_disp(void){

    //function set 1
    RW = 0;
    RS = 0;
    DB1 = 1; //Hello LCD
    DB2 = 1;
    DB3 = 0;
    DB4 = 0;
    TRIG;

    //function set 2
    __delay_ms(5);
    TRIG;

    //function set 3
    __delay_ms(2);
    TRIG;

    // 4bit mode
    __delay_ms(2);
    DB1 = 0; 
    DB2 = 1;
    DB3 = 0;
    DB4 = 0;
    TRIG;

    //Display lines and font MS Nibble
    __delay_ms(2);
    DB1 = 0; 
    DB2 = 1;
    DB3 = 0;
    DB4 = 0;
    TRIG;
    //Display lines and font LS Nibble
    DB1 = 0; 
    DB2 = 0;
    DB3 = 1;
    DB4 = 1;
    TRIG;

    //Display off MS Nibble
    __delay_ms(1);
    DB1 = 0; 
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;
   //Display off LS Nibble 
    DB1 = 0; 
    DB2 = 0;
    DB3 = 0;
    DB4 = 1;
    TRIG;

    //Display Clear MS Nibble
    __delay_ms(5);
    DB1 = 0;
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;
    //display clear LS Nibble
    DB1 = 1; 
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;

    //Entry Mode MS Nibble
    __delay_ms(1);
    DB1 = 0;
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;
    //entry mode LS Nibble
    DB1 = 0;
    DB2 = 1;
    DB3 = 1;
    DB4 = 0;
    TRIG;

}

Upvotes: 0

Views: 270

Answers (1)

Amol Saindane
Amol Saindane

Reputation: 1598

By default all pins of PIC micro controller are analog inputs. Here you are using PORTC as digital output. So you need to initialise PORTC as below

PORTC = 0x00;   // Init PORTC
LATC = 0x00;    // Data Latch
ANSELC = 0x00;  // This will enable PORTC as digital
TRISC = 0x00;   // Output Pins

I hope this works for you. You need to refer Datasheet of PIC16F1829.

Upvotes: 1

Related Questions