Gerard-Meier

Untitled

Mar 13th, 2011
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. void updateQuad(struct MotorModel *motor) {
  2.     unsigned char newQuadState = 0b00;
  3.     signed long beforePosition = motor->position;
  4.  
  5.     if(*motor->sensorPort & motor->sensorAMask) {
  6.         newQuadState |= 0b01;
  7.     }
  8.     if(*motor->sensorPort & motor->sensorBMask) {
  9.         newQuadState |= 0b10;
  10.     }
  11.  
  12.     //PORTBbits.RB5 = 0;
  13.    
  14.     if(motor->quadState != newQuadState) {
  15.         switch(newQuadState) {
  16.             case 0b00:
  17.                     //PORTBbits.RB6 = 0;
  18.                     //PORTBbits.RB7 = 0;
  19.                     if(motor->quadState & 0b01) motor->position++;
  20.                     if(motor->quadState & 0b10) motor->position--;
  21.                 break;
  22.             case 0b01:
  23.                     if(motor->quadState & 0b11) motor->position++;
  24.                     if(motor->quadState & 0b00) motor->position--;
  25.                     //PORTBbits.RB6 = 1;
  26.                     //PORTBbits.RB7 = 0;
  27.                 break;
  28.             case 0b10:
  29.                     if(motor->quadState & 0b00) motor->position++;
  30.                     if(motor->quadState & 0b11) motor->position--;
  31.                     //PORTBbits.RB6 = 0;
  32.                     //PORTBbits.RB7 = 1;
  33.                 break;
  34.             case 0b11:
  35.                     if(motor->quadState & 0b10) motor->position++;
  36.                     if(motor->quadState & 0b01) motor->position--;
  37.                     //PORTBbits.RB6 = 1;
  38.                     //PORTBbits.RB7 = 1;
  39.                 break;
  40.         }
  41.         /*
  42.         if(motor->position > beforePosition) {
  43.             PORTBbits.RB7 = 0;
  44.             PORTBbits.RB6 = 1;
  45.            
  46.         } else if(motor->position < beforePosition) {
  47.             PORTBbits.RB7 = 1;
  48.             PORTBbits.RB6 = 0;
  49.         }
  50.         */
  51.         motor->quadState = newQuadState;
  52.     }
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment