【发布时间】:2023-06-19 05:52:02
【问题描述】:
我正在学习微控制器编程。我需要帮助才能使用 Atmega8L-8PU 在 WinAVR 上完成我的程序。 我添加了 3 个按钮,当按下按钮时:第一个按钮将提供 15 分钟的输出,第二个按钮将提供 30 分钟,最后一个按钮将提供 45 分钟。每次经过后,应自动重置以供下次按下。
这是我编写的代码,但我无法添加持续时间。如果有人能做到,那对我很有帮助。提前谢谢:)。
#define numberofButtons 3
#include <avr/io.h>
#include"buttonpress.h"
int main(void)
{
DDRB = 0b00000000;
DDRD = 0b00000111;
PORTB = (1<<PINB0)|(1<<PINB1)|(1<<PINB2);
while(1)
{
if (buttonpressed(0, PINB, 0, 100))
{
PORTD ^= (1<<PIND0);
}
if (buttonpressed(1, PINB, 1, 100))
{
PORTD ^= (1<<PIND1);
}
if (buttonpressed(2, PINB, 2, 100))
{
PORTD ^= (1<<PIND2);
}
}
}
我已经尝试过这种方式,但它也不起作用........ :(
#define numberofButtons 3
#include"buttonpress.h"
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned char seconds =0;
int minutes;
int main ()
{
DDRB = 0b00000000;
DDRD = 0b00000111;
PORTB = (1<<PINB0)|(1<<PINB1)|(1<<PINB2);
volatile int seconds;
DDRD |= (1<<PIND0)|(1<<PIND1)|(1<<PIND2);
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
sei(); // Enable global interrupts
OCR1A = 15624; // Set CTC compare value to 1Hz at 8MHz AVR clock, with a prescaler of 64
TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64
while (1)
{
if(seconds==60)
{
minutes++;
seconds=0;
}
{
if (buttonpressed(0, PINB, 0, 100))
{
PORTD ^= (1<<PIND0);
int total_seconds=900;
while(total_seconds-seconds!=0)
{
//Delay of 15 min
}
}
} return 0;
}
}
ISR (TIMER1_COMPA_vect)
{
seconds++;
}
【问题讨论】:
-
是否有可用的函数以秒(或其他类似单位)获取当前时间?
-
我不明白,先生,因为我是编程新手,但我认为 15 分钟可以在 900 秒内计算,如果您有延迟代码,请提供给我。
-
这是默认时钟。这里没有外部时钟。