SHARE
TWEET

Untitled

a guest Mar 20th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <msp430.h>
  2. #include "rand.h"
  3.  
  4. unsigned char* dataptr;
  5. unsigned int data_len = 28;
  6. unsigned int count = 0;
  7.  
  8. unsigned char seq[] = {0x00, 0x00, 0x00, 0x00, \
  9.                        0xE2, 0xFF, 0x00, 0x00, \
  10.                        0xE2, 0xFF, 0x00, 0x00, \
  11.                        0xE2, 0xFF, 0x00, 0x00, \
  12.                        0xE2, 0xFF, 0x00, 0x00, \
  13.                        0xE2, 0xFF, 0x00, 0x00, \
  14.                        0xFF, 0xFF, 0xFF, 0xFF};
  15.  
  16. const unsigned int lights[360]={
  17.   0,   0,   0,   0,   0,   1,   1,   2,\
  18.   2,   3,   4,   5,   6,   7,   8,   9,\
  19.  11,  12,  13,  15,  17,  18,  20,  22,\
  20.  24,  26,  28,  30,  32,  35,  37,  39,\
  21.  42,  44,  47,  49,  52,  55,  58,  60,\
  22.  63,  66,  69,  72,  75,  78,  81,  85,\
  23.  88,  91,  94,  97, 101, 104, 107, 111,\
  24. 114, 117, 121, 124, 127, 131, 134, 137,\
  25. 141, 144, 147, 150, 154, 157, 160, 163,\
  26. 167, 170, 173, 176, 179, 182, 185, 188,\
  27. 191, 194, 197, 200, 202, 205, 208, 210,\
  28. 213, 215, 217, 220, 222, 224, 226, 229,\
  29. 231, 232, 234, 236, 238, 239, 241, 242,\
  30. 244, 245, 246, 248, 249, 250, 251, 251,\
  31. 252, 253, 253, 254, 254, 255, 255, 255,\
  32. 255, 255, 255, 255, 254, 254, 253, 253,\
  33. 252, 251, 251, 250, 249, 248, 246, 245,\
  34. 244, 242, 241, 239, 238, 236, 234, 232,\
  35. 231, 229, 226, 224, 222, 220, 217, 215,\
  36. 213, 210, 208, 205, 202, 200, 197, 194,\
  37. 191, 188, 185, 182, 179, 176, 173, 170,\
  38. 167, 163, 160, 157, 154, 150, 147, 144,\
  39. 141, 137, 134, 131, 127, 124, 121, 117,\
  40. 114, 111, 107, 104, 101,  97,  94,  91,\
  41.  88,  85,  81,  78,  75,  72,  69,  66,\
  42.  63,  60,  58,  55,  52,  49,  47,  44,\
  43.  42,  39,  37,  35,  32,  30,  28,  26,\
  44.  24,  22,  20,  18,  17,  15,  13,  12,\
  45.  11,   9,   8,   7,   6,   5,   4,   3,\
  46.   2,   2,   1,   1,   0,   0,   0,   0,\
  47.   0,   0,   0,   0,   0,   0,   0,   0,\
  48.   0,   0,   0,   0,   0,   0,   0,   0,\
  49.   0,   0,   0,   0,   0,   0,   0,   0,\
  50.   0,   0,   0,   0,   0,   0,   0,   0,\
  51.   0,   0,   0,   0,   0,   0,   0,   0,\
  52.   0,   0,   0,   0,   0,   0,   0,   0,\
  53.   0,   0,   0,   0,   0,   0,   0,   0,\
  54.   0,   0,   0,   0,   0,   0,   0,   0,\
  55.   0,   0,   0,   0,   0,   0,   0,   0,\
  56.   0,   0,   0,   0,   0,   0,   0,   0,\
  57.   0,   0,   0,   0,   0,   0,   0,   0,\
  58.   0,   0,   0,   0,   0,   0,   0,   0,\
  59.   0,   0,   0,   0,   0,   0,   0,   0,\
  60.   0,   0,   0,   0,   0,   0,   0,   0,\
  61.   0,   0,   0,   0,   0,   0,   0,   0};
  62. /**\
  63.  * main.c
  64.  */
  65. int main(void)
  66. {
  67.     BCSCTL3 |= LFXT1S_2; //Set VLO as smckl
  68.     WDTCTL = WDT_ADLY_16; //Set multiplier for VLO
  69.  
  70.     IE1 |= WDTIE; //Set watchdog timer interrupts
  71.  
  72.     P1DIR |= BIT5 + BIT7; //Set p1.5 and p1.7 as output
  73.     P1SEL = BIT5 + BIT7; //Enable UCB0CLK and UCB0SIMO
  74.     P1SEL2 = BIT5 + BIT7; //Enable UCB0CLK and UCB0SIMO
  75.  
  76.     UCB0CTL1 |= UCSWRST; // Reset software on USB0
  77.     UCB0CTL0 |= UCMST + UCCKPH + UCSYNC + UCMSB;  // Set for 3-pin and 8-bit master
  78.  
  79.     UCB0CTL1 |= UCSSEL_2; // Set values for SMCLK
  80.     UCB0BR0 |= 0;
  81.     UCB0BR1 = 0;
  82.  
  83.     UCB0CTL1 &= ~UCSWRST; //Start USCI state machine
  84.  
  85.     IE2 |= UCB0TXIE; // Enable USCI0 TX interrupt
  86.  
  87.     __bis_SR_register(GIE); //Enable interrupts
  88.  
  89.     TA1CCR0 = 1000;  // Set timer A1 frequency
  90.     TA1CTL = TASSEL_1 + MC_1; // Set timer multiplier
  91.  
  92.     dataptr = &seq[0]; // Set pointer to first entry in sequence
  93.     UCB0TXBUF = *dataptr; //Initially push to transmit buffer
  94.  
  95.     srand(20);
  96.  
  97.     int last_change = 0;
  98.  
  99.         //Initialize angle
  100.         int angle = 0;
  101.  
  102.         // Given step size
  103.         int step_size = rand();
  104.  
  105.         // Variables for holding the colors values
  106.         int red;
  107.         int blue;
  108.         int green;
  109.  
  110.         while(1 == 1){
  111.  
  112.             __bis_SR_register(LPM3_bits); //Enter LPM3 to delay cycle
  113.  
  114.             int cur_rand = rand();
  115.             step_size = (7*last_change + 16 - cur_rand)>>3;
  116.             if(step_size == last_change){
  117.                 angle = 0;
  118.             }
  119.             angle += step_size;
  120.  
  121.             if(angle > 360){
  122.                 angle -= 360;
  123.             }
  124.  
  125.             if(angle < 0){
  126.                 angle += 360;
  127.             }
  128.  
  129.             red = lights[(angle+120)%360];
  130.             blue = lights[angle];
  131.             green = lights[(angle+240)%360];
  132.  
  133.  
  134.             seq[7] = red;                       //Update the R, G, B message to all be the same color
  135.             seq[6] = green;
  136.             seq[5] = blue;
  137.  
  138.             seq[11] = red;
  139.             seq[10] = green;
  140.             seq[9] = blue;
  141.  
  142.             seq[15] = red;
  143.             seq[14] = green;
  144.             seq[13] = blue;
  145.  
  146.             seq[19] = red;
  147.             seq[18] = green;
  148.             seq[17] = blue;
  149.  
  150.             seq[23] = red;
  151.             seq[22] = green;
  152.             seq[21] = blue;
  153.  
  154.             seq[27] = red;
  155.             seq[26] = green;
  156.             seq[25] = blue;
  157.  
  158.             last_change = step_size;
  159.  
  160.             UCB0TXBUF = *dataptr; //Add current message to the transmit buffer
  161.         }
  162. }
  163.  
  164. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  165. #pragma vector=USCIAB0TX_VECTOR
  166. __interrupt void USCIB0TX_ISR(void)
  167. #elif defined(__GNUC__)
  168. void __attribute__((interrupt(USCIAB0TX_VECTOR))) USCIB0TX_ISR (void)
  169. #else
  170. #error Compiler not supported!
  171. #endif
  172. {
  173.     // Send all other sequence elements
  174.     if (count <= data_len){
  175.         UCB0TXBUF = *dataptr;
  176.         dataptr++;
  177.         count++;
  178.     }
  179.     // Reset data pointer and sequence
  180.     else {
  181.         IFG2 &= ~UCB0TXIFG;
  182.         count = 0;
  183.         dataptr = &seq[0];
  184.     }
  185. }
  186. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  187. #pragma vector = WDT_VECTOR
  188. __interrupt void watchdog_timer(void)
  189. #elif defined(__GNUC__)
  190. void __attribute__((interrupt(WDT_VECTOR))) watchdog_timer(void)
  191. #else
  192. #error Compiler not supported!
  193. #endif
  194. {
  195.     __bic_SR_register_on_exit(LPM3_bits);
  196. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top