Advertisement
Goreish_Ahmed

IR sensor code

Jul 2nd, 2022
1,009
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.33 KB | None
  1. #include <msp430.h>
  2. volatile unsigned int start_time;
  3. volatile unsigned int stop_time;
  4. volatile unsigned int max_time;
  5. volatile unsigned int counter = 0;
  6.  
  7. #pragma vector = TIMER0_A1_VECTOR
  8. __interrupt void counter1(void){
  9.     counter++;
  10.     TA0CTL &= ~TAIFG;
  11. }
  12.  
  13. #pragma vector = PORT1_VECTOR
  14. __interrupt void IR_SENSORS(void){
  15.     if((P1IFG & 1) != 0){
  16.         start_time = counter;
  17.         max_time = TA0CCR0 * 2000;
  18.         if((P1IN & 1) != 0){
  19.             stop_time = start_time;
  20.             if(stop_time < max_time){
  21.                 P2OUT ^= 1<<3;
  22.             }
  23.         }
  24.  
  25.         P1IFG &= ~1;
  26.     }
  27.     if((P1IFG & 1<<1) != 0){
  28.         P2OUT ^= 1<<4;
  29.         P1IFG &= ~(1<<1);
  30.     }
  31.     if((P1IFG & 1<<2) != 0){
  32.         P2OUT ^= 1<<5;
  33.         P1IFG &= ~(1<<2);
  34.     }
  35. }
  36.  
  37. int main(void){
  38.       WDTCTL = WDTPW | WDTHOLD;    // stop watchdog timer
  39.       DCOCTL = CALDCO_1MHZ;
  40.       BCSCTL1 = CALBC1_1MHZ;
  41.  
  42.       TA0CTL |= TASSEL_2 | ID_1 | MC_1 | TAIE; //1 SEC = 1MHZ.
  43.       TA0CCR0 = 1000; // 1ms
  44.     P2DIR |= 1<<3 | 1<<4 | 1<<5;
  45.     P2OUT |= 1<<3 | 1<<4 | 1<<5;
  46.  
  47.     P1DIR &= ~(1 | 1<<1 | 1<<2);
  48.     P1REN |= 1 | 1<<1 | 1<<2;
  49.     P1OUT &= ~(1 | 1<<1 | 1<<2);
  50.  
  51.     P1IE |= 1 | 1<<1 | 1<<2;
  52.     P1IES &= ~(1 | 1<<1 | 1<<2);
  53.  
  54.     __enable_interrupt();
  55.  
  56.  
  57.  
  58.     while(1){
  59.  
  60.     }
  61.  
  62.     return 0;
  63. }
  64.  
Advertisement
RAW Paste Data Copied
Advertisement