为什么我的 LED 在测量到一定距离时不亮? Atmega328p 和超声波 HC-SR04 传感器

问题描述

我使用 Atmega16 来控制带有超声波传感器的 LED。基本上,如果距离小于 100 厘米,LED 灯会亮起,否则 LED 灯会熄灭。

%systemdrive%

问题是我必须在我的项目中使用 Atmega328p。因为 328p 没有 PORTA 我把代码改成:

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


#define  Trigger_pin PA0

volatile int TimerOverflow = 0;


ISR(TIMER1_OVF_vect)
{
    TimerOverflow++;
}

int main(void)
{
    char string[10];
    long count;
    double distance;
    
    DDRA = 0x01;

    DDRB = 0x01;

    sei();              
    TimsK = (1 << TOIE1);
    TCCR1A = 0x00;              

    while(1)
    {

        PORTA |= (1 << Trigger_pin);
        _delay_us(10);
        PORTA &= (~(1 << Trigger_pin));
        
        TCNT1 = 0;          
        TCCR1B = 0x41;      
        TIFR = 1<<ICF1;     
        TIFR = 1<<TOV1;     
        
        
        while ((TIFR & (1 << ICF1)) == 0);  
        TCNT1 = 0;          
        TCCR1B = 0x01;      
        TIFR = 1<<ICF1;     
        TIFR = 1<<TOV1;     
        TimerOverflow = 0;  

        while ((TIFR & (1 << ICF1)) == 0); 
        count = ICR1 + (65535 * TimerOverflow); 
        
        distance = (double)count / 466.47;
        if (distance <= 100)
            PORTB |= (1<<0);
        else PORTB &= (~(1<<0));
        
        _delay_ms(200);
    }
}

我正在使用 Proteus 进行仿真,并且在仿真日志中获得了正确的距离,但连接到 B 端口引脚 1 的 LED 保持关闭。 可能是什么问题?引脚配置看起来不错,我遵循了与以前相同的逻辑。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)