Need helps to how to interface PIC16F877A with graphic LCD

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

Re: Need helps to how to interface PIC16F877A with graphic L

Postby Brian Griffin » Wed Mar 02, 2011 8:43 pm

sengyee88 WROTE:
2. Mostly people would use the KS0108 controller for the GLCD, but I may be wrong. The ST7920 controller has character generator ROM which you can print characters inside without having to define your GLCD fonts and ways and means of printing it out on the screen, which takes up quite some program space.

The disadvantage is, you can't have custom fonts inside - they are fixed in the LCD. Unless if you make another driver for that...

3. Refer to the datasheet. They may have given you a list of commands or instructions if they are I2C or SPI.

5. "Driver program" means a software with a fragment of the code to be tested.

The ST7920 is rare due to the presence of the ability of printing characters, as I told you previously, and it's much direct and easier to use than the KS0108. However, I haven't actually used the ST7920 since I don't have those in my hands right now.


Opppss.. i'm getting more confuse about the driver and controller. So with the GLCD i bought from Cytron, can i use KS0108?


It's nothing confusing.

You cannot use a program code which controls the KS0108 for the ST7920 and vice versa.

You need to write the codes for ST7920. Please read the datasheet.
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

Re: Need helps to how to interface PIC16F877A with graphic L

Postby ABSF » Wed Mar 02, 2011 10:20 pm

sengyee88 WROTE:
2. Mostly people would use the KS0108 controller for the GLCD, but I may be wrong. The ST7920 controller has character generator ROM which you can print characters inside without having to define your GLCD fonts and ways and means of printing it out on the screen, which takes up quite some program space.

3. Refer to the datasheet. They may have given you a list of commands or instructions if they are I2C or SPI.

5. "Driver program" means a software with a fragment of the code to be tested.

The ST7920 is rare due to the presence of the ability of printing characters, as I told you previously, and it's much direct and easier to use than the KS0108. However, I haven't actually used the ST7920 since I don't have those in my hands right now.


Opppss.. i'm getting more confuse about the driver and controller. So with the GLCD i bought from Cytron, can i use KS0108?


I will explain it in a metaphorical way. There are many SBC(Single Board computer) using different MCU. Some use PIC, some use AVR and some use propeller chip. SOme SBC using AVR called themselves arduino while some may call SK18C or SK40C which uses PIC. And different MCU has different instruction sets. So here we have 3 things: SBC, MCU and instruction sets. A SBC using AVR cannot execute PIC instruction, right?

It is the same with GLCD, there are different makes of GLCD like TG12864 (made by company X) or my WG12864A (by company Y). The TG12864 uses ST7920 (display chip) while my WG12864A uses KS0108. The ST7920 and KS0108 executes different instruction sets.

So you see, The TG12864 is equivalent to an SBC, the ST7920 is like MCU while the driver is similar to instruction sets in the above metaphor. There's nothing confusing about. Now you want to use the KS0108 driver/library for your GLCD which uses a ST7920 is like saying "Can I use the a program written for PIC and run it on the arduino ?"

HTH

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: Need helps to how to interface PIC16F877A with graphic L

Postby sengyee88 » Wed Mar 02, 2011 11:36 pm

I will explain it in a metaphorical way. There are many SBC(Single Board computer) using different MCU. Some use PIC, some use AVR and some use propeller chip. SOme SBC using AVR called themselves arduino while some may call SK18C or SK40C which uses PIC. And different MCU has different instruction sets. So here we have 3 things: SBC, MCU and instruction sets. A SBC using AVR cannot execute PIC instruction, right?

It is the same with GLCD, there are different makes of GLCD like TG12864 (made by company X) or my WG12864A (by company Y). The TG12864 uses ST7920 (display chip) while my WG12864A uses KS0108. The ST7920 and KS0108 executes different instruction sets.

So you see, The TG12864 is equivalent to an SBC, the ST7920 is like MCU while the driver is similar to instruction sets in the above metaphor. There's nothing confusing about. Now you want to use the KS0108 driver/library for your GLCD which uses a ST7920 is like saying "Can I use the a program written for PIC and run it on the arduino ?"


