PIC MCU and LCD

Have you got the greatest 48 bit multiplier ever conceived? Prove it - post your code here.

Moderator: phalanx

PIC MCU and LCD

Postby arix » Fri Sep 17, 2010 11:36 pm

Hi,
I've got an LCD from China and I'm trying to put it to work with my PIC MCU for first time.

The test code they supplied with the device had comments in Chinese but using google translator site I translated them.

The functions in the code are straightforward, but I can't understand the way they assign the MCU PIN to the LCD ones. Mostly for data pins of LCD as I can't even see them in code (well, novice!).

The code indicates:
Code: Select all
sbit CS=P2^5;      //Chip select
sbit RES=P2^7;      //Reset
sbit RS=P2^4;      //Data / command select
sbit RW=P2^3;           // Data port using the P0


which means that, PINS 5,7,4 and 3 of Port D are assigned to CS, RES, RS, and RW of LCD, right? Then where are data pins? O_O

Here is all info I have:
Code: Select all
#include"reg52.h"

#define WINDOW_XADDR_START   0x0050 // Horizontal Start Address Set
#define WINDOW_XADDR_END   0x0051 // Horizontal End Address Set
#define WINDOW_YADDR_START   0x0052 // Vertical Start Address Set
#define WINDOW_YADDR_END   0x0053 // Vertical End Address Set
#define GRAM_XADDR          0x0020 // GRAM Horizontal Address Set
#define GRAM_YADDR          0x0021 // GRAM Vertical Address Set
#define GRAMWR              0x0022 // memory write

/* LCD color */
#define White          0xFFFF
#define Black          0x0000
#define Blue           0x001F
#define Blue2          0x051F
#define Red            0xF800
#define Magenta        0xF81F
#define Green          0x07E0
#define Cyan           0x7FFF
#define Yellow         0xFFE0

sbit CS=P2^5;      //Chip select
sbit RES=P2^7;      //Reset
sbit RS=P2^4;      //Data / command select
sbit RW=P2^3;           // Data port using the P0
//====================================================//

void main (void);
void ILI9325_Initial(void);
void show_colour_bar (void);
void Write_Cmd_Data(unsigned char x, unsigned int y);
void Write_Cmd(unsigned char DH,unsigned char DL);
void Write_Data(unsigned char DH,unsigned char DL);
void delayms(unsigned int tt);
void show_photo(void);
void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color);
unsigned char code pic[];
void  Write_Data_U16(unsigned int y);
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1);
void ClearScreen(unsigned int bColor);
void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
void LCD_PutString(unsigned short x, unsigned short y, char *s, unsigned int fColor, unsigned int bColor);

void LCD_PutChar8x8(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor);
void PutGB3232(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor);
//================================================//

void  main(void)
{   
   CS=1;
   delayms(5);
   RES=0;
   delayms(5);
   RES=1;
   delayms(5);
   ILI9325_Initial();
   while(1)
   {
   


ClearScreen(0x0);

//show_colour_bar();   


LCD_PutString(2,40,"http://shop58797699.taobao.com/",Red,Blue);                  ///Foreground color and background color, can be directly pre-defined, it can fill in 16 hexadecimal numbers
LCD_PutString(40,60,"I LOVE MY JOB ",Blue2,Yellow);
LCD_PutString(40,80,"Yao Shuo ElectronicMagenta,Green);
LCD_PutString(40,100,"Professional technical support forum",0x07e0,0xf800);
LCD_PutString(40,120,"Professional Development Board",0xF800,Yellow);
LCD_PutString(40,140,"1234567890",0xF800,Yellow);
LCD_PutString(20,160,"abcdefghijklmnopqistuvwxyz",0xF800,Yellow);
LCD_PutString(20,180,"`,./<>';:[]{}\|?)(-=+*&^%$",0xF800,Yellow);
PutGB3232(20,200,"Love",Blue,Yellow);   //32x32 Chinese characters to write
PutGB3232(60,200,"Single",Blue,Red);
PutGB3232(100,200,"red",Blue,Magenta);
PutGB3232(140,200,"bye",Blue,Green);
PutGB3232(180,200,"hi",Blue,Cyan);   
PutGB3232(20,240,"come",Blue,0X00);   
PutGB3232(60,240,"go",Blue,0X00);   
PutGB3232(100,240,"dind",Blue,0X00);   
PutGB3232(140,240,"bund",Blue,0X00);   
PutGB3232(180,240,"shang",Blue,0X00);
PutGB3232(20,280,"chik",Blue,0X00);      
PutGB3232(60,280,"chap",Blue,0X00);   
PutGB3232(100,280,"sa­",Blue,0X00);   
PutGB3232(140,280,"pu",Blue,0X00);   
PutGB3232(180,280,"ra",Blue,0X00);   

//   show_photo();

      while(1);   


   }
}


void ClearScreen(unsigned int bColor)
{
 unsigned int i,j;
 LCD_SetPos(0,240,0,320);//320x240
 for (i=0;i<320;i++)
   {
   
      for (j=0;j<240;j++)
          Write_Data_U16(bColor);

   }
}

/////////////////////////////////////////////////////////////
#include "8X16.h"
void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor)
{
 unsigned int i,j;
 LCD_SetPos(x,x+8-1,y,y+16-1);
 for(i=0; i<16;i++) {
      unsigned char m=Font8x16[c*16+i];
      for(j=0;j<8;j++) {
         if((m&0x80)==0x80) {
            Write_Data_U16(fColor);
            }
         else {
            Write_Data_U16(bColor);
            }
         m<<=1;
         }
      }
}


void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor) {

      LCD_PutChar8x16( x, y, c, fColor, bColor );
   }


#include "GB1616.h"   //16 * 16 character font

void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor){
   unsigned int i,j,k;

   LCD_SetPos(x,  x+16-1,y, y+16-1);

   for (k=0;k<64;k++) { // 64 marked the number of self in Chinese characters library, circulation check inner code)
     if ((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])){
       for(i=0;i<32;i++) {
        unsigned short m=codeGB_16[k].Msk[i];
        for(j=0;j<8;j++) {
         if((m&0x80)==0x80) {
            Write_Data_U16(fColor);
            }
         else {
            Write_Data_U16(bColor);
            }
         m<<=1;
         }
        }
      } 
     }   
   }

   #include "GB3232.h"   //32 * 32 character font

void PutGB3232(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor){
   unsigned int i,j,k;

   LCD_SetPos(x,  x+32-1,y, y+32-1);

   for (k=0;k<15;k++) { //15 mark in the number of Chinese characters library self, loop the query code
     if ((codeGB_32[k].Index[0]==c[0])&&(codeGB_32[k].Index[1]==c[1])){
       for(i=0;i<128;i++) {
        unsigned short m=codeGB_32[k].Msk[i];
        for(j=0;j<8;j++) {
         if((m&0x80)==0x80) {
            Write_Data_U16(fColor);
            }
         else {
            Write_Data_U16(bColor);
            }
         m<<=1;
         }
        }
      } 
     }   
   }

void LCD_PutString(unsigned short x, unsigned short y, unsigned char *s, unsigned int fColor, unsigned int bColor) {
    unsigned char l=0;
   while(*s) {
      if( *s < 0x80)
          {
         LCD_PutChar(x+l*8,y,*s,fColor,bColor);
         s++;l++;
         }
      else
          {
         PutGB1616(x+l*8,y,(unsigned char*)s,fColor,bColor);
         s+=2;l+=2;
         }
      }
   }
//==================== R G B color display ====================//



void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
{
   unsigned int i,j;
   //address_set();
   LCD_SetPos(x0,x1,y0,y1);
   for (i=y0;i<=y1;i++)
   {
      for (j=x0;j<=x1;j++)
          Write_Data_U16(Color);

   }


}

//====================== Display Color ======================//
void show_colour_bar (void)

{
   int V,H;
   LCD_SetPos(0,240,0,320);//320x240

   for(H=0;H<240;H++)
   {
      for(V=0;V<40;V++)
      Write_Data(0xf8,0x00);
   }

   for(H=0;H<240;H++)
   {
      for(V=40;V<80;V++)
      Write_Data(0x07,0xe0);
   }

   for(H=0;H<240;H++)
   {
      for(V=80;V<120;V++)
      Write_Data(0x00,0x1f);
   }

   for(H=0;H<240;H++)
   {
      for(V=120;V<160;V++)
      Write_Data(0xff,0xe0);
   }

   for(H=0;H<240;H++)
   {
      for(V=160;V<200;V++)
      Write_Data(0xf8,0x1f);
   }

   for(H=0;H<240;H++)
   {
      for(V=200;V<240;V++)
      Write_Data(0x07,0xff);
   }

   for(H=0;H<240;H++)
   {
      for(V=240;V<280;V++)
      Write_Data(0xff,0xff);
   }

   for(H=0;H<240;H++)
   {
      for(V=280;V<320;V++)
      Write_Data(0x00,0x00);
   }

}

void show_photo(void)
{
   unsigned char j;
   unsigned int i;
   unsigned long s=0;

   LCD_SetPos(0,240,0,320);//320x240

   for (i=0;i<75;i++)
   {
   for (j=0;j<240;j++)
   Write_Data(0xff,0xff);
      
   }

   for (i=0;i<170;i++)
   {
   for (j=0;j<55;j++)
      Write_Data(0xff,0xff);

      for (j=0;j<130;j++)
      Write_Data(pic[s++],pic[s++]);
      
      for (j=0;j<55;j++)
      Write_Data(0xff,0xff);
   }

    for (i=0;i<75;i++)
   {
   for (j=0;j<240;j++)
   Write_Data(0xff,0xff);
      
   }

   }

//=======================================================//



void  Write_Cmd_Data (unsigned char x,unsigned int y)
{
   unsigned char m,n;
   m=y>>8;
   n=y;
   Write_Cmd(0x00,x);
   Write_Data(m,n);

}


void  Write_Data_U16(unsigned int y)
{
   unsigned char m,n;
   m=y>>8;
   n=y;
   Write_Data(m,n);

}
//====================== Write command ==========================//

void Write_Cmd(unsigned char DH,unsigned char DL)
{
 
    

   CS=0;
   RS=0;

   P1=DH;
   RW=0;
   RW=1;

   P1=DL;
   
   RW=0;
   RW=1;
   CS=1;
}

//===================== Write data ===========================//

void Write_Data(unsigned char DH,unsigned char DL)
{
 
 
   
   CS=0;
   
   RS=1;
   P1=DH;
   RW=0;
    RW=1;

   P1=DL;   
   RW=0;
   RW=1;
   CS=1;
}





//=======================================================
void delayms(unsigned int count)
{
    int i,j;                                                                               
    for(i=0;i<count;i++)                                                                   
       {
        for(j=0;j<260;j++);
       }                                                                                     
}



//===================== Initialization code =======================//



void ILI9325_Initial(void)
{
//############# void Power_Set(void) ################//
Write_Cmd_Data(0x0000,0x0001);
delayms(100);

Write_Cmd_Data(0x0015,0x0030);
Write_Cmd_Data(0x0011,0x0040);
Write_Cmd_Data(0x0010,0x1628);
Write_Cmd_Data(0x0012,0x0000);
Write_Cmd_Data(0x0013,0x104d);
delayms(100);
Write_Cmd_Data(0x0012,0x0010);
delayms(100);
Write_Cmd_Data(0x0010,0x2620);
Write_Cmd_Data(0x0013,0x344d); //304d
delayms(100);

Write_Cmd_Data(0x0001,0x0100);
Write_Cmd_Data(0x0002,0x0300);
Write_Cmd_Data(0x0003,0x1030);
Write_Cmd_Data(0x0008,0x0604);
Write_Cmd_Data(0x0009,0x0000);
Write_Cmd_Data(0x000A,0x0008);

Write_Cmd_Data(0x0041,0x0002);
Write_Cmd_Data(0x0060,0x2700);
Write_Cmd_Data(0x0061,0x0001);
Write_Cmd_Data(0x0090,0x0182);
Write_Cmd_Data(0x0093,0x0001);
Write_Cmd_Data(0x00a3,0x0010);
delayms(50);

//################# void Gamma_Set(void) ####################//
 Write_Cmd_Data(0x30,0x0000);      
 Write_Cmd_Data(0x31,0x0502);      
 Write_Cmd_Data(0x32,0x0307);      
 Write_Cmd_Data(0x33,0x0305);      
 Write_Cmd_Data(0x34,0x0004);      
 Write_Cmd_Data(0x35,0x0402);      
 Write_Cmd_Data(0x36,0x0707);      
 Write_Cmd_Data(0x37,0x0503);      
 Write_Cmd_Data(0x38,0x1505);      
 Write_Cmd_Data(0x39,0x1505);
 delayms(50);

//################## void Display_ON(void) ####################//
 Write_Cmd_Data(0x0007,0x0001);
 delayms(50);
 Write_Cmd_Data(0x0007,0x0021);
 Write_Cmd_Data(0x0007,0x0023);
 delayms(50);
 Write_Cmd_Data(0x0007,0x0033);
 delayms(50);
 Write_Cmd_Data(0x0007,0x0133);

}

 





/*===========================================================*/

/*************************************************************
Function name: LCD_DefineDispWindow
Function: define display form
Parameters: x0: X coordinate in the form of the smaller
x1: X coordinate in the form of the greater
y0: Y coordinates form the smaller
y1: Y coordinates form the greater
Return Value: None
*************************************************************/
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
{
  Write_Cmd_Data(WINDOW_XADDR_START,x0);
  Write_Cmd_Data(WINDOW_XADDR_END,x1);
  Write_Cmd_Data(WINDOW_YADDR_START,y0);
  Write_Cmd_Data(WINDOW_YADDR_END,y1);
  Write_Cmd_Data(GRAM_XADDR,x0);
  Write_Cmd_Data(GRAM_YADDR,y0);
  Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
}


Image
Image
Image
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sat Sep 18, 2010 5:44 am

From your photos I'd guess 'P2' indicates port on U3 and data is output on P1, the 8-bit port on U4, marked D7-D0
<=>
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sat Sep 18, 2010 8:47 am

polly wrote:From your photos I'd guess 'P2' indicates port on U3 and data is output on P1, the 8-bit port on U4, marked D7-D0


Where exactly is this U3, on the PIC MCU please?
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sat Sep 18, 2010 10:36 am

I've no idea. You deduced 'P2' in the code referred to port D of some PIC; in the code you posted the function writing to the LCD takes byte arguments and writes them to 'P1'. In the photograph you posted the reset lines etc are connected to 'U3' and the data lines D7-D0 are connected to 'U4'. If that's not a PIC in your pic .. then there's some information wanting. Wherever you found justification that P2 means Port D, find what 'P1' equates to.
<=>
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sat Sep 18, 2010 10:46 am

polly wrote:I've no idea. You deduced 'P2' in the code referred to port D of some PIC; in the code you posted the function writing to the LCD takes byte arguments and writes them to 'P1'. In the photograph you posted the reset lines etc are connected to 'U3' and the data lines D7-D0 are connected to 'U4'. If that's not a PIC in your pic .. then there's some information wanting. Wherever you found justification that P2 means Port D, find what 'P1' equates to.



Well I must clarify that, I bought this LCD you see in the pictures, and the code you have in the above posts was supplied with it, for a test. It is to be uploaded to an MCU, then connect that MCU to this LCD pins.

Those IC on the LCD are not MCU.

My main problem is that I don't see in the code above, where the DB0-7 will be connected. My guess is that this is related to the reg52.h file which is included.

Also, I only guess that P0,1,2,3 are related to a conceptual MCU standard ports as A, B, C, D on PICs.

I want to connect a PIC to this LCD and test it, but I don't know to what port connect pins of my PIC!

:(
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sat Sep 18, 2010 5:43 pm

Which PIC are you using ? If I were using an 18F2550 I think I'd connect its PortB7-0 directly to the data lines
D7-D0 as shown in your picture going to the lower circuit. For the other controls I'd need to use Port A because
there's not much else except a few pins on Port C which are often used as reset, bootloader switch inputs and suchlike. I'd have to be sure to set up Port A as a digital I/O rather than analogue for my particular chip. If you
have Port D, E available on your PIC then either should be a good choice.
I don't quite understand that reset code, to me the '^' (caret) operator is exclusive OR so I guess you'd want to replace this sort of thing:
Code: Select all
sbit CS=P2^5;      //Chip select
sbit RES=P2^7;      //Reset
sbit RS=P2^4;      //Data / command select
sbit RW=P2^3;           // Data port using the P0

with:
Code: Select all
#define CS   PORTAbits.RA5
#define RES PORTAbits.RA7
#define RS   PORTAbits.RA4
#define RW  PORTAbits.RA3
#define P1  PORTB


( assuming you're using the Microchip compiler ) .. using whichever pin assignments you've chosen.

With those definitions in place translating their pin names to your choice of pins the code ought to compile
HTH !
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sat Sep 18, 2010 10:16 pm

polly wrote:Which PIC are you using ? If I were using an 18F2550 I think I'd connect its PortB7-0 directly to the data lines
D7-D0 as shown in your picture going to the lower circuit. For the other controls I'd need to use Port A because
there's not much else except a few pins on Port C which are often used as reset, bootloader switch inputs and suchlike. I'd have to be sure to set up Port A as a digital I/O rather than analogue for my particular chip. If you
have Port D, E available on your PIC then either should be a good choice.
I don't quite understand that reset code, to me the '^' (caret) operator is exclusive OR so I guess you'd want to replace this sort of thing:
Code: Select all
sbit CS=P2^5;      //Chip select
sbit RES=P2^7;      //Reset
sbit RS=P2^4;      //Data / command select
sbit RW=P2^3;           // Data port using the P0

with:
Code: Select all
#define CS   PORTAbits.RA5
#define RES PORTAbits.RA7
#define RS   PORTAbits.RA4
#define RW  PORTAbits.RA3
#define P1  PORTB


( assuming you're using the Microchip compiler ) .. using whichever pin assignments you've chosen.

With those definitions in place translating their pin names to your choice of pins the code ought to compile
HTH !


Polly so many thanks! I posted the same question to many forums to get the answer you gave me, but the only answer out of all other forums to the same question was that this program is written for 8052 MCU. I'm a novice and knowing that it is for 8052 doesn't help much.

Well, I'm using a PIC18F4550. So I have port D and E (according to datasheet).

Please check if I've got the idea right:

this:
Code: Select all
#define RW  PORTAbits.RA3

is assigning only one pin RA3 to the RW pin of the LCD.

while this:
Code: Select all
#define P1  PORTB

is assigning all of 8 pins of PORTB to P1, because further in code we have for instance:
void Write_Cmd(unsigned char DH,unsigned char DL)
{



CS=0;
RS=0;

P1=DH;
RW=0;
RW=1;

P1=DL;

RW=0;
RW=1;
CS=1;
}


where setting P1=DH assigns 4 of data pins from LCD and P1=DL the next 4 pins, right?

I'm still confused in one thing:
1) when we have P1=DH, 4 pins from LCD are read to PIC, but after, when we use P1=DL, again the same pins are used.
2) suppose that physically the data pins of LCD, i.e. DB0-7 are connected to the PORTB. Where in the code are they used? On such P1=DL? how do you see this please?

I appreciate your patience for novices :D
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sun Sep 19, 2010 7:37 am

Yes, the #define statements are assigning the functions to specific pins and also translating the names for pins, ports used in the original compiler ( P1, P2) to the names used by the Microchip compiler and header files, (PORTB, PORTBbits.RB2 ) . That means the #include reg52.h will need to be replaced with: "#include p18f4550.h .. which is the file that relates those pin and port names to actual PIC hardware addresses when using Microchip's C18 compiler.
Regarding the data write function it looks to me like two 8bit arguments are passed on the same 8 connections in sequence, high byte strobed with a negative pulse on the RW line and then the low byte sent across the same 8 bit wide bus from PIC to LCD pins D0-7 . Unsigned char is an 8 bit value. I expect the LCD module treats the
first RW op after a reset as the high byte and the next in sequence as the low byte.
So, once you've connected to PICS port bits 7-0 to the LCD and used: "#define P1 PORTB , the line: "P1 = DL" is treated as: "PORTB = DL" ; copying the 8 bit value in DL across to the port's output. ( Correction to the above, all the times I've been writing: PORTx, PORTxbits .. I should have written LATx, LATxbits, referring specifically to setting the output circuitry on each PIC port rather than its inputs.
The only other thing to worry about is the 'delay' instructions, where you'll need to use the corresponding function from Microchip's compiler, together with the settings for your particular clock speed.
Best.
<=>
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sun Sep 19, 2010 8:32 am

polly wrote: The only other thing to worry about is the 'delay' instructions, where you'll need to use the corresponding function from Microchip's compiler, together with the settings for your particular clock speed.
Best.


Great! I understand all what you say, Polly, and about what quoted:

I use a 20Mhz crystal, so what do you suggest for delay function please?
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sun Sep 19, 2010 9:20 am

Ah, with the USB PICs it can look complicated. There's a diagram in the 18fx550 data sheet showing the oscillator configuration, you'll need that to figure out your configuration bytes' settings. For this PIC the crystal oscillator is usually divided down to 4MHz so you have one configuration byte which specifies that divisor. The 4MHz signal is multiplied, by a phase locked loop, to 96MHz internally so the USB section can run at that speed.
Another divisor setting in the configuration bytes specifies a divisor for the 96MHz signal to derive the processor clock so, with a 20MHz crystal you may still have an FOSC in the CPU running at some other frequency, 48MHz is common.
If it's a sticking point, tell what kind of board you are using for your 18f4550 and how you program the PIC .. USB bootloader or Mplab IDE / Pickit ?
<=>
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sun Sep 19, 2010 9:54 am

polly wrote:Ah, with the USB PICs it can look complicated. There's a diagram in the 18fx550 data sheet showing the oscillator configuration, you'll need that to figure out your configuration bytes' settings. For this PIC the crystal oscillator is usually divided down to 4MHz so you have one configuration byte which specifies that divisor. The 4MHz signal is multiplied, by a phase locked loop, to 96MHz internally so the USB section can run at that speed.
Another divisor setting in the configuration bytes specifies a divisor for the 96MHz signal to derive the processor clock so, with a 20MHz crystal you may still have an FOSC in the CPU running at some other frequency, 48MHz is common.
If it's a sticking point, tell what kind of board you are using for your 18f4550 and how you program the PIC .. USB bootloader or Mplab IDE / Pickit ?


You know, even though I'm a novice, I'm going to make a PDA by myself, so that at least to learn something! :D

Before this, I bought Nokia 6100 LCD, then learned UV Photoresist method and made a normal footprint, and ironed it (my first SMD soldering too). it worked ok! I got feeling that I'm a hero, and got speed in buying many parts ;)

Next I found this LCD which is of 40xRGBx320 resolution, bigger than Nokia 6100 LCD and changed to use it.

I also bought ENC28J60, and plan for an Ethernet. I have Nintendo Lite Touch screen which I successfully put to work.

Also, I bough also IR soldering machine and hot air + fine soldering iron for SMD. I received some days ago my Bluetooth BC01B CSR BGA (91 balls) chip from USA to make something at least one step above elementary. (one just want to eat these chips, that much beautiful! ;))

At the moment, I'm trying to put each part to work independently with the PIC, then I'll finish the 75 pages of Eagle tutorial and make the PCB with UV Photoresist method based on auto-rout of Eagle.

(I also plan to buy a printer that prints on DVD, to print on my final PCB after UV processing it with solder mask, i.e. getting it coated with that green protective layer. If you could tell me if this idea of printer would work or not, I appreciate it a lot).

My future plans after this is to buy a GPS chip from Sparkfun and add you know, as many components to this PDA as I can. I'm thinking in terms of math, to "integrate" some knowledge around this axis, like d(my experimental PDA) :D

Right now I was working based on your kind suggestions, to get the LCD to work, but now that you add this new info, I think I'll first use my PIC16F877A, with a 20Mhz crystal.

Just to 100% be based on what you told above, I connected my PicKit2 to Microchip IDE, and there used the C18 compiler so I'm exactly implementing as you explained.

So please guide me if it is possible.

Thank you!
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sun Sep 19, 2010 10:51 am

.. as long as C18 supports 16F parts .. but really, the 18f4550 clocking scheme isn't so bad and you have the added bonus of USB connectivity as well as all those ports. I pulled the configuration settings from uChip's demo board code in their USB stack and appended them to the code you posted; it nearly compiles but your font header files are missing. At least it shows how to switch from 8052 to 18f4550. FWIW:
Code: Select all
#ifdef ORIG_8052             // original code
 #include"reg52.h"
 #include "8X16.h"
 #include "GB1616.h"        //16 * 16 character font
 #include "GB3232.h"   //32 * 32 character font
#else      // port to 18f4550
#include "p18f4550.h"       // uChip's header file
// Configuration bits for PICDEM FS USB Demo Board (based on PIC18F4550)
        #pragma config PLLDIV   = 5          // (20 MHz crystal on PICDEM FS USB board)
        #pragma config CPUDIV   = OSC1_PLL2  // CPU Fosc is 96 MHz / 2
        #pragma config USBDIV   = 2          // Clock source from 96MHz PLL/2
        #pragma config FOSC     = HSPLL_HS
        #pragma config FCMEN    = OFF
        #pragma config IESO     = OFF
        #pragma config PWRT     = OFF
        #pragma config BOR      = ON
        #pragma config BORV     = 3
        #pragma config VREGEN   = ON      //USB Voltage Regulator
        #pragma config WDT      = OFF
        #pragma config WDTPS    = 32768
        #pragma config MCLRE    = ON
        #pragma config LPT1OSC  = OFF
        #pragma config PBADEN   = OFF
//      #pragma config CCP2MX   = ON
        #pragma config STVREN   = ON
        #pragma config LVP      = OFF
//      #pragma config ICPRT    = OFF       // Dedicated In-Circuit Debug/Programming
        #pragma config XINST    = OFF       // Extended Instruction Set
        #pragma config CP0      = OFF
        #pragma config CP1      = OFF
//      #pragma config CP2      = OFF
//      #pragma config CP3      = OFF
        #pragma config CPB      = OFF
//      #pragma config CPD      = OFF
        #pragma config WRT0     = OFF
        #pragma config WRT1     = OFF
//      #pragma config WRT2     = OFF
//      #pragma config WRT3     = OFF
        #pragma config WRTB     = OFF       // Boot Block Write Protection
        #pragma config WRTC     = OFF
//      #pragma config WRTD     = OFF
        #pragma config EBTR0    = OFF
        #pragma config EBTR1    = OFF
//      #pragma config EBTR2    = OFF
//      #pragma config EBTR3    = OFF
        #pragma config EBTRB    = OFF
//
// Delay
#define FOSC_MHZ 48                    // Specify FOSC freq in MHz
// 18f4550 pin allocations
#define  CS LATDbits.RD0
#define RES LATDbits.RD1
#define  RS LATDbits.RD2
#define  RW LATDbits.RD3
//
#define p1  LATB
// end 18f4550 definitions       
#endif

#define WINDOW_XADDR_START   0x0050 // Horizontal Start Address Set
#define WINDOW_XADDR_END   0x0051 // Horizontal End Address Set
#define WINDOW_YADDR_START   0x0052 // Vertical Start Address Set
#define WINDOW_YADDR_END   0x0053 // Vertical End Address Set
#define GRAM_XADDR          0x0020 // GRAM Horizontal Address Set
#define GRAM_YADDR          0x0021 // GRAM Vertical Address Set
#define GRAMWR              0x0022 // memory write

/* LCD color */
#define White          0xFFFF
#define Black          0x0000
#define Blue           0x001F
#define Blue2          0x051F
#define Red            0xF800
#define Magenta        0xF81F
#define Green          0x07E0
#define Cyan           0x7FFF
#define Yellow         0xFFE0

#ifdef ORIG_8052
sbit CS=P2^5;      //Chip select
sbit RES=P2^7;      //Reset
sbit RS=P2^4;      //Data / command select
sbit RW=P2^3;           // Data port using the P0
#endif
//====================================================//

void main (void);
void ILI9325_Initial(void);
void show_colour_bar (void);
void Write_Cmd_Data(unsigned char x, unsigned int y);
void Write_Cmd(unsigned char DH,unsigned char DL);
void Write_Data(unsigned char DH,unsigned char DL);
void delayms(unsigned int tt);
void show_photo(void);
void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color);
void  Write_Data_U16(unsigned int y);
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1);
void ClearScreen(unsigned int bColor);
void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
void LCD_PutString(unsigned short x, unsigned short y, char *s, unsigned int fColor, unsigned int bColor);

void LCD_PutChar8x8(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor);
void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor);
void PutGB3232(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor);
//
#ifdef ORIG_8052
unsigned char code pic[];
#else
unsigned rom char pic[];
#endif

//================================================//

void  main(void)
{   
   CS=1;
   delayms(5);
   RES=0;
   delayms(5);
   RES=1;
   delayms(5);
   ILI9325_Initial();
   while(1)
   {
   
ClearScreen(0x0);

//show_colour_bar();   

LCD_PutString(2,40,"http://shop58797699.taobao.com/",Red,Blue);                  ///Foreground color and background color, can be directly pre-defined, it can fill in 16 hexadecimal numbers
LCD_PutString(40,60,"I LOVE MY JOB ",Blue2,Yellow);
// supplied missing quotes
LCD_PutString(40,80,"Yao Shuo ElectronicMagenta,Green");
LCD_PutString(40,100,"Professional technical support forum",0x07e0,0xf800);
LCD_PutString(40,120,"Professional Development Board",0xF800,Yellow);
LCD_PutString(40,140,"1234567890",0xF800,Yellow);
LCD_PutString(20,160,"abcdefghijklmnopqistuvwxyz",0xF800,Yellow);
LCD_PutString(20,180,"`,./<>';:[]{}\|?)(-=+*&^%$",0xF800,Yellow);
PutGB3232(20,200,"Love",Blue,Yellow);   //32x32 Chinese characters to write
PutGB3232(60,200,"Single",Blue,Red);
PutGB3232(100,200,"red",Blue,Magenta);
PutGB3232(140,200,"bye",Blue,Green);
PutGB3232(180,200,"hi",Blue,Cyan);   
PutGB3232(20,240,"come",Blue,0X00);   
PutGB3232(60,240,"go",Blue,0X00);   
PutGB3232(100,240,"dind",Blue,0X00);   
PutGB3232(140,240,"bund",Blue,0X00);   
PutGB3232(180,240,"shang",Blue,0X00);
PutGB3232(20,280,"chik",Blue,0X00);     
PutGB3232(60,280,"chap",Blue,0X00);   
PutGB3232(100,280,"sa",Blue,0X00);   
PutGB3232(140,280,"pu",Blue,0X00);   
PutGB3232(180,280,"ra",Blue,0X00);   

//   show_photo();

      while(1);   


   }
}


void ClearScreen(unsigned int bColor)
{
unsigned int i,j;
LCD_SetPos(0,240,0,320);//320x240
for (i=0;i<320;i++)
   {
   
      for (j=0;j<240;j++)
          Write_Data_U16(bColor);

   }
}

/////////////////////////////////////////////////////////////
// moved to TOF #include "8X16.h"
void LCD_PutChar8x16(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor)
{
unsigned int i,j;
LCD_SetPos(x,x+8-1,y,y+16-1);
for(i=0; i<16;i++) {
      unsigned char m=Font8x16[c*16+i];
      for(j=0;j<8;j++) {
         if((m&0x80)==0x80) {
            Write_Data_U16(fColor);
            }
         else {
            Write_Data_U16(bColor);
            }
         m<<=1;
         }
      }
}


void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor) {

      LCD_PutChar8x16( x, y, c, fColor, bColor );
   }


// moved to TOF #include "GB1616.h"   //16 * 16 character font

void PutGB1616(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor){
   unsigned int i,j,k;

   LCD_SetPos(x,  x+16-1,y, y+16-1);

   for (k=0;k<64;k++) { // 64 marked the number of self in Chinese characters library, circulation check inner code)
     if ((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])){
       for(i=0;i<32;i++) {
        unsigned short m=codeGB_16[k].Msk[i];
        for(j=0;j<8;j++) {
         if((m&0x80)==0x80) {
            Write_Data_U16(fColor);
            }
         else {
            Write_Data_U16(bColor);
            }
         m<<=1;
         }
        }
      } 
     }   
   }

// moved to TOF    #include "GB3232.h"   //32 * 32 character font

void PutGB3232(unsigned short x, unsigned short  y, unsigned char c[2], unsigned int fColor,unsigned int bColor){
   unsigned int i,j,k;

   LCD_SetPos(x,  x+32-1,y, y+32-1);

   for (k=0;k<15;k++) { //15 mark in the number of Chinese characters library self, loop the query code
     if ((codeGB_32[k].Index[0]==c[0])&&(codeGB_32[k].Index[1]==c[1])){
       for(i=0;i<128;i++) {
        unsigned short m=codeGB_32[k].Msk[i];
        for(j=0;j<8;j++) {
         if((m&0x80)==0x80) {
            Write_Data_U16(fColor);
            }
         else {
            Write_Data_U16(bColor);
            }
         m<<=1;
         }
        }
      } 
     }   
   }

void LCD_PutString(unsigned short x, unsigned short y, unsigned char *s, unsigned int fColor, unsigned int bColor) {
    unsigned char l=0;
   while(*s) {
      if( *s < 0x80)
          {
         LCD_PutChar(x+l*8,y,*s,fColor,bColor);
         s++;l++;
         }
      else
          {
         PutGB1616(x+l*8,y,(unsigned char*)s,fColor,bColor);
         s+=2;l+=2;
         }
      }
   }
//==================== R G B color display ====================//



void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
{
   unsigned int i,j;
   //address_set();
   LCD_SetPos(x0,x1,y0,y1);
   for (i=y0;i<=y1;i++)
   {
      for (j=x0;j<=x1;j++)
          Write_Data_U16(Color);

   }


}

//====================== Display Color ======================//
void show_colour_bar (void)

{
   int V,H;
   LCD_SetPos(0,240,0,320);//320x240

   for(H=0;H<240;H++)
   {
      for(V=0;V<40;V++)
      Write_Data(0xf8,0x00);
   }

   for(H=0;H<240;H++)
   {
      for(V=40;V<80;V++)
      Write_Data(0x07,0xe0);
   }

   for(H=0;H<240;H++)
   {
      for(V=80;V<120;V++)
      Write_Data(0x00,0x1f);
   }

   for(H=0;H<240;H++)
   {
      for(V=120;V<160;V++)
      Write_Data(0xff,0xe0);
   }

   for(H=0;H<240;H++)
   {
      for(V=160;V<200;V++)
      Write_Data(0xf8,0x1f);
   }

   for(H=0;H<240;H++)
   {
      for(V=200;V<240;V++)
      Write_Data(0x07,0xff);
   }

   for(H=0;H<240;H++)
   {
      for(V=240;V<280;V++)
      Write_Data(0xff,0xff);
   }

   for(H=0;H<240;H++)
   {
      for(V=280;V<320;V++)
      Write_Data(0x00,0x00);
   }

}

void show_photo(void)
{
   unsigned char j;
   unsigned int i;
   unsigned long s=0;

   LCD_SetPos(0,240,0,320);//320x240

   for (i=0;i<75;i++)
   {
   for (j=0;j<240;j++)
   Write_Data(0xff,0xff);
     
   }

   for (i=0;i<170;i++)
   {
   for (j=0;j<55;j++)
      Write_Data(0xff,0xff);

      for (j=0;j<130;j++)
      Write_Data(pic[s++],pic[s++]);
     
      for (j=0;j<55;j++)
      Write_Data(0xff,0xff);
   }

    for (i=0;i<75;i++)
   {
   for (j=0;j<240;j++)
   Write_Data(0xff,0xff);
     
   }

   }

//=======================================================//



void  Write_Cmd_Data (unsigned char x,unsigned int y)
{
   unsigned char m,n;
   m=y>>8;
   n=y;
   Write_Cmd(0x00,x);
   Write_Data(m,n);

}


void  Write_Data_U16(unsigned int y)
{
   unsigned char m,n;
   m=y>>8;
   n=y;
   Write_Data(m,n);

}
//====================== Write command ==========================//

void Write_Cmd(unsigned char DH,unsigned char DL)
{
 
   

   CS=0;
   RS=0;

   P1=DH;
   RW=0;
   RW=1;

   P1=DL;
   
   RW=0;
   RW=1;
   CS=1;
}

//===================== Write data ===========================//

void Write_Data(unsigned char DH,unsigned char DL)
{

   CS=0;
   
   RS=1;
   P1=DH;
   RW=0;
    RW=1;

   P1=DL;   
   RW=0;
   RW=1;
   CS=1;
}

//=======================================================
void delayms(unsigned int count)
{
    int i,j;                                                                               
    for(i=0;i<count;i++)                                                                   
       {
#ifdef ORIG_8052
        for(j=0;j<260;j++);
#else       
        Delay1KTCYx( x * FOSC_MHZ / 4 ) ;  // Use uChip C18 compiler lib delay function
#endif       
       }                                                                                     
}


//===================== Initialization code =======================//

void ILI9325_Initial(void)
{
//############# void Power_Set(void) ################//
Write_Cmd_Data(0x0000,0x0001);
delayms(100);

Write_Cmd_Data(0x0015,0x0030);
Write_Cmd_Data(0x0011,0x0040);
Write_Cmd_Data(0x0010,0x1628);
Write_Cmd_Data(0x0012,0x0000);
Write_Cmd_Data(0x0013,0x104d);
delayms(100);
Write_Cmd_Data(0x0012,0x0010);
delayms(100);
Write_Cmd_Data(0x0010,0x2620);
Write_Cmd_Data(0x0013,0x344d); //304d
delayms(100);

Write_Cmd_Data(0x0001,0x0100);
Write_Cmd_Data(0x0002,0x0300);
Write_Cmd_Data(0x0003,0x1030);
Write_Cmd_Data(0x0008,0x0604);
Write_Cmd_Data(0x0009,0x0000);
Write_Cmd_Data(0x000A,0x0008);

Write_Cmd_Data(0x0041,0x0002);
Write_Cmd_Data(0x0060,0x2700);
Write_Cmd_Data(0x0061,0x0001);
Write_Cmd_Data(0x0090,0x0182);
Write_Cmd_Data(0x0093,0x0001);
Write_Cmd_Data(0x00a3,0x0010);
delayms(50);

//################# void Gamma_Set(void) ####################//
Write_Cmd_Data(0x30,0x0000);     
Write_Cmd_Data(0x31,0x0502);     
Write_Cmd_Data(0x32,0x0307);     
Write_Cmd_Data(0x33,0x0305);     
Write_Cmd_Data(0x34,0x0004);     
Write_Cmd_Data(0x35,0x0402);     
Write_Cmd_Data(0x36,0x0707);     
Write_Cmd_Data(0x37,0x0503);     
Write_Cmd_Data(0x38,0x1505);     
Write_Cmd_Data(0x39,0x1505);
delayms(50);

//################## void Display_ON(void) ####################//
Write_Cmd_Data(0x0007,0x0001);
delayms(50);
Write_Cmd_Data(0x0007,0x0021);
Write_Cmd_Data(0x0007,0x0023);
delayms(50);
Write_Cmd_Data(0x0007,0x0033);
delayms(50);
Write_Cmd_Data(0x0007,0x0133);

}

