Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.01 KB | None | 0 0
  1.  
  2.  // Importing libraries to enable the use of interrupts and I/O control.
  3.  #include <avr/io.h>
  4.  #include <avr/interrupt.h>
  5.  
  6.  // Defining our timer variables.
  7.  #define F_CPU 4000000 //Defining the CPU speed.
  8.  #define tikk F_CPU/64 // Define the amounts of clock cycles needed
  9.  //for a second to pass into a smaller amount so that it can fit into 16bit registers.
  10.  
  11.  // Global type structures.
  12.  /******************************************************************
  13.  In this part we define our type structures that are to be used.
  14.  We have made one to store different quantities of time, and one for when a racer finishes.
  15.  *******************************************************************/
  16.  typedef struct
  17.  {
  18.  // defining the variables as volatile so that the
  19.  // the original variable will always be used and not local copies.
  20.  volatile uint8_t min;
  21.  volatile uint8_t sec;
  22.  volatile uint8_t hour;
  23.  volatile uint8_t hundredths;
  24.  }       time_type;
  25.  
  26.  typedef struct  
  27.  {
  28. time_type finish_time;
  29.     uint8_t runner_id;
  30.  } runner_type;
  31.  
  32.  typedef struct runner_type_d node;
  33.  
  34.  struct runner_type_d
  35.  {
  36.   // In this struct we use the previously made struct to account for time.
  37.   // and in addition add when a racer finishes.
  38.  time_type finish_time;
  39.  uint8_t runner_id;
  40.  node* prior;
  41.  node* next;
  42.  }
  43.  
  44. typedef struct
  45. {
  46. volatile time_type runner_time;
  47. volatile uint8_t runner_id;
  48. volatile uint8_t new_runner;   
  49. }   next_runner;
  50.  
  51.  // Global variables
  52.  time_type time = {0,0,0,0};
  53.      
  54.  next_runner nextRunner;
  55.  
  56.  runner_type runner[255];
  57.  runner_type * runner_p[255];
  58.  node * head_dyn_list;
  59.  node * point_dyn_list;
  60.  
  61.  // Prototypes of functions that are used in this class.
  62.  static void initTimer(void);   // Setting up the timer.
  63.  static void incTid(&time);     // Increments the time when called upon.
  64.  static void stopTime(void);   // Stops the timer/clock.
  65.  static void startTime(void);// Initializes/starts the timer/clock.
  66.  static void initPort(void);    // Setting up ports to be used.
  67.  static void racerFinish(next_runner * newRunner_p); // A function that will record a racers result.
  68.  static void newEvent(next_runner *newRunner_p);
  69.  static void sorterEtterNummer(void);
  70.  static void sorterStaticList(void);
  71.  static void sorterStaticList_p(void);
  72.  static void sorterDynamicList(void);
  73.  /******************************************************************
  74.  Our interrupt service routine, what we want to happen
  75.  when the different types of interrupt occur.
  76.  ******************************************************************/
  77.  ISR(TIMER1_COMPA_vect) // Will initiate when compare register A is equal to the timer/when a second has passed.
  78.  {
  79.  incTid(&time);// increments the time by one second.
  80.  }
  81.  ISR(TIMER1_CAPT_vect)// Will initiate when an input capture occurs/ when a racer crosses the goal line.
  82.  {
  83.      racerFinish(&nextRunner);
  84.  }
  85.  /******************************************************************
  86.  We use this function to set up our timers, interrupts and compare & capture registers.
  87.  ******************************************************************/
  88.  static void initTimer(void)
  89.  {
  90.  TCCR1B = (1 << WGM12 );              // Setting up for CTC mode.
  91.  TCNT1  = 0;                          // Resetting our timer to zero.
  92.  OCR1A  = tikk;                       // Loading the value of one second(clock cycles) in compare register A.
  93.  TIMSK = (1 << OCIE1A | 1<<TICIE1);   // Enabling interrupt on compare match and input capture.
  94.  TCCR1B |= ((1<<ICNC1) | (1<<ICES1)); // Activate the Input Capture Noise Canceler, and rising edge triggering.  
  95.  sei();                               //Enabling global interrupts.
  96.  }
  97.  /******************************************************************
  98.  Here we have two functions, one that stops and one that starts the timer/clock.
  99.  ******************************************************************/
  100.  static void stopTime(void);
  101.  {
  102.  TCCR1B &= ~((1 << CS10 ) | (1 << CS11 )); //Stopping the timer.
  103.  }
  104.  static void startTime(void)
  105.  {
  106.  TCCR1B |= ((1 << CS10 ) | (1 << CS11 )); // Starting the timer.
  107.  /******************************************************************
  108.  Here we are setting up our ports for input or output.
  109.  ******************************************************************/
  110.  }
  111.  static void initPort(void)
  112.  {
  113.  //DDRx specifies direction, PINx reads input and PORTx loads output.
  114.  DDRE = 0; // Input
  115.  DDRD = 0; // Input
  116.  DDRA = 0xff; // Output
  117.  PORTD = 0; // All bits are cleared.
  118.  }
  119.  
  120.  static void incTid(time_type *tide_elapsed)
  121. {
  122.     if (time_elapsed -> sec <59)
  123.     (tide_elapsed -> sec)++;        
  124.     else
  125.     {
  126.         (tide_elapsed -> sec) = 0;      
  127.         if (tide_elapsed -> min < 59)
  128.         (tide_elapsed -> min)++;
  129.         else
  130.         {
  131.             (tide_elapsed -> min) = 0;
  132.         }
  133.     }
  134.  }
  135.  
  136.  static void racerFinish(next_runner * new_runner_p)
  137.  {
  138.      new_runner_p ->runner_time.min = time.min;
  139.      new_runner_p ->runner_time.sec = time.sec;
  140.      new_runner_p ->runner_id = PINE;
  141.      if(new_runner_p->runner_id !=0)
  142.      new_runner_p-> new_runner = 1;
  143.  
  144.  }
  145.  static void newEvent(next_runner *new_runner)
  146.  {
  147.      static uint8_t runner_number = 0;
  148.      runner_type * temp
  149.      node * tempD;
  150.      
  151.      {
  152.         runner[runner_number].finish_time.min = nextRunner->runner_type.min;
  153.         runner[runner_number].finish_time.sec = nextRunner->runner_type.sec;
  154.         runner[runner_number].finish_time.hundredths = nextRunner->runner_type.hundredths;
  155.         runner[runner_number].runner_id= nextRunner->runner_id;
  156.      {
  157.      
  158.      temp=malloc(sizeof(runner_type));
  159.      if(temp == NULL)
  160.      {
  161.          //noe er galt
  162.          assert(0);  // stops the program
  163.      }
  164.      temp->finish_time.min = new_runner->runner_type.min;
  165.      temp->finish_time.sec = new_runner->runner_type.sec;
  166.      temp->finish_time.hundredths= new_runner->runner_type.hundredths;
  167.      temp->runner_id = new_runner->runner_id;
  168.      runner_p[number]=temp;
  169.      //nummer++;
  170.  }
  171.  
  172.  int main(void)
  173.  {
  174.  initPort();
  175.  initTimer();
  176. while(PIND & (1 << 1));//Testing on bit 0 on PIND until set.
  177. {
  178.     startTime();
  179. }
  180.  
  181. while(PIND & (1 << 0)) ://Testing on bit 0 on PIND until cleared.
  182. {
  183.     stopTime();
  184. }
  185.    
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement