Author Topic: PIC MCU and LCD  (Read 1301 times)

0 Members and 1 Guest are viewing this topic.

Offline arixIITopic starter

  • Jr. Member
  • **
  • Posts: 8
  • Helpful? 0
PIC MCU and LCD
« on: September 18, 2010, 02:53:06 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]
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 C are assigned to CS, RES, RS, and RW of LCD, right? Then where are data pins? O_O

What exactly I need to know is how "physically" connect the PIC and LCD, i.e. which PIN of PIC to connect to which of LCD. The pin assignment style I know is like this:
Code: [Select]
void main()
{
TRISB = 0; // Configure PORTB as output
PORTB = 0xAA; // Turn on odd numbered port pins
}

but in the code bellow you see:
Code: [Select]
//===================== 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;
}

i.e. DL is assigned to the pin called "P1", and where is that PIN on my PIC? is it P1A, P1B, P1C...? I'm going to use a PIC18F4550 with this LCD. How to understand this P1, P2^x style please?

Here is all info I have:

Code: [Select]
#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);
}





Offline Abdulla M.A.

  • Jr. Member
  • **
  • Posts: 30
  • Helpful? 0
    • My personal website
Re: PIC MCU and LCD
« Reply #1 on: September 19, 2010, 12:46:36 AM »
GLCD data sheet??? the name of it's controller?

Abdulla
"A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engin

paulstreats

  • Guest
Re: PIC MCU and LCD
« Reply #2 on: September 19, 2010, 11:22:24 AM »
Where you mention above that the data appears to be going to the P0 pin, this is actually going to the full port(P0 means PORT0 on ARM's where PORT0 is a collection of 8 pins or possibly more depending).

 The code is likely made for an ARM mcu where the port are labelled p0, p1 etc Pics use Port A, Port B etc.

 By the looks of it the code is just using the LCD in 8bit mode (rather than the 16 bit mode. It does use 16 bit mode but just uses the 8bit data bus by sending the highest 8 bit first and then sending the lowest 16 bits). So Just connect PORT D on your PIC to DB0 - DB7 on the LCD.
 in the code section instead of
P1 = DH;
use:
PORTD = DH;


Where it says


sbit RES=P2^7;
It is assisgning Pin7 on Port2 to RES, Rather than assigning PIN2 to RES, which is where I think your confusion lies (Just a difference in port and pin assigning/ labelling between microcontrollers).
« Last Edit: September 19, 2010, 12:16:10 PM by paulstreats »

 


Get Your Ad Here

data_list