c - 连接超声波距离传感器 (HCSR-04) 与 avr ATmega-32?

标签 c sensors avr atmega

我正在使用带有 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/

相关文章:

c - 简单中断处理程序 : request_irq returns error code -22

c - RTP丢包处理

ios - 当手指经过时,Swift Button 的作用就像一个传感器

algorithm - 如何使用 3 轴加速度计和 3 轴陀螺仪和 gps 进行导航

c - pow和powf有什么区别

filter - 与卡尔曼滤波器的传感器融合

c - 为什么这个编译器屏障不强制排序?

c - 发送 2 个 SPI 字节问题

c - 为什么 Atmega8 上的 MPU-6050 通过 I2C 输出显示恒定值?

c - 对 nxn 矩阵的所有可能排列进行编程,在 c 中给出唯一的和