Guest User

Untitled

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