/*===========================================================*/

/*************************************************************
Function name: LCD_DefineDispWindow
Function: define display form
Parameters: x0: X coordinate in the form of the smaller
x1: X coordinate in the form of the greater
y0: Y coordinates form the smaller
y1: Y coordinates form the greater
Return Value: None
*************************************************************/
static void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
{
  Write_Cmd_Data(WINDOW_XADDR_START,x0);
  Write_Cmd_Data(WINDOW_XADDR_END,x1);
  Write_Cmd_Data(WINDOW_YADDR_START,y0);
  Write_Cmd_Data(WINDOW_YADDR_END,y1);
  Write_Cmd_Data(GRAM_XADDR,x0);
  Write_Cmd_Data(GRAM_YADDR,y0);
  Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
}

<=>
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sun Sep 19, 2010 11:14 am

Many thanks, I'll now go for compiling it. I just don't have 48Mhz today to try with 18F4550, but I'm really eager to see if I can put the LCD to do some work, at least to change color on screen! That's why I thought to use PIC16 for today.

I'll now append also those other *.h files because I get a "syntax error" in them I can't get rid of, may be you could kindly point me out of it.

Edit:
I get this error:
Code: Select all
C:\Documents and Settings\WHW\Desktop\prj\PIC\tutorials\LCD_TOUCH_SD\9325TP.C:57:Error [1205] unknown member 'RD0' in '__tag_51'

may be the port definition as:
Code: Select all
#define  CS LATDbits.RD0

is not right with C18?
This is my 57th line:
Code: Select all
   CS=1;

means it didn't understand the "define" statement right?

Also, it complains if I try to compile it for PIC16F877 by changing to:
Code: Select all
#include "p16f877.h"
Attachments
all_files.zip
All the files *.c and *.h here
(13.2 KiB) Downloaded 68 times
Last edited by arix on Sun Sep 19, 2010 11:56 am, edited 1 time in total.
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Re: PIC MCU and LCD

Postby polly » Sun Sep 19, 2010 11:54 am

That's the thing, you don't need a 48MHz crystal. The configuration bits are such that a
20MHz xtal is divided by 5 for the proper 4Mhz input to PLL which 96MHz output is divided by 2
for a 48MHz system clock.
<=>
polly
 
Posts: 190
Joined: Mon Jun 05, 2006 2:10 pm

Re: PIC MCU and LCD

Postby arix » Sun Sep 19, 2010 12:04 pm

polly wrote:That's the thing, you don't need a 48MHz crystal. The configuration bits are such that a
20MHz xtal is divided by 5 for the proper 4Mhz input to PLL which 96MHz output is divided by 2
for a 48MHz system clock.


you mean keeping the 48Mhz definition there, but physically using a 20Mhz?

Edit:
I've errors do to using LAT in defining ports. I changed to PORTB and PORTDbits and the errors of that kind disappeard, but anyway such failure:
Code: Select all
----------------------------------------------------------------------
Debug build of project `C:\Documents and Settings\WHW\Desktop\prj\PIC\tutorials\LCD_TOUCH_SD\lcdtst.mcp' started.
Preprocessor symbol `__DEBUG' is defined.
Sun Sep 19 22:14:10 2010
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Documents and Settings\WHW\Desktop\prj\PIC\tutorials\LCD_TOUCH_SD\9325TP.o".
Clean: Done.
Executing: "C:\mcc18\bin\mcc18.exe" -p=18F4550 "9325TP.C" -fo="9325TP.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
Executing: "C:\mcc18\bin\mplink.exe" /l"C:\MCC18\lib" "9325TP.o" /z__MPLAB_BUILD=1 /z__MPLAB_DEBUG=1 /o"lcdtst.cof" /M"lcdtst.map" /W
MPLINK 4.1, Linker
Copyright (c) 2006 Microchip Technology Inc.
Error - linker command file required.
Errors    : 1

Link step failed.
----------------------------------------------------------------------
Debug build of project `C:\Documents and Settings\WHW\Desktop\prj\PIC\tutorials\LCD_TOUCH_SD\lcdtst.mcp' failed.
Preprocessor symbol `__DEBUG' is defined.
Sun Sep 19 22:14:11 2010
----------------------------------------------------------------------
BUILD FAILED


And this one is from *.h that seems to be illegal definition for C18 at all:
Code: Select all
C:\Documents and Settings\WHW\Desktop\prj\PIC\tutorials\LCD_TOUCH_SD\GB3232.h:12:Error: syntax error
arix
 
Posts: 58
Joined: Wed Dec 17, 2008 7:11 am

Next

Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest