Advertisement
Guest User

aosasoa

a guest
Oct 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.45 KB | None | 0 0
  1. #define ECHO BIT1                          //configure Pins
  2. #define TRIG BIT2
  3. #define SER BIT0
  4. #define RCLK BIT1
  5. #define SRCLK BIT2
  6.  
  7.  
  8.  
  9. #include <msp430g2553.h>                  // include necessary templates
  10. #include <intrinsics.h>
  11. #include <stdint.h>
  12.  
  13.  
  14. volatile unsigned int distance_in_cm=0;   // set up intgers for measuring
  15. volatile unsigned int start_time;
  16. volatile unsigned int total_time;
  17. volatile unsigned int up=0;
  18.  
  19. int y;
  20.  
  21. char array[8][8] ={                     // set up matrix
  22.           {1,0,0,0,0,0,0,0},
  23.                   {1,1,0,0,0,0,0,0},
  24.                   {1,1,1,0,0,0,0,0},
  25.                   {1,1,1,1,0,0,0,0},
  26.                   {1,1,1,1,1,0,0,0},
  27.                   {1,1,1,1,1,1,0,0},
  28.                   {1,1,1,1,1,1,1,0},
  29.                   {1,1,1,1,1,1,1,1}
  30.                   };
  31.  
  32.  
  33. void init (void){
  34.  
  35.  
  36.     WDTCTL = WDTPW | WDTHOLD;                        //kill watchdog timer
  37.     TACCTL0 |= CM_3 + SCS + CAP + CCIE + CCIS_0;     // set Timer A0 in capture mode
  38.     TA0CTL = TASSEL_2 + ID_0 + MC_2;                 // configure timer A0
  39.     P1DIR = TRIG;                                    // set trigger as output
  40.     P1SEL = ECHO;                                    // set echo as input (capture mode)
  41.     P2OUT &= ~(SER | RCLK | SRCLK);                  // preset SER RCLK and SRCLK of the 74HC595 off
  42.     P2DIR |= SER | RCLK | SRCLK;                     // set SER RCLK and SRCLK of the 74HC595 as output
  43.     TA1CTL = TASSEL_2 | ID_3 | MC_1 | TACLR;         // configure timer A1
  44.     TA1CCR0 = 100;
  45.  
  46.     int i;
  47.  
  48.  
  49.     for(i=0; i<=16; i++){                           // clear bit register
  50.           while ((TA1CTL & TAIFG) == 0){
  51.           }
  52.           TACTL &= ~TAIFG;
  53.           P2OUT ^= SRCLK;
  54.     }
  55.  
  56. }
  57. //--------------------------------------------------------
  58. void Delay (long ms){                                   // Delay
  59.     int i;
  60.     for (i=0; i<=ms;i++)
  61.     {
  62.         __delay_cycles(1000);
  63.     }
  64. }
  65.  
  66. //-------------------------------------------------------
  67. void ClockCycle (void){                               // clockcyle void for the 74HC595
  68.     int i;
  69.     for (i=0;i<=1;i++){
  70.         while ((TA1CTL & TAIFG) ==0 ){
  71.         }
  72.         TA1CTL &= ~TAIFG;
  73.         P2OUT ^= SRCLK;
  74.     }
  75. }
  76. //-------------------------------------------------------
  77.  
  78. void Show (void)                                    // show void for the 74HC595
  79. {
  80.     P2OUT |= RCLK;
  81.     P2OUT &= ~RCLK;
  82. }
  83. //-------------------------------------------------------
  84. void Set (void){                                    // Set high void for the 74HC595
  85.  
  86.     P2OUT |= SER;
  87.     ClockCycle();                              // always one clockcycle high
  88.     P2OUT &= ~SER;
  89. }
  90. //-------------------------------------------------------
  91.  
  92. void SetPattern (char arr[8][8])                   // void that sets pattern depending on chosen row of matrix
  93. {
  94.  
  95.    int x;
  96.  
  97.  
  98.    for (x=0; x<=7; x++){                          // checks al coloms of matrix
  99.  
  100.     if (arr[y][x] != 0){                      // if not zero, then set, else dont set (clockcycle)
  101.         Set();
  102.     }
  103.     else {
  104.         ClockCycle();
  105.     }
  106.  
  107.    }
  108.  
  109.    Show();                                        // show the setted array
  110.    Delay(5);                                      // wait five milliseconds
  111.  
  112. }
  113.  
  114.  
  115. //-------------------------------------------------------
  116.  
  117.  
  118. void main(void) {
  119.  
  120. init();
  121.  
  122. __enable_interrupt();                                     // enabling interrupts
  123. while (1)                                                 // do this forever
  124.     {
  125.         up = 1;                                                   // when pulse is send, up =1 (high edge),  TACCR0 detects rising and falling edges
  126.         P1OUT |= TRIG;                                    // sending pulses for the HC-SR04
  127.        _delay_cycles(20);
  128.            P1OUT &= ~TRIG;
  129.            _delay_cycles(60000);                          // wait long enough for the ISR to kick in
  130.     }
  131. }
  132.  
  133. //------------------------------------------------------
  134. #pragma vector=TIMER0_A0_VECTOR
  135. __interrupt void TIMERA0_ISR (void){
  136. if (up){
  137.         start_time = TACCR0;                                      // check for high edge, if so, TACCR0 is the start time
  138.     }
  139. else{
  140.         total_time = TACCR0 - start_time;                        // when up = 0, your time the pulse went and came back is TACCR0- start time
  141.     distance_in_cm = total_time/58;                          // distance is the time divided by 58
  142.  
  143.     if (distance_in_cm > 32 ){
  144.         y=0;
  145.     }
  146.     else if (distance_in_cm < 32 && distance_in_cm > 25){     // checking distance, depending on what the distance is, it
  147.         y=1;                                                      // configures a diffrent row of the matrix
  148.     }
  149.     else if (distance_in_cm < 25 && distance_in_cm > 19){
  150.         y=2;
  151.     }
  152.     else if (distance_in_cm < 19 && distance_in_cm > 13){
  153.         y=3;
  154.     }
  155.     else if (distance_in_cm < 13 && distance_in_cm > 9){
  156.         y=4;
  157.     }
  158.     else if (distance_in_cm < 9 && distance_in_cm > 6){
  159.         y=5;
  160.     }
  161.     else if (distance_in_cm < 6 && distance_in_cm > 3){
  162.         y=6;
  163.     }
  164.     else if (distance_in_cm < 3 ){
  165.         y=7;
  166.     }
  167.     SetPattern (array);
  168. }
  169. up=!up;                                              // after this, up needs to be opposite as it was
  170. TA0CTL &= ~TAIFG;                                    // clear timer flag
  171. TACCTL0 &= ~CCIFG;                                   // clear CCI flag
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement