GLCD with PIC18F and 16F

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

GLCD with PIC18F and 16F

Post by chrischristian » Thu Jul 28, 2016 3:02 am

Hi, I have managed to get the GLCD working with PIC 16F877A and now trying to migrate the code on 18F45k80, but the LCD just lights up, I can adjust the contrast but nothing else happens. All I have changed is data port to PORTB from PORTD on 16F and Control Port from PORTB on 16F to PORTD on 18F. The library I am using is from http://en.radzio.dxp.pl/ks0108/

Everything works smoothly on PIC16F and 18F is just blank screen. Compiler is XC8 using MPLABx. Any help please ?

P.S. 18F is on a PCB so can't really change much in the circuit. Circuit is not missing any connection, I have checked it four times. The circuit is similar to Pong clock project on SparFun GLCD page.

My Config bits are :

Code: Select all

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF      // Extended Instruction Set (Disabled)

// CONFIG1H
#pragma config FOSC = HS1       // Oscillator (HS oscillator (Medium power, 4 MHz - 16 MHz))
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = ON       // Brown Out Detect (Controlled with SBOREN bit)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTC    // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)
FOR 16F the config bits are :

Code: Select all

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS 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 = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#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)


Thanks !

User avatar
phalanx
Non-SFE Guru
Posts: 1972
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: GLCD with PIC18F and 16F

Post by phalanx » Thu Jul 28, 2016 5:46 am

Can you post the section of your code for both the 16F and 18F where you initialize all of your I/O pins and detail all the signal connections of the LCD? I want to know specifically what pins on the PIC18 each LCD signal is connected to. My first suspicion is the analog functionality of the PIC is overriding the digital I/O you require. On the 16F, you disable the analog functionality by writing certain bits in ADCON1 where on the 18F you need to write values to ANCON0 and ANCON1.

-Bill

chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

Re: GLCD with PIC18F and 16F

Post by chrischristian » Thu Jul 28, 2016 11:06 pm

Hi phalanx,
This is the port setup code in application

Code: Select all

