Pic18f4550 LCD 4x20 not working

Thread Starter

Geid D

Joined Jun 28, 2015
36
did that, still no symbol on screen...
can anyone confirm that my lcd init is correct?

Code:
#include <xc.h>

#define LCD_RS LATBbits.LATB0
#define LCD_EN LATBbits.LATB2

#define LCD_D4 LATBbits.LATB3
#define LCD_D5 LATBbits.LATB5
#define LCD_D6 LATBbits.LATB6
#define LCD_D7 LATBbits.LATB7

#define LCD_RS_DIR TRISBbits.TRISB0
#define LCD_EN_DIR TRISBbits.TRISB2

#define LCD_D4_DIR TRISBbits.TRISB3
#define LCD_D5_DIR TRISBbits.TRISB5
#define LCD_D6_DIR TRISBbits.TRISB6
#define LCD_D7_DIR TRISBbits.TRISB7

void LCD_Strobe(void);

void main(void) {
   
__delay_ms(200);
TRISA = 0x00;
LATAbits.LATA5 = 0; // led off

LCD_RS_DIR = 0;           
LCD_EN_DIR = 0; 
LCD_D4_DIR = 0; 
LCD_D5_DIR = 0; 
LCD_D6_DIR = 0; 
LCD_D7_DIR = 0;
  
LCD_RS = 0;           
LCD_EN = 0; 
LCD_D4 = 0; 
LCD_D5 = 0; 
LCD_D6 = 0; 
LCD_D7 = 0; 
__delay_ms(30);
// Wake up call 1
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Wake up call 2
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Wake up call 3
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Function set 4 bit
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 0; LCD_Strobe();

// Function set 4 bit / 2 line - 5x10 font
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();

// Display on curson on blink on
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Clear Display
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 1; LCD_Strobe();

// Entry mode set
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe(); // last 2 ?

// goto line 2
LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();

// write "W character"

LCD_RS = 1; 

LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 1; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

LCD_RS = 0;

// end of lcd test


LATAbits.LATA5 = 1; // led on

while (1) { // loop
}
   
}

void LCD_Strobe(void) { 
    LCD_EN = 1;                                             
    __delay_ms(100); // __delay_us(120);
    LCD_EN = 0;     
}
 

MrChips

Joined Oct 2, 2009
23,245
I have said it once before.

Write the simplest code to blink an LED.
Then add the simplest code to display a single character.
If the LCD library does not read back the BUSY status then I see no reason why the code should hang.

I never use libraries to interface to LCD since the code to send characters to the LCD is so simple.

As @AlbertHall points out, it is imperative that you make sure that the contrast setting is set properly otherwise you are working in the dark.
 

Thread Starter

Geid D

Joined Jun 28, 2015
36
I have said it once before.

Write the simplest code to blink an LED.
Then add the simplest code to display a single character.
If the LCD library does not read back the BUSY status then I see no reason why the code should hang.

I never use libraries to interface to LCD since the code to send characters to the LCD is so simple.

As @AlbertHall points out, it is imperative that you make sure that the contrast setting is set properly otherwise you are working in the dark.
the code i posted above your post... its simpliest routine. how can i simplify it more? :)
led blinking is no problem here :)
 

jayanthd

Joined Jul 4, 2015
906
This is totally wrong.

Code:
[LIST=1]
[*]#define _XTAL_FREQ 48000000
[*]// CONFIG1L
[*]#pragma config PLLDIV = 5       // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
[*]#pragma config CPUDIV = OSC1_PLL2                                                                                                                                                                                                                                                                                                         // System Clock Postscaler Selection bits ([Primary Oscillator Src: /2][96 MHz PLL Src: /3])
[*]#pragma config USBDIV = 2       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
[*]

[*]// CONFIG1H
[*]#pragma config FOSC = HSPLL_HS  // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
[*]#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
[*]#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
[/LIST]
Refer my code for correct values.


This is the correct settings for a 20.0 MHz Crystal which gives 20 MHz / 5 = 4 MHz input to USB PLL which then gives 96 MHz / 2 = 48 MHz USB Clock and 20 MHz Primary Clock / 1 = 20 MHz directly drives the system.

The Oscillator section diagram in the datasheet explains this clearly.

Code:
[LIST=1]
[*]#define _XTAL_FREQ 20000000UL
[*]

[*]// PIC18F4550 Configuration Bit Settings
[*]

[*]// 'C' source line config statements
[*]

[*]// CONFIG1L
[*]#pragma config PLLDIV = 5       // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
[*]#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
[*]#pragma config USBDIV = 2       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
[*]

[*]// CONFIG1H
[*]#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator (HS))
[*]#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
[*]#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
[*]
[/LIST]


Code:
#pragma config PLLDIV = 5       // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
20 MHz / 5 = 4 MHz for USB PLL input.

Code:
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator (HS))
Primary Oscillator drives the system directly and so it is 20 MHz only and HS.
You don't use PLL for system clock and hence no HS_PLL.

Code:
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
Primary Oscillator 20 MHz / 1 = 20 MHz system clock

20 MHz / 5 = 4 MHz input into USB PLL circuit which then gives 96 MHz an dthis is divided by 2 to get 48 MHz USB Clock.

96 MHz PLL Src: /2
 
Last edited:

Thread Starter

Geid D

Joined Jun 28, 2015
36
changed now the values you posted, still my code dont show symbol "W" on line 2 as expecting...
C:
#define _XTAL_FREQ 20000000UL
// CONFIG1L
#pragma config PLLDIV = 5       // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
#pragma config CPUDIV = OSC1_PLL2                                                                                                                                                                                                                                                                                                         // System Clock Postscaler Selection bits ([Primary Oscillator Src: /2][96 MHz PLL Src: /3])
#pragma config USBDIV = 2       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)

// CONFIG1H
#pragma config FOSC = HS  // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
#pragma config FCMEN = ON      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define LCD_RS LATBbits.LATB0
#define LCD_EN LATBbits.LATB2

#define LCD_D4 LATBbits.LATB3
#define LCD_D5 LATBbits.LATB5
#define LCD_D6 LATBbits.LATB6
#define LCD_D7 LATBbits.LATB7

#define LCD_RS_DIR TRISBbits.TRISB0
#define LCD_EN_DIR TRISBbits.TRISB2

#define LCD_D4_DIR TRISBbits.TRISB3
#define LCD_D5_DIR TRISBbits.TRISB5
#define LCD_D6_DIR TRISBbits.TRISB6
#define LCD_D7_DIR TRISBbits.TRISB7

void LCD_Strobe(void);

void main(void) {
 
__delay_ms(200);
TRISA = 0x00;
LATAbits.LATA5 = 0; // led off

LCD_RS_DIR = 0;         
LCD_EN_DIR = 0;
LCD_D4_DIR = 0;
LCD_D5_DIR = 0;
LCD_D6_DIR = 0;
LCD_D7_DIR = 0;

LCD_RS = 0;         
LCD_EN = 0;
LCD_D4 = 0;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
__delay_ms(30);
// Wake up call 1
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Wake up call 2
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Wake up call 3
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Function set 4 bit
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 0; LCD_Strobe();

// Function set 4 bit / 2 line - 5x10 font
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 1; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();

// Display on curson on blink on
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

// Clear Display
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 1; LCD_Strobe();

// Entry mode set
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe(); // last 2 ?

// goto line 2
LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_Strobe();

// write "W character"

LCD_RS = 1;

LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 1; LCD_Strobe();
LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 1; LCD_D7 = 1; LCD_Strobe();

LCD_RS = 0;

// end of lcd test


LATAbits.LATA5 = 1; // led on

while (1) { // loop
}
 
}

void LCD_Strobe(void) {
    LCD_EN = 1;                                           
    __delay_ms(100); // __delay_us(120);
    LCD_EN = 0;   
}
Mod edit: code tag qualifier=C
 
Last edited by a moderator:

bertus

Joined Apr 5, 2008
21,319
Hello,

It looks like the original strobe of 120 uS has been commented out and replace by the 100 mS delay:

__delay_ms(100); // __delay_us(120);

Bertus
 

jayanthd

Joined Jul 4, 2015
906
In #65 code, ADCON1 settings of 0x0F to disable ADC on all PORTs are missing. Don't know how LED pin which also has analog function is working when ADCON1 is not configured to make the LED and LCD pins as digital output pins.
 

Thread Starter

Geid D

Joined Jun 28, 2015
36
Hello,

It looks like the original strobe of 120 uS has been commented out and replace by the 100 mS delay:

__delay_ms(100); // __delay_us(120);

Bertus
this was fast way to ensure that delays requarements in all steps will be fullfiled :) can big delays create lcd mulfunctions?

In #65 code, ADCON1 settings of 0x0F to disable ADC on all PORTs are missing. Don't know how LED pin which also has analog function is working when ADCON1 is not configured to make the LED and LCD pins as digital output pins.
will test code with this line soon, thanks...
 

JohnInTX

Joined Jun 26, 2012
4,382
I am not sure how you arrived at the code in #65 vs some of the earlier postings but you do not have the required delays between the various init operations - 'wake up call', function set etc. See pp. 46 of the attached spec. For example, after line 107 (wake up call 1) you need to wait more than 4.1ms before continuing. You need to wait various times after each subsequent init operation. If you're running fast, consider the processing times on pp24 or be prepared to read the busy flag.

EDIT: Lots of outfits make 44780 controller clones. Sometimes the timings vary. Check the datasheet for your particular manufacturer.

Good luck!
 

Attachments

Thread Starter

Geid D

Joined Jun 28, 2015
36
hello again :) with this code LCD works, it displays letter "K", but this letter is generated by these temporary lines:

int c='K';
LCD_Chr(1,1,c);

but if i uncomment line below, code dont work on hardawe (lcd dont work, led dont work) as i described in the first post of this thread.
//LCD_Out(2,1,"Test");

and as i mentioned erlier i noticed that if i use data pointers (char*) somehow nothing works.

so no LCD problem, no delay problems, no PortB problems... code compiles without errors... any ideas? :)

Code:
#define _XTAL_FREQ 48000000
// CONFIG1L
#pragma config PLLDIV = 5       // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
#pragma config CPUDIV = OSC1_PLL2                                                                                                                                                                                                                                                                                                         // System Clock Postscaler Selection bits ([Primary Oscillator Src: /2][96 MHz PLL Src: /3])
#pragma config USBDIV = 2       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)

// CONFIG1H
#pragma config FOSC = HSPLL_HS  // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


#include <xc.h>

#define _LCD_FIRST_ROW          0x80     //Move cursor to the 1st row
#define _LCD_SECOND_ROW         0xC0     //Move cursor to the 2nd row
#define _LCD_THIRD_ROW          0x94     //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW         0xD4     //Move cursor to the 4th row
#define _LCD_CLEAR              0x01     //Clear display
#define _LCD_RETURN_HOME        0x02     //Return cursor to home position, returns a shifted display to its original position Display data RAM is unaffected.
#define _LCD_CURSOR_OFF         0x0C     //Turn off cursor
#define _LCD_UNDERLINE_ON       0x0E     //Underline cursor on
#define _LCD_BLINK_CURSOR_ON    0x0F     //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT   0x10     //Move cursor left without changing display data RAM
#define _LCD_MOVE_CURSOR_RIGHT  0x14     //Move cursor right without changing display data RAM
#define _LCD_TURN_ON            0x0C     //Turn Lcd display on
#define _LCD_TURN_OFF           0x08     //Turn Lcd display off
#define _LCD_SHIFT_LEFT         0x18     //Shift display left without changing display data RAM
#define _LCD_SHIFT_RIGHT        0x1E     //Shift display right without changing display data RAM
                                                                                
#define LCD_STROBE_DELAY 100

#define LCD_RS LATBbits.LATB0
#define LCD_EN LATBbits.LATB2
#define LCD_D4 LATBbits.LATB3
#define LCD_D5 LATBbits.LATB5
#define LCD_D6 LATBbits.LATB6
#define LCD_D7 LATBbits.LATB7

#define LCD_RS_Direction TRISBbits.TRISB0
#define LCD_EN_Direction TRISBbits.TRISB2
#define LCD_D4_Direction TRISBbits.TRISB3
#define LCD_D5_Direction TRISBbits.TRISB5
#define LCD_D6_Direction TRISBbits.TRISB6
#define LCD_D7_Direction TRISBbits.TRISB7
                                           
void LCD_Strobe(void);                                           
void LCD_Send_Data(char out_char);                                                                                
void LCD_Cmd(char out_char);
void LCD_Chr(char row, char column,unsigned char out_char);
void LCD_Chr_Cp(char out_char);
void LCD_Init(void);
void LCD_Out(char row, char col,const char *text);
void LCD_Out_Cp(const char *text);


void main(void) 

{
   
ADCON1 = 0x0F;
   
ADCON0bits.ADON = 0;  
 
ADCON1bits.PCFG0 = 1;
ADCON1bits.PCFG1 = 1;
ADCON1bits.PCFG2 = 1;
ADCON1bits.PCFG3 = 1;

TRISA = 0x00;
TRISB = 0x00;
TRISD = 0x00;

LCD_RS_Direction = 0;            
LCD_EN_Direction = 0;   
LCD_D4_Direction = 0;   
LCD_D5_Direction = 0;   
LCD_D6_Direction = 0;   
LCD_D7_Direction = 0;
   
LCD_RS = 0;            
LCD_EN = 0;   
LCD_D4 = 0;   
LCD_D5 = 0;   
LCD_D6 = 0;   
LCD_D7 = 0;   

LATAbits.LATA5 = 1; // led on

LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
   
//LCD_Out(2,1,"Test");

int c='K';
LCD_Chr(1,1,c);


while(1) {
    
    // blink led
    LATAbits.LATA5 = 0;
    __delay_ms(1000);
    LATAbits.LATA5 = 1;
    __delay_ms(1000);
   
                                                                     
}

}

// FUNCTIONS

void LCD_Strobe(void) { 
    LCD_EN = 1;                                             
    __delay_us(100); // 100
    LCD_EN = 0;
   
}
void LCD_Send_Data(char out_char) {
    if(out_char & 0x10) LCD_D4 = 1; else LCD_D4 = 0;
    if(out_char & 0x20) LCD_D5 = 1; else LCD_D5 = 0;
    if(out_char & 0x40) LCD_D6 = 1; else LCD_D6 = 0;
    if(out_char & 0x80) LCD_D7 = 1; else LCD_D7 = 0;
    LCD_Strobe();
    if(out_char & 0x01) LCD_D4 = 1; else LCD_D4 = 0;
    if(out_char & 0x02) LCD_D5 = 1; else LCD_D5 = 0;
    if(out_char & 0x04) LCD_D6 = 1; else LCD_D6 = 0;
    if(out_char & 0x08) LCD_D7 = 1; else LCD_D7 = 0;
    LCD_Strobe();
}
void LCD_Cmd(char out_char) {
    LCD_RS = 0;
    LCD_Send_Data(out_char);
  
    if(out_char == 0x01)__delay_ms(2);
}
void LCD_Chr(char row, char column,unsigned char out_char) {
    switch(row) {
        case 1:
        LCD_Cmd(0x80 + (column - 1));
        break;
        case 2:
        LCD_Cmd(0xC0 + (column - 1));
        break;
        case 3:
        LCD_Cmd(0x94 + (column - 1));
        break;
        case 4:
        LCD_Cmd(0xD4 + (column - 1));
        break;
    }
    LCD_RS = 1;
    LCD_Send_Data(out_char);         
}
void LCD_Chr_Cp(char out_char) {
    LCD_RS = 1;                     
    LCD_Send_Data(out_char);     
}
void LCD_Init(void) { 
    __delay_ms(200);
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
    LCD_Strobe();
    __delay_ms(30);
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
    LCD_Strobe();
    __delay_ms(30);
    LCD_D4 = 1;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
    LCD_Strobe();
    __delay_ms(30);
    LCD_D4 = 0;
    LCD_D5 = 1;
    LCD_D6 = 0;
    LCD_D7 = 0;
    LCD_Strobe();
    __delay_ms(30);
    LCD_Cmd(0x28);
    LCD_Cmd(0x06);
}
void LCD_Out(char row, char col,const char *text) {
    while(*text)
         LCD_Chr(row, col++, *text++);
}
void LCD_Out_Cp(const char *text) {
    while(*text)
         LCD_Chr_Cp(*text++);
}
 

jayanthd

Joined Jul 4, 2015
906
Change this

Code:
void LCD_Out(char row, char col,const char *text) {
    while(*text)
         LCD_Chr(row, col++, *text++);
}
to

Code:
void LCD_Out(char row, char col, char *text) {
    while(*text)
         LCD_Chr(row, col++, *text++);
}
Let me know the results.
 

Thread Starter

Geid D

Joined Jun 28, 2015
36
now i get compiler warning on line LCD_Out(2,1,"Test");

Warning (359) illegal conversion between pointer types.

and nothing works.
 

jayanthd

Joined Jul 4, 2015
906
now i get compiler warning on line LCD_Out(2,1,"Test");

Warning (359) illegal conversion between pointer types.

and nothing works.
Revert.

Why these are again changed from my code?

Code:
#define _XTAL_FREQ 48000000
#pragma config FOSC = HSPLL_HS
I told you that they should be

Code:
#define _XTAL_FREQ 20000000
#pragma config FOSC = HS
 

Thread Starter

Geid D

Joined Jun 28, 2015
36
you version:

#define _XTAL_FREQ 20000000
#pragma config FOSC = HS

gives me wrong delays, now led blinks twice faster (half a second) with __delay_ms(1000).
and LCD_Out(2,1,"Test"); still dont work.

i dont think that problem is in Oscilator area...
 

jayanthd

Joined Jul 4, 2015
906
you version:

#define _XTAL_FREQ 20000000
#pragma config FOSC = HS

gives me wrong delays, now led blinks twice faster (half a second) with __delay_ms(1000).
and LCD_Out(2,1,"Test"); still dont work.

i dont think that problem is in Oscilator area...
What is your Crystal frequency?
 

JohnInTX

Joined Jun 26, 2012
4,382
you version:

#define _XTAL_FREQ 20000000
#pragma config FOSC = HS

gives me wrong delays, now led blinks twice faster (half a second) with __delay_ms(1000).
and LCD_Out(2,1,"Test"); still dont work.

i dont think that problem is in Oscilator area...
As a reminder, the XTAL_FREQ value doesn't control the oscillator, it just tells the compiler what effective clock rate you have set up so that it knows how to calculate the delay cycles.
 

jayanthd

Joined Jul 4, 2015
906
How are you testing the delays? Do you have a scope to test it or are you testing it in Proteus?

If you are testing it in Proteus then you might be using wrong oscillator value for Processor Clock frequency for PIC18F4550 model.

I have tested my code in Proteus and also hardware and they give correct delays and also the LCD works.

Try this code and see what it prints.

Code:
char i = 0;
for(i = 1; i < 21; i++)  {
LCD_Chr(1,i,'A');
LCD_Chr(2,i,'A');
LCD_Chr(3,i,'A');
LCD_Chr(4,i,'A');
}
 
Last edited:
Top