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!!!
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!!!
Rich (BB code):
CODE:
#define F_CPU 11059200UL
#define USART_BAUDRATE 9600 // Baud Rate value
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
volatile uint32_t result;
volatile int capture_pulse;
int start_timer;
void init_int()
{
cli();
MCUCR |= (0<<ISC11)|(1<<ISC10);//|(1<<ISC01)|(0<<ISC00);
GICR |=(1<<INT1);//|(1<<INT0);
sei();
}
ISR(INT1_vect)
{
if ((PIND & 0x08) == 0x08)
{
// initialize overflow counter variable
result = 0;
TCCR0 |= (0<<CS02)|(0<<CS01)|(1<<CS00); // no Prescaler //F_CPU/8
// initialize counter
TCNT0 = 0;
// enable overflow interrupt
TIMSK |= (1 << TOIE0);
}
else
{
TCCR0 &= ~(1<<CS00); //stop timer
result += TCNT0;
//TCNT0 = 0; // reset timer
capture_pulse=1;
}
}
ISR(TIMER0_OVF_vect)
{
result += 255; //add overflow count of TCNT0
}
void USARTInit()
{
//Set Baud rate
UBRRL = BAUD_PRESCALE; //ubrr_value;
UBRRH = BAUD_PRESCALE; //(ubrr_value>>8);
//Set Frame Format
UCSRC=(1<<URSEL)|(3<<UCSZ0);
// UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes
//Enable The receiver and transmitter
UCSRB=(1<<TXEN);//|(1<<RXEN);
//UCSRB |= (1 << RXCIE); // Set receive interrupt ON
UBRRL = BAUD_PRESCALE;
// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}
void USARTWriteChar(char data)
{
//Wait untill the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
//Now write the data to USART buffer
UDR=data;
}
// Send string through USART
void usart_send_string(char *str)
{
int len,j;
//char count[3];
len = strlen(str);
for (j=0;j<len;j++)
{
USARTWriteChar(str[j]);
//_delay_ms(10);
}
//return len;
}
void Trg()
{
PORTB &= ~(1 << PB0);
_delay_us(10);
PORTB |= (1 << PB0);
_delay_us(12);
PORTB &= ~(1 << PB0);
//_delay_us(10);
}
int main(void)
{
int i;
result=0;
char value[7];
int distance = 0;
DDRB=0b00000001;
USARTInit();
init_int();
DDRD=0x00;
while(1)
{
capture_pulse=0;
int j=0; //temporary variable
Trg();
while(!capture_pulse);
USARTWriteChar(45);
// calculate duration
distance = (17013.0/F_CPU)* (float)result;
// dist = duration * speed of sound * 1/2
// dist in cm = duration in s * 340.26 * 100 * 1/2
// = 17013*duration
//distance = 17013.0 * duration;
//distance=result;
for (i=0;i<=5;i++)
{
j=distance%10; //extraction LSD of TCNT1
distance=distance/10;
value[5-i]=48+j; //converting decimal to ASCII
//j=0;//USARTWriteChar(value);
}
value[6] = '\0';
usart_send_string(value);
distance = 0;
_delay_ms(1000);
}
}