Ultrasonic Sensor freaking me out:(

Discussion in 'Embedded Systems and Microcontrollers' started by s1904j, Jul 9, 2014.

  1. s1904j

    Thread Starter New Member

    Jul 9, 2014
    1
    0
    hey, I am using HC-SR-04 ultrasonic sensor to calculate distance and send data to rs232 serial i have used B0 for trigger and D3(INT1) for level interrupt
    I am using timer0 for the calculation of pulse width but i am getting wierd output.
    I checked USART is working property, i think some where interrupt or timer is not working properly.
    plzz help!!!
    Code ( (Unknown Language)):
    1.  
    2. CODE:
    3. #define F_CPU 11059200UL
    4. #define USART_BAUDRATE 9600 // Baud Rate value
    5. #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
    6.  
    7. #include <avr/io.h>
    8. #include <avr/interrupt.h>
    9. #include <util/delay.h>
    10. #include <string.h>
    11.  
    12. volatile uint32_t result;
    13. volatile int capture_pulse;
    14. int start_timer;
    15.  
    16. void init_int()
    17. {
    18.     cli();
    19.     MCUCR |= (0<<ISC11)|(1<<ISC10);//|(1<<ISC01)|(0<<ISC00);
    20.     GICR |=(1<<INT1);//|(1<<INT0);
    21.     sei();
    22. }
    23.  
    24. ISR(INT1_vect)
    25. {
    26.     if ((PIND & 0x08) == 0x08)
    27.     {
    28.        
    29.         // initialize overflow counter variable
    30.         result = 0;
    31.  
    32.         TCCR0 |= (0<<CS02)|(0<<CS01)|(1<<CS00); // no Prescaler //F_CPU/8
    33.  
    34.         // initialize counter
    35.         TCNT0 = 0;
    36.  
    37.         // enable overflow interrupt
    38.         TIMSK |= (1 << TOIE0);
    39.  
    40.     }
    41.     else
    42.     {
    43.    
    44.         TCCR0 &= ~(1<<CS00);    //stop timer
    45.         result += TCNT0;
    46.         //TCNT0 = 0;                // reset timer
    47.         capture_pulse=1;
    48.     }
    49. }
    50.  
    51. ISR(TIMER0_OVF_vect)
    52. {
    53.     result += 255;  //add overflow count of TCNT0
    54. }
    55.  
    56. void USARTInit()
    57. {
    58.     //Set Baud rate
    59.     UBRRL = BAUD_PRESCALE; //ubrr_value;
    60.     UBRRH = BAUD_PRESCALE; //(ubrr_value>>8);
    61.     //Set Frame Format
    62.     UCSRC=(1<<URSEL)|(3<<UCSZ0);
    63.     //  UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
    64.     // Use 8-bit character sizes
    65.     //Enable The receiver and transmitter
    66.     UCSRB=(1<<TXEN);//|(1<<RXEN);
    67.     //UCSRB |= (1 << RXCIE); // Set receive interrupt ON
    68.     UBRRL = BAUD_PRESCALE;
    69.     // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
    70.     UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
    71.     // into the high byte of the UBRR register
    72. }
    73.  
    74.  
    75. void USARTWriteChar(char data)
    76. {
    77.     //Wait untill the transmitter is ready
    78.     while(!(UCSRA & (1<<UDRE)))
    79.     {
    80.         //Do nothing
    81.     }
    82.     //Now write the data to USART buffer
    83.     UDR=data;
    84. }
    85.  
    86. // Send string through USART
    87. void usart_send_string(char *str)
    88. {
    89.     int len,j;
    90.     //char count[3];
    91.     len = strlen(str);
    92.     for (j=0;j<len;j++)
    93.         {
    94.         USARTWriteChar(str[j]);
    95.         //_delay_ms(10);
    96.         }
    97.     //return len;
    98. }
    99.  
    100. void Trg()
    101. {
    102.     PORTB &= ~(1 << PB0);
    103.     _delay_us(10);
    104.     PORTB |= (1 << PB0);
    105.     _delay_us(12);
    106.     PORTB &= ~(1 << PB0);
    107.     //_delay_us(10);
    108. }
    109.  
    110. int main(void)
    111. {
    112.     int i;
    113.     result=0;
    114.     char value[7];
    115.     int distance = 0;
    116.  
    117.     DDRB=0b00000001;
    118.  
    119.     USARTInit();
    120.     init_int();
    121.     DDRD=0x00;    
    122.  
    123.    
    124.     while(1)
    125.     {
    126.         capture_pulse=0;
    127.         int j=0; //temporary variable
    128.         Trg(); 
    129.  
    130.         while(!capture_pulse);     
    131.             USARTWriteChar(45);
    132.            
    133.             // calculate duration
    134.             distance = (17013.0/F_CPU)* (float)result;
    135.             // dist = duration * speed of sound * 1/2
    136.             // dist in cm = duration in s * 340.26 * 100 * 1/2
    137.             // = 17013*duration
    138.             //distance = 17013.0 * duration;
    139.             //distance=result;
    140.             for (i=0;i<=5;i++)
    141.             {
    142.                 j=distance%10;  //extraction LSD of TCNT1
    143.                 distance=distance/10;  
    144.                 value[5-i]=48+j;        //converting decimal to ASCII
    145.                 //j=0;//USARTWriteChar(value);
    146.             }
    147.             value[6] = '\0';
    148.             usart_send_string(value);
    149.             distance = 0;
    150.     _delay_ms(1000);
    151.     }
    152. }
    153.  
     
Loading...