我使用的是超声波测距/声纳传感器(HCSR-04)和AVR ATmega32A。我已经编写了理解传感器如何工作的概念的代码,但是尽管程序编译正确,设备没有给出任何结果。
液晶屏显示“超出范围”,这意味着PINC1不会太高。我检查位是高还是低的方法是正确的吗?我应该使用bit_is_set(PORTC,PINC1)
函数吗?我使用的是默认频率1 MHz。PINC0连接到trig,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);
}
}
发布于 2015-03-07 10:27:25
我在重复几次的代码中看到的主要问题是如何读取输入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
寄存器是输出锁存器,将包含写入它们的最后一个值或上电默认值0。您需要使用PIN
寄存器来读取I/O引脚的当前状态。
发布于 2016-04-25 16:30:14
在检查回声引脚时,您需要将滴答计数设为TCNT1 = 300000而不是30000,因为您的时钟是1000000 HZ,因此每0.1us TCNT1递增1,因此对于300000滴答,您将获得30000 us (等待时间),这等于回声在5米处返回所需的时间(HCSR04的最大范围)
https://stackoverflow.com/questions/28896680
复制相似问题