Alright. I got that!
Does the sample source code for PR21 work? i tried to build in the MPLAB, it stated build failed with the error -------------------------------------------------------------------------------------------------------------------------------------
Debug build of project `C:\Users\Wong\Desktop\TRy\PR21.mcp' started.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed Mar 02 23:35:07 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F452 "D:\FYP\Cytron\DIY Project Set - Gaming with Accelerometer\Source Code PR21\PR21.c" -fo="PR21.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
MPLAB C18 v3.30 (evaluation)
Copyright 2000-2009 Microchip Technology Inc.
Days remaining until evaluation becomes feature limited: 60
D:\FYP\Cytron\DIY Project Set - Gaming with Accelerometer\Source Code PR21\PR21.c:1:Error: syntax error
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `C:\Users\Wong\Desktop\TRy\PR21.mcp' failed.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed Mar 02 23:35:11 2011
----------------------------------------------------------------------
BUILD FAILED

so how should i correct this error?

Thank You!
sengyee88
Novice
 
Posts: 19
Joined: Tue Mar 01, 2011 3:36 pm

Re: Need helps to how to interface PIC16F877A with graphic L

Postby ABSF » Thu Mar 03, 2011 5:20 am

Have you read this thread as suggested by Ober?

viewtopic.php?f=21&t=10722&hilit=graphic+LCD

If that still didnt answer your question, you might PM PICTRAIL to see if he is free to help. I think he has got the combinations working. :)

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: Need helps to how to interface PIC16F877A with graphic L

Postby sengyee88 » Thu Mar 03, 2011 11:16 am

Have you read this thread as suggested by Ober?

viewtopic.php?f=21&t=10722&hilit=graphic+LCD

If that still didnt answer your question, you might PM PICTRAIL to see if he is free to help. I think he has got the combinations working. :)


yes, i read that before.

how should i pm PICTRAIL?
sengyee88
Novice
 
Posts: 19
Joined: Tue Mar 01, 2011 3:36 pm

Re: Need helps to how to interface PIC16F877A with graphic L

Postby ober » Thu Mar 03, 2011 5:44 pm

sengyee88 WROTE:Does the sample source code for PR21 work? i tried to build in the MPLAB, it stated build failed with the error -------------------------------------------------------------------------------------------------------------------------------------
Debug build of project `C:\Users\Wong\Desktop\TRy\PR21.mcp' started.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed Mar 02 23:35:07 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "C:\MCC18\bin\mcc18.exe" -p=18F452 "D:\FYP\Cytron\DIY Project Set - Gaming with Accelerometer\Source Code PR21\PR21.c" -fo="PR21.o" -D__DEBUG -Ou- -Ot- -Ob- -Op- -Or- -Od- -Opa-
MPLAB C18 v3.30 (evaluation)
Copyright 2000-2009 Microchip Technology Inc.
Days remaining until evaluation becomes feature limited: 60
D:\FYP\Cytron\DIY Project Set - Gaming with Accelerometer\Source Code PR21\PR21.c:1:Error: syntax error
Halting build on first failure as requested.
----------------------------------------------------------------------
Debug build of project `C:\Users\Wong\Desktop\TRy\PR21.mcp' failed.
Language tool versions: mpasmwin.exe v5.30.01, mplink.exe v4.30.01, mcc18.exe v3.30
Preprocessor symbol `__DEBUG' is defined.
Wed Mar 02 23:35:11 2011
----------------------------------------------------------------------
BUILD FAILED


What is the version of MPLAB IDE are you using? The code has been compiled with MPLAB IDE v8.30 and MCC18 v3.30 which is downloadable from the PR21 page, the hex is tested on the real hardware.

You can try getting the latest MPLAB IDE and MCC18. The error might because of different version of MPLAB IDE and MCC18. Good luck. :D
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: Need helps to how to interface PIC16F877A with graphic L

Postby PICTRAIL » Thu Mar 03, 2011 6:21 pm

Hi sengyee88,
PICTRAIL Here... I was once in your shoe but no worries because it works now...
Foremost trying the PR21 you need to check your compiler to be C18 because microcontroller used is of PIC 18 family.

Meanwhile since you are using PIC16F877A, you only need to change the Port Settings of your PIC.

To use the program on PIC16F877A,
1. Create a new project file.
2. Select PIC16F877A as your IC.
3. Select HI-Tech 8/10/16 as your compiler.
4. Create a new folder and save your project in it.
5. Copy the code i attached below and save it as *.c file.
6. Load the file to your source.

Now you can compile and load the hex file to your microcontroller.

What i have done is that,
1. I change the pin declaration in #define to PR... (Note its different for PIC18).
2. In the grapich fucntion, i changed the rom char to const char.

After running the program you will see that some functions are not used in the program (Like that of accelerometer), you may remove the from the program just to increase your execution time.

The only problem i am facing now is displaying Text after i display graphics. I hope to overcome that soon.

Try the program and let me know how it works... I hope its helpful.

CODE: SELECT_ALL_CODE
//===========================================================================================
//   Author            :CYTRON   TECHNOLOGIES
//   Project            :DIY Project(PR21)
//   Project description   :Use PIC16F877A microcontroller  DS-LCD-TG12864B-03
//                       Graphical LCD (GLCD) for display.
//   Modified by      : Ademola E. Ogidi (PICTRAIL)
//===========================================================================================


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

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

__CONFIG ( 0x3F32 );      //configuration for the  microcontroller

//===========================================================================================               
//   Define
//============================================================================================
#define   lcd_data   PORTD                  //LCD 8-bit data PORT
#define   rst         RC4
#define   rs         RC5            //RS pin of the LCD display
#define   rw         RC6            //R/W pin of the LCD display   
#define   e         RC7            //E pin of the LCD display
#define psb         RC3
#define   b_light      RC2            //backlight of the LCD display (1 to on backlight)

#define   buzzer      RC0            //buzzer (1 to on buzzer)
#define   sw1         RB0            //button (active low)
#define   sw2         RB1            //button (active low)
#define z_axis       RA0
#define y_axis       RA1
#define   x_axis      RA2           
#define preset      RA4
#define st          RB2

#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
//==============================================================================================
const 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
//=====================================================================================
const 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
}


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

Re: Need helps to how to interface PIC16F877A with graphic L

Postby sengyee88 » Thu Mar 03, 2011 10:48 pm

Hi sengyee88,
PICTRAIL Here... I was once in your shoe but no worries because it works now...
Foremost trying the PR21 you need to check your compiler to be C18 because microcontroller used is of PIC 18 family.

Meanwhile since you are using PIC16F877A, you only need to change the Port Settings of your PIC.

To use the program on PIC16F877A,
1. Create a new project file.
2. Select PIC16F877A as your IC.
3. Select HI-Tech 8/10/16 as your compiler.
4. Create a new folder and save your project in it.
5. Copy the code i attached below and save it as *.c file.
6. Load the file to your source.

Now you can compile and load the hex file to your microcontroller.

What i have done is that,
1. I change the pin declaration in #define to PR... (Note its different for PIC18).
2. In the grapich fucntion, i changed the rom char to const char.

After running the program you will see that some functions are not used in the program (Like that of accelerometer), you may remove the from the program just to increase your execution time.

The only problem i am facing now is displaying Text after i display graphics. I hope to overcome that soon.

Try the program and let me know how it works... I hope its helpful.


Hello PICTRAIL, i tried to compile and load the hex file into PIC16F877A, but it does not work, it just showed a empty screen. I'm not sure whether is the my built hardware connection problem or lack of some components. May i know are you using the PR21 kit or you build your own hardware?

All right, let me explain what i have with my hardware circuit.
1. a usual power supply part to the PIC16 ( switch, 2 elec. capacitors, diodes, resistors, LED, and a 5V voltage regulator)
2. 128x64 GLCD (VSS->GND, VDD-> 5V Vcc, Vo->potentiometer, RS->RC5, RW->RC6, E->RC7, RST->RC4, PSB-RC3, DB0-7 ->RD0-7, LEDK->ground, LEDA->5V Vcc)
3. ICSP Programmer ( MCLR, RB6,RB7, GND)
4. Crystal Oscillator, pushbutton switch.

I just need a simple GLCD that can display graphic only.. not the gaming with accelerometer.

Hope u get what i meant here.
Can i have ur messenger contact so that i can contact u directly?

Thanks PICTRAIL!

oh yea, can some of u all teach my how to upload the photo here?
sengyee88
Novice
 
Posts: 19
Joined: Tue Mar 01, 2011 3:36 pm

Re: Need helps to how to interface PIC16F877A with graphic L

Postby ABSF » Thu Mar 03, 2011 11:05 pm

3. ICSP Programmer ( MCLR, RB6,RB7, GND)


Did you connect the 10K resistor and 1n4148 on MCLR? I think it need a logic "high" when switched on for the PIC to jump to address "0" for program startup.

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: Need helps to how to interface PIC16F877A with graphic L

Postby sengyee88 » Thu Mar 03, 2011 11:29 pm

Did you connect the 10K resistor and 1n4148 on MCLR? I think it need a logic "high" when switched on for the PIC to jump to address "0" for program startup.

Yes. I did connect it a 1N4148 and a 4k7 resistor to MCLR
sengyee88
Novice
 
Posts: 19
Joined: Tue Mar 01, 2011 3:36 pm

PreviousNext

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 4 guests

cron