Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Clock source & fuse bits:
- //Internal oscillator 8Mhz: CKSEL3..1=0010 SUT1..0=00
- //External oscillator from board: CKSEL3..1=0000 SUT1..0=00
- //Low Power Crystal Oscillator: CKSEL3..1=1111 SUT1..0=11
- //LCD on port C
- //Switches on port D
- //LEDs on port B
- //**********************************************************
- //Game of Reflex
- //**********************************************************
- //Jacek Tadus
- //Ehsan Ghasemi
- #include<avr/io.h>
- #include<avr/interrupt.h>
- #include <avr/eeprom.h>
- #include"lcd.h"
- #include"stdlib.h"
- //timeout values for each timer
- #define t1 250
- #define t2 1
- //the three task subroutines
- void task1(void); //blinks LED0 at 4 or 8 Hz
- void task2(void); //turn off LEDs wait random time
- void task3(void); //turn LEDs on
- void initialize(void); //all the usual MCU stuff
- volatile unsigned char time1, time2; //timeout counters
- volatile unsigned char led; //light states
- volatile unsigned char state = 0; //state
- volatile unsigned int countDelay = 0; //counter for delay
- volatile short reactionTime; //counter for reaction time
- int flag = 0; //state flag
- uint16_t MEM; //memory location in EEPROM value
- int bestTime; //holder for best time read from EEPROM
- volatile short tempTime; //temporary holder for best time
- volatile unsigned char buttonDetect = 0; //holder for switch value
- char buffer2[7]; //buffer size
- char buffer3[17]; //buffer size
- //randomnumber generation
- volatile double randomNumber; //random number holder
- volatile double low; //low random number
- volatile double high; //high random number
- uint16_t eeprom_read_word(const uint16_t *addr);
- void eeprom_write_word(uint16_t *addr, uint16_t value);
- //timer0 compare Interrupt Service Routine (ISR)
- ISR(TIMER1_COMPA_vect) {
- //decrement the three times if they are not already zero
- if (time1 > 0)
- --time1;
- if (time2 > 0)
- --time2;
- }
- //external Interupt Service Routine (ISR) for port D
- ISR(PCINT3_vect) {
- if (buttonDetect == 0b01111111) {
- state = 0;
- }
- else if (buttonDetect == 0b11111110){
- if (state == 0)
- state = 1;
- else if (state == 2)
- state = 3;
- else if (state > 3)
- state = 0;
- }
- }
- //entry point and task scheduler loop
- int main(void) {
- initialize();
- //main task scheduler loop
- while (1) {
- switch (state) {
- //STATE ZERO
- //blining light at 1Hz
- case (0):
- if (time1 == 0)
- task1();
- buttonDetect = PIND;
- break;
- //STATE ONE
- //turn lights off
- //wait around two seconds
- //detect cheating if true go to state zero
- //move to state two
- case (1):
- task2();
- flag = 0;
- state = 2;
- if (PIND == 0b11111110) {
- lcd_clrscr();
- lcd_puts("Stop cheating \n");
- state = 4;
- }
- break;
- //STATE TWO
- //turn LEDs on
- //time reaction time
- //move to state three
- case (2):
- if (time2 == 0) {
- reactionTime++;
- task3();
- }
- break;
- //STATE THREE
- //display reaction time in ms
- //compare reaction time with reaction time stored in EEPROM if less write new calue to EEPROM
- //move to state zero after reset button pressed
- case (3):
- lcd_gotoxy(0,1); //set location on LCD
- itoa(reactionTime, buffer2, 10); //convert integer to strin
- if (flag == 0) {
- lcd_clrscr();
- lcd_puts("Time (ms):"); //score in ms
- lcd_puts(buffer2);
- tempTime = reactionTime;
- if (tempTime < eeprom_read_word(&MEM)) {
- eeprom_write_word(&MEM, tempTime);
- }
- bestTime = eeprom_read_word(&MEM);
- itoa(bestTime, buffer3, 10);
- lcd_puts("\nHigh (ms):"); //score in ms
- lcd_puts(buffer3);
- flag = 1;
- reactionTime = 0;
- }
- buttonDetect = PIND;
- break;
- default:
- break;
- }
- }
- }
- //task 1
- void task1(void) {
- time1 = t1; //reset the task timer
- led = led ^ 0xff; //toggle the zero's to toggle the LEDs
- PORTB = led;
- }
- //task 2
- void task2(void) {
- time1 = t1; //reset the task time
- countDelay = 0; //reset the counter
- PORTB = 0xff; //turn LEDs off
- while (countDelay <= 6) //time delay
- {
- if (time1 == 0) {
- countDelay++;
- time1 = t1;
- }
- }
- }
- //task 3
- void task3(void) {
- time2 = t2; //reset time
- PORTB &= 0x00; //turn all LEDs on
- }
- //set it all up
- void initialize(void) {
- //set up the ports
- DDRD = 0x00; //PORTC is an input
- DDRB = 0xff; //PORTB is an ouput
- //set timer0
- TCCR1A = 0b00000000; //CTC mode no PWM, the most significant 6 bits do not matter for this lab
- TCCR1B = 0b00001011; //prescaler 64
- TIMSK1 = 0b00000010; //enable Output Compare A Match interrupt
- OCR1A = 250; //set the compare register to 250 clock ticks -> Output Compare A Match interrupt occurs with 1KHz frequency
- PCICR = 0b00001000; //enable Pin Change Interrupt PCIE3
- PCMSK3 = 0b10000001; //enable I/O pin 0 this looks at switch 0.
- //initialize display, cursor off
- lcd_init(LCD_DISP_ON);
- //init the LED status (all off)
- led = 0xff;
- //init the task timers
- time1 = t1;
- time2 = t2;
- //initialize randomNumber
- randomNumber = 0;
- low = 8;
- high = 15;
- //clear display and home cursor
- lcd_clrscr();
- //crank up the ISRs
- sei();
- }
Add Comment
Please, Sign In to add comment