Boe Bot (in C)
So I am using a Dragon 12 Plus (MC9S12 micro controller) to operate my little boe bot. I have been writing this code for a week and I am banging my head on why it won't work... I know it is something stupid I am missing, or messing up.
I made it remote controlled, so I have a basic decoder/encoder TX/RX circuit going on.
Ok, so... My issue right now, is I am in the phase of my code where I am trying to develop a nested switch statement inside an if else to read incoming signals from my transmitter, IE, Valid transmission (when a button is pressed), and I need to match the button pressed with the port H bit.
So..
I have VT set to Port H bit 4
forward set to bit 3
reverse to bit 2
left to bit 1
right to bit 0
so after VT reads a high, it jumps into the switch statement, and checks each case for a directional bit, and displays the direction to the LCD. "Stopped" will be a function where if I am not pressing a directional button on the transmitter, the boe bot will stop.
here is a brief hard code format I am trying to follow. I will post up my actual code next time I am at work (it is on my hdd there).
That is the basics of it.. I hard coded it today, setting VT manually to a given value in the switch statement, and it worked fine, but I cannot for the life of me set vt to the correct syntax to break into the switch statement when it goes high...
also, I have my porth initializations set like so...
I will update more later on.
I made it remote controlled, so I have a basic decoder/encoder TX/RX circuit going on.
Ok, so... My issue right now, is I am in the phase of my code where I am trying to develop a nested switch statement inside an if else to read incoming signals from my transmitter, IE, Valid transmission (when a button is pressed), and I need to match the button pressed with the port H bit.
So..
I have VT set to Port H bit 4
forward set to bit 3
reverse to bit 2
left to bit 1
right to bit 0
so after VT reads a high, it jumps into the switch statement, and checks each case for a directional bit, and displays the direction to the LCD. "Stopped" will be a function where if I am not pressing a directional button on the transmitter, the boe bot will stop.
here is a brief hard code format I am trying to follow. I will post up my actual code next time I am at work (it is on my hdd there).
int vt; //declared before main vt = PTH; //set vt to Port H if (vt == 1) { //Mask PTH bits so it only reads PTH bit 4 (vt) vt &= 0x10; switch(vt) case 0x18: writeLine("Forward ", 0); //My function to write to the LCD break; case 0x14: writeLine("Reverse ", 0); break; case 0x12: writeLine("Left ", 0); break; case 0x11: writeLine("Right ", 0); break; default: writeLine("Invalid ",0); } else { writeLine("stopped "), 0); } }
That is the basics of it.. I hard coded it today, setting VT manually to a given value in the switch statement, and it worked fine, but I cannot for the life of me set vt to the correct syntax to break into the switch statement when it goes high...
also, I have my porth initializations set like so...
DDRH = 0x00; //Sets port h bits for "inputs" PPSH = 0x00; //Writing a 1 to them sets them low, and writing a 0 sets them high PERH = 0x1F; //Write a 1 to enable the shift polarity //Should set all the Port H bits I am using to active low
I will update more later on.
Comments
Set VT to the value of PTH
Test if VT is equal to 1.
Set VT to (VT & 0x10)···· -> This causes VT to equal 0 and I don't think that's what you want.
You will only enter the if block when VT is 1 (0000 0001).· The result of (0000 0001 & 0001 0000) is 0000 0000.
Possibly you mean to say if (VT) instead of if (VT == 1)?
I see what you are saying, but I am not sure how I would write something to check and see if bit 4 (0001 0000) is set to high or not. Any ideas?
I was also doing some thinking, and inside the switch statement, I should probably do...
so that it scans for the PTH bits instead of just random bits.... Right?
So maybe something like this?
Post Edited (w8f8) : 4/19/2010 2:47:49 AM GMT
This will test if bit 4 is set.
Your case statements will not work.· The expressions must be constants.
Post Edited (FooWho) : 4/19/2010 4:00:26 AM GMT
set my directionals to a set bit?
i.e.
etc?
Also, why do you have if (VT & 0x10) {...} instead of if (VT &= 0x10) {...}? Or, if (VT = 0x10) {...} Shouldn't I be masking the other bits?
Also, how does that check if it goes high? Do I need to put VT ==1 after my "if"?
Post Edited (w8f8) : 4/19/2010 4:56:18 AM GMT
To test if bit 4 is set in VT, you need to do a bit-wise AND with 0x10.· This result will be non-zero if bit 4 was set.· This is where the
statement comes from.· You do not want to change the value of VT at this point, since it still contains information that you need.· Once inside the if block, you can use the switch statement to execute the correct case.· The correct way to do that would look something like this:
etc.
Actually, if I undersatand you correctly, the if statement is superfluous.· You could simply switch on VT and test for the 0x18, 0x14, etc cases.· If you don't match any of those cases, either bit 4 was not set or bit 4 was set but·more than 1 of the other bits was also set.
Post Edited (FooWho) : 4/19/2010 6:47:20 AM GMT
Its up and working =D
Thank you for your help thus far. Now I have to get the servos running off the PWM! =D
Any tips on where to start there?
Post Edited (w8f8) : 4/19/2010 5:04:33 PM GMT