Advertisement
Guest User

Cyber37 MSP430 code with CSS ROBOT

a guest
Feb 27th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.53 KB | None | 0 0
  1. /*
  2. Par cyber37 pour http://skyduino.wordpress.com
  3. */
  4.  
  5.  
  6. #include <msp430g2231.h>
  7. #include    "stdbool.h"
  8.  
  9. #define     TXD         BIT1    // TXD on P1.1
  10. #define     RXD         BIT2    // RXD on P1.2
  11.  
  12. #define     Bit_time        104 // 9600 Baud, SMCLK=1MHz (1MHz/9600)=104
  13. #define     Bit_time_5      52  // Time for half a bit.
  14.  
  15. // ASCII values for the commands
  16. #define     MODE0           0x30 // initial
  17. #define     MODE1           0x31 // Marche AV
  18. #define     MODE2           0x32 // Marche AR / stop
  19. #define     MODE3           0x33 // Droite
  20. #define     MODE4           0x34 // Gauche
  21. #define     MODE5           0x35 // Gauche sur place
  22. #define     MODE6           0x36 // Droite sur place
  23.  
  24. unsigned char BitCnt;       // Bit count, used when transmitting byte
  25. unsigned int TXByte;        // Value sent over UART when Transmit() is called
  26. unsigned int RXByte;        // Value recieved once hasRecieved is set
  27.  
  28. unsigned int i;         // for loop variable
  29.  
  30. bool isReceiving;       // Status for when the device is receiving
  31. bool hasReceived;       // Lets the program know when a byte is received
  32. bool isGoingLeft;
  33. bool isGoingRight;
  34.  
  35. void ResetTurnState(void);
  36. void Transmit(void);
  37. void Receive(void);
  38.  
  39.  
  40. unsigned int mystate = 0;
  41. void main(void)
  42. {
  43.     WDTCTL = WDTPW + WDTHOLD;
  44.  
  45.     if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
  46.     {
  47.         while(1); // If cal constants erased, trap CPU!!
  48.     }
  49.     BCSCTL1 = CALBC1_1MHZ; // Set range
  50.     DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation
  51.  
  52.  
  53.  
  54.     P1SEL |= TXD;               // Connected TXD to timer
  55.     P1DIR |= TXD;
  56.  
  57.     P1IES |= RXD;               // RXD Hi/lo edge interrupt
  58.     P1IFG &= ~RXD;              // Clear RXD (flag) before enabling interrupt
  59.     P1IE |= RXD;
  60.  
  61.     P1DIR = (BIT0 + BIT6 + BIT5 + BIT4 + BIT7);               // set both leds to output
  62.     P1OUT = 0;                  // and set them off initially
  63.  
  64.     isReceiving = false;            // Set initial values
  65.     hasReceived = false;
  66.  
  67.     P1SEL &= ~0x08;                 // Select Port 1 P1.3 (push button)
  68.     P1IE |= 0x08;                   // Port 1 Interrupt Enable P1.3 (push button)
  69.     __enable_interrupt();               // Enable interrupts
  70.  
  71.     __bis_SR_register(GIE);         // interrupts enabled\
  72.  
  73.  
  74.     while(1)
  75.     {
  76.         if (hasReceived)        // If the device has recieved a value
  77.         {
  78.             Receive();
  79.         }
  80.         if (mystate == 0)
  81.         {
  82.             P1OUT = 0x01; // LED on
  83.             _delay_cycles(800000);
  84.             P1OUT = 0x00; // LED Off
  85.             _delay_cycles(800000);
  86.  
  87.  
  88.         }
  89.  
  90.         /*#define       MODE1           0x31 // Marche AV
  91. #define     MODE2           0x32 // Marche AR
  92. #define     MODE3           0x33 // Droite
  93. #define     MODE4           0x34 // Gauche*/
  94.         else if (mystate == 1) // Marche AV
  95.         {
  96.             P1OUT &= ~(BIT7 + BIT4); // Sens = 0
  97.             _delay_cycles(1000000);
  98.             P1OUT |= (BIT5 + BIT6); // On active La marche AV
  99.             ResetTurnState();
  100.         }
  101.         else if (mystate == 2) // TEST A GAUCHE
  102.         {
  103.             P1OUT =0x00;
  104.             ResetTurnState();
  105.         }
  106.         else if (mystate == 3) // Droite
  107.         {
  108.             P1OUT &= ~(BIT7 + BIT6 + BIT4); // On coupe le moteur de droite ..
  109.             _delay_cycles(1000000);
  110.             P1OUT |= (BIT5); // On active La marche AV de gauche
  111.             ResetTurnState();
  112.         }
  113.         else if (mystate == 4) // Gauche
  114.         {
  115.             P1OUT &= ~(BIT4 + BIT5 + BIT7); // On coupe le moteur de gauche ..
  116.             _delay_cycles(1000000);
  117.             P1OUT |= (BIT6); // On active La marche AV de droite
  118.             ResetTurnState();
  119.         }
  120.         else if (mystate == 5) // Gauche sur place
  121.         {
  122.             if (isGoingLeft)
  123.             {
  124.                 P1OUT &= ~(BIT7); // Sens moteur droite UP
  125.                 _delay_cycles(800000);
  126.                 P1OUT |= (BIT4);
  127.                 _delay_cycles(800000);
  128.                 P1OUT |= (BIT6 +BIT5); // On active La marche AV de droite
  129.             }
  130.             else
  131.             {
  132.                 P1OUT &= ~(BIT7+BIT6+BIT5); // Sens moteur droite UP
  133.                 _delay_cycles(800000);
  134.                 P1OUT |= (BIT4);
  135.                 _delay_cycles(800000);
  136.                 P1OUT |= (BIT6 +BIT5); // On active La marche AV de droite
  137.                 isGoingRight = false;
  138.                 isGoingLeft = true;
  139.             }
  140.         }
  141.         else if (mystate == 6) // droite sur place
  142.         {
  143.             if (isGoingRight)
  144.             {
  145.                 P1OUT &= ~(BIT4); // Sens moteur droite UP
  146.                 _delay_cycles(800000);
  147.                 P1OUT |= (BIT7);
  148.                 _delay_cycles(800000);
  149.                 P1OUT |= (BIT6 +BIT5); // On active La marche AV de droite
  150.             }
  151.             else
  152.             {
  153.                 P1OUT &= ~(BIT4+BIT5+BIT6); // Sens moteur droite UP
  154.                 _delay_cycles(800000);
  155.                 P1OUT |= (BIT7);
  156.                 _delay_cycles(800000);
  157.                 P1OUT |= (BIT6 +BIT5); // On active La marche AV de droite
  158.                 isGoingLeft= false;
  159.                 isGoingRight = true;
  160.             }
  161.         }
  162.     }
  163. }
  164. /**
  165.  * Handles the received byte and calls the needed functions.\
  166.  **/
  167. void ResetTurnState()
  168. {
  169.     isGoingLeft=false;
  170.     isGoingRight=false;
  171. }
  172. void Receive()
  173. {
  174.     hasReceived = false;    // Clear the flag
  175.     switch(RXByte)      // Switch depending on command value received
  176.     {
  177.         case MODE0:
  178.             mystate = 0;
  179.             break;
  180.         case MODE1:
  181.             mystate = 1;
  182.             break;
  183.  
  184.         case MODE2:
  185.             mystate = 2;
  186.             break;
  187.  
  188.         case MODE3:
  189.             mystate = 3;
  190.             break;
  191.  
  192.         case MODE4:
  193.             mystate = 4;
  194.             break;
  195.  
  196.         case MODE5:
  197.             mystate = 5;
  198.             break;
  199.  
  200.         case MODE6:
  201.             mystate = 6;
  202.             break;
  203.  
  204.         default:;
  205.     }
  206. }
  207. /**
  208.  * Transmits the value currently in TXByte. The function waits till it is
  209.  *   finished transmiting before it returns.
  210.  **/
  211. void Transmit()
  212. {
  213.     while(isReceiving);             // Wait for RX completion
  214.  
  215.     TXByte |= 0x100;                // Add stop bit to TXByte (which is logical 1)
  216.     TXByte = TXByte << 1;               // Add start bit (which is logical 0)
  217.     BitCnt = 0xA;                   // Load Bit counter, 8 bits + ST/SP
  218.  
  219.     CCTL0 = OUT;                    // TXD Idle as Mark
  220.     TACTL = TASSEL_2 + MC_2;            // SMCLK, continuous mode
  221.     CCR0 = TAR;                 // Initialize compare register
  222.     CCR0 += Bit_time;               // Set time till first bit
  223.     CCTL0 =  CCIS0 + OUTMOD0 + CCIE;        // Set signal, intial value, enable interrupts
  224.     while ( CCTL0 & CCIE );             // Wait for previous TX completion
  225. }
  226.  
  227.  
  228.  
  229.  
  230. #pragma vector=PORT1_VECTOR
  231. __interrupt void Port_1(void)
  232. {
  233.     if (P1IFG & 0x08)
  234.     {
  235.         P1IFG &= ~0x08;
  236.         if (mystate < 3)
  237.         {
  238.             mystate++;
  239.         }
  240.         else
  241.         {
  242.             mystate = 0;
  243.         }
  244.     }
  245.     else
  246.     {
  247.     isReceiving = true;
  248.  
  249.     P1IE &= ~RXD;           // Disable RXD interrupt
  250.     P1IFG &= ~RXD;          // Clear RXD IFG (interrupt flag)
  251.  
  252.     TACTL = TASSEL_2 + MC_2;    // SMCLK, continuous mode
  253.     CCR0 = TAR;         // Initialize compare register
  254.     CCR0 += Bit_time_5;     // Set time till first bit
  255.     CCTL0 = OUTMOD1 + CCIE;     // Dissable TX and enable interrupts
  256.  
  257.     RXByte = 0;         // Initialize RXByte
  258.     BitCnt = 0x9;           // Load Bit counter, 8 bits + ST
  259.     }
  260. }
  261.  
  262. /**
  263.  * Timer interrupt routine. This handles transmiting and receiving bytes.
  264.  **/
  265. #pragma vector=TIMERA0_VECTOR
  266. __interrupt void Timer_A (void)
  267. {
  268.     if(!isReceiving)
  269.     {
  270.         CCR0 += Bit_time;               // Add Offset to CCR0
  271.         if ( BitCnt == 0)               // If all bits TXed
  272.         {
  273.             TACTL = TASSEL_2;           // SMCLK, timer off (for power consumption)
  274.             CCTL0 &= ~ CCIE ;           // Disable interrupt
  275.         }
  276.         else
  277.         {
  278.             CCTL0 |=  OUTMOD2;          // Set TX bit to 0
  279.             if (TXByte & 0x01)
  280.                 CCTL0 &= ~ OUTMOD2;     // If it should be 1, set it to 1
  281.             TXByte = TXByte >> 1;
  282.             BitCnt --;
  283.         }
  284.     }
  285.     else
  286.     {
  287.         CCR0 += Bit_time;                       // Add Offset to CCR0
  288.         if ( BitCnt == 0)
  289.         {
  290.             TACTL = TASSEL_2;                   // SMCLK, timer off (for power consumption)
  291.             CCTL0 &= ~ CCIE ;                   // Disable interrupt
  292.  
  293.             isReceiving = false;
  294.  
  295.             P1IFG &= ~RXD;                      // clear RXD IFG (interrupt flag)
  296.             P1IE |= RXD;                        // enabled RXD interrupt
  297.  
  298.             if ( (RXByte & 0x201) == 0x200)     // Validate the start and stop bits are correct
  299.             {
  300.                 RXByte = RXByte >> 1;           // Remove start bit
  301.                 RXByte &= 0xFF;                 // Remove stop bit
  302.                 hasReceived = true;
  303.             }
  304.             __bic_SR_register_on_exit(CPUOFF);  // Enable CPU so the main while loop continues
  305.         }
  306.         else
  307.         {
  308.             if ( (P1IN & RXD) == RXD)           // If bit is set?
  309.                 RXByte |= 0x400;            // Set the value in the RXByte
  310.             RXByte = RXByte >> 1;               // Shift the bits down
  311.             BitCnt --;
  312.         }
  313.     }
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement