LCD Display

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

LCD Display

Postby PICTRAIL » Thu Jan 27, 2011 10:17 am

Hi, i bought PR29 prototype board from cytron last year november and i downloaded the sample code from online which worked perfectly until december. Early this year i formated my PC after which i needed to install new MPLAB, PIC2KIT and Hitech C Compiler. Again i downloaded same sample source code from http://www.cytron.com.my/viewProduct.ph ... !!!In9pIY= to test my device. The problem now is that my LCD is powered but i can't see any message whereas it works on simulation with RealPic.

Is anyone going through same problem? What be the likely solution... Somebody kindly help.

Regards,
Ebby.
PICTRAIL
Apprentice
 
Posts: 40
Joined: Mon Dec 27, 2010 3:32 pm

Re: LCD Display

Postby ABSF » Thu Jan 27, 2011 11:05 am

PICTRAIL WROTE:i bought PR29 prototype board from cytron last year november and i downloaded the sample code from online which worked perfectly until december.

Early this year i formated my PC after which i needed to install new MPLAB, PIC2KIT and Hitech C Compiler. Again i downloaded same sample source code to test my device. The problem now is that my LCD is powered but i can't see any message whereas it works on simulation with RealPic.

Regards,
Ebby.


OK, PR29 is a fingerprint attendance system using '876A, right?

What problem did you face in December, when it was not working properly?

What did you see on the LCD early this year after you loaded in the program? A flashing cursor or dark rectangles?

What do you mean by "works on simlation with RealPic"? Is "RealPic" some kind of PIC simulation program?

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: LCD Display

Postby PICTRAIL » Thu Jan 27, 2011 9:31 pm

Hi Allen,
Yes! PR29 is a fingerprint attendance system.
In december there was no problem the device worked perfectly. I was able to see message on LCD.
After reinstalling MPLAB and PIC2KIT this year, i can only see dark rectangles on the first row of the 2x16 LCD.
I meant when i run software simulation to check the code, i use a software called "REALPIC" to check if there is error with my hex file but it works well and perform according to my program.
The main error now is that if i load same hex file into my program, i will only see dark rectangle. I tried with 2 different PIC.

Thanks for your anticipated assistance.


Ebby.
PICTRAIL
Apprentice
 
Posts: 40
Joined: Mon Dec 27, 2010 3:32 pm

Re: LCD Display

Postby ABSF » Fri Jan 28, 2011 12:13 am

After reinstalling MPLAB and PIC2KIT this year, i can only see dark rectangles on the first row of the 2x16 LCD.


Since I dont have any experience using PR#29. I will base my trouble-shooting using the schematics and the User Manual.

The Fingerprint device is connected to UART, and the PC (GUI) is connected to another UART. With these 2 disconnected, you should be able to get a message on the LCD screen when the power is up, right? Then the message will prompt you to choose either "User Mode" or "Admin Mode". On the schematics, the LCD to PIC connections are straight forward using PB for data and RC4 & RC5 for E and RS.

There are 3 possibilities that would cause no disply on LCD. 1. software code 2. PIC 16F876 3. LCD itself.

Since you have already tested the software using a simulator, you can rule out possibility #1.

Then I would confirm that the PIC is good. To do this I would use the PICKit2 to confirm. Did you program the PIC successfully with the "success message" from the PICKit2 ? If the answer is YES, then what is left is the LCD itself. I would confirm that the contrast preset is good using an ohm meter? I read from one of the thread that a faulty preset of the SK40C could cause problems on the LCD too. If that is still OK, I would suspect that the LCD itself is faulty. Do you have another LCD to confirm?

You can also use another program from SK40C to test the LCD. The program was for 16F877 and it uses RD0-7 for data and RB 4 & 5 for RS and E. So you need to modify it slightly in order to use it to test your LCD.

Whether you have formated the PC or not doesnt afftect much on the working of PR29 as long as your PICKIT2 programmer is working and the original HEX file is OK.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: LCD Display

Postby PICTRAIL » Fri Jan 28, 2011 1:16 pm

Thanks Allen,
I've tried everything you mentioned but the problem still persist. I tried with both PR29 evaluation kit and likewise SK40C only thing i can see is Rectangle Boxes on the first row of my 2x16 LCD. I even tried to adjust the contrast but nothing shows... It's really frustrating.
PICTRAIL
Apprentice
 
Posts: 40
Joined: Mon Dec 27, 2010 3:32 pm

Re: LCD Display

Postby ABSF » Fri Jan 28, 2011 4:08 pm

You mean that you have changed the LCD?

If the LCD is changed and you still have the same problem. It is highly possible that the continuity between the 10 wires of LCD to the PIC RB and RC4&5 is opened somewhere. May be a dry joint at the PIC socket or at the LCD. I have the some problem with my LCD when I first used it with my SK40C. After I soldered the LCD pins again, the fault was gone.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: LCD Display

Postby shiyan » Fri Jan 28, 2011 10:38 pm

ABSF WROTE:You mean that you have changed the LCD?
If the LCD is changed and you still have the same problem. It is highly possible that the continuity between the 10 wires of LCD to the PIC RB and RC4&5 is opened somewhere. May be a dry joint at the PIC socket or at the LCD. I have the some problem with my LCD when I first used it with my SK40C. After I soldered the LCD pins again, the fault was gone.
Allen


Woh... changed the LCD already and still have problem on getting message? Might be connection problem like allen said. I did it before too. If possible, check the connection using multimeter, use the continuity check, pin by pin, refer to the schematic. Starting from RS, R/W, E, DB7......DB0.

Good luck
User avatar
shiyan
Amateur
 
Posts: 189
Joined: Wed Jun 09, 2010 10:59 am

Re: LCD Display

Postby PICTRAIL » Sat Jan 29, 2011 12:25 pm

Thank you so much everyone... At last i got it....
The problem was with PIN declaration in MPLAB program. PIN connection on hardware was different from the declared PINs in the program.

My next problem is with GLCD. Can someone tell me the function of ChipSet 1 and ChipSet 2. i dont really understand how i can use it with PIC16F877A.

Program below is for PIC18F452 (Cytron PR21). Please how can i modify this to be used for PIC16F877A.
Thanks for your kind support. I wish you all GONG XI FA CAI.

CODE: SELECT_ALL_CODE
//===========================================================================================
//   Author            :CYTRON   TECHNOLOGIES
//   Project            :DIY Project(PR21-Gaming with Accerlerometer)
//   Project description   :Use PIC18F452 microcontroller together with the ADXL330
//                   accelerometer to measure the gravity force and DS-LCD-TG12864B-03
//                       Graphical LCD (GLCD) for display.
//===========================================================================================


//===========================================================================================
//   Include
//===========================================================================================
#include <p18f452.h>

//============================================================================================
//   Configuration
//============================================================================================

#pragma config OSC = HSPLL, OSCS = ON
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config WDT = OFF
#pragma config CCP2MUX = OFF
#pragma config LVP = OFF                  //configuration for the  microcontroller
   
//===========================================================================================               
//   Define
//============================================================================================
#define   lcd_data   LATD                  //LCD 8-bit data PORT
#define   rst         LATCbits.LATC4
#define   rs         LATCbits.LATC5            //RS pin of the LCD display
#define   rw         LATCbits.LATC6            //R/W pin of the LCD display   
#define   e         LATCbits.LATC7            //E pin of the LCD display
#define psb         LATCbits.LATC3
#define   b_light      LATCbits.LATC2            //backlight of the LCD display (1 to on backlight)

#define   buzzer      LATCbits.LATC0            //buzzer (1 to on buzzer)
#define   sw1         PORTBbits.RB0            //button (active low)
#define   sw2         PORTBbits.RB1            //button (active low)
#define z_axis       PORTAbits.RA0
#define y_axis       PORTAbits.RA1
#define   x_axis      PORTAbits.RA2            
#define preset      PORTAbits.RA4
#define st          LATBbits.LATB2
#define   channel0     0b10000001               // Configuration for read ADC channel 0 (RA0)
#define   channel1    0b10001001               // Configuration for read ADC channel 1 (RA1)
#define   channel2   0b10010001               // Configuration for read ADC channel 2 (RA2)



//==============================================================================================
// variable
//==============================================================================================
int xaxis ,yaxis, zaxis,p;
int x_off,y_off,z_off;

char d_data, d_data1;
char p_x,p_y;
unsigned int sens = 500;
signed long x_velo=0,y_velo=0,x_dis=0,y_dis=0,last_x=0,last_y=0;

char test[] = {"testing0"};
char cali [] = {"  CALIBRATE...  "};
char mssg1[] = {"  LET IT FLAT "};
char mssg2[] = {" AND PRESS SW1 "};
char mssg3[] = {"FREESPACE      "};
char mssg4[] = {"FRICTION       "};

//==============================================================================================
// Graphic Picture Data
//==============================================================================================
rom char graphic[3][1024] = {{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFE,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3F,0x80,0x01,0x80,0x00,0x00,0x03,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x7F,0x80,0x03,0x80,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xF3,0x80,0x07,0x00,0x00,0x00,0x00,0x3C,0x40,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xC3,0xB8,0xFF,0xBF,0x3E,0x1F,0xC0,0x0E,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x80,0x39,0xDF,0xBE,0xFE,0x3F,0xC0,0x07,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x80,0x39,0xDF,0xBE,0xFE,0x3F,0xC0,0x03,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x80,0x3B,0x9E,0x71,0xC2,0x39,0xC0,0x01,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x84,0x3F,0x9C,0x61,0xC6,0x39,0xC0,0x01,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0x8F,0x3E,0x3C,0xE1,0x86,0x73,0x80,0x01,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x03,0xFE,0x3C,0x3E,0xC1,0xFE,0x63,0x80,0x01,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0xFC,0x3C,0x3E,0xC1,0xFE,0xE3,0x80,0x01,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xF8,0x38,0x1D,0xC0,0xF9,0xE7,0x00,0x03,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xF0,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
},
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xF8,0x07,0x03,0xC3,0xC7,0xFC,0x00,0x78,0x70,0x1D,0xFF,0x1F,0xE0,0x00,
0x00,0x03,0xFC,0x07,0x03,0xC3,0xC7,0xFC,0x01,0xFE,0x38,0x39,0xFF,0x1F,0xF0,0x00,
0x00,0x03,0x9E,0x0F,0x83,0xE7,0xC7,0x00,0x01,0xCE,0x38,0x39,0xC0,0x1C,0x70,0x00,
0x00,0x07,0x0C,0x0F,0x83,0xE7,0xC7,0x00,0x03,0x87,0x3C,0x79,0xC0,0x1C,0x70,0x00,
0x00,0x07,0x00,0x1D,0xC3,0xE7,0xC7,0x00,0x03,0x87,0x1C,0x71,0xC0,0x1C,0x70,0x00,
0x00,0x07,0x3E,0x1D,0xC3,0xA5,0xC7,0xF8,0x03,0x87,0x1C,0x71,0xFE,0x1F,0xE0,0x00,
0x00,0x07,0x3E,0x3D,0xE3,0xBD,0xC7,0xF8,0x03,0x87,0x1E,0xF1,0xFE,0x1F,0xC0,0x00,
0x00,0x07,0x0E,0x38,0xE3,0xBD,0xC7,0x00,0x03,0x87,0x0E,0xE1,0xC0,0x1C,0xE0,0x00,
0x00,0x07,0x0E,0x3F,0xE3,0xBD,0xC7,0x00,0x03,0x87,0x0E,0xE1,0xC0,0x1C,0xE0,0x00,
0x00,0x03,0x9E,0x7F,0xF3,0x99,0xC7,0x00,0x01,0xCE,0x07,0xC1,0xC0,0x1C,0x70,0x00,
0x00,0x03,0xFE,0x70,0x73,0x99,0xC7,0xFC,0x01,0xFE,0x07,0xC1,0xFF,0x1C,0x70,0x00,
0x00,0x00,0xF8,0xE0,0x3B,0x99,0xC7,0xFC,0x00,0x78,0x07,0xC1,0xFF,0x1C,0x38,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
},
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x93,0x93,0x05,0x38,0x39,0x82,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x92,0x12,0x05,0x20,0x19,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0x90,0x92,0x05,0x08,0x29,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x13,0x93,0x07,0x38,0x3D,0x82,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x04,0x84,0x84,0x83,0x94,0xCC,0xE7,0x07,0x39,0xF8,0x3F,0x28,0x00,0x00,
0x00,0x00,0x16,0x96,0x96,0x82,0x14,0x88,0x95,0x84,0x25,0x68,0x2D,0x28,0x00,0x00,
0x00,0x00,0x1B,0x1B,0x1B,0x02,0x1C,0x88,0x94,0x84,0x25,0x08,0x21,0x38,0x00,0x00,
0x00,0x00,0x09,0x09,0x09,0x13,0x88,0xC8,0xE4,0xA7,0x39,0x09,0x21,0x10,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
}
};

//=====================================================================================
//Icon graphic data
//=====================================================================================
rom char icon [8] = { 0x00,0x18,0x3C,0x7E,0x7E,0x3C,0x18,0x00};

//=====================================================================================
//   function prototype            (every function must have a function prototype)
//=====================================================================================
void init(void);
void delay(unsigned long data);         
void send_config(unsigned char data);
void lcd_read(char x, char y);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);

void lcd_xy(unsigned char x,unsigned char y);
void d_graphic(char k);
void clr_graphic(void);
void d_icon(int x,int y);   
void freespace(void);
void friction(void);
void box(void);
void calibrate(void);

unsigned int read_adc(char config);



//==================================================================================
//   main function               (main fucntion of the program)
//===================================================================================
void main(void)
{
   //assign variable
   unsigned char i=0,database;            
   unsigned char data[10];
   unsigned int m,j;

   init();
   d_graphic(0);                           //display cytron logo
   delay(300000);                           //delay for pic display

   // display claibrating messages
   send_config(0b00110000);                  //set to function set configuration
   lcd_clr();                              //clear the lcd
   send_string(cali);                        //display string
   lcd_goto (8);                           //cursor at 3rd character line
   send_string(mssg1);                        //display string mssg1
   lcd_goto(24);                           //cursor at 4th  character line
   send_string(mssg2);                        //display string mssg2

   while(sw1);                              //wait for button pushed
   while(!sw1);                           //wait for button release
   calibrate();                           //taking the current force as reference for flat plane
   lcd_clr();                              //clear the lcd
   

   lcd_goto(1);                           //cursor at 3rd character, 1st character line
   send_string(mssg3);                        //display string mssg3
   lcd_goto(17);                           //cursor at 3rd character, 2st character line
   send_string(mssg4);                        //display string mssg4
   lcd_goto(0);                           //cursor at 1st character, 1st character line
   send_char(0x10);                        //display symbol arrow
   m =0 ;                                 //set the variable 0

   while(1)                              //loop forever
   {

      if (!sw1)                           //if sw1 is pressed, increase mode
      {
         while(!sw1);                     //wait until sw1 is release
         m++;                           //increament m
         if (m>1) m=0;                     //if m more than 1 , clear m

         if (m==1)                         //if m is 1
         {
            lcd_goto(0);                  //cursor at 1st line
            send_char('  ');               //space 1st and 2nd character , 1st line
            lcd_goto(16);                  //cursor at 2nd line
            send_char(0x10);               //display arrow at 2nd charater, 2nd line
         }
         else
         {
            lcd_goto(0);                  //cursor at 1st line
            send_char(0x10);               //display arrow at 2nd charater, 1st line
            lcd_goto(16);                  //cursor at 2nd line
            send_char('  ');               //space 1st and 2nd character , 2nd line
         }
      }

      else if (!sw2)                        //if sw is pressed
      {
         while(!sw2);                     //wait until sw2 is released
         lcd_clr();                        //clear all character
         clr_graphic();                     //clear all graphic

         // 16 bit timer initial status and start timer
         TMR0H = 194;                     // value for timer reach overflow in 50 ms for clock 10MIPS      
         TMR0L = 246;
         T0CONbits.TMR0ON = 1;               //start timer

         while(1)                        //loop forever
         {
            if (INTCONbits.TMR0IF==1)         //if timer over flow ( occur every 50 ms) // to update data every 50 ms
            {
               INTCONbits.TMR0IF==0;         //clear the flag bit
               TMR0H=194;                  //value for timer reach overflow in 50 ms for clock 10MIPS      
               TMR0L=246;
               if (m==1) friction();         //if mode is 1 , the do calculation friction, else do calculation freespace.
               else    freespace();         //update the data ( required for more accurate intergration ( faster, more accurate ))

               i++;                     //increment variable i
            }
               
            if (i==10)                      //if i reach ten ( around 500 ms) update the icon position
            {
               if((x_dis/sens >60)||(x_dis/sens<-60)||(y_dis/sens>28)||(y_dis/sens<-28))   //if the icon position not in range of glcd
               {                                                               //mean 'the ball drop out from glcd'
                  d_graphic(1);            //display graphic
                  delay(300000);            //dealy for display the graphic
                  d_graphic(2);            //display another graphic
                  delay(300000);            
                   while(1);
               }
               d_icon(60-(x_dis/sens),28+(y_dis/sens));            //display icon at updated x,y position
               i=0;                     //clear the variable i
            }
         }
      }   
   }
}

//==============================================================================
//initialization
//===============================================================================================================================

void init()
{
   //set I/O input output
   TRISD = 0b00000000;               //configure PORTB I/O direction
   TRISC = 0b00000000;               //configure PORTC I/O direction
   TRISB = 0b00000011;
   TRISA = 0b00111111;               //configure PORTA I/O direction
   
   // ADC configuration   
   ADCON1 = 0b11000011;            //set all portA as Analog Input, right justified   

   //Timer 0 configuration
   T0CON = 0b00000100;               //prescale 1: 32


   // setup for pwm               //For controlling back light
   PR2 = 255;
   T2CON =     0b00000100;            //prescale 1:
   CCP1CON =   0b00001100;            //Configuration for pwm
   CCP2CON =   0b00001100;

   //configure lcd
   rst = 0;
   rst = 1;
   delay (100000);                  
   psb  = 1;

   //basic instruction config
   send_config(0b00110000);         //function set
   lcd_clr();                     //clear display at lcd
   send_config(0b00000010);         //lcd return to home
   send_config(0b00000110);         //entry mode-cursor increase 1
   send_config(0b00001100);         //display on, cursor off and cursor blink off

   //set initial condition
   send_config(0b00110100);         //extended function set
   send_config(0b00000010);
   send_config(0b00000100);
   send_config(0b00110000);         //function set
   
   lcd_clr();                     //clear lcd
   CCPR1L = 50;                  //set the brightness of the back lcd

}


//   functions
//===============================================================================================================================
void beep(void)                     //short beep function
{
   buzzer=1;                     //on buzzer
   delay(10000);                  //short delay
   buzzer=0;                     //off buzzer
   delay(10000);                  //short delay
}

//calculation for freespace
//===============================================================================================================================
void freespace(void)               
{
   xaxis=read_adc(channel0);                     //read the force acting on x axis
   yaxis=read_adc(channel1);                     //reat the force acting on y axis

   xaxis-=x_off;                              //offset with value obtain before game start
   yaxis-=y_off;                              //offset with value obtain before game start

   if (((last_x-xaxis)==1)||((xaxis-last_x)==1)) xaxis=0;   //filter noise
   if (((last_y-yaxis)==1)||((last_y-yaxis)==-1)) yaxis=0;   //filter noise

   x_velo+=xaxis/2;                           //intergrate the force resultance velocity
   y_velo+=yaxis/2;                           //intergrate the force resultance velocity

   x_dis+=x_velo;                              //intergrate the velocity resultance distance
   y_dis+=y_velo;                              //intergrate the velocity resultance distance
}

//calculation for constant friction (
//===============================================================================================================================
void friction(void)
{
   xaxis=read_adc(channel0);                     //read the force acting on x axis
   yaxis=read_adc(channel1);                     //reat the force acting on y axis

   xaxis-=x_off;                              //offset with value obtain before game start
   yaxis-=y_off;                              //offset with value obtain before game start

   if (((last_x-xaxis)==1)||((xaxis-last_x)==1)) xaxis=0;      //filter noise
   if (((last_y-yaxis)==1)||((last_y-yaxis)==-1)) yaxis=0;      //filter noise

   x_velo+=xaxis/2;                           //intergrate the force resultance velocity
   y_velo+=yaxis/2;                           //intergrate the force resultance velocity

   if(x_velo>0) x_velo--;                        //decreace the velocity ( making like having friction force )
   else if (x_velo<0) x_velo++;
   
   if(y_velo>0) y_velo--;
   else if (y_velo<0) y_velo++;

   x_dis += x_velo;                           //intergrate the velocity resultance distance
   y_dis += y_velo;                           //intergrate the velocity resultance distance
}

// ADC function
//===============================================================================================================================
unsigned int read_adc(char config)
{
   unsigned short i;
   unsigned long result ,result_temp=0;

   ADCON0 = config;
   delay(1000);                              // delay after changing configuration
   for(i=20;i>0;i-=1)                           //looping 200 times for getting average value
   {
      ADCON0bits.GO = 1;                        //ADGO is the bit 2 of the ADCON0 register
      while(ADCON0bits.GO==1);                  //ADC start, ADGO=0 after finish ADC progress
      result=ADRESH;
      result=result<<8;                        //shift to left for 8 bit
      result=result|ADRESL;                     //10 bit result from ADC

      result_temp+=result;      
   }
   result = result_temp/20;                     //getting average value
   ADCON0bits.ADON = 0;                        //adc module is shut off
   return result;
}

void calibrate(void)
{
      x_off = read_adc(channel0);                  //read x_axis accelero value and save as offset
      y_off = read_adc(channel1);                  //read x_axis accelero value and save as offset
      z_off = read_adc(channel2);                  //read x_axis accelero value and save as offset
}



// LCD function
// Config function
//===============================================================================================================================
void delay(unsigned long data)                     //delay function, the delay time
{                                          //depend on the given value
   for( ;data>0;data-=1);   
}

void send_config(unsigned char data)               //send lcd configuration
{
   rw=0;                                    //set lcd to write mode
   rs=0;                                    //set lcd to configuration mode
   lcd_data=data;                              //lcd data port = data
   e=1;                                    //pulse e to confirm the data
   delay(20);
   e=0;
   delay(20);
}

void send_char(unsigned char data)                  //send lcd character
{
    rw=0;                                    //set lcd to write mode
   rs=1;                                    //set lcd to display mode
   lcd_data=data;                              //lcd data port = data
   e=1;                                    //pulse e to confirm the data
   delay(20);      
   e=0;
   delay(20);
}


// char function (make sure is in basic function before use lcd function)
//===============================================================================================================================

void lcd_goto(unsigned char data)                  //set the location of the lcd cursor
{                              
       send_config(0x80+data);      
// -----------------------------------------------------
// | |00|01|02|03|04|05|06|07| |                   //1 location can store 2 character
// | |17|18|19|...........|15| |
// | |09|10|11|...........|23| |
// | |24|.................|31| |
// -----------------------------------------------------   
}

void lcd_clr(void)                              //clear the lcd
{   
    send_config(0x01);
   delay(600);   
}

void send_string(const char *s)                     //send a string to display in the lcd
{         
   unsigned char i=0;
     while (s && *s)send_char (*s++);
}



void function( char data)                        //1 for extention , 0 for basic  * must off graphic before go back basic function
{
   send_config(0x30 |data << 2);
}


// graphic function ( make sure in extention function before use grapchic funcion )
//===============================================================================================================================
// CGRAM map for GLCD Display

//         y               x (column )
//   row      |    0   | 1   | 2   | 3   | 4   | 5   | 6   | 7   |
//   0     0   |UB   |LB   |   |   |   |   |   |   |   |
//   1     1   |      |   |   |   |   |   |   |   |
//   2     2   |      |   |   |   |   |   |   |   |
//   3     3   |      |   |   |   |   |   |   |   |
//        '   |      |   |   |   |   |   |   |   |
//        '   |      |   |   |   |   |   |   |   |
//        '   |      |   |   |   |   |   |   |   |
//   31    31   |_______|___|___|___|___|___|___|___|
//         |     8   | 9 |10   |11   |12   |13   |14   |15   |
//   32     0   |UB   |LB   |   |   |   |   |   |   |   |
//   33     1   |      |   |   |   |   |   |   |   |
//   34     2   |      |   |   |   |   |   |   |   |
//   35     3   |      |   |   |   |   |   |   |   |
//        '   |      |   |   |   |   |   |   |   |
//        '   |      |   |   |   |   |   |   |   |
//        '   |      |   |   |   |   |   |   |   |
//   63    31   |      |   |   |   |   |   |   |   |

// UB - upper byte , LB - lower byte
// To write an LB, UB must wrote first.


// display an icon at position x,y
void d_icon(int x,int y)                        //display first bit of 8x8 icon at x,y position
{
   char i,j;

   if (p_x%16 <8)                              //if x position is on upper byte of 16 bit
   {
      for (j=0;j<8;j++)                        //repeat for 8 vertical line
      {
         lcd_xy(p_x/16,(p_y+j)%64);               //go to the location and y is re-adjust after adding j ( > 64 after adding j )
         send_char(0);                        //send upperbyte with shifting according to the x position
         send_char(0);                        //send lowerbyte with shifting according to the x position
      }
   }

   else
   {
      for (j=0;j<8;j++)
      {
         lcd_xy(p_x/16,(p_y+j)%64);               //go to the location and y is re-adjust after adding j ( > 64 after adding j )
         send_char (0);                        //send dummy data to the upperbyte
         send_char(0);                        //send lowerbyte data with shifting according to the x position
         if(p_x/16 == 7) lcd_xy(0,p_y+j);
         send_char(0);                        //send upperbyte of the next 'x' with shifting according to the x position
      }
   }



   if (x%16 <8)                              //if x position is on upper byte of 16 bit
   {
      for (j=0;j<8;j++)                        //repeat for 8 vertical line
      {
         lcd_xy(x/16,(y+j)%64);                  //go to the location and y is re-adjust after adding j ( > 64 after adding j )
         send_char(icon[j]>> x%8);               //send upperbyte with shifting according to the x position
         send_char(icon[j]<< (8-x%8));            //send lowerbyte with shifting according to the x position
      }
   }

   else
   {
      for (j=0;j<8;j++)
      {
         lcd_xy(x/16,(y+j)%64);                  //go to the location and y is re-adjust after adding j ( > 64 after adding j )
         send_char (0);                        //send dummy data to the upperbyte
         send_char(icon[j]>> x%8);               //send lowerbyte data with shifting according to the x position
         if(x/16 == 7) lcd_xy(0,y+j);
         send_char(icon[j]<< (8-x%8));            //send upperbyte of the next 'x' with shifting according to the x position
      }   
   }
   p_x = x;                                 //save as previous location
   p_y= y;                                    //save as previous location

}


void lcd_xy(unsigned char x,unsigned char y)         //set the xy position for graphic
{
      if( y >= 32)
      {                                    //if vertical more or equal 32
         send_config(0x80+y-32);                  //adjust the vertical line
         send_config(0x80+x+8);                  //for more detail refer GDRAM in user manual
      }

      else
      {
          send_config(0x80+y);                  //vertical (Y) max 63
         send_config(0x80+x);                  //horizontal (X) max 8 (horizontal is divided by 16 bit data (represented in 2 bytes))
      }
}


// display the picture according to the data array defined
//===============================================================================================================================
void d_graphic(char k)
{
   unsigned int i,j,y;
   send_config(0b00111100);                              //set to extention mode nad graphic off

   y = 0b10000000;                                       //set y as vertical configuration

   for(j = 0; j <32 ; j++)                                 //32 line
   {
      send_config(y++);                                 //increase line
      send_config(0b10000000);                           //set add back to 0
      for(i=0;i<16;i++)send_char(graphic[k][i+j*16]);            //display first half

      for(i=0;i<16;i++)send_char(graphic[k][i+512+j*16]);         //display second half **since the x add is increased more than 7. See CGRAM map above or refer user manual for CGRAM map for more detail.
   }

   send_config(0b00111110);                              //set to extention mode and graphic on
}

// clear all the graphic
//===============================================================================================================================
void clr_graphic()
{
   unsigned int i,j,y;
   send_config (0b00111100);                              //set to extention mode and graphic off

   y = 0b10000000;                                       //set y as vertical configuration
   for(j = 0; j <32 ; j++)                                 //32 line
   {
      send_config(y++);                                 //increase line
      send_config(0b10000000);                           //set add back to 0
      for(i=0;i<32;i++)send_char(0);                        //clear all
   }
   send_config(0b00111110);                              //set to extention mode and graphic on
}
PICTRAIL
Apprentice
 
Posts: 40
Joined: Mon Dec 27, 2010 3:32 pm

Re: LCD Display

Postby ABSF » Sun Jan 30, 2011 8:34 am

My next problem is with GLCD. Can someone tell me the function of ChipSet 1 and ChipSet 2. i dont really understand how i can use it with PIC16F877A.


What ChipSet 1 & 2 are you refering to? I can't find them in the C codes and the schematics of PR21.
A little more explanation is needed.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: LCD Display

Postby Brian Griffin » Sun Jan 30, 2011 11:56 am

If I'm not mistaken, the "Chipset 1" and "Chipset 2" refers to the halves of the screen. You must know that the casual KS0108 have 8 x (8 rows * 64 pixels) on the left, and the same one on the right side of the screen.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests

cron