Advertisement
the_Charger

Micro Lab 10

Nov 20th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. #include <hidef.h> /* common defines and macros */
  2. #include <mc9s12dg256.h> /* derivative information */
  3. #pragma LINK_INFO DERIVATIVE "mc9s12dg256b"
  4. #include "main_asm.h" /* interface to the assembly module */
  5. #define oneSecond 97
  6. #define twoSeconds 194
  7. #define halfSecond 48
  8. #define taskOne 1
  9. #define taskTwo 2
  10. #define taskThree 3
  11. #define taskFour 4
  12. #define loopTask 5
  13. #define reset 0
  14. #define forever 1
  15. #define LEDOn 1
  16. #define toggle 0x01
  17. #define shortDelay 25
  18. unsigned short ticks = reset ;
  19. int task = taskOne;
  20.  
  21. /*
  22. Ticks is how we are setting the ON/OFF duration
  23. Each tick, we increment and wait until a value
  24. Corresponding with the length of time we want is reached
  25. Then we XOR the port and reset the ticks
  26. */
  27.  
  28. void interrupt 7 handler() {
  29.       ticks++;
  30.       if(task == taskOne){
  31.                 if(ticks >= halfSecond){
  32.                           PORTB = PORTB^toggle;
  33.                           ticks = reset;
  34.                  }
  35.                    clear_RTI_flag();
  36.       }
  37.  
  38.       if(task == taskTwo){
  39.                 if(ticks == oneSecond){
  40.                           PORTB = PORTB^toggle;
  41.                           ticks = reset;
  42.                  }
  43.                    clear_RTI_flag();
  44.       }
  45.  
  46.  
  47.       if(task == taskThree){
  48.                 if(ticks == twoSeconds){
  49.                             PORTB = PORTB^toggle;
  50.                             ticks = reset;
  51.                 }
  52.                 clear_RTI_flag();
  53.       }
  54. }
  55.  
  56. /*
  57. When a button is pressed, we move to the next task
  58. The tasks are cycled in order (one,two,three,four)
  59. And when the last task is completed, the next task
  60. Will be taskOne again (one, two, three, four, one,..)
  61. Special Case: If taskFour, disable IRQ and turn off LED
  62. */
  63.  
  64. void interrupt 6 handler1() {
  65.       task++;
  66.       if(task == loopTask){
  67.         task = taskOne;
  68.         RTI_enable();
  69.         PORTB = LEDOn;
  70.       }
  71.  
  72.       if(task == taskFour){
  73.         RTI_disable();
  74.         PORTB = reset;
  75.       }
  76.  
  77.       /*
  78.       Debounce
  79.       */
  80.       ms_delay(shortDelay);
  81.       while(SW5_down()) {
  82.       };
  83.       ms_delay(shortDelay);
  84.                    
  85.  }
  86.  
  87. /*
  88. In our main, we are simply allowing our interupts and configuring the LED
  89. of interest for output
  90. */
  91.  
  92. void main(void)
  93. {
  94.       PLL_init(); // set system clock frequency to 24 MHz
  95.       seg7_disable();
  96.       led_enable();
  97.       _asm(cli); //enable interrupts
  98.  
  99.       RTI_init();
  100.       IRQ_enable();
  101.  
  102.       while(forever) {
  103.       };
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement