首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AVR中的超声波传感器

AVR中的超声波传感器
EN

Stack Overflow用户
提问于 2016-12-17 06:42:03
回答 2查看 1.3K关注 0票数 1

我正在努力创造一个超声波测距探测器。我目前正在测试传感器,以确保它工作正常。我已经将echo引脚和触发器引脚分别连接到PC4和PC5。当我运行这段代码时,理想情况下它会向我的显示器发送6。但是,它显示为0。这让我相信代码没有正确地与传感器接口。请帮帮忙。

代码语言:javascript
运行
复制
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

void DisplayIt(int i);

int main(void)
{

    while(1)
    {
        DDRC = 0xFF;
        int i = 0;
        PORTC = 0b00000000;
        _delay_us(2);
        PORTC = 0b00100000;
        _delay_us(10);
        PORTC = 0x00;

        DDRC = 0x00;
        if (PINC == 0b00010000)
        {
            i = 6;
        }
        DisplayIt(i);
    }

}
EN

回答 2

Stack Overflow用户

发布于 2017-01-14 17:47:26

我不知道你用的是什么超声波传感器。但我想那是因为你没有等到传感器收到回声信号。基于我用过的超声波传感器,SRF04,它有一个这样的时序图:

我修改了你的代码,这样当传感器检测到它前面的物体(我们从回波信号的到来就知道了)时,它就可以打印"6“。

代码如下:

代码语言:javascript
运行
复制
while(1) {
  DDRC = 0xFF;  // Configure all Port C pins as an output
  int i = 0;

  PORTC = 0b00100000;  // Write 1 (high) to PORTC.5 (trigger pin)
  _delay_us(10);  // Keep PORTC.5 to give high signal output for 10us
  PORTC = 0x00;  // Write 0 (low) to PORTC.5

  // The code above completes Trigger Input To Module (see Timing Diagram image)

  DDRC = 0x00;  // Configure all Port C pins as an input
  while (PINC.4 == 0);  // Wait until PINC.4 (echo pin) has 1 (high) value
  if(PINC.4 == 1) i = 6;  // Once PINC.4 is high, while loop will break and this line will be executed

  DisplayIt(i);

  _delay_ms(10);  // Allow 10ms from End of Echo to Next Trigger Pulse
}
票数 2
EN

Stack Overflow用户

发布于 2016-12-17 07:52:33

PINCPORTC是同一个寄存器。

在读取该寄存器之前,PORTC = 0x00;将该寄存器的内容设置为0。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41193449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档