LCD dem16217 syh-ly and PIC18f45k22 problems

Discussion in 'Embedded Systems and Microcontrollers' started by A.Zanev, Mar 17, 2015.

  1. A.Zanev

    Thread Starter New Member

    Feb 10, 2015
    6
    0
    hello everyone i have a problem with running the LCD.. I have been banging my head for a long time and i will need some help :/ .. i will post my Header, code and library
    ------------------------------------------------------HEADER ----------------------------------------------------------------------
    Code (Text):
    1. /*
    2. * File:   Header.h
    3. * Author: knn
    4. *
    5. * Created on 11. februar 2015, 22:07
    6. */
    7.  
    8. #ifndef HEADER_H
    9. #define    HEADER_H
    10.  
    11. #ifdef    __cplusplus
    12. extern "C" {
    13. #endif
    14.  
    15.  
    16.  
    17.  
    18. #ifdef    __cplusplus
    19. }
    20. #endif
    21.  
    22. #endif    /* HEADER_H */
    23.  
    24. // PIC18F45K22 Configuration Bit Settings
    25.  
    26. // 'C' source line config statements
    27.  
    28.  
    29.  
    30. // #pragma config statements should precede project file includes.
    31. // Use project enums instead of #define for ON and OFF.
    32.  
    33. // CONFIG1H
    34. #pragma config FOSC = INTIO7       // INTIO7  works  XT    is giving problems....Oscillator Selection bits (XT oscillator)
    35. #pragma config PLLCFG = ON      // 4X PLL Enable (Oscillator multiplied by 4)
    36. #pragma config PRICLKEN = ON    // Primary clock enable bit (Primary clock is always enabled)
    37. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    38. #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    39.  
    40. // CONFIG2L
    41. #pragma config PWRTEN = OFF     // Power-up Timer Enable bit (Power up timer disabled)
    42. #pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
    43. #pragma config BORV = 190       // Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
    44.  
    45. // CONFIG2H
    46. #pragma config WDTEN = OFF      // Watchdog Timer Enable bits (Watch dog timer is always disabled. SWDTEN has no effect.)
    47. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    48.  
    49. // CONFIG3H
    50. #pragma config CCP2MX = PORTC1  // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    51. #pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
    52. #pragma config CCP3MX = PORTB5  // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
    53. #pragma config HFOFST = ON      // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)
    54. #pragma config T3CMX = PORTC0   // Timer3 Clock input mux bit (T3CKI is on RC0)
    55. #pragma config P2BMX = PORTD2   // ECCP2 B output mux bit (P2B is on RD2)
    56. #pragma config MCLRE = INTMCLR  // Was EXTMCLR  MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
    57.  
    58. // CONFIG4L
    59. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    60. #pragma config LVP = OFF         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
    61. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    62.  
    63. // CONFIG5L
    64. #pragma config CP0 = OFF        // Code Protection Block 0 (Block 0 (000800-001FFFh) not code-protected)
    65. #pragma config CP1 = OFF        // Code Protection Block 1 (Block 1 (002000-003FFFh) not code-protected)
    66. #pragma config CP2 = OFF        // Code Protection Block 2 (Block 2 (004000-005FFFh) not code-protected)
    67. #pragma config CP3 = OFF        // Code Protection Block 3 (Block 3 (006000-007FFFh) not code-protected)
    68.  
    69. // CONFIG5H
    70. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
    71. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
    72.  
    73. // CONFIG6L
    74. #pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
    75. #pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
    76. #pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
    77. #pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)
    78.  
    79. // CONFIG6H
    80. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
    81. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    82. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    83.  
    84. // CONFIG7L
    85. #pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
    86. #pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
    87. #pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
    88. #pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
    89.  
    90. // CONFIG7H
    91. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
    92.  

    ----------------------------------------------- MAIN -----------------------------------------
    Code (Text):
    1. ///*
    2. // * File:   main.c
    3. // * Author: Platinum
    4. // *
    5. // * Created on March 17, 2015, 10:41 AM
    6. // */
    7.  
    8.  
    9. #include "Header.h"
    10. #include <xlcd.h>
    11. #include <stdio.h>
    12. #include <stdlib.h>
    13. #include <delays.h>
    14. #include <xc.h>
    15. #define _XTAL_FREQ  8000000
    16.  
    17. void init_XLCD(void);              //Initialize LCD display
    18. void DelayFor18TCY(void) {                             // dalay for 20 TCY
    19.     //Delay10TCYx(2);
    20.     __delay_us(39);
    21. }
    22.  
    23. void DelayPORXLCD(){                                  // delay for 15 ms
    24.      __delay_ms(15);//
    25. }
    26.  
    27. void DelayXLCD(){                                     // delay for 5 ms
    28.     __delay_ms(5);//
    29. }
    30.  
    31. void main(void)
    32. {
    33.  
    34.    init_XLCD();                    //Call the Initialize LCD display function
    35.  
    36.  
    37.  
    38.     while(1)
    39.     {
    40.         putrsXLCD("Hello World");          //Display "Hello World"
    41.     SetDDRamAddr(0x40);            //shift cursor to beginning of second line
    42.     putrsXLCD("LCD Display");      //Display "LCD display"
    43.     }
    44. }
    45.  
    46. void init_XLCD(void)                //Initialize LCD display
    47. {   PORTBbits.RB4=1;
    48.   PORTBbits.RB6=1;
    49.     OpenXLCD(FOUR_BIT&LINES_5X7);  //configure LCD in 4-bit Data Interface mode
    50.                                      //and 5x7 characters, multiple line display
    51.     while(BusyXLCD());             //Check if the LCD controller is not busy
    52.                                      //before writing some commands?
    53.     WriteCmdXLCD(0x06);            // move cursor right, don?t shift display
    54.     WriteCmdXLCD(0x0C);            //turn display on without cursor
    55.  
    56. }
    57.  
    ----------------------------------------------- XLCD.H (LIBRARY) ----------------------------------

    Code (Text):
    1. #ifndef __XLCD_H
    2. #define __XLCD_H
    3. #include "p18cxxx.h"
    4. /* PIC18 XLCD peripheral routines.
    5. *
    6. *   Notes:
    7. *      - These libraries routines are written to support the
    8. *        Hitachi HD44780 LCD controller.
    9. *      - The user must define the following items:
    10. *          - The LCD interface type (4- or 8-bits)
    11. *          - If 4-bit mode
    12. *              - whether using the upper or lower nibble
    13. *          - The data port
    14. *              - The tris register for data port
    15. *              - The control signal ports and pins
    16. *              - The control signal port tris and pins
    17. *          - The user must provide three delay routines:
    18. *              - DelayFor18TCY() provides a 18 Tcy delay
    19. *              - DelayPORXLCD() provides at least 15ms delay
    20. *              - DelayXLCD() provides at least 5ms delay
    21. */
    22.  
    23. /* Interface type 8-bit or 4-bit
    24. * For 8-bit operation uncomment the #define BIT8
    25. */
    26. /* #define BIT8 */
    27.  
    28. /* When in 4-bit interface define if the data is in the upper
    29. * or lower nibble.  For lower nibble, comment the #define UPPER
    30. */
    31. /* #define UPPER */
    32.  
    33. /* DATA_PORT defines the port to which the LCD data lines are connected */
    34. #define DATA_PORT              PORTB
    35. #define TRIS_DATA_PORT         TRISB
    36.  
    37. /* CTRL_PORT defines the port where the control lines are connected.
    38. * These are just samples, change to match your application.
    39. */
    40. #define RW_PIN   LATBbits.LATB5           /* PORT for RW */
    41. #define TRIS_RW  TRISBbits.TRISB5        /* TRIS for RW */
    42.  
    43. #define RS_PIN   LATBbits.LATB4           /* PORT for RS */
    44. #define TRIS_RS  TRISBbits.TRISB4        /* TRIS for RS */
    45.  
    46. #define E_PIN    LATBbits.LATB6          /* PORT for D  */
    47. #define TRIS_E   TRISBbits.TRISB6        /* TRIS for E  */
    48.  
    49. /* Display ON/OFF Control defines */
    50. #define DON         0b00001111  /* Display on      */
    51. #define DOFF        0b00001011  /* Display off     */
    52. #define CURSOR_ON   0b00001111  /* Cursor on       */
    53. #define CURSOR_OFF  0b00001101  /* Cursor off      */
    54. #define BLINK_ON    0b00001111  /* Cursor Blink    */
    55. #define BLINK_OFF   0b00001110  /* Cursor No Blink */
    56.  
    57. /* Cursor or Display Shift defines */
    58. #define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
    59. #define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
    60. #define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
    61. #define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */
    62.  
    63. /* Function Set defines */
    64. #define FOUR_BIT   0b00101100  /* 4-bit Interface               */
    65. #define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
    66. #define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
    67. #define LINE_5X10  0b00110100  /* 5x10 characters               */
    68. #define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */
    69.  
    70. #ifdef _OMNI_CODE_
    71. #define PARAM_SCLASS
    72. #else
    73. #define PARAM_SCLASS auto
    74. #endif
    75.  
    76. #ifndef MEM_MODEL
    77. #ifdef _OMNI_CODE_
    78. #define MEM_MODEL
    79. #else
    80. #define MEM_MODEL far  /* Change this to near for small memory model */
    81. #endif
    82. #endif
    83.  
    84. /* OpenXLCD
    85. * Configures I/O pins for external LCD
    86. */
    87. void OpenXLCD(PARAM_SCLASS unsigned char);
    88.  
    89. /* SetCGRamAddr
    90. * Sets the character generator address
    91. */
    92. void SetCGRamAddr(PARAM_SCLASS unsigned char);
    93.  
    94. /* SetDDRamAddr
    95. * Sets the display data address
    96. */
    97. void SetDDRamAddr(PARAM_SCLASS unsigned char);
    98.  
    99. /* BusyXLCD
    100. * Returns the busy status of the LCD
    101. */
    102. unsigned char BusyXLCD(void);
    103.  
    104. /* ReadAddrXLCD
    105. * Reads the current address
    106. */
    107. unsigned char ReadAddrXLCD(void);
    108.  
    109. /* ReadDataXLCD
    110. * Reads a byte of data
    111. */
    112. char ReadDataXLCD(void);
    113.  
    114. /* WriteCmdXLCD
    115. * Writes a command to the LCD
    116. */
    117. void WriteCmdXLCD(PARAM_SCLASS unsigned char);
    118.  
    119. /* WriteDataXLCD
    120. * Writes a data byte to the LCD
    121. */
    122. void WriteDataXLCD(PARAM_SCLASS char);
    123.  
    124. /* putcXLCD
    125. * A putc is a write
    126. */
    127. #define putcXLCD WriteDataXLCD
    128.  
    129. /* putsXLCD
    130. * Writes a string of characters to the LCD
    131. */
    132. void putsXLCD(PARAM_SCLASS char *);
    133.  
    134. /* putrsXLCD
    135. * Writes a string of characters in ROM to the LCD
    136. */
    137. void putrsXLCD(const char *);
    138.  
    139. /* User defines these routines according to the oscillator frequency */
    140. extern void DelayFor18TCY(void);
    141. extern void DelayPORXLCD(void);
    142. extern void DelayXLCD(void);
    143.  
    144. #endif
    145.  
    please if you have any ideas share them! Thanks in advance. I will be following the thread so i can answer any question you might have for me!
    -------------------------------------------------------------------------------------------------------------------
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,507
    2,367
    Picmicro have a LCD routine that comes with the Picdem2 plus demo board that works with the 18f24k22, the programs should be available for download on the picmicro site where the board details are posted.
    Max.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I've been using the XLCD "library" for years now with good results. I say "library" as it the verion I downloaded included some very platform specific items, like that "DelayFor18TCY" function: 18 cycles was only correct for that PIC running at that speed.

    One huge thing I notice too is the init_XLCD does not include the power on delay: the LCD needs some time (check the data sheet!) after power is applied before it can work.
     
Loading...