我正在使用带有 AVR ATmega32A 的超声波测距/声纳传感器 (HCSR-04)。我已经编写了理解传感器工作原理的代码,但尽管程序编译正确,但设备没有给出任何结果。
LCD 显示“超出范围”,这意味着 PINC1 没有变高。我检查某个位是高还是低的方法正确吗?我应该使用 bit_is_set(PORTC,PINC1)
函数吗?我使用默认频率 1 MHz。 PINC0 连接到触发器,PINC1 是回波输入。这是代码:
#include<avr/io.h>
#include<util/delay.h>
#include"lcd.h"
int main(void){
ini_lcd();
TCCR1B|=1<<CS10;
DDRC|=1<<PINC0;
int a=0,b=0;
while(1){
TCNT1=0;
PORTC|=1<<PINC0;
while(TCNT1<100);
PORTC&=~(1<<PINC0);
TCNT1=0;
while(!(PORTC&(1<<PINC1))) && TCNT1<30000) ; // checking if echo has become high and not exceeding the time for max range i.e 5 m
///send_string("out");
b=TCNT1;
if(b<30000){
TCNT1=0;
while(PORTC&(1<<PINC1)); // waiting until the echo become low again
a=TCNT1;
go_to_pos(1,5);
send_int(a/58);
} else {
go_to_pos(1,5);
send_string("out of range");
}
_delay_ms(20);
}
}
最佳答案
我在重复几次的代码中看到的主要问题是如何尝试读取输入 I/O 行,例如:
while(PORTC&(1<<PINC1)); // waiting until the echo become low again
应该变成:
while(PINC&(1<<PINC1)); // waiting until the echo become low again
PORT
寄存器是输出锁存器,将包含最后写入的值或上电默认值零。您需要使用PIN
寄存器来读取I/O引脚的当前状态。
关于c - 连接超声波距离传感器 (HCSR-04) 与 avr ATmega-32?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28896680/