Shop OBEX P1 Docs P2 Docs Learn Events
Boe Bot (in C) — Parallax Forums

Boe Bot (in C)

w8f8w8f8 Posts: 5
edited 2010-04-19 19:45 in Robotics
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).

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

  • FooWhoFooWho Posts: 17
    edited 2010-04-18 19:27
    I don't know if this will help, but when I look at that C code, I read it as follows...

    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)?
  • w8f8w8f8 Posts: 5
    edited 2010-04-19 02:29
    Well, the value set for VT should be hex 10, or 0001 0000 in binary.

    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...

    case PTH == 0x18:  //etc etc
    


    so that it scans for the PTH bits instead of just random bits.... Right?

    So maybe something like this?

    int vt; //declared before main
    
    vt = PTH &= 0x10;  //set vt to Port H 0001 0000
    
    if (vt == 1)
    {  
        switch(vt)
              case PTH == 0x18:
              writeLine("Forward          ", 0);  //My function to write to the LCD
              break;
            
              case PTH == 0x14:
              writeLine("Reverse          ", 0);
              break;
    
              case PTH == 0x12:
              writeLine("Left          ", 0);
              break;
    
              case PTH == 0x11:
              writeLine("Right        ", 0);
              break;
    
              default:
              writeLine("Invalid      ",0);
    }
    else
    {
         writeLine("stopped      "), 0);
      }
    }
    

    Post Edited (w8f8) : 4/19/2010 2:47:49 AM GMT
  • FooWhoFooWho Posts: 17
    edited 2010-04-19 03:54
    w8f8 said...
    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?
    if (VT & 0x10) {...}
    

    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
  • w8f8w8f8 Posts: 5
    edited 2010-04-19 04:48
    How would I go about doing that?

    set my directionals to a set bit?

    i.e.

    int fwd;
    fwd == PTH &= 0x18;
    
    ...
    ...
    ...
    switch 
    {...
    
    case fwd == 1:
    do...
    
    


    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
  • FooWhoFooWho Posts: 17
    edited 2010-04-19 06:40
    If I understand correctly, you have a variable, VT, that will have bit 4 set if you have recieved a valid transmission.· Bits 0 through 3 will be set based on the directional command that was recieved.

    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
    if (VT & 0x10) {...}
    

    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:
    switch (VT)
    {
        case 0x18:
           // Go forward
        case 0x14:
           // Go backwards
        ...
    }
    

    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
  • w8f8w8f8 Posts: 5
    edited 2010-04-19 12:25
    Woohoo!!

    void set24MHzClock(void);
    void initializePortHReceiver(void);
    void init_PWM(void);
    
    void main(void) 
    {    
     unsigned char VT;
     set24MHzClock( );
     LCD_init();
     init_PWM();
      
    VT = PORTB & 0x1F;     
    
      
    if(VT & 0x10)
    {
    
      switch(VT)
      {
        case 0x17:
        writeLine("Right           ", 0);
        break;
        
        
        case 0x1B:              
        writeLine("Left            ", 0);
        break;
        
        case 0x1E:
        writeLine("Forward         ", 0);
        break;
        
        case 0x1D:
        writeLine("Reverse         ", 0);
        break;
    
        
        default:
        writeLine("Invalid         ", 0);
        break;                   
      }
    }
       else
      {      
        writeLine("Stopped          ", 0);
      }
     }
    



    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
  • FooWhoFooWho Posts: 17
    edited 2010-04-19 19:45
    Congrats!· I don't have any advice for you on the servos.· I am a total beginner with the robot hobby, but I know a few things about C code so I tried to lend a hand as far as that goes.
Sign In or Register to comment.