1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Problem interfaceing PIC18F2550 and KS0108 LCD

Discussion in 'Microcontrollers' started by hkBattousai, Nov 6, 2009.

  1. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    I've been trying to interface 18F2550 and a 128x64 pixel graphic LCD for weeks. I tried several libraries found on the web, but none of them made the LCD print a single pixel on the screen. I'm really stuck at the moment, and I need guidance and new ideas.

    The best library I found so far is the one I found in this forum. You can access the thread from here. For simplicity, I'm going to add source source code of the library below.

    I also checked the signal levels at both PIC and LCD pinouts by an oscilloscope, there are meaningful pulses, the system PIC is working for sure.

    The major parts I used are listed below:
    1. IDE: MATLAB 8.40 Academic
    2. Compiler: Microchip C18 3.34 Academic
    3. LCD: WG12864B, with KS0108 Controller (Detailed datasheet, Electrical specifications)
    4. PIC: PIC18F2550, with 20MHz crystal(Page, Datasheet)

    These are photos of my LCD:
    Front, Back

    And these are photos of my circuit:
    Front, Up



    Code:

    glcd.h
    Code (text):
    #include <p18f2550.h>

    #define GLCD_Data   PORTB
    #define b_GLCD_GCS1 LATCbits.LATC7
    #define b_GLCD_GCS2 LATCbits.LATC6
    #define b_GLCD_RS   LATCbits.LATC0
    #define b_GLCD_RW   LATCbits.LATC1
    #define b_GLCD_E    LATAbits.LATA5
    #define b_GLCD_On   LATCbits.LATC2
    #define b_GLCD_BL   LATAbits.LATA4  // Not used in my project

    #define TRIS_Data    TRISB
    #define b_TRIS_GCS1  TRISCbits.TRISC7 //GCS1
    #define b_TRIS_GCS2  TRISCbits.TRISC6 //GCS2
    #define b_TRIS_RS    TRISCbits.TRISC0 //RS
    #define b_TRIS_RW    TRISCbits.TRISC1 //RW
    #define b_TRIS_E     TRISAbits.TRISA5 //E
    #define b_TRIS_On    TRISCbits.TRISC2 //RST
    #define b_TRIS_BL    TRISAbits.TRISA4 //backlight  

    void Delay(void);
    unsigned char GLCD_Read(void);
    void Wait_Not_Busy(void);
    void GLCD_Write_Cmd(unsigned char data);
    void GLCD_Write_Data (unsigned char data);
    void ClearScreen(void);
    void Init_GLCD(void);
    void PutChar(unsigned char data);
    unsigned char GLCD_Read_Data(void);
    void SetPos(unsigned char x,unsigned char y);
    void WritePosition(void);
    void plot(unsigned char x,unsigned char y);
    void hline(unsigned char x,unsigned char y1,unsigned char y2);
    void vline(unsigned char x1,unsigned char x2,unsigned char y);
    void box(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2);
    void PutMessage(static char rom *Message);
    void PutLogo(static char rom *logo);
    glcd.c
    Code (text):

    #include <p18f2550.h>
    #include "glcd.h"

    const rom unsigned char Font[96][7];
    unsigned char i,XPos,YPos,W;

    void plot(unsigned char x,unsigned char y){
    unsigned char d;
        if(x>63){
            b_GLCD_GCS1=0;
            b_GLCD_GCS2=1;
            x-=64;
        }
        else
        {
            b_GLCD_GCS1=1;
            b_GLCD_GCS2=0;
        }
        GLCD_Write_Cmd(0x40+x);         //write column address
        GLCD_Write_Cmd(0xb8+(y>>3));    //write row address
        d=GLCD_Read_Data();             //dummy read
        d=GLCD_Read_Data();
        GLCD_Write_Cmd(0x40+x);         //write column address again
        d=d&(0xff-(1<<(y&7)));
        GLCD_Write_Data(d);
    }

    void hline(unsigned char x,unsigned char y1,unsigned char y2){
        for(i=y1;i<y2;i++)
            plot(x,i);
    }

    void vline(unsigned char x1,unsigned char x2,unsigned char y){
        for(i=x1;i<x2;i++)
            plot(i,y);
    }

    void box(unsigned char x1,unsigned char y1,
        unsigned char x2,unsigned char y2){
        vline(x1,x2,y1);
        vline(x1,x2,y2);
        hline(x1,y1,y2);
        hline(x2,y1,y2);
    }

    void Delay(void){
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
        _asm    NOP _endasm
    }

    unsigned char GLCD_Read(void){
        b_GLCD_E=1;Delay();
        Delay();
        W=GLCD_Data;Delay();
        b_GLCD_E=0;Delay();
        return W;
    }

    void Wait_Not_Busy(void){
        TRIS_Data=0xff;
        b_GLCD_RS=0;
        b_GLCD_RW=1;
        if (b_GLCD_GCS1==1 && b_GLCD_GCS2==1){
            b_GLCD_GCS1=0;
            while (GLCD_Read()&0x80);
            b_GLCD_GCS1=1;
            b_GLCD_GCS2=0;
            while (GLCD_Read()&0x80);
            b_GLCD_GCS2=1;
        }
        else{
            while (GLCD_Read()&0x80);
        }
        TRIS_Data=0x00;
    }

    void GLCD_Write_Cmd(unsigned char data){
        Wait_Not_Busy();Delay();
        GLCD_Data = data;Delay();
        b_GLCD_RS=0;Delay();
        b_GLCD_RW=0;Delay();
        b_GLCD_E=1;Delay();
        Delay();
        b_GLCD_E=0;Delay();
    }

    void GLCD_Write_Data (unsigned char data){
        Wait_Not_Busy();Delay();
        GLCD_Data = data;Delay();
        b_GLCD_RS=1;Delay();
        b_GLCD_RW=0;Delay();
        b_GLCD_E=1;Delay();
        Delay();Delay();
        b_GLCD_E=0;Delay();
    }

    void MoveRight(void){
        if(++XPos==64){
            WritePosition();
        }
        if(XPos==128){
            XPos=0;
            YPos+=8;
            YPos=YPos&0x3f;
            WritePosition();
        }
    }

    void WritePosition(void){
        if(XPos>63){
            b_GLCD_GCS1=0;
            b_GLCD_GCS2=1;
        }
        else{
            b_GLCD_GCS1=1;
            b_GLCD_GCS2=0;
        }
        GLCD_Write_Cmd(0x40+(XPos&0x3f));   //column=0
        GLCD_Write_Cmd(0xb8+((YPos&0x3f)>>3));  //row=0
    }

    unsigned char GLCD_Read_Data(void){
        Wait_Not_Busy();
        TRIS_Data=0xff;
        b_GLCD_RS=1;
        b_GLCD_RW=1;
        b_GLCD_E=1;
        Delay();
        W=GLCD_Data;
        b_GLCD_E=0;
        TRIS_Data=0x00;
        return W;
    }

    void ClearScreen(void){
    unsigned char i,j;
        b_GLCD_GCS1=1;Delay();
        b_GLCD_GCS2=1;Delay();
        for(i=0;i<8;i++)
        {
            GLCD_Write_Cmd(0x40);Delay();   //y=0
            GLCD_Write_Cmd(0xb8+i);Delay(); //x=0
            for(j=0;j<0x40;j++)
            {
                GLCD_Write_Data(0xff);Delay();
            }  
        }
        SetPos(0,0);Delay();
    }

    void Init_GLCD(void){
    unsigned char i;

        b_TRIS_GCS1=0;Delay();
        b_TRIS_GCS2=0;Delay();
        b_TRIS_RS=0;Delay();
        b_TRIS_RW=0;Delay();
        b_TRIS_E=0;Delay();
        b_TRIS_On=0;Delay();
        b_TRIS_BL=0;Delay();

        b_GLCD_On=1;Delay();
        b_GLCD_GCS1=1;Delay();
        b_GLCD_GCS2=1;Delay();
        b_GLCD_BL=1;Delay();
        GLCD_Write_Cmd(0x3f);Delay();   //display on
        GLCD_Write_Cmd(0xc0);Delay();   //z=0
        ClearScreen();Delay();
    }

    void PutChar(unsigned char data){
    unsigned char i,d;
        if(data<32){
            switch(data){
                case 13:
                    XPos=0;
                case 10:
                    XPos=0;
                    YPos+=8;
                    YPos=YPos&63;
            }
            WritePosition();
        }
        else{
            for(i=0;i<7;i++){
                d=Font[data-32][i];
                if(d!=0x55){
                    GLCD_Write_Data(d);
                    MoveRight();
                }
            }
            GLCD_Write_Data(0xff);
            MoveRight();
        }
    }

    void PutMessage(static char rom *Message){
        while(*Message!=0)
            if(*Message==0x16){
                *Message++;
                XPos=*Message++;
                YPos=*Message++;
                WritePosition();
            }
            else
                PutChar(*Message++);
    }  

    void PutLogo(static char rom *logo){
    unsigned char w,h,bitcount,Byte;
        w=*logo++;
        h=*logo++;
        bitcount=0;
        do{
            for(i=0;i<w;i++){
                if(bitcount==0){
                    bitcount=8;
                    Byte=*logo++;
                }
                if(Byte&1) plot(XPos,YPos);
                XPos++;
                Byte/=2;
                bitcount--;
            }
            YPos++;
            XPos-=w;
        }while(--h);
    }

    void SetPos(unsigned char x,unsigned char y){
        XPos=x;
        YPos=y;
        WritePosition();
    }

    const rom unsigned char Font[96][7]={
        0xFF,0xFF,0xFF,0x55,0x55,0x55,0x55, //  32   
        0xFF,0xA0,0xFF,0x55,0x55,0x55,0x55, //  33  !
        0xF8,0xFF,0xF8,0x55,0x55,0x55,0x55, //  34  ""
        0xEB,0x80,0xEB,0x80,0xEB,0x55,0x55, //  35  #
        0xD9,0xB6,0x80,0xB6,0xCD,0x55,0x55, //  36  $
        0x9C,0xEC,0xF7,0x9B,0x9C,0x55,0x55, //  37  %
        0xC9,0xB6,0xFF,0xDD,0xAF,0x55,0x55, //  38  &
        0xFB,0xFC,0xFF,0x55,0x55,0x55,0x55, //  39  '
        0xE3,0xDD,0xBE,0x55,0x55,0x55,0x55, //  40  (
        0xBE,0xDD,0xE3,0x55,0x55,0x55,0x55, //  41  )
        0xEB,0xD5,0xE3,0xD5,0xEB,0x55,0x55, //  42  *
        0xF7,0xF7,0xC1,0xF7,0xF7,0x55,0x55, //  43  +
        0x7F,0x9F,0x55,0x55,0x55,0x55,0x55, //  44  ,
        0xF7,0xF7,0xF7,0xF7,0xF7,0x55,0x55, //  45  -
        0xBF,0x55,0x55,0x55,0x55,0x55,0x55, //  46  .
        0x9F,0xEF,0xF7,0xFB,0xFC,0x55,0x55, //  47  /
        0xC1,0xAE,0xB6,0xBA,0xC1,0x55,0x55, //  48  0
        0xFF,0xBD,0x80,0xBF,0xFF,0x55,0x55, //  49  1
        0x9D,0xAE,0xB6,0xB6,0xB9,0x55,0x55, //  50  2
        0xDD,0xBE,0xB6,0xB6,0xC9,0x55,0x55, //  51  3
        0xE7,0xEB,0xED,0x80,0xEF,0x55,0x55, //  52  4
        0xD8,0xB6,0xB6,0xB6,0xCE,0x55,0x55, //  53  5
        0xC3,0xB5,0xB6,0xB6,0xCF,0x55,0x55, //  54  6
        0xFE,0x8E,0xF6,0xFA,0xFC,0x55,0x55, //  55  7
        0xC9,0xB6,0xB6,0xB6,0xC9,0x55,0x55, //  56  8
        0xF9,0xB6,0xB6,0xD6,0xE1,0x55,0x55, //  57  9
        0xEB,0x55,0x55,0x55,0x55,0x55,0x55, //  58  :
        0x7F,0x97,0x55,0x55,0x55,0x55,0x55, //  59  ;
        0xF7,0xEB,0xDD,0xBE,0x55,0x55,0x55, //  60  <
        0xEB,0xEB,0xEB,0xEB,0x55,0x55,0x55, //  61  =
        0xBE,0xDD,0xEB,0xF7,0x55,0x55,0x55, //  62  >
        0xFD,0xFE,0xAE,0xF6,0xF9,0x55,0x55, //  63  ?
        0xC1,0xBE,0xA2,0xFF,0xB1,0x55,0x55, //  64  @
        0x83,0xED,0xEE,0xED,0x83,0x55,0x55, //  65  A
        0x80,0xB6,0xB6,0xB6,0xC9,0x55,0x55, //  66  B
        0xC1,0xBE,0xBE,0xBE,0xDD,0x55,0x55, //  67  C
        0x80,0xBE,0xBE,0xDD,0xE3,0x55,0x55, //  68  D
        0x80,0xB6,0xB6,0xB6,0xBE,0x55,0x55, //  69  E
        0x80,0xF6,0xF6,0xF6,0xFE,0x55,0x55, //  70  F
        0xC1,0xBE,0xB6,0xD6,0x8D,0x55,0x55, //  71  G
        0x80,0xF7,0xF7,0xF7,0x80,0x55,0x55, //  72  H
        0xBE,0x80,0xBE,0x55,0x55,0x55,0x55, //  73  I
        0xDF,0xBF,0xBE,0xC0,0xFE,0x55,0x55, //  74  J
        0x80,0xF7,0xEB,0xDD,0xBE,0x55,0x55, //  75  K
        0x80,0xBF,0xBF,0xBF,0x55,0x55,0x55, //  76  L
        0x80,0xFD,0xF3,0xFD,0x80,0x55,0x55, //  77  M
        0x80,0xF9,0xF7,0xCF,0x80,0x55,0x55, //  78  N
        0xC1,0xBE,0xBE,0xBE,0xC1,0x55,0x55, //  79  O
        0x80,0xF6,0xF6,0xF6,0xF9,0x55,0x55, //  80  P
        0xC1,0xBE,0xAE,0xDE,0xA1,0x55,0x55, //  81  Q
        0x80,0xF6,0xE6,0xD6,0xB9,0x55,0x55, //  82  R
        0xD9,0xB6,0xB6,0xB6,0xCD,0x55,0x55, //  83  S
        0xFE,0xFE,0x80,0xFE,0xFE,0x55,0x55, //  84  T
        0xC0,0xBF,0xBF,0xBF,0xC0,0x55,0x55, //  85  U
        0xF0,0xCF,0xBF,0xCF,0xF0,0x55,0x55, //  86  V
        0xF0,0xCF,0xBF,0xC7,0xBF,0xCF,0xF0, //  87  W
        0x9C,0xEB,0xF7,0xEB,0x9C,0x55,0x55, //  88  X
        0xF8,0xF7,0x8F,0xF7,0xF8,0x55,0x55, //  89  Y
        0x9E,0xAE,0xB6,0xBA,0xBC,0x55,0x55, //  90  Z
        0x80,0xBE,0xBE,0x55,0x55,0x55,0x55, //  91  [
        0xFC,0xFB,0xF7,0xEF,0x9F,0x55,0x55, //  92 
        0xBE,0xBE,0x80,0x55,0x55,0x55,0x55, //  93  ]
        0xF7,0xFB,0xFD,0xFB,0xF7,0x55,0x55, //  94  ^
        0xBF,0xBF,0xBF,0xBF,0xBF,0x55,0x55, //  95  _
        0xFC,0xFB,0xFF,0x55,0x55,0x55,0x55, //  96  `
        0xDF,0xAB,0xAB,0x87,0x55,0x55,0x55, //  97  a
        0x80,0xD7,0xBB,0xBB,0xC7,0x55,0x55, //  98  b
        0xC7,0xBB,0xBB,0xD7,0x55,0x55,0x55, //  99  c
        0xC7,0xBB,0xBB,0xD7,0x80,0x55,0x55, //  100 d
        0xC7,0xAB,0xAB,0xB7,0x55,0x55,0x55, //  101 e
        0xF7,0x81,0xF6,0xFD,0x55,0x55,0x55, //  102 f
        0x67,0x5B,0x5B,0xA7,0x55,0x55,0x55, //  103 g
        0x80,0xF7,0xFB,0xFB,0x87,0x55,0x55, //  104 h
        0xC2,0xBF,0x55,0x55,0x55,0x55,0x55, //  105 i
        0x7F,0x7B,0x82,0x55,0x55,0x55,0x55, //  106 j
        0x80,0xEF,0xD7,0xBB,0x55,0x55,0x55, //  107 k
        0xFE,0x80,0xFF,0x55,0x55,0x55,0x55, //  108 l
        0x83,0xF7,0xFB,0x87,0xFB,0x87,0x55, //  109 m
        0x83,0xF7,0xFB,0xFB,0x87,0x55,0x55, //  110 n
        0xC7,0xBB,0xBB,0xC7,0x55,0x55,0x55, //  111 o
        0x03,0xE7,0xDB,0xDB,0xE7,0x55,0x55, //  112 p
        0xE7,0xDB,0xDB,0xE7,0x03,0x55,0x55, //  113 q
        0x83,0xF7,0xFB,0xFB,0xF7,0x55,0x55, //  114 r
        0xB7,0xAB,0xAB,0xDB,0x55,0x55,0x55, //  115 s
        0xFB,0xC1,0xBB,0x55,0x55,0x55,0x55, //  116 t
        0xC3,0xBF,0xBF,0xDF,0x83,0x55,0x55, //  117 u
        0xE3,0xDF,0xBF,0xDF,0xE3,0x55,0x55, //  118 v
        0xC3,0xBF,0xCF,0xBF,0xC3,0x55,0x55, //  119 w
        0xBB,0xD7,0xEF,0xD7,0xBB,0x55,0x55, //  120 x
        0xE3,0x5F,0x5F,0x83,0x55,0x55,0x55, //  121 y
        0x9B,0xAB,0xAB,0xB3,0x55,0x55,0x55, //  122 z
        0xF7,0xC9,0xBE,0x55,0x55,0x55,0x55, //  123 {
        0xFF,0x80,0xFF,0x55,0x55,0x55,0x55, //  124 |
        0xBE,0xC9,0xF7,0x55,0x55,0x55,0x55, //  125 }
        0xFF,0xFF,0x55,0x55,0x55,0x55,0x55, //  126 ~
        0x01,0x7D,0x7D,0x7D,0x01,0x55,0x55, //  127 
    };
    main.c
    Code (text):
    #include <p18f2550.h>
    #include "glcd.h"
    #pragma config WDT      = OFF
    #pragma config MCLRE        = ON
    #pragma config DEBUG        = OFF
    #pragma config LVP      = OFF
    #pragma config PLLDIV       = 5         // need 5 for 20MHz xtal
    #pragma config CPUDIV       = OSC1_PLL2 // CPU Clock = 96 MHz/2 = 48 MHz
    #pragma config USBDIV       = 2         // 96MHz PLL/2 = 48 MHz for USB clock
    #pragma config FOSC     = HSPLL_HS  // High Speed Crystal / Resonator with PLL enabled

    void main (void)
    {
        Init_GLCD();
       
        PutMessage((rom char*)"\x16\x24\x08 Blueroom\x16\x20\x10 Electronics\n Title:\n Author:\n Date:\n Hardware:");
        PutMessage((rom char*)"\x16\x38\x18Graphic demo.");
        PutMessage((rom char*)"\x16\x38\x20Mike Webb.");
        PutMessage((rom char*)"\x16\x38\x28June 20 2007.");
        PutMessage((rom char*)"\x16\x38\x30Unicorn.");
        box(1,1,126,62);
       
        while (1);
       
        // Working "Hello World" example
        /*unsigned char i, j;
        TRISB = 0;
        while (1)
        {
            PORTB = 0x00;
            Delay1KTCYx(i);
            PORTB = 0xFF;
            Delay1KTCYx(255-i);
        }*/
    }

    Do you have any suggestions to make this project work? If you need any other details about my system, please feel free to ask.

    Any help or guidance will be appreciated greatly.
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    Hi,

    If you look at the original code it starts with,
    Code (text):

    void main (void){
    unsigned char i;
    [COLOR="Red"]   ADCON1=0x0f;            // all digital
        CMCON=7;        // no comparators[/COLOR]
        Init_GLCD();
        // ascii 22 (0x16) is set position followed by x,y
        PutMessage((rom char*)"\x16\x24\x08 Blueroom\x16\x20\x10 Electronics\n Title:\n Author:\n Date:\n Hardware:");
        PutMessage((rom char*)"\x16\x38\x18Graphic demo.");
        PutMessage((rom char*)"\x16\x38\x20Mike Webb.");
        PutMessage((rom char*)"\x16\x38\x28June 20 2007.");
     
    Note the bits in red. These make the port digital and are needed.

    Also, I don't understand why you are toggling the LCD data port in you main look. What do you expect that to do?

    Mike.
     
    • Like Like x 1
  3. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    Thank you very much for your reply Mike.
    I added those two line, but it is still the same. The display screen is empty.
    Any other ideas?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia

    Maybe you have it connected up wrong.

    Do you have,
    Reset to 5V?
    Vee to a pot with the slider to V0 and other end to Vss?

    I've attached a schematic for a project I did that shows how mine is connected up. As you can see my pinout is different to yours but most of the designs I've seen have the same pinout as mine.

    Mike.
     

    Attached Files:

    • GLCD.png
      GLCD.png
      File size:
      29.2 KB
      Views:
      1,773
  6. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    I did both of these, you can see it in the photo of the circuit.
    I connected 5V to reset, my blink-a-led code is working.
    One of the end of the pot is connected to Vdd instead of Vss. I did so, because datasheet recommends to do that way. Since you said it, I tried connecting to Vss, but it didn't change anything.

    My LCD model is WG12864B (the bold B here is the 'model identifier'), it is a B model LCD. I believe you used a A model LCD. A and B model LCDs have different pinout configurations (e.g.; for B model LCDs, pin #1 is connected to Vcc and pin #2 is to GND, and for A model, these pins are reversed).


    I have one important thing to ask about the LCD you used while writing this library. Which of the CS1, CS2, E, RST (Reset), RS (D/I'), R/W' pins were complemented on the LCD side (not on the PIC side (i.e.; in your code))?

    For my LCD,
    CSx are not complemented,
    E is not complemented.
    R/W': R is not complemented, W is complemented,
    RS (D/I', D: Data, I: Instruction): D is not complemented, I is complemented
    RST is complemented.

    I think, the reason my circuit is not working is that the LCD you used and the one I'm using have different complemention for some pins. For example, if CSx pins were complemented in you LCD pinout, your library won't work in my system if my CSx pins are not complemented.

    Thank you in advance. I think we are going to solve this problem. :)
     
    Last edited: Nov 7, 2009
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    Unfortunately, I just checked and the polarity of all the control signals are the same.

    Can you run in debug mode and see where it gets to? Does it get through the initialization routine?

    Mike.
     
  8. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    How can I debug it? Should I simulate it in MPLAB, or connect a LED to see which part of the code is working at steady state? Actually I never used debugging before.
     
  9. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    What do you use to program your chip?

    Mike.
     
  10. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    Photo: http://img265.imageshack.us/img265/5751/picture002io.jpg

    I am using an anonymous PIC programmer I bought from electronics part store. There's a "Universal USB PIC Programmer" text on it.
    I'm using USBurn v1.10a2 as programmer software.
     
  11. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    OK. so no debug capability. If you have a "debug" LED then use that to work out how far the code gets. I suspect it won't get through the init code.

    Mike.
     
    Last edited: Nov 7, 2009
  12. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    Ok, I will do it and report the result here.
     
  13. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    I'm back. Sorry for the delay. Too busy these days, grad school assignments + job...

    I did this debugging thing. Here is my new code:

    main.c
    Code (text):
    #include <p18f2550.h>
    #include "delays.h"
    #include "glcd.h"
    #pragma config WDT      = OFF
    #pragma config MCLRE    = ON
    #pragma config DEBUG    = OFF
    #pragma config LVP      = OFF
    #pragma config PLLDIV   = 5         // need 5 for 20MHz xtal
    #pragma config CPUDIV   = OSC1_PLL2 // CPU Clock = 96 MHz/2 = 48 MHz
    #pragma config USBDIV   = 2         // 96MHz PLL/2 = 48 MHz for USB clock
    #pragma config FOSC     = HSPLL_HS  // High Speed Crystal / Resonator with PLL enabled

    void blinkLed(unsigned char led);
    //unsigned char j;

    void main (void)
    {
        ADCON0=0x00;
        ADCON1=0x0f;
        CMCON=7;
       
        TRISAbits.TRISA0 = 0;
        TRISAbits.TRISA1 = 0;
        TRISAbits.TRISA2 = 0;
        TRISAbits.TRISA3 = 0;
        TRISAbits.TRISA4 = 0;
        LATAbits.LATA0 = 0;
        LATAbits.LATA1 = 0;
        LATAbits.LATA2 = 0;
        LATAbits.LATA3 = 0;
        LATAbits.LATA4 = 0;
       
        blinkLed(0);
        Init_GLCD();
        blinkLed(1);
        PutMessage((rom char*)"\x16\x24\x08 Blueroom\x16\x20\x10 Electronics\n Title:\n Author:\n Date:\n Hardware:");
        blinkLed(2);
        PutMessage((rom char*)"\x16\x38\x18Graphic demo.");
        blinkLed(3);
        PutMessage((rom char*)"\x16\x38\x20Mike Webb.");
        blinkLed(4);
        PutMessage((rom char*)"\x16\x38\x28June 20 2007.");
        blinkLed(0);
        PutMessage((rom char*)"\x16\x38\x30Unicorn.");
        blinkLed(1);
        box(1,1,126,62);
        blinkLed(2);
        while (1);
        blinkLed(3);
    }

    void blinkLed(unsigned char led)
    {  
        int j; for (j=0; j<10; j++)
        {
            switch (led)
            {
                case 0:
                    LATAbits.LATA0 = 1;
                    Delay10KTCYx(0);
                    LATAbits.LATA0 = 0;
                    Delay10KTCYx(0);
                    break;
                case 1:
                    LATAbits.LATA1 = 1;
                    Delay10KTCYx(0);
                    LATAbits.LATA1 = 0;
                    Delay10KTCYx(0);
                    break;
                case 2:
                    LATAbits.LATA2 = 1;
                    Delay10KTCYx(0);
                    LATAbits.LATA2 = 0;
                    Delay10KTCYx(0);
                    break;
                case 3:
                    LATAbits.LATA3 = 1;
                    Delay10KTCYx(0);
                    LATAbits.LATA3 = 0;
                    Delay10KTCYx(0);
                    break;
                case 4:
                    LATAbits.LATA4 = 1;
                    Delay10KTCYx(0);
                    LATAbits.LATA4 = 0;
                    Delay10KTCYx(0);
                    break;
                default:
                    LATAbits.LATA0 = 0;
                    LATAbits.LATA1 = 0;
                    LATAbits.LATA2 = 0;
                    LATAbits.LATA3 = 0;
                    LATAbits.LATA4 = 0;
                    break;
            }
        }  
    }
    I didn't modify any other file.

    As a short explanation of this code,
    Five leds are connected to PORTA bits 0 to 4, and sequentially one of them blinks 10 times at every step of the program.

    I ran the code, the result is, the program terminates without being stuck. Every LCD command under the main() function is executed.The LCD still displays nothing.

    Tomorrow night I am going to try complementing CSx bits, that's my last hope. I don't want to mess up the library, so I will use 7404 (NOT gates).

    Until then, if you have any suggestions or ideas please share with me.

    Wish me luck, I need it...
     
  14. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    Folks, I have good news:
    [​IMG]

    Complementing CSx pins worked after all.
    I think we need different libraries for different LCDs, like we use different header files for different PICs.
     
  15. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Could you not just invert the CSx line in code?
     
  16. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    Yeah, I will do so next.
     
  17. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    A simple way to do it in code is to inset 1- into all the GCS lines.

    For example,
    Code (text):

    #include <p18f4550.h>
    #include <GLCD.h>

    const rom unsigned char Font[96][7];
    unsigned char i,XPos,YPos,W;

    void plot(unsigned char x,unsigned char y){
    unsigned char d;
        if(x>63){
            b_GLCD_GCS1=[COLOR="Red"]1-[/COLOR]0;
            b_GLCD_GCS2=[COLOR="Red"]1-[/COLOR]1;
            x-=64;
        }
        else
        {
            b_GLCD_GCS1=[COLOR="Red"]1-[/COLOR]1;
            b_GLCD_GCS2=[COLOR="Red"]1-[/COLOR]0;
        }
        GLCD_Write_Cmd(0x40+x);     //write column address
        GLCD_Write_Cmd(0xb8+(y>>3));    //write row address
        d=GLCD_Read_Data();     //dummy read
        d=GLCD_Read_Data();
        GLCD_Write_Cmd(0x40+x);     //write column address again
        d=d&(0xff-(1<<(y&7)));
        GLCD_Write_Data(d);
    }
     
    Doing it this way makes it easier to see if you inadvertently missed one. You could also do a global search and replace to make it even easier.

    Mike.
     
    Last edited: Nov 12, 2009
  18. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    Great idea, thank you.
     
  19. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
  20. Tymoty

    Tymoty New Member

    Joined:
    Nov 10, 2009
    Messages:
    11
    Likes:
    0
    Hi all, I have a similar problem. I used code from this thread, but program does't get throught init routine. It fall down in clearscreen in for cyklus (i used leds for location). Please, please help me. I haven't idea how it resolve
     
  21. hkBattousai

    hkBattousai Member

    Joined:
    Oct 30, 2009
    Messages:
    196
    Likes:
    0
    I want to help you, but I need more information.

    Can you tell me what LCD you are using. I need an LCD code like "XG12864A".
    Can you post here the pin configuration of your LCD, and upload the datasheet?

    Connect five LEDs to PA0...PA5 pins as seen in the photo in this post.
    Next run the code in this post.
    Check if all of the lines in you code is executed and your program doesn't get stuck at any point.
     

Share This Page