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
- #include<avr/io.h>
- #include<avr/interrupt.h>
- #include <avr/eeprom.h>
- #include"lcd.h"
- #include"stdlib.h"
- //timeout values for each task
- #define t1 250
- #define t2 125
- #define t3 1
- //the three task subroutines
- void task1(void); //blinks LED0 at 4 or 8 Hz
- void task2(void); //blinks LED1 at 8 Hz
- void task3(void); //detect button and modify task1 rate
- void initialize(void); //all the usual MCU stuff
- volatile unsigned char time1, time2, time3; //timeout counters
- volatile unsigned char led; //light states
- volatile unsigned char state = 0;
- volatile unsigned char time4;
- volatile unsigned int x;
- volatile short reactionTime = 0;
- long i;
- int flag = 0;
- uint16_t EEMEM EEPROM;
- volatile short highScore;
- volatile unsigned char task;
- volatile unsigned char task5;
- volatile char seconds;
- volatile char milisec;
- char buffer[7];
- char buffer2[7];
- //randomnumber generation
- volatile double randomNumber;
- volatile double low;
- volatile double high;
- unsigned char tsk2m;
- 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;
- if (time3 > 0)
- --time3;
- if (time4 > 0)
- --time4;
- }
- ISR(PCINT3_vect) {
- if (task == 0b01111111) {
- state = 0;
- }
- else if (task == 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) {
- case (0):
- if (time1 == 0)
- task1();
- task = PIND;
- break;
- case (1):
- //randomNumber = rand()/RAND_MAX;
- //randomNumber = low + randomNumber * (high - low);
- task2();
- flag = 0;
- state = 2;
- if (PIND == 0b11111110) {
- lcd_clrscr();
- lcd_puts("Stop cheating \n");
- lcd_puts("you bastard");
- state = 4;
- }
- break;
- case (2):
- if (time3 == 0) {
- reactionTime++;
- task3();
- }
- seconds = reactionTime/1000;
- milisec = reactionTime%1000;
- break;
- case (3):
- // highScore = eeprom_read_word(&EEPROM);
- // if (reactionTime < highScore) {
- // eeprom_write_word(&EEPROM, reactionTime);
- // }
- // highScore = eeprom_read_word(&EEPROM);
- //Convert integer to string type
- itoa(highScore, buffer, 10);
- itoa(seconds, buffer2, 10);
- if (flag == 0) {
- lcd_clrscr();
- lcd_puts("Your score:");
- lcd_puts(buffer2);
- lcd_puts("\nHigh score:");
- lcd_puts(buffer);
- flag = 1;
- reactionTime = 0;
- }
- task = PIND;
- break;
- default:
- break;
- }
- }
- }
- //task 1
- void task1(void) {
- time1 = t1; //reset the task timer
- led = led ^ 0xff; //toggle the zero's
- PORTB = led;
- }
- //task 2
- void task2(void) {
- time1 = t1; //reset the task time
- x = 0; //reset the counter
- PORTB = 0xff; //turn LEDs off
- while (x <= 6) //time delay
- {
- if (time1 == 0) {
- x++;
- time1 = t1;
- }
- }
- }
- //task 3
- void task3(void) {
- time3 = t3; //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.
- EICRA = 0b00000010;
- /* initialize display, cursor off */
- lcd_init(LCD_DISP_ON);
- //eeprom_write_word(&EEPROM, 10);
- //init the LED status (all off)
- led = 0xff;
- //init the task timers
- time1 = t1;
- time2 = t2;
- time3 = t3;
- time4 = t1;
- //initialize randomNumber
- randomNumber = 0;
- low = 8;
- high = 15;
- // LCD
- /* clear display and home cursor */
- lcd_clrscr();
- task = 0;
- task5 = 0;
- //crank up the ISRs
- sei();
- }
Add Comment
Please, Sign In to add comment