我在使用ISR()时遇到问题-此功能无法正常工作。我在Atmega 2560(Robotdyn Mega + WiFi R3 ATmega2560 + ESP8266)上使用Atmel Studio 7,USBASP编程器,AVRDude_Prog 3.3和RobotDyn开发板。
我要从HC-SR04传感器读取信号以获取测量距离。我阅读了有关Atmega 2560及其计时器计数器设置的数据表:从0计数器开始到第4计数器。我试图设置每个中断设置,但这对我没有帮助(
我使用PORTA上的LED查找计数器,中断和设置的问题。但是现在我没有任何结果。
当ISR(TIMER0_OVF_vect)启动时,用于启用LED的简单代码也无法正常工作,但是我从youtube视频中复制了此代码进行测试(https://youtu.be/vl5H_Q1slYY?t=1531)。
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED1 0
#define LED2 1
int main(void)
{
TCCR0B = (1<<CS00) | (1 << CS02); //8Mhz/1024/255
TIMSK0 = (1<<TOIE0);
DDRA = 0xff;
PORTA = 0b00000100;
sei();
while(1)
{
}
}
ISR(TIMER0_OVF_vect)
{
PORTA ^= (1<<LED1);
}
#define F_CPU 16000000UL
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/io.h>
#include <stdio.h>
#define SREG _SFR_IO8(0x3f)
unsigned char OVF_counter, zamer_1, zamer_T, zamer_t, duty;
unsigned long t, T, T1, f, num;
unsigned int rising_1, rising_2, falling;
ISR(TIMER4_OVF_vect)
{
PORTA = 0b00001111;
}
ISR(TIMER0_OVF_vect)
{
PORTA = 0b00000011;
}
ISR(TIMER4_CAPT_vect)
{
}
void init_timer (void)
{
TIMSK4 |= (1 << ICIE4)|(1 << TOIE4);
TCCR4B |= (1 << ICNC4)|(1 << CS42);
TIFR4 |= (1 << TOV4);
TCCR0B = (1 << CS01);
TIFR0 |= (1 << TOV0);
SREG = 0b10000000;
SREG = 0x80;
}
void capture (void)
{
PORTB = 0b00001000;
_delay_ms(10);
PORTB = 0x00;
rising_1 = ICR4;
T=TCNT4;
TCCR4B &= ~(1 << ICES4);
falling = ICR4;
T1=TCNT4;
TCCR4B |= (1 << ICES4);
t = falling - rising_1 ;
}
int main(void)
{
SREG = 0x80;
DDRL = 0x00;
DDRB = 0xff;
DDRA = 0xff;
init_timer();
while (1)
{
capture();
if(t >= 1000000)
{
PORTA = 0b00000000;
}
if(t >= 100000)
{
PORTA = 0b00000111;
}
else if(t >= 1)
{
PORTA = 0b00000011;
}
else if(t >= 0)
{
PORTA = 0b00000001;
}
else
{
PORTA = 0b00001111;
num=0;
}
}
}