
Looks like captalex has somewhere to start now for his GLCD project

Moderators: Chuckt, Garth, bitfogav
i've included the whole array into the source file because the compiler wouldnt let me use the #include<eva.c> header.odessa wrote:Hi,
It works fine in the test code using your Bitmap instead of the truck
There isn't anything in the code you posted there to draw the bitmap
Jay
Code: Select all
unsigned short j,k;
const unsigned short eva_bmp[1024]
void main()
{
ADCON1 = 0x0F; // Set AN pins to Digital I/O
Glcd_Init(&PORTB, 0, 1, 2, 3, 5, 4, &PORTD);//Glcd_Init_EP4,see Autocomplete//(CONTROL PORT,cs1,cs2,(d/i)/rs),r/w,rst,en,DATAPORT)
do {
// Draw circles
Glcd_Fill(0); // Clear screen
Glcd_Write_Text("ALEXISTHEKING",3,20,2);
j = 4;
delay_ms(300);
Glcd_Fill(0);
while (j < 25) {
Glcd_Circle(63,25,j,2);
j += 3;
}
delay_ms(40);
//draw an image
Glcd_image(eva_bmp);
delay_ms(300);
// Draw Lines
Glcd_Fill(0); // Clear screen
Glcd_Write_Text("HEISBACKRUNCOWRUN",2,10,2); //(text,page(0-7),x coordinate,color,0ff,on,invert)
delay_ms(400);
} while (1);
}
hey bitfogav, how's the pong game coming. i was hoping that i could get a sneak at your code for movibg the paddles and generating the ball . thanks.bitfogav wrote:captalex wrote:
finally man!!, bitfogav this is what i have been looking for /trying to do using the mikro C glcd libraries , but i didnt know how. i hope you could help me with porting your code to mikro C using a pic18f4550 and a wg12864b ks0108 based lcd display. and also adding an additional paddle and scoring! hence(pong).
Hi captalex, Im not familiar with mikroC, but looking at your test code, aren't you missing the GLCD librarys Or Headers? and dont you have to include the header file for which microchip you are using?, also I cant test your schematics as I dont have proteus
And as for your display backlight not working, all Ive done on mine is connect it up as this, im using a 10k potentiometer going to my 5volt supply and then the other two connections connect directly to the GLCD pin 3 and 18, this is to control the contrast of the display, you also need to connect pins 19 to 5+ and pin 20 to ground to operate the backlight.
Hey Alex, sure. ive done it using Swordfish Basic, so it might help if you download the Swordfish SE version which you can get from here http://www.sfcompiler.co.uk/swordfish/d ... index.html your probably need the modules that come with Swordfish to understand my code better?. I haven't had much time the last week to do much more with it yet.captalex wrote:hey bitfogav, how's the pong game coming. i was hoping that i could get a sneak at your code for moving the paddles and generating the ball . thanks.
Code: Select all
Device = 18F2620 // select our device
Clock = 32 // internal 8Mhz * 4 PLL 32Mhz Clock
Config OSC = INTIO67 // configure OSC for internal
// User options
#Option GLCD_MODEL = KS0108 // GLCD driver
#option GLCD_SCREEN_WIDTH = 128 // Screen Width in Pixels
#option GLCD_SCREEN_HEIGHT = 64 // Screen Height in Pixels
#Option GLCD_DATA = PORTB // data port
#Option GLCD_RS = PORTC.0 // RS pin
#Option GLCD_RW = PORTC.1 // RW pin
#Option GLCD_EN = PORTC.2 // EN pin
#Option GLCD_CS1 = PORTC.3 // chip select
#Option GLCD_CS2 = PORTC.4 // chip select
#Option GLCD_RST = PORTC.5 // reset pin
#Option GLCD_ASPECT_RATIO = 100 // aspect ratio, smaller number will squeeze y for GLCD circles and box
#Option GLCD_INIT_DELAY = 50 // initialisation delay (ms)
#Option GLCD_INVERT_CS = true // invert CS lines...
// Modules
Include "glcd.bas" // main GLCD module
Include "graphics.bas" // support for GLCD
Include "utils.bas" // support to make all MCU pins Digital
Include "fixedfont.bas" // include support for Fonts
// Constants, Variables and Identifiers
Const topScreen = 2, // set paddle max travel (top display)
bottomScreen = 41, // set paddle max travel (bottom display)
wall_Left = 1, // set left wall position for hit detection
wall_Bottom = 60, // set bottom wall position for hit detection
wall_Top = 1, // set top wall position for hit detection
ballMissPaddle = 130 // set max value for ball missing paddle
Dim button1 As PORTA.1, //
button2 As PORTA.2, // button pins
button3 As PORTA.3, //
button4 As PORTA.4, //
button5 As PORTA.5 //
Dim ballX As Byte, //
ballY As Byte, // ball data variables
ballSize As Word, //
ballSpeed As Byte // ball speed
Dim Ball_Hit_Top, // ball direction booleans
Ball_Direct_Up,
Ball_Direct_Left,
Ball_Hit_Left As Boolean
Dim paddleX1, //
paddleY1, // paddle position data
paddleX2, //
paddleY2 As Byte //
// Subroutines
Sub init_border()
GLCD.Line(0,0,127,0) // draws line (top of screen)
GLCD.Line(0,0,0,63) // draws line (left of screen)
GLCD.Line(0,63,127,63) // draws line (bottom of screen)
End Sub
Sub init_variables()
ballX = 64 // starting position X for ball
ballY = 36 // starting position Y for ball
ballSize = 2 // ball size fixed
ballSpeed = 30 // ball speed
Ball_Hit_Top = false // wall flags
Ball_Direct_Up = false //
Ball_Direct_Left = false //
Ball_Hit_Left = false //
paddleX1 = 120 //
paddleY1 = 15 // starting point of paddle X,Y
paddleX2 = 121 //
paddleY2 = 35 //
End Sub
Sub ball(ballX1 As Word, ballY1 As Word, ballSize1 As Word)
GLCD.Square(ballX1,ballY1,ballSize1) // draw ball on screen X,Y (ball size)
End Sub
Sub ballDirect()
If Ball_Direct_Left = true Then // ball direction X
Dec(ballX)
Else
Inc(ballX)
EndIf
If Ball_Direct_Up = true Then // ball direction Y
Dec(ballY)
Else
Inc(ballY)
EndIf
End Sub
Sub checkWalls()
If ballX = wall_Left And Ball_Hit_Left = true Then // check wall left
Ball_Direct_Left = false
Ball_Hit_Left = false
EndIf
If ballY = wall_Top And Ball_Hit_Top = true Then // check wall top
Ball_Direct_Up = false
Ball_Hit_Top = false
EndIf
If ballY = wall_Bottom And Ball_Hit_Top = false Then // check wall bottom
Ball_Direct_Up = true
Ball_Hit_Top = true
EndIf
End Sub
Sub drawBall()
ball(ballX,ballY,ballSize) // draw new ball on screen
SetPixel(ballX+1,ballY+1) // set pixel in middle of ball
DelayMS(ballSpeed) // delay so we see ball on screen, also ball speed
Pen.Color = 0 //
ball(ballX,ballY,ballSize) // clears ball previous pos (can only be done by deleting pixels)
Pen.Color = 1 //
ball(ballX,ballY,ballSize) // draw new ball on screen
SetPixel(ballX+1,ballY+1) // set pixel in middle of ball
DelayMS(10) // delay so we see ball on screen, also ball speed
Pen.Color = 0 //
ball(ballX,ballY,ballSize) // clears ball previous pos (can only be done by deleting pixels)
Pen.Color = 1 //
End Sub
Sub update_paddle1()
If button2 = 0 And paddleY1 >= topScreen Then // check button up & check paddle max travel top
Pen.Color = 0 //
GLCD.Rectangle(paddleX1,paddleY1,paddleX2,paddleY2) // clear paddle previous data
Pen.Color = 1 //
paddleY1 = paddleY1 -1 // set paddle to go up on display
paddleY2 = paddleY2 -1 //
DelayMS(3) // delay to slow down paddle movement
EndIf
If button1 = 0 And paddleY1 <= bottomScreen Then // check button down & check paddle max travel bottom
Pen.Color = 0 //
GLCD.Rectangle(paddleX1,paddleY1,paddleX2,paddleY2) // clear paddle previous data
Pen.Color = 1 //
paddleY1 = paddleY1 +1 // set paddle to go down on display
paddleY2 = paddleY2 +1 //
DelayMS(3) // delay to slow down paddle movement
EndIf
GLCD.Rectangle(paddleX1,paddleY1,paddleX2,paddleY2) // update our NEW paddle pos data
End Sub
Sub checkBallHitPaddle()
// check to see if our ball has hit the paddle
If ballX >= paddleX1-2 And ballX <= paddleX2-2 And ballY >= paddleY1 And ballY <= paddleY2 Then
Ball_Direct_Left = true // change ball direction
Ball_Hit_Left = true // change ball direction
ElseIf ballX = ballMissPaddle Then // check to see if ball missed our paddle
// reset game
init_variables // setup variables
GLCD.Cls // clear display before we start main prog
init_border // setup display border
GLCD.SetFont(Fixed)
GLCD.WriteAt(8,12,"GLCD")
GLCD.WriteAt(12,22,"Game")
GLCD.WriteAt(15,32,"by")
GLCD.WriteAt(8,42,"bitfogav")
EndIf
End Sub
// PORT setup and PLL Multiplier (set internal Osc to 32Mhz)
TRISA = %00111110 // set PORTA bits 1-5 Inputs (buttons)
TRISB = %00000000 // Set all Ports output
TRISC = %00000000 //
OSCCON = %01111100 // Sets up the internal oscillator for 8Mhz
OSCTUNE.6 = 1 // Frequency Multiplier PLL for INTOSC Enable bit (Enabled)
SetAllDigital // set MCU pins as digital
init_variables // setup variables
GLCD.Cls // clear display before we start main prog
init_border // setup display border
GLCD.SetFont(Fixed)
GLCD.WriteAt(8,12,"GLCD")
GLCD.WriteAt(12,22,"Game")
GLCD.WriteAt(15,32,"by")
GLCD.WriteAt(8,42,"bitfogav")
// Main Programme
While true
ballDirect // ball direction
checkWalls // check to see if ball hit any walls
drawBall // draws ball on display
update_paddle1 // update paddle on display
checkBallHitPaddle // check to see if ball has hit our paddle
Wend
Code: Select all
//header file for mapping the buttons to port pins
#include "button_check.h"
#define Pressed 0
//*****************************************************************************
//* Name : CAPTONG - *
//* Author : [Captalex and his minions] *
//* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
//* : All Rights Reserved *
//* Date : 10/june/2011 *
//* Version : 0.0.1 *
//* Notes : a one player pong game , my first try at graphics *
//* : microcontroller programming. *
//* : *
//*****************************************************************************
/**ALL Constants, Variables and Identifiers **/
//use ints
int topScreen = 2; // set paddle max travel (top display)
int bottomScreen = 41; // set paddle max travel (bottom display)
int wall_Left = 1; // set left wall position for hit detection
int wall_Bottom = 60; // set bottom wall position for hit detection
int wall_Top = 1; // set top wall position for hit detection
int ballMissPaddle = 130; // set max value for ball missing paddle
//how do i change Dim to match mikro C format and use Byte and word??? can i just use int instead for Byte ?
// ball data variables
int ballX; // ball initial x coordinate
int ballY; // ball initial y coordinate
int ballX1; // hold previous x values into a new varaiable
int ballY1; // hold previous y values into a new varaiable
int ballSize; // initial size for the ball
int ballSpeed; // ball speed
// ball direction ints ,I set as ints, because mikroc has no bool variable
int Ball_Hit_Top;
int Ball_Direct_Up;
int Ball_Direct_Left;
int Ball_Hit_Left;
// paddle position data
int paddleX1; //paddle initial x coordinate
int paddleY1; //paddle initial y coordinate
int paddleX2; //paddle final x coordinate
int paddleY2; //paddle final x coordinate
//Function prototypes for each of the games functions
/* draws a border for the game */
void init_border()
{
// draws line (top of screen)
Glcd_Line(0,0,127,0,1);
// draws line (left of screen)
Glcd_Line(0,0,0,63,1);
// draws line (bottom of screen)
Glcd_Line(0,63,127,63,1);
} //for init_border();
/*initailizes the game event variables*/
void init_variables()
{
ballX = 64 ; // starting position X for ball
ballY = 36 ; // starting position Y for ball
ballX1 = ballX+1; //set old x values into a new varaiable
ballY1 = ballY+1; //set old y values into a new varaiable
ballSize = 2 ; // ball size fixed
ballSpeed = 30 ; // ball speed
// Boder wall flags
Ball_Hit_Top = 0; // Ball_Hit_Top = 0;
Ball_Direct_Up = 0; // Ball_Direct_Up = 0;
Ball_Direct_Left = 0; // Ball_Direct_Left = 0;
Ball_Hit_Left = 0; // Ball_Hit_Left = 0;
// starting point of paddle X,Y
paddleX1 = 120 ; //
paddleY1 = 15 ; // starting point of paddle X,Y
paddleX2 = 121 ; //
paddleY2 = 35 ; //
} //for init_variables();
void ball (int ballX1 , int ballY1, int ballX1, int ballY1)
{ //help here how do i implement the ball function using word in mikro c
// draw ball on screen X,Y (ball size)
Glcd_Box(ballX1,ballY1,ballX1,ballY1,1);
} //for ball();
/*the 4 possible ball's directions (left-right-up-down) */
void ballDirect()
{
if (Ball_Direct_Left == 1)
{ // move the ball left
ballX--; }
else
{ ballX++; //move the ball right
} //for first if
if (Ball_Direct_Up == 1)
{ // move the ball direction up
ballY--; }
else
{ ballY++; //move the ball down
} //for second if
} //for ballDirect();
/*checks if ball has hit bottom or the top if the screen */
void checkWalls()
{
if (( ballX == wall_Left ) && (Ball_Hit_Left == 1))
{ // check wall left
Ball_Direct_Left = 0; //Ball_Direct_Left = 0;
Ball_Hit_Left = 0; //Ball_Hit_Left = 0;
} //for first if
else if ((ballY == wall_Top) && (Ball_Hit_Top ==1))
{ // check wall top
Ball_Direct_Up = 0; //Ball_Direct_Up = 0;
Ball_Hit_Top = 0; //Ball_Hit_Top = 0;
} //for second if
// else ((ballY = wall_Bottom) && (Ball_Hit_Top = false))
// { // check wall bottom
// Ball_Direct_Up = 1; //Ball_Direct_Up = 1;
// Ball_Hit_Top = 1; //Ball_Hit_Top = 1;
// } //for third if
} //for checkWalls();
/* draws the ball on the lcd screen*/
void drawBall()
{
ball(ballX,ballY,ballX1,ballY1); // draw new ball on screen
Glcd_Set_Side(ballX+1);
Glcd_Set_X(ballX+1);
//Glcd_Set_Page(ballY+1); // set pixel in middle of ball
//delay_ms(ballSpeed); // delay so we can see the ball on the screen, also see the ball's speed
Glcd_Box(ballX,ballY,ballX1,ballY1,0);
ball(ballX,ballY,ballX1,ballY1); // clears ball previous pos (can only be done by deleting pixels)
Glcd_Box(ballX,ballY,ballX1,ballY1,1);
ball(ballX,ballY,ballX1,ballY1); // draw new ball on screen
// set pixel in middle of ball
Glcd_Set_Side(ballX+1);
Glcd_Set_X(ballX+1);
Glcd_Set_Page(ballY+1);
delay_ms(10); // delay so we see ball on screen, also ball speed
Glcd_Box(ballX,ballY,ballX1,ballY1,0);
ball(ballX,ballY,ballX1,ballY1); // clears ball previous pos (can only be done by deleting pixels)
Glcd_Box(ballX,ballY,ballX1,ballY1,1);
} //for drawBall();
void update_paddle1()
{
//button 2 is up
if ((Switch_up == Pressed) && ( paddleY1 >= topScreen))
{ // check button up & check paddle max travel top
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,0);
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,1);
paddleY1 = paddleY1 -1; // set paddle to go up on display
paddleY2 = paddleY2 -1; //
delay_ms(3); // delay to slow down paddle movement
} //for up if condition
//button1 is down
if ((Switch_down == Pressed) && ( paddleY1 <= bottomScreen))
{ // check button down & check paddle max travel bottom
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,0); // clear paddle previous data
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,1);
paddleY1 = paddleY1 +1; // set paddle to go down on display
paddleY2 = paddleY2 +1; //
delay_ms(3); // delay to slow down paddle movement
} //for down if condition
Glcd_Rectangle(paddleX1,paddleY1,paddleX2,paddleY2,1); // update our NEW paddle pos data
} //for update_paddle1();
/*when ball hits paddles select the direction it goes in*/
void checkBallHitPaddle()
{
// check to see if our ball has hit the paddle
if (ballX >= paddleX1-2) && (ballX <= paddleX2-2) && (ballY >= paddleY1) && (ballY <= paddleY2)
{
Ball_Direct_Left = 1; // change ball direction Ball_Direct_Left = 1;
Ball_Hit_Left = 1; // change ball direction Ball_Hit_Left = 1;
}
else if (ballX = ballMissPaddle)
{ // check to see if ball missed our paddle
// reset game
init_variables(); // setup variables , init_variables();
Glcd_Fill(0); // Glcd_Fill(0); clear display before we start main prog
init_border(); // setup display border , init_border();
} // for first else if }
// for if
} //for checkBallHitPaddle();
void main()
{
ADCON1 = 0x0F; // set MCU pins as digital and Set AN pins to Digital I/O
Glcd_Init(&PORTB,0,1,2,3,5,4,&PORTD); // (CONTROL PORT,cs1,cs2,[(d/i)/rs],r/w,rst,en,DATAPORT)
Glcd_Fill(0); // clear display before we start main prog
TRISA = 0xFF; // set PORTA bits 1-5 Inputs (buttons) ,TRISA = 0xFF; // RA0 input
TRISC = 0; // PORTC outputs, for led indicator to show game is on
PORTC.F2 = 0; // Turn OFF LED indicator on port RC2
/*Welcome message*/
Glcd_Write_Text("A",8,1,1);
Glcd_Write_Text("PONG",12,2,1);
Glcd_Write_Text("GAME",15,3,1);
Glcd_Write_Text("BY",8,4,1);
Glcd_Write_Text("CAPTALEX",15,5,1);
Glcd_Write_Text("ENJOY",15,6,1);
// Main Game loop
while (1)
{
init_variables(); // setup game variables
init_border(); // setup display border
ballDirect(); // ball direction
checkWalls(); // check to see if ball hit any walls
drawBall(); // draws ball on display
update_paddle1(); // update paddle on display
checkBallHitPaddle(); // check to see if ball has hit our paddle
} // for end of main game while condition
} // for void main
Code: Select all
//header file for mapping the buttons to port pins in mikro c
#define Switch_up PORTA.F0 // button2
#define Switch_right PORTA.F1
#define Switch_down PORTA.F2 // button1
#define Switch_left PORTA.F3
#define Switch_Start PORTA.F4
Public Sub Square(pX, pY, pSize As TXY)
#if GLCD_ASPECT_RATIO <> 100
Rectangle(pX, pY, pX + pSize, pY + pSize * AspectRatio / 100)
#else
Rectangle(pX, pY, pX + pSize, pY + pSize)
#endif
End Sub
Code: Select all
ADCON1 = 0x0F; // set MCU pins as digital and Set AN pins to Digital I/O
TRISA = 0xFF; // set PORTA bits 1-5 Inputs (buttons) ,TRISA = 0xFF; // RA0 input
TRISC = 0; // PORTC outputs, for led indicator to show game is on
PORTC.F2 = 0; // Turn OFF LED indicator on port RC2
Glcd_Init(&PORTB,0,1,2,3,5,4,&PORTD); // (CONTROL PORT,cs1,cs2,[(d/i)/rs],r/w,rst,en,DATAPORT)
Glcd_Fill(0); // clear display before we start main prog
bitfogav wrote:Im not sure if that ported code will work or not with mikroC?the GLCD functions in swordfish are more than likely different to that in the GLCD functions in miKroC. When I call the GLCD.Square function in swordfish I know that inside the swordfish square function it also calls the Rectangle function and thats how it draws a Square on the GLCD using swordfish:
this is the function(subroutine) for drawing a square using the swordfish module (or Class known in C language)Public Sub Square(pX, pY, pSize As TXY)
#if GLCD_ASPECT_RATIO <> 100
Rectangle(pX, pY, pX + pSize, pY + pSize * AspectRatio / 100)
#else
Rectangle(pX, pY, pX + pSize, pY + pSize)
#endif
End Sub
You say when compiling the code in mikroC you get an error? What if you compile the code with changing the GLCD_Init after you have declared your PORT settings? not sure if that will make any difference but worth a try?!.
As for using Int instead of Bytes and Words then that should be ok, When I programme I try to use the smallest data type as possible to reduce programme memory.Code: Select all
ADCON1 = 0x0F; // set MCU pins as digital and Set AN pins to Digital I/O TRISA = 0xFF; // set PORTA bits 1-5 Inputs (buttons) ,TRISA = 0xFF; // RA0 input TRISC = 0; // PORTC outputs, for led indicator to show game is on PORTC.F2 = 0; // Turn OFF LED indicator on port RC2 Glcd_Init(&PORTB,0,1,2,3,5,4,&PORTD); // (CONTROL PORT,cs1,cs2,[(d/i)/rs],r/w,rst,en,DATAPORT) Glcd_Fill(0); // clear display before we start main prog
I didnt know that mikroC doesn't support BOOL, (booleans). the way you've done it looks like it should work.
init_variables(); // setup game variables
init_border(); // setup display border
bitfogav wrote:Ive had another look over your code but still can't see why your getting that error?
TRISA Identifier redefined message means that the same identifier already exists in some unit
in your project? but I cant find it in any other unit and also you've got the ; colons in the correct places.
One thing though, you need to take out these two lines from your main loop though, and put them out above your main loop so your program reads them before everything starts, I call these before the main loop to setup the variables and set the border for the display, you dont need to call these again. (well not here anyway).init_variables(); // setup game variables
init_border(); // setup display border
bitfogav wrote:Hi Captalex,
Ive made up a breadboard with a GLCD using a 18f4550, I have a 4mhz Ext crystal and two buttons connected up to the microchip, Ive connected up the GLCD and the rest of the circuit as you have in your project, changed my code to suit (datalines, etc) and the code runs fineCan I just point out that the 18F4550 has an Internal Oscillator 8 MHz.
Heres a picture of it working: Heres the New Hex file with the changes, And a text file of the changed code.
Users browsing this forum: No registered users and 7 guests