Guest User

Untitled

a guest
Aug 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. //FINAL EXAM A1
  2. #include <mc9s12c32.h>
  3.  
  4. unsigned int dummyread, count = 0;
  5. unsigned int t1, t2;
  6.  
  7. #pragma interrupt_handler tc0_isr
  8. void tc0_isr()
  9. {
  10. dummyread = TC0;
  11. t1 = TCNT;
  12. count++;
  13. }
  14.  
  15. #pragma interrupt_handler tc1_isr
  16. void tc1_isr()
  17. {
  18. dummyread = TC1;
  19. t2 = TCNT;
  20. count++;
  21. }
  22.  
  23. void main()
  24. {
  25. unsigned float t;
  26.  
  27. TSCR1=0x90; // enable timer and allow fast flag clear
  28. TIOS = 0xfc; // set tco and tc1 to input compare
  29. DDRT = ~0x03; // set pt0 and pt1 as input capture
  30. TCTL4 = 0x05; // pt0 and pt1 rising edge capture 0000.0101
  31.  
  32. dummyread = TC0;
  33. dummyread = TC1;
  34.  
  35. INTER_ON(); // globally interupt enable
  36.  
  37. *(void (**)()) 0x0fee = tc0_isr;
  38. *(void (**)()) 0x0fec = tc1_isr;
  39. INTR_ON(); // globally enable interupts
  40. TIE = 0x03; // enable timer interupt tco and tc1
  41.  
  42. while(1)
  43. {
  44. while(count < 2);
  45. count = 0;
  46. if(t2 > t1) t = t2 - t1;
  47. else t = 0xffff - t1 + t2 + 1;
  48.  
  49. t = 1000000 / t;
  50.  
  51. // math to convert 1 foot per X cycles to X feet per 1 second
  52.  
  53. // 1 foot 10,000,000 cycles 1,000 feet
  54. // -------------- * ------------------ = ------------
  55. // 10,000 cycles 1 second 1 second
  56.  
  57. printf("speed = %d\n", t);
  58. }
  59. }
Add Comment
Please, Sign In to add comment