使用C设计atmega328p微控制器的反应计时器

时间:2018-07-29 13:26:26

标签: c embedded atmega

我必须做的基本描述:

“反应计时器”是一个嵌入式系统,用于测量用户对信号的反应时间,并以三个LED的比例显示。当用户按下系统上的“就绪”按钮时,“反应计时器”将关闭系统中的所有LED,并等待1到10秒之间的随机时间。在此等待时间到期后,“反应计时器”将打开蓝色LED并向用户发出信号。然后,用户尽快按下系统上的“ react”按钮以对信号做出反应。然后,“反应计时器”关闭用于发出信号的蓝色LED,并测量信号与用户对信号的反应之间经过的时间。如果用户对信号的反应时间小于一秒,大于一秒但小于两秒或大于两秒,则“反应计时器”分别打开绿色,橙色或红色LED以显示用户的表现。

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h> //library includes srand(), rand() methods
#include <time.h>           //library include time()
#define F_CPU 4000000UL;    //Define F_CPU as 4MHz

int volatile randomnum;     //Declare volatile variable for random number generation
int volatile speed;         //Declare volatile variable to get the reaction speed

int main(void)
{
DDRB = DDRB | 0b00100111; //Configure PB5,PB0,PB1,PB2 as outputs                        
DDRD = DDRD & 0b11110011; //Configure PD2(INT0), PD3(INT3) as inputs
PORTD = PORTD | 0b00001100;     //Pull-up PD2, PD3
EICRA = (EICRA & 0b11111010) | 0b00001010;      //Set external interrupt on falling edge of INT0,INT1
EIMSK = EIMSK | 0b00000011;                     //Enable external interrupt on INT0,INT1
sei();                                          //Enable interrupts globally
TCCR1B = (TCCR1B & 0b11111101) | 0b00000101;    //Set clock source as F_CPU/1024 
TCNT1 = 0;                                      //Default TCNT value as 0
while (1) 
{

}
return 0;
}

ISR(INT0_vect){
time_t t;                                           //Declare time variable
PORTB = 0;                                          //Switch off all LEDs
srand( (unsigned) time(&t) );                       //Seed the random number using time
randomnum = rand() % 10 + 1;                        //Generate a random number between 1-10
TCNT1 = ( 65535 - (randomnum * 3906.25) ) + 1;      //Assign the value for TCNT to get the delay of random number seconds
TIMSK1 = TIMSK1 | 0b00000001;                       //Enable timer overflow interrupt
}

ISR(TIMER1_OVF_vect){                   //ISR for timer overflow interrupt
PORTB=PORTB|0b00100000;                 //Switch ON LED connected to PB5
}

ISR(INT1_vect){
speed = TCNT1 / 3906.25;                            //Calculate the time taken to react
if(speed > 2)                                       //If reaction time greater than 2s
{                                       
    PORTB = PORTB | 0b00000100;                     //Switch ON LED connected to PB2
}
else if ( (speed<2) && (speed>1) )                  //If reaction time greater than 1s but less than 2s
{
    PORTB = PORTB | 0b00000010;                     //Switch ON LED connected to PB1
}
else if(speed < 1)                                  //If reaction time less than 1s
{
    PORTB = PORTB | 0b00000001;                     //Switch ON LED connected to PB0
}
}

我试图在C程序上找到对上述嵌入式系统代码的引用。但是失败了,因为所有这些都在arduino。

上面的程序似乎仅在随机数生成为1时才起作用。我找不到错误在哪里。您能否参考我的代码并指出错误所在?

1 个答案:

答案 0 :(得分:0)

我认为您应该在启用相关中断之前清除溢出标志。 否则,如果该标志已被设置(即自启动程序以来已过去了约17秒),则中断将立即触发。 在启用溢出中断之前,请尝试以下操作:

TIFR1 = 0b00000001;         // Clear timer overflow flag