Advertisement
Goreish_Ahmed

HCSR04_not_working

Jun 29th, 2021
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <msp430.h>
  2.  
  3. volatile unsigned int distance_in_cm = 0;
  4. volatile unsigned int rising_edge = 0;
  5. volatile unsigned int start_time;
  6. volatile unsigned int total_time;
  7.  
  8. void turn_on_timer(){
  9.     P1OUT |= 1;
  10. }
  11.  
  12. void turn_off_timer(){
  13.     P1OUT &= ~(1);
  14. }
  15.  
  16.  
  17. #pragma vector = TIMER0_A1_VECTOR
  18. __interrupt void distance(void){  //TIMERA0 interrupt
  19.     if(rising_edge){
  20.         start_time = TACCR0;    //checks for rising edge, if so, TACCRO is the start time
  21.     }else{
  22.         total_time = TACCR0 - start_time; //when rising_edge = 0, your saved time the pulse went and came back is TACCR0 - start_time
  23.         distance_in_cm = total_time/58;
  24.  
  25.     }
  26.  
  27.     TA0CTL &= TAIFG;
  28. }
  29.  
  30. int main(void){
  31.     DCOCTL = 0;
  32.     BCSCTL1 = CALBC1_1MHZ;     //submain clock 1 MHZ
  33.     DCOCTL = CALDCO_1MHZ;
  34.     WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
  35.  
  36.     TA0CTL = TASSEL_2 | ID_3 | MC_2 | TAIE;
  37.     TA0CCR0 = 100;
  38.     //   TASSEL_2 = Timer A clock source select : 2 - SMCLK
  39.     //   ID_3 = Timer A input divider : 3 - 100*8 = 800 microseconds = 0,0008 seconds
  40.     //   MC_2 = Timer A mode control : 2 - Continous up
  41.     //   enable timer interrupt
  42.  
  43.     __enable_interrupt();
  44.     P1DIR &= ~(1<<1); //echo pin
  45.     P1DIR |= 1; //trig pin
  46.     P1OUT &= ~1;
  47.     P2DIR |= 1<<2;
  48.     P2OUT &= ~(1<<2);
  49.  
  50.     P2DIR |= (1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  51.     P2OUT &= ~(1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  52.     P1IE |= 1<<1;
  53.     P1IES &= ~1<<1;
  54.  
  55.     while(1){
  56.         rising_edge = 1; //after pulse has been send rising_edge = 1, TACCR0 detects rising and falling edges
  57.         turn_on_timer();
  58.         __delay_cycles(10); //wait 10 microseconds
  59.         turn_off_timer();
  60.         __delay_cycles(60000); // wait long enough for the interrupt service routine to kick in. 60000 microseconds = 0,06 seconds
  61.         TA0CTL |= TAIFG;
  62.     }
  63.  
  64.     rising_edge != rising_edge; //after this, the rising_edge needs to be the opposite as it was
  65.     if(distance_in_cm > 32){
  66.         P2OUT |= 1<<3;
  67.     }else if(distance_in_cm < 32 && distance_in_cm > 25){
  68.         P2OUT |= 1<<4;
  69.     }else if(distance_in_cm < 25 && distance_in_cm > 20){
  70.         P2OUT |= 1<<5;
  71.     }else if(distance_in_cm < 20 && distance_in_cm > 15){
  72.             P2OUT |= 1<<6;
  73.     }else if(distance_in_cm < 15 && distance_in_cm > 10){
  74.         P2OUT |= 1<<7;
  75.     }
  76.  
  77.     while(1);
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement