簡體   English   中英

使用C設計atmega328p微控制器的反應計時器

[英]Designing a reaction timer for atmega328p microcontroller using C

我必須做的基本描述:

“反應計時器”是一個嵌入式系統,用於測量用戶對信號的反應時間,並以三個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時才起作用。我找不到錯誤在哪里。 您能否參考我的代碼並指出錯誤所在?

我認為您應該在啟用相關中斷之前清除溢出標志。 否則,如果該標志已被設置(即自啟動程序以來已過去約17秒),則中斷將立即觸發。 在啟用溢出中斷之前,請嘗試以下操作:

TIFR1 = 0b00000001;         // Clear timer overflow flag

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM