0
我正在使用超聲波傳感器讀取一些項目的一部分,並用串行通信發送它,我編寫代碼並給出了隨機讀取,有時給出0作爲讀取,是公式我用於找到正確的距離!?,或者有另一個公式,我使用內部8MHz時鐘的Atmega32,有人可以幫助我,並知道我的代碼有什麼問題!與AVR接口的超聲波傳感器
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void inti_serial();
static volatile int pulse = 0;
static volatile int change = 0;
int main(void)
{
/* Replace with your application code */
inti_serial();
MCUCR |= (1 << ISC00); //Any logical change on INT0
GICR |= (1 << INT0); //Enable INT0
TCCR1A=0;
sei();
while (1)
{
PORTC |= (1<<0);
_delay_us(15);
PORTC &= ~(1<<0);
while(!(UCSRA & (1<<UDRE)));
UDR = ((pulse/2)*1*(1/F_CPU)*343) ;
_delay_ms(100);
}
}
ISR(INT0_vect){
if (change==1)//when logic from HIGH to LOW
{
TCCR1B=0;//disabling counter
pulse=TCNT1;//count memory is updated to integer
TCNT1=0;//resetting the counter memory
change=0;
}
if (change==0)//when logic change from LOW to HIGH
{
TCCR1B|=(1<<CS10);//enabling counter
change=1;
}
}
void inti_serial()
{
UCSRB |= (1<<TXEN);
UCSRC |= (1<<UCSZ0) | (1<<UCSZ1) | (1<<URSEL);
UBRRL = 0x33;
}
[序列化C中的數據結構]的可能重複(https://stackoverflow.com/questions/371371/serialize-data-structures-in-c) –
@GianlucaGhettini這個問題與序列化無關,但遠程地,與*串行通訊*。 – JimmyB