Help with LCD 2x16 and printing text

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Help with LCD 2x16 and printing text

#1 Post by chris11jed » 06 Nov 2022 04:38

Hello all,

I am having difficulty with printing text onto the 2x16 LCD (one taken from my old easyPICV7 dev-board) on a PCB I have made.
All the traces from the MCU to the LCD pins are correct, as per the mikroC PRO for PIC32 Help files for the LCD Library.
I would like to ask if anyone can help me get text to appear on the LCD please?

MCU:
  • PIC32MX695F512L
  • Primary OSC: 20MHz crystal resonator
  • Secondary OSC: 32.768kHz crystal resonator
  • Sys-Clk frequency: 100MHz
Power:
  1. MCU Power = +3V3
  2. LCD VCC = +5V
Software:
  • mikroC PRO for PIC32 v.4.0.0
Code:
Pin naming code;

Code: Select all

// ============================================================================= Pin Declarations Section
// ----------------------------------------------------------------------------- LCD Connections
sbit LCD_RS   at LATA0_bit;
sbit LCD_EN   at LATA1_bit;
sbit LCD_D4   at LATA4_bit;
sbit LCD_D5   at LATA5_bit;
sbit LCD_D6   at LATA6_bit;
sbit LCD_D7   at LATA7_bit;
sbit LCD_BKLT at LATD0_bit;

sbit LCD_RS_Direction   at TRISG14_bit;
sbit LCD_EN_Direction   at TRISG15_bit;
sbit LCD_D4_Direction   at TRISA4_bit;
sbit LCD_D5_Direction   at TRISA5_bit;
sbit LCD_D6_Direction   at TRISA6_bit;
sbit LCD_D7_Direction   at TRISA7_bit;
sbit LCD_BKLT_Direction at TRISD0_bit;
Variables Section, LCD specific code;

Code: Select all

// ============================================================================= Variables Section
// ----------------------------------------------------------------------------- LCD
             //0123456789012345
char txt1[] = "0123456789012345";
char txt2[] = "ABCDEFGHIJKLMNOP";
MCU Set-Up Section, LCD specific code:

Code: Select all

// ----------------------------------------------------------------------------- PIC Set-Up
void Init_PIC() {                                                               // Configure Registers, Ports/Pins here
  // --------------------------------------------------------------------------- Registers
  CHECON = 0x32;                                                                // Cache Control Register (Copied from LCD Library Help files)
  AD1PCFG = 0xFFFF;                                                             // Configure AN pins as digital I/O
  JTAGEN_bit = 0;                                                               // Disable JTAG
  PMAEN = 0;                                                                    // This and PMCON 'ON' bit must be cleared so as to make LATE usable.
  PMCON = 0;                                                                    // Disable Parallel Port Contol

  // --------------------------------------------------------------------------- Port pin directions
  // -- LCD --
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);
            //0123456789012345
  txt1[16] = "--- Welcome! ---";
  txt2[16] = "- Please Wait! -";
  Lcd_Out(1, 1, txt1);
  Lcd_Out(2, 1, txt2);
}
Main Loop Section, LCD specific and other code which works;

Code: Select all

// ============================================================================= Main Program Section
void main() {
  // --------------------------------------------------------------------------- Config area
  Init_PIC();                                                                   // Configure PIC
  Init_Vari();                                                                  // Configure variables
  LED_Test();                                                                   // Flash LEDs

  // --------------------------------------------------------------------------- Display program running message on LCD
  Lcd_Cmd(_LCD_CLEAR);
            //0123456789012345
  txt1[16] = "--All Complete--";
  txt2[16] = "-Ready to test!-";
  Lcd_Out(1, 1, txt1);
  Lcd_Out(2, 1, txt2);

  // --------------------------------------------------------------------------- Main Program Loop
  while (1) {                                                                    // Loop
    // ------------------------------------------------------------------------- Buttons for LCD
    // -- LCD INC --
    if (Button (&PORTA, 9, 1, 1)) {                                             // LCD INC pressed
       OS_LCD_INC = 1;
    }
    if (OS_LCD_INC && Button(&PORTA, 9, 1, 0)) {                                // LCD INC released
       BKLT_Current_Duty++;
       if (BKLT_Current_Duty >= 11) {
          BKLT_Current_Duty = 0;
       }
       Alter_LCD_Backlight(BKLT_Current_Duty);
       OS_LCD_INC = 0;
    }

    // -- LCD DEC --
    if (Button (&PORTA, 10, 1, 1)) {                                            // LCD DEC pressed
       OS_LCD_DEC = 1;
    }
    if (OS_LCD_DEC && Button(&PORTA, 10, 1, 0)) {                               // LCD DEC released
       if (BKLT_Current_Duty == 0) {
          BKLT_Current_Duty = 10;
       } else {
          BKLT_Current_Duty--;
       }
       Alter_LCD_Backlight(BKLT_Current_Duty);
       OS_LCD_DEC = 0;
    }
    
    // ------------------------------------------------------------------------- Buttons under Red LEDs
    // -- BTN A --
    if (Button (&PORTB, 12, 1, 1)) {                                            // BTN A pressed
       OS_BTN_A = 1;
    }
    if (OS_BTN_A && Button(&PORTB, 12, 1, 0)) {                                 // BTN A released
       RED_LED_A = ~RED_LED_A;
       RED_5A = RED_LED_A;
       OS_BTN_A = 0;
    }
    
    // -- BTN B --
    if (Button (&PORTB, 13, 1, 1)) {                                            // BTN B pressed
       OS_BTN_B = 1;
    }
    if (OS_BTN_B && Button(&PORTB, 13, 1, 0)) {                                 // BTN B released
       RED_LED_B = ~RED_LED_B;
       RED_5B = RED_LED_B;
       OS_BTN_B = 0;
    }
    
    // -- BTN C --
    if (Button (&PORTB, 14, 1, 1)) {                                            // BTN C pressed
       OS_BTN_C = 1;
    }
    if (OS_BTN_C && Button(&PORTB, 14, 1, 0)) {                                 // BTN C released
       RED_LED_C = ~RED_LED_C;
       RED_5C = RED_LED_C;
       OS_BTN_C = 0;
    }
    
    // -- BTN D --
    if (Button (&PORTB, 15, 1, 1)) {                                            // BTN D pressed
       OS_BTN_D = 1;
    }
    if (OS_BTN_D && Button(&PORTB, 15, 1, 0)) {                                 // BTN D released
       RED_LED_D = ~RED_LED_D;
       RED_5D = RED_LED_D;
       OS_BTN_D = 0;
    }
  }
}
PCB Background/Context:
  1. MCU controls several LEDs (successfully tested).
  2. LCD 2x16 display for general messaging (currently testing).
  3. Four ADC inputs. 10K trimpots (not yet tested).
  4. PS2 port for Keyboard (not yet tested).
  5. I2C will control five PCA9956B IC's. 1MHz clock speed required, which is why the MCU is run at 100MHz sysclock frequency (not yet tested).
  6. Provision for I2C to communicate between another of these PCB's (not yet tested).
LCD Code Test Results:
  1. Code compiles well to MCU.
  2. Code initialises registers and pins first. Within this, the "Welcome... please wait" message should display on the LCD after LCD initialisation. It does not display this.
  3. After MCU initialisation code and variable initialisation code, LEDs are tested (flash each colour) and this works.
  4. Then the LCD should clear, and a new message saying "All complete... ready to test" should appear. It does not appear.
  5. Within the main loop, especially the 'While-loop', the buttons that raise or lower the backlight of the LCD via PWM work, as does the four other buttons that turn on or off LEDs.
  6. The LCD will display all character spaces on both rows as 'block'-characters, or, each character is completely lit up. No text.
Thoughts:
  • LCD_RS equivalent pin is RA0, which is also 'TMS', which is JTAG Test Mode
  • LCD_EN equivalent pin is RA1, which is also 'TCK', which is JTAG Test Clock Input Pin
  • LCD_D4 equivalent pin is RA4, which is also 'TDI', which is JTAG Test Data Input Pin
  • LCD_D5 equivalent pin is RA5, which is also 'TDO', which is JTAG Test Data Output Pin
  • LCD_D6 equivalent pin is RA6, which is also 'TRCLK', which is Trace Clock
  • LCD_D7 equivalent pin is RA7, which is also 'TRD3', which is Trace Data Bits
I have the line of code in the MCU initialisation section;

Code: Select all

JTAGEN_bit = 0;
Is that enough to prevent the LATA pins from acting in their JTAG or Trace peripheral function? Or do I need some other code to do this?
Bonus question, I can't find "JTAGEN" bit in any register. Where exactly is it? Especially, which datasheet? As I have looked in a few now.
If there isn't any other code needed, to do with a register or at least stopping the JTAG and Trace functioning of LATA pins, then can anyone see why the LCD isn't displaying text, but block characters instead?

P.S:
  • I do not have a development board for the PIC32MX695F512L, as the one I want is out of stock.
  • Plus, the few PIC32MX695F512L chips I do have were a chance find that a friend-of-a-friend had.
  • After getting them, I really put this PCB together to cover some bases as a testing rig, so to speak.
  • It will eventually be used to do things, but it's all I have at the moment.
  • Therefore, I can't really test the LCD help file test code from LATB as it's connected to in the help file.
  • And again, if people need the entire code of the project thus far, ask, and I'll put it up.
Thank you for reading, and any help :)

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: Help with LCD 2x16 and printing text

#2 Post by chris11jed » 06 Nov 2022 06:55

Update:

I remembered I actually do have a way to test the mikroC PRO for PIC32 Help files 'LCD Library' example.

On a breadboard, with some LED's added for fun, I hooked up the same 2x16 LCD to the same pins as is shown in the help file example. Namely, all on LATB.

Here's my altered code, essentially the same as the help files, for this ugly hack breadboard test :lol:

Code: Select all

// LCD module connections
sbit LCD_RS at LATB2_bit;
sbit LCD_EN at LATB3_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "mikroElektronika";
char txt2[] = "LV32MX v6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";

char i;                              // Loop variable

void Move_Delay() {                  // Function used for text moving
  Delay_ms(750);                     // You can change the moving speed here
}

void main(){
  CHECON = 0x32;
  AD1PCFG = 0xFFFF;      // Configure AN pins as digital I/O
  JTAGEN_bit = 0;        // Disable JTAG
  PMAEN = 0;             // This and PMCON 'ON' bit must be cleared so as to make LATE usable.
  PMCON = 0;             // Disable Parallel Port Contol

  TRISA = 0;             // Initialize PORTA as output
  TRISB = 0;             // Initialize PORTB as output
  TRISC = 0;             // Initialize PORTC as output
  TRISD = 0;             // Initialize PORTD as output
  TRISE = 0;
  TRISF = 0;             // Initialize PORTE as output
  TRISG = 0;             // Initialize PORTG as output

  LATA = 0;              // Set PORTA to zero
  LATB = 0;              // Set PORTB to zero
  LATC = 0;              // Set PORTC to zero
  LATD = 0;              // Set PORTD to zero
  LATE = 0;
  LATF = 0;              // Set PORTE to zero
  LATG = 0;              // Set PORTG to zero

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,6,txt3);                 // Write text in first row

  Lcd_Out(2,6,txt4);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,5,txt2);                 // Write text in second row

  Delay_ms(2000);

  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while(1) {                         // Endless loop
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }

    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
    
    LATA0_bit = 1;
    LATB0_bit = 1;
    LATD0_bit = 1;
    LATE0_bit = 1;
    LATF0_bit = 1;
    LATG0_bit = 1;
    Delay_ms(1000);

    LATA0_bit = 0;
    LATB0_bit = 0;
    LATD0_bit = 0;
    LATE0_bit = 0;
    LATF0_bit = 0;
    LATG0_bit = 0;
    Delay_ms(1000);
  }
}
Results:

It works! The LED's flash on and off after the message on the LCD does its thing. :D

My questions now:
  1. Again, why isn't the text being displayed in my original project, on the LCD, when I use LATA?
  2. Because the pins on LATA I am using have other uses, namely, for JTAG and Trace, is this causing my issue?
  3. If so, how can I ensure those pins are I/O and not anything else?
  4. Also if so, which PDF can I read for more? Especially, which one shows the register where 'JTAGEN_bit' is located?
  5. If so, or if not, and still the main question, how can I get the LCD to display my text on the original pcb, and not just a 'block character' in each character slot on both rows?
Thank you for any help or pointers!

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: Help with LCD 2x16 and printing text

#3 Post by AntiMember » 06 Nov 2022 09:45

https://ww1.microchip.com/downloads/en/ ... 01156J.pdf
page 334, DDPCON bit 3.
page 342:
bit 3 JTAGEN: JTAG Port Enable bit
1 = Enable the JTAG port
0 = Disable the JTAG port

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: Help with LCD 2x16 and printing text

#4 Post by chris11jed » 07 Nov 2022 08:07

Hi AntiMember,

Thank you for pointing me in the right direction with the register, the link and page numbers to look at! :D

I noted on the diagram on page 343, Figure 29-2 "Programming, Debugging, and Trace Ports Block Diagram" that all I need is JTAGEN_bit disabled, and the DEBUG<1:0> bits set.

The window 'Edit Project' already has 'DEBUG<1:0>' as 11. And I already have JTAGEN_bit as 0. TROEN_bit and TDOEN_bit are already 0 by default, although I added in that code just in case.

Unfortunately, I still can't get any text on the LCD. :roll: So am still a bit unsure what could be going on here.

To anyone out there, has anyone managed to get the 2x16 LCD to work on LATA?

Again, using these pin configurations:

Code: Select all

// ----------------------------------------------------------------------------- LCD Connections
sbit LCD_RS   at LATA0_bit;
sbit LCD_EN   at LATA1_bit;
sbit LCD_D4   at LATA4_bit;
sbit LCD_D5   at LATA5_bit;
sbit LCD_D6   at LATA6_bit;
sbit LCD_D7   at LATA7_bit;

sbit LCD_RS_Direction   at TRISG14_bit;
sbit LCD_EN_Direction   at TRISG15_bit;
sbit LCD_D4_Direction   at TRISA4_bit;
sbit LCD_D5_Direction   at TRISA5_bit;
sbit LCD_D6_Direction   at TRISA6_bit;
sbit LCD_D7_Direction   at TRISA7_bit;
If so, I'd love to hear from you. Especially the how part of it :lol:

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: Help with LCD 2x16 and printing text

#5 Post by AntiMember » 07 Nov 2022 14:10

sbit LCD_RS at LATA0_bit;
sbit LCD_EN at LATA1_bit;
..................................
sbit LCD_RS_Direction at TRISG14_bit; ???
sbit LCD_EN_Direction at TRISG15_bit; ???

chris11jed
Posts: 156
Joined: 15 Jun 2011 06:37

Re: Help with LCD 2x16 and printing text

#6 Post by chris11jed » 08 Nov 2022 05:46

I am not sure how many times I looked through the code, and that blatant mistake was there! :roll: :lol:

Again, a big Thank You to you AntiMember!!

Just corrected the issue, and the text is displayed upon the LCD. Am shamefully embarrassed, but very happy, hahaha!

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: Help with LCD 2x16 and printing text

#7 Post by AntiMember » 08 Nov 2022 14:18

Good luck!

Post Reply

Return to “mikroC PRO for PIC32 General”