// Timer 0 Setup //
    ///////////////////
    TMR0 = 0;
    T0CONbits.PSA = 0;
    T0CONbits.T0PS = 0b111;
    T0CONbits.T0CS = 0;
    /*
    OPTION_REGbits.PSA = 0; //Prescaler assigned to Timer 0 (other option is to
                            //the Watchdog timer (WDT))

    OPTION_REGbits.PS = 0b111;  //Set the prescaler to 1:256
    OPTION_REGbits.T0CS = 0;    //Use the instruction clock (Fcy/4) as the timer
                                //clock. Other option is an external oscillator
                                //or clock on the T0CKI pin
     */
    INTCONbits.T0IF = 0; // make sure interrupt flag is 0 at startup
    INTCONbits.T0IE =1 ; // enable interrupt

    
    
    PSPCONbits.PSPMODE = 0; // Set PORTE to GPIO mode
   // TRISEbits.PSPMODE =0; // Set portE to Digital I/O
    TRISEbits.TRISE0=1; // E0 is input
    TRISEbits.TRISE1=1; // E1 is input
    TRISEbits.TRISE2=1; // E2 is input
    /*
    ADCON1bits.PCFG0 = 1; // inputs are digital
    ADCON1bits.PCFG1 =1;
    ADCON1bits.PCFG2 =1;
    ADCON1bits.PCFG3 =1;
I have also tried these bit settings at startup

Code: Select all

    PSPCONbits.PSPMODE = 0;
    INTCON2bits.RBPU =1;
    PADCFG1bits.RDPU =0;
    INTCONbits.RBIE =0 ;
    ADCON0 = 0;
    ADCON1 = 0 
but no luck

This is the PORT definition in GLCD lib

Code: Select all

#define KS0108_DATA_PORT	PORTB
#define KS0108_DATA_DIR		TRISB
#define KS0108_DATA_PIN		PORTB

#define KS0108_CTRL_PORT	PORTD
#define KS0108_CTRL_DIR		TRISD

#define KS0108_RS			(1 << 3)
#define KS0108_RW			(1 << 5)
#define KS0108_EN			(1 << 2)

#define KS0108_CS1			(1 << 1)
#define KS0108_CS2			(1 << 0)
#define KS0108_CS3			(1 << 1)
This is the port definition in GLCD lib for 16F

Code: Select all

#define KS0108_DATA_PORT	PORTD
#define KS0108_DATA_DIR		TRISD
#define KS0108_DATA_PIN		PORTD

#define KS0108_CTRL_PORT	PORTB
#define KS0108_CTRL_DIR		TRISB

#define KS0108_RS			(1 << 3)
#define KS0108_RW			(1 << 5)
#define KS0108_EN			(1 << 2)

#define KS0108_CS1			(1 << 1)
#define KS0108_CS2			(1 << 0)
#define KS0108_CS3			(1 << 1)

This is the PORT init routine in GLCDlib

Code: Select all

void GLCD_InitializePorts(void)
{
KS0108_CTRL_PORT |= (KS0108_CS1 | KS0108_CS2 | KS0108_CS3);
KS0108_CTRL_DIR &= ~(KS0108_CS1 | KS0108_CS2 | KS0108_CS3 | KS0108_RS | KS0108_RW | KS0108_EN);
KS0108_CTRL_DIR = 0x00;
KS0108_CTRL_PORT |= (KS0108_CS1 | KS0108_CS2 | KS0108_CS3);
}
Thanks for looking into this, I am very much confused at this, I know in the end it will turn out to be something simple cause the code really works perfectly fine with 16F.

P.S. I made all the pins of PORTB and PORTD HIGH to see if they work as GPIO and all seems to be ok . I have attached the Schematic.

Thanks,
Chris C
Attachments
Schematic.png

chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

Re: GLCD with PIC18F and 16F

Post by chrischristian » Sat Jul 30, 2016 1:36 am

Just an update, I have tried changing to "read from port and write to latch"

Code: Select all

#define KS0108_DATA_PORT	LATB
#define KS0108_DATA_DIR		TRISB
#define KS0108_DATA_PIN		PORTB

#define KS0108_CTRL_PORT	LATD
#define KS0108_CTRL_DIR		TRISD

#define KS0108_RS			(1 << 3)
#define KS0108_RW			(1 << 5)
#define KS0108_EN			(1 << 2)

#define KS0108_CS1			(1 << 1)
#define KS0108_CS2			(1 << 0)
#define KS0108_CS3			(1 << 1)
#define KS0108_RST          (1 << 4)
and adding these at the begining of main

Code: Select all

    PSPCONbits.PSPMODE = 0;
    INTCON2bits.RBPU =1;
    PADCFG1bits.RDPU =0;
    INTCONbits.RBIE =0 ;
    ADCON0 = 0;
    ADCON1 = 0;
    CCP1CONbits.CCP1M = 0x00;
    CCP5CONbits.CCP5M = 0x00;
but still no joy :(

chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

Re: GLCD with PIC18F and 16F

Post by chrischristian » Sat Jul 30, 2016 5:17 am

I did some basic troubleshooting, made a simple code to check the PORT values and it is completely unexpected, I am not setting the PORT functions/configs correctly. Please see the attached image. I was giving +5V to each pins of PORTB and PORTD but as it shows in the image, they remained 0x00. Also when I set the PORTS to output and make LATB and LATD 0xFF, the PORT values are 0x2C and something else not 0XFF.

I think I need to get by basics right, can anyone tell me how to correctly set PORT directions and functions for GPIO with PIC18F45K80 ?
Attachments
debug.png

chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

Re: GLCD with PIC18F and 16F

Post by chrischristian » Sat Jul 30, 2016 9:46 pm

Ok, So I managed to get the PORTS working , as Input and Output,but still no luck with GLCD I am attaching the "helloworld" of my GLCD project here. Can anyone guide me on this please. ? The device is PIC18F45K80.

Thanks,
Chris
Attachments
GLCD_Test.X.zip
(347.01 KiB) Downloaded 62 times

User avatar
phalanx
Non-SFE Guru
Posts: 1972
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: GLCD with PIC18F and 16F

Post by phalanx » Tue Aug 02, 2016 10:00 am

I'm ultra busy at work so I haven't had the time to look at your issue in more depth. If you can bear with me, I'll try and find some time over the next few days to take a better look.

-Bill

chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

Re: GLCD with PIC18F and 16F

Post by chrischristian » Sat Aug 06, 2016 7:29 pm

I even tried changing the LCD, but no joy, so its not a hardware fault :(

chrischristian
Posts: 8
Joined: Sat Feb 27, 2010 12:55 am

Re: GLCD with PIC18F and 16F

Post by chrischristian » Sun Aug 07, 2016 2:55 am

So Finally it is solved :D , I was trying to put RESET pin to HIGH through a pull up, and the RESET was also connected to a GPIO (which I was pulling HIGH in firmware, but for some reason it wasn't going HIGH before init routine of LCD), so out of frustration I decided to pull RESET HIGH without pull up and, it did the trick. I knew it must be something stupid. !!!!. After 8 years of experience with electronics/software, it still continues to give me curve balls. !!
Thanks everybody for trying to help !!

Post Reply