8051 game project
I'm new in forum and with programming 8051 compatible micro controllers.
I'm studying Electrotecnics Engineering and one of my classes is "Micro Controllers".
We have to build a project until the end of the academic semester. The project is to build a small game that uses an LCD (HD44780), a 4 character LCD to show the score and a set of 5 switches that will handle movement (up, down, left, right and difficulty level set).
The difficulty level will be selected upon the transition state of pin P3_4 which will be connected to a switch. So, when the user wants to play in basic mode, will let the switch open. If he wants to play in advanced mode, he will close the switch.
The question is, how do I check when a transition occurs in this pin, using C language?
[ Edited Thu Nov 27 2014, 08:35 pm ]
/* say we want to read P0.1 */ char pin; pin = P0_1; /* now pin has status value */ /* 1 - for high, 0 for low */
If you do not want to use a variable then, you can also use simple if loop like this:
if (P0_1) { /* Pin is high */ } else { /* Pin is low */ }
The problem in that is that there is the possibility that the user doesn't have to turn On or Off the switch.
My idea was to wait until user choose a level or be able to continue with the current selection.
The code I already have, initiates the LCD, then shows 2 messages (the game's name and the game's authors) and then builds the custom characters in the CGRAM. The next step would be to ask the user which level he wants to play. Basic or Advanced. It's this step I need to implement. Like this:
1st: Show the message "Basic or Advanced?"
2nd: Wait for for the user to press the switch in P3_4 or let the user use the current level (the current s witch's state).
Note1: Yet 2 interrupts are available to use.
Note2: I'm using the MCU 8051 Ide with SDCC compiler.
[ Edited Fri Nov 28 2014, 12:48 pm ]
The above task will be addressed later when I have a bit more ahead in the project codding.
I have created a few custom chars in CGRAM and already sent 2 of them into DDRAM of LCD. Now I need to know how to move one towards the other. Is there any dynamic process to do it? Like shifting bits right/left of the current char address I want to move????
I have created a few custom chars in CGRAM and already sent 2 of them into DDRAM of LCD. Now I need to know how to move one towards the other. Is there any dynamic process to do it? Like shifting bits right/left of the current char address I want to move????PsySc0rpi0n
As far as I know CGRAM patterns can only be used the same way as
inbuilt chars, either displayed or not.
Each game character needs two patterns in CGRAM for every
left/right shift
Display them in pairs, to give the effect of a smooth movement.
The next step would be to ask the user which level he wants to play. Basic or Advanced. It's this step I need to implement. Like this:
1st: Show the message "Basic or Advanced?"
2nd: Wait for for the user to press the switch in P3_4 or let the user use the current level (the current s witch's state).PsySc0rpi0n
Perhaps something like this.
If the user does not select "Advanced" mode within a few seconds
it defaults to basic.
int selectLevel() { int count=500; //set 5 second timeout print ("Press button for Advanced mode"); while (count--) //count down timeout { shortdelay() // say 10 milliseconds if(button) break; } //return 0 for basic, none zero for Advanced return count; }
[ Edited Tue Dec 02 2014, 01:32 am ]
I have used a ternary operator instead of using whiles and ifs. I think it's a bit faster
Here's how I did it:
#define GL P3_4 __bit set_game_level (void){ return GL ? 0:1; }
Now I need to know how to move chars around the LCD. I need to make the bullet move towards the target in the other corner of the LCD.
Any ideas?
[ Edited Tue Dec 02 2014, 02:18 am ]
Now I need to know how to move chars around the LCD. I need to make the bullet move towards the target in the other corner of the LCD.
Any ideas?PsySc0rpi0n
Sorry, I have no experience in LCD graphics, so no ideas.
Even my last post might be wrong
I have used a ternary operator instead of using whiles and ifs. I think it's a bit faster
Here's how I did it:#define GL P3_4 __bit set_game_level (void){ return GL ? 0:1; }PsySc0rpi0n
It's aways good to try new things, but speed is not important for this
button press.
Ternary operators don't always create faster code, it depends on the
compiler.
In this case I'm not sure what it achieves as GL is seen as 0 or 1
and your routine returns 0 or 1.
Now I need to know how to move chars around the LCD. I need to make the bullet move towards the target in the other corner of the LCD.
Any ideas?PsySc0rpi0n
Sorry, I have no experience in LCD graphics, so no ideas.
Even my last post might be wrong
I have used a ternary operator instead of using whiles and ifs. I think it's a bit faster
Here's how I did it:#define GL P3_4 __bit set_game_level (void){ return GL ? 0:1; }PsySc0rpi0n
It's aways good to try new things, but speed is not important for this
button press.
Ternary operators don't always create faster code, it depends on the
compiler.
In this case I'm not sure what it achieves as GL is seen as 0 or 1
and your routine returns 0 or 1.ExperimenterUK
Well, maybe you're right. Need to look once again at the code. Tomorrow I'll do it.
Thanks for your help.
Psy
As your lcd has a fixed size then I suggest to use a local buffer for it and fill data in the buffer then flush the buffer on lcd. Its the same technique used in linux framebuffers too. The best thing about this method is you can manipulate lcd buffer with data you want and you dont have to worry about what was the last state of lcd. Hope you get my idea.ajay_bhargav
You mean to set a block of memory aside (of the same size as LCD) and perform all motion actions in memory and then send them to the LCD?