Guest User

Untitled

a guest
Jan 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. //Clock source & fuse bits:
  2. //Internal oscillator 8Mhz: CKSEL3..1=0010 SUT1..0=00
  3. //External oscillator from board: CKSEL3..1=0000 SUT1..0=00
  4. //Low Power Crystal Oscillator: CKSEL3..1=1111 SUT1..0=11
  5. //LCD on port C
  6. //Switches on port D
  7. //LEDs on port B
  8. //**********************************************************
  9. //Game of Reflex
  10. //**********************************************************
  11. //Jacek Tadus
  12. //Ehsan Ghasemi
  13.  
  14. #include<avr/io.h>
  15. #include<avr/interrupt.h>
  16. #include <avr/eeprom.h>
  17. #include"lcd.h"
  18. #include"stdlib.h"
  19.  
  20. //timeout values for each timer
  21. #define t1 250
  22. #define t2 1
  23.  
  24.  
  25. //the three task subroutines
  26. void task1(void); //blinks LED0 at 4 or 8 Hz
  27. void task2(void); //turn off LEDs wait random time
  28. void task3(void); //turn LEDs on
  29.  
  30. void initialize(void); //all the usual MCU stuff
  31.  
  32. volatile unsigned char time1, time2;        //timeout counters
  33. volatile unsigned char led;                 //light states
  34. volatile unsigned char state = 0;           //state
  35. volatile unsigned int countDelay = 0;       //counter for delay
  36. volatile short reactionTime;                //counter for reaction time
  37. int flag = 0;                               //state flag
  38. uint16_t MEM;                               //memory location in EEPROM value
  39. int bestTime;                               //holder for best time read from EEPROM
  40. volatile short tempTime;                    //temporary holder for best time
  41. volatile unsigned char buttonDetect = 0;    //holder for switch value
  42.  
  43. char buffer2[7];                            //buffer size
  44. char buffer3[17];                           //buffer size
  45.  
  46. //randomnumber generation
  47. volatile double randomNumber;               //random number holder
  48. volatile double low;                        //low random number
  49. volatile double high;                       //high random number
  50.  
  51. uint16_t eeprom_read_word(const uint16_t *addr);
  52. void eeprom_write_word(uint16_t *addr, uint16_t value);
  53.  
  54. //timer0 compare Interrupt Service Routine (ISR)
  55. ISR(TIMER1_COMPA_vect) {
  56.     //decrement the three times if they are not already zero
  57.     if (time1 > 0)
  58.         --time1;
  59.     if (time2 > 0)
  60.         --time2;
  61. }
  62. //external Interupt Service Routine (ISR) for port D
  63. ISR(PCINT3_vect) {
  64.  
  65.     if (buttonDetect == 0b01111111) {
  66.         state = 0;
  67.        
  68.     }
  69.     else if (buttonDetect == 0b11111110){
  70.         if (state == 0)
  71.             state = 1;
  72.         else if (state == 2)
  73.             state = 3;
  74.         else if (state > 3)
  75.             state = 0;
  76.     }
  77. }
  78.  
  79. //entry point and task scheduler loop
  80. int main(void) {
  81.     initialize();
  82.  
  83.     //main task scheduler loop
  84.     while (1) {
  85.        
  86.         switch (state) {
  87.        
  88.         //STATE ZERO
  89.         //blining light at 1Hz
  90.         case (0):
  91.             if (time1 == 0)
  92.                 task1();
  93.             buttonDetect = PIND;
  94.             break;
  95.        
  96.         //STATE ONE
  97.         //turn lights off
  98.         //wait around two seconds  
  99.         //detect cheating if true go to state zero
  100.         //move to state two
  101.         case (1):
  102.             task2();
  103.             flag = 0;
  104.             state = 2;
  105.             if (PIND == 0b11111110) {
  106.                 lcd_clrscr();
  107.                 lcd_puts("Stop cheating \n");
  108.                 state = 4;
  109.             }
  110.             break;
  111.        
  112.         //STATE TWO
  113.         //turn LEDs on 
  114.         //time reaction time
  115.         //move to state three  
  116.         case (2):
  117.  
  118.             if (time2 == 0) {
  119.                 reactionTime++;
  120.                 task3();
  121.             }
  122.             break;
  123.        
  124.         //STATE THREE
  125.         //display reaction time in ms
  126.         //compare reaction time with reaction time stored in EEPROM if less write new calue to EEPROM
  127.         //move to state zero after reset button pressed
  128.         case (3):
  129.            
  130.             lcd_gotoxy(0,1);                                //set location on LCD
  131.             itoa(reactionTime, buffer2, 10);                //convert integer to strin
  132.             if (flag == 0) {
  133.                 lcd_clrscr();
  134.                 lcd_puts("Time (ms):");                     //score in ms
  135.                 lcd_puts(buffer2);
  136.                 tempTime = reactionTime;
  137.                  
  138.                 if (tempTime < eeprom_read_word(&MEM)) {
  139.                       eeprom_write_word(&MEM, tempTime);
  140.                     }
  141.                
  142.                 bestTime = eeprom_read_word(&MEM);
  143.                 itoa(bestTime, buffer3, 10);
  144.                 lcd_puts("\nHigh (ms):");                   //score in ms
  145.                 lcd_puts(buffer3);
  146.                 flag = 1;      
  147.                 reactionTime = 0;
  148.             }
  149.  
  150.             buttonDetect = PIND;
  151.             break;
  152.  
  153.         default:
  154.             break;
  155.         }
  156.  
  157.     }
  158.  
  159. }
  160.  
  161. //task 1
  162. void task1(void) {
  163.     time1 = t1;                 //reset the task timer
  164.     led = led ^ 0xff;           //toggle the zero's to toggle the LEDs
  165.     PORTB = led;
  166. }
  167.  
  168. //task 2
  169. void task2(void) {
  170.     time1 = t1;                 //reset the task time
  171.     countDelay = 0;             //reset the counter
  172.     PORTB = 0xff;               //turn LEDs off
  173.  
  174.     while (countDelay <= 6)     //time delay
  175.     {
  176.         if (time1 == 0) {
  177.             countDelay++;
  178.             time1 = t1;
  179.         }
  180.     }
  181.  
  182. }
  183. //task 3
  184. void task3(void) {
  185.     time2 = t2;                 //reset time
  186.     PORTB &= 0x00;              //turn all LEDs on
  187.  
  188. }
  189.  
  190. //set it all up
  191. void initialize(void) {
  192.  
  193.     //set up the ports
  194.     DDRD = 0x00;        //PORTC is an input
  195.     DDRB = 0xff;        //PORTB is an ouput
  196.  
  197.     //set timer0
  198.     TCCR1A = 0b00000000; //CTC mode no PWM, the most significant 6 bits do not matter for this lab
  199.     TCCR1B = 0b00001011; //prescaler 64
  200.     TIMSK1 = 0b00000010; //enable Output Compare A Match interrupt
  201.     OCR1A = 250;         //set the compare register to 250 clock ticks -> Output Compare A Match interrupt occurs with 1KHz frequency
  202.  
  203.     PCICR = 0b00001000;  //enable Pin Change Interrupt PCIE3
  204.     PCMSK3 = 0b10000001; //enable I/O pin 0 this looks at switch 0.
  205.    
  206.     //initialize display, cursor off
  207.     lcd_init(LCD_DISP_ON);
  208.  
  209.     //init the LED status (all off)
  210.     led = 0xff;
  211.  
  212.     //init the task timers
  213.     time1 = t1;
  214.     time2 = t2;
  215.  
  216.     //initialize randomNumber
  217.     randomNumber = 0;
  218.     low = 8;
  219.     high = 15;
  220.    
  221.     //clear display and home cursor
  222.     lcd_clrscr();
  223.  
  224.     //crank up the ISRs
  225.     sei();
  226. }
Add Comment
Please, Sign In to add comment