Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Importing libraries to enable the use of interrupts and I/O control.
- #include <avr/io.h>
- #include <avr/interrupt.h>
- // Defining our timer variables.
- #define F_CPU 4000000 //Defining the CPU speed.
- #define tikk F_CPU/64 // Define the amounts of clock cycles needed
- //for a second to pass into a smaller amount so that it can fit into 16bit registers.
- // Global type structures.
- /******************************************************************
- In this part we define our type structures that are to be used.
- We have made one to store different quantities of time, and one for when a racer finishes.
- *******************************************************************/
- typedef struct
- {
- // defining the variables as volatile so that the
- // the original variable will always be used and not local copies.
- volatile uint8_t min;
- volatile uint8_t sec;
- volatile uint8_t hour;
- volatile uint8_t hundredths;
- } time_type;
- typedef struct
- {
- time_type finish_time;
- uint8_t runner_id;
- } runner_type;
- typedef struct runner_type_d node;
- struct runner_type_d
- {
- // In this struct we use the previously made struct to account for time.
- // and in addition add when a racer finishes.
- time_type finish_time;
- uint8_t runner_id;
- node* prior;
- node* next;
- }
- typedef struct
- {
- volatile time_type runner_time;
- volatile uint8_t runner_id;
- volatile uint8_t new_runner;
- } next_runner;
- // Global variables
- time_type time = {0,0,0,0};
- next_runner nextRunner;
- runner_type runner[255];
- runner_type * runner_p[255];
- node * head_dyn_list;
- node * point_dyn_list;
- // Prototypes of functions that are used in this class.
- static void initTimer(void); // Setting up the timer.
- static void incTid(&time); // Increments the time when called upon.
- static void stopTime(void); // Stops the timer/clock.
- static void startTime(void);// Initializes/starts the timer/clock.
- static void initPort(void); // Setting up ports to be used.
- static void racerFinish(next_runner * newRunner_p); // A function that will record a racers result.
- static void newEvent(next_runner *newRunner_p);
- static void sorterEtterNummer(void);
- static void sorterStaticList(void);
- static void sorterStaticList_p(void);
- static void sorterDynamicList(void);
- /******************************************************************
- Our interrupt service routine, what we want to happen
- when the different types of interrupt occur.
- ******************************************************************/
- ISR(TIMER1_COMPA_vect) // Will initiate when compare register A is equal to the timer/when a second has passed.
- {
- incTid(&time);// increments the time by one second.
- }
- ISR(TIMER1_CAPT_vect)// Will initiate when an input capture occurs/ when a racer crosses the goal line.
- {
- racerFinish(&nextRunner);
- }
- /******************************************************************
- We use this function to set up our timers, interrupts and compare & capture registers.
- ******************************************************************/
- static void initTimer(void)
- {
- TCCR1B = (1 << WGM12 ); // Setting up for CTC mode.
- TCNT1 = 0; // Resetting our timer to zero.
- OCR1A = tikk; // Loading the value of one second(clock cycles) in compare register A.
- TIMSK = (1 << OCIE1A | 1<<TICIE1); // Enabling interrupt on compare match and input capture.
- TCCR1B |= ((1<<ICNC1) | (1<<ICES1)); // Activate the Input Capture Noise Canceler, and rising edge triggering.
- sei(); //Enabling global interrupts.
- }
- /******************************************************************
- Here we have two functions, one that stops and one that starts the timer/clock.
- ******************************************************************/
- static void stopTime(void);
- {
- TCCR1B &= ~((1 << CS10 ) | (1 << CS11 )); //Stopping the timer.
- }
- static void startTime(void)
- {
- TCCR1B |= ((1 << CS10 ) | (1 << CS11 )); // Starting the timer.
- /******************************************************************
- Here we are setting up our ports for input or output.
- ******************************************************************/
- }
- static void initPort(void)
- {
- //DDRx specifies direction, PINx reads input and PORTx loads output.
- DDRE = 0; // Input
- DDRD = 0; // Input
- DDRA = 0xff; // Output
- PORTD = 0; // All bits are cleared.
- }
- static void incTid(time_type *tide_elapsed)
- {
- if (time_elapsed -> sec <59)
- (tide_elapsed -> sec)++;
- else
- {
- (tide_elapsed -> sec) = 0;
- if (tide_elapsed -> min < 59)
- (tide_elapsed -> min)++;
- else
- {
- (tide_elapsed -> min) = 0;
- }
- }
- }
- static void racerFinish(next_runner * new_runner_p)
- {
- new_runner_p ->runner_time.min = time.min;
- new_runner_p ->runner_time.sec = time.sec;
- new_runner_p ->runner_id = PINE;
- if(new_runner_p->runner_id !=0)
- new_runner_p-> new_runner = 1;
- }
- static void newEvent(next_runner *new_runner)
- {
- static uint8_t runner_number = 0;
- runner_type * temp
- node * tempD;
- {
- runner[runner_number].finish_time.min = nextRunner->runner_type.min;
- runner[runner_number].finish_time.sec = nextRunner->runner_type.sec;
- runner[runner_number].finish_time.hundredths = nextRunner->runner_type.hundredths;
- runner[runner_number].runner_id= nextRunner->runner_id;
- {
- temp=malloc(sizeof(runner_type));
- if(temp == NULL)
- {
- //noe er galt
- assert(0); // stops the program
- }
- temp->finish_time.min = new_runner->runner_type.min;
- temp->finish_time.sec = new_runner->runner_type.sec;
- temp->finish_time.hundredths= new_runner->runner_type.hundredths;
- temp->runner_id = new_runner->runner_id;
- runner_p[number]=temp;
- //nummer++;
- }
- int main(void)
- {
- initPort();
- initTimer();
- while(PIND & (1 << 1));//Testing on bit 0 on PIND until set.
- {
- startTime();
- }
- while(PIND & (1 << 0)) ://Testing on bit 0 on PIND until cleared.
- {
- stopTime();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement