Guest User

Untitled

a guest
Jan 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. /***********************************************************************
  2. ; ECE 362 - Experiment 6 - Fall 2012
  3. ;***********************************************************************
  4. ;
  5. ; Completed by: Akash Kashyap
  6. ; 2916-K
  7. ; Lab Division 006
  8. ;
  9. ;
  10. ; Academic Honesty Statement: In entering my name above, I hereby certify
  11. ; that I am the individual who created this HC(S)12 source file and that I
  12. ; have not copied the work of any other student (past or present) while
  13. ; completing it. I understand that if I fail to honor this agreement, I will
  14. ; receive a grade of ZERO and be subject to possible disciplinary action.
  15. ;
  16. ;
  17. ;***********************************************************************/
  18. #include <hidef.h> /* common defines and macros */
  19. #include "derivative.h" /* derivative-specific definitions */
  20. #include <mc9s12c32.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24.  
  25.  
  26. // All funtions after main should be initialiezed here
  27. char inchar(void);
  28. void outchar(char x);
  29. void tdisp();
  30.  
  31.  
  32. // ASCII character definitions
  33. int CR = 0x0D;//Return
  34.  
  35.  
  36. // Variable declarations
  37. int tenthsec = 0; // One-tenth second flag
  38. int leftpb = 0; // left pushbutton flag
  39. int rghtpb = 0; // right pushbutton flag
  40. int runstp = 0; // run/stop flag
  41. int rticnt = 0; // RTICNT (variable)
  42. int prevpb = 0; // previous state of pushbuttons (variable)
  43.  
  44. unsigned int shotclock = 240;
  45. int prev_leftpb = 0;
  46. int prev_rightpb = 0;
  47. int rticnt = 0;
  48.  
  49. /***********************************************************************
  50. Initializations
  51. ***********************************************************************/
  52. void initializations(void) {
  53.  
  54. //; Set the PLL speed (bus clock = 24 MHz)
  55. CLKSEL = CLKSEL & 0x80; //; disengage PLL from system
  56. PLLCTL = PLLCTL | 0x40; //; turn on PLL
  57. SYNR = 0x02; //; set PLL multiplier
  58. REFDV = 0; //; set PLL divider
  59. while (!(CRGFLG & 0x08)){ }
  60. CLKSEL = CLKSEL | 0x80; //; engage PLL
  61.  
  62. // Disable watchdog timer (COPCTL register)
  63. COPCTL = 0x40 ; //COP off; RTI and COP stopped in BDM-mode
  64.  
  65. // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
  66. SCIBDH = 0x00; //set baud rate to 9600
  67. SCIBDL = 0x9C; //24,000,000 / 16 / 156 = 9600 (approx)
  68. SCICR1 = 0x00; //$9C = 156
  69. SCICR2 = 0x0C; //initialize SCI for program-driven operation
  70. DDRB = 0x10; //set PB4 for output mode
  71. PORTB = 0x10; //assert DTR pin on COM port
  72.  
  73. // Initialize Port AD pins 7 and 6 for use as digital inputs
  74. DDRAD = 0 ; //program port AD for input mode
  75. ATDDIEN = 0xC0 ; //program PAD7 and PAD6 pins as digital inputs
  76.  
  77.  
  78. // Add additional port pin initializations here (e.g., Other DDRs, Ports)
  79.  
  80.  
  81.  
  82. // Add RTI/interrupt initializations here
  83. CRGINT = CRGINT | 0x80;
  84. RTICTL = 0x51;
  85.  
  86. }
  87.  
  88. /***********************************************************************
  89. Main
  90. ***********************************************************************/
  91. void main(void) {
  92. initializations();
  93. EnableInterrupts;
  94.  
  95.  
  96. for(;;) {
  97.  
  98. if(tenthsec == 1){
  99. if(runstp == 1){
  100. shotclock = shotclock - 1;
  101. //update seven seg
  102. if(shotclock >= 100){
  103. //update using upper two digits of shotclock value
  104. }
  105. else{
  106. //update using lower two digi
  107. }
  108. }
  109. tenthsec = 0;
  110. print_num(shotclock)
  111. }
  112.  
  113. if(leftpb == 1){
  114. runstp = 0; //clear the "run/stop" flag
  115. //reset 7-segment display to "24"
  116. leftpb = 0; //clear the left pushbutton flag
  117. }
  118.  
  119. if(rightpb == 1){
  120. rightpb = 0; //clear the left pushbutton flag
  121. //toggle run/stop flag
  122. if(runstp == 0){
  123. runstp = 1;
  124. }
  125. else{
  126. runstp = 0;
  127. }
  128. //toggle run/stop LED
  129. }
  130.  
  131. if(shotclock == 0){
  132. runstop = 0;
  133. //turn on the "time expired" LED on left-most DP
  134. //turn off the run/stop LED
  135. }
  136.  
  137. /*; Main program loop (state machine)
  138. Start of main program-driven polling loop
  139. ;
  140. main
  141.  
  142. ; If the "tenth second" flag is set, then
  143. ; - clear the "tenth second" flag
  144. ; - If the "run/stop" flag is set, then
  145. ; + decrement the shot clock value by one-tenth of a second
  146. ; + update the 7-segment displays
  147. ; - Endif
  148. ; Endif
  149.  
  150.  
  151. ; If the left pushbutton ("reset shot clock") flag is set, then:
  152. ; - clear the "run/stop" flag
  153. ; - reset the 7-segment display to "24"
  154. ; - clear the left pushbutton flag
  155. ; Endif
  156.  
  157.  
  158. ; If the right pushbutton ("start/stop") flag is set, then
  159. ; - clear the right pushbutton flag
  160. ; - toggle the "run/stop" flag
  161. ; - toggle the "run/stop" LED
  162. ; Endif
  163.  
  164.  
  165. ; If the shot clock has reached "00", then:
  166. ; - clear the "run/stop" flag
  167. ; - turn on the "time expired" LED
  168. ; - turn off the "run/stop" LED
  169. ; Endif
  170. */
  171.  
  172.  
  173. _FEED_COP(); /* feeds the dog */
  174. } /* loop forever */
  175. for(;;){} //code retrieved from page 2 of 362 c Tutorial PDF
  176. /* please make sure that you never leave main */
  177. }
  178.  
  179.  
  180.  
  181. /***********************************************************************
  182. ; RTI interrupt service routine: rti_isr
  183. ;
  184. ; Initialized for 1.408 ms interrupt rate
  185. ;
  186. ; Samples state of pushbuttons (PAD7 = left, PAD6 = right)
  187. ;
  188. ; If change in state from "high" to "low" detected, set pushbutton flag
  189. ; leftpb (for PAD7 H -> L), rghtpb (for PAD6 H -> L)
  190. ; Recall that pushbuttons are momentary contact closures to ground
  191. ;
  192. ; Also, keeps track of when one-tenth of a second's worth of RTI interrupts go by,
  193. ; and sets the "tenth second" flag
  194. ;***********************************************************************/
  195. interrupt 7 void RTI_ISR( void)
  196. {
  197. rticnt = rticnt + 1;
  198. // set CRGFLG bit
  199. CRGFLG = CRGFLG | 0x80;
  200.  
  201. if(PAD7 != prev_leftpb){
  202. prev_leftpb = PAD7;
  203. leftpb = 1;
  204. }
  205.  
  206. if(PAD6 != prev_rightpb){
  207. prev_rightpb = PAD7;
  208. rightpb = 1;
  209. }
  210.  
  211. if(rticnt == 710){
  212. rticnt = 0;
  213. tenthsec = 1;
  214. }
  215.  
  216.  
  217. }
  218.  
  219. /***********************************************************************
  220. ; Shot clock display routine "tdisp"
  221. ;
  222. ; Displays the current count
  223. ;
  224. ; NOTE: These values are in BCD and must be converted to
  225. ; "7-segment display code" -- table lookup should be
  226. ; used to perform this conversion
  227. ;***********************************************************************/
  228. void tdisp()
  229. {
  230.  
  231. }
  232.  
  233. /***********************************************************************
  234. ; Add any additional subroutines needed here (e.g., count_down, BCD_to_7seg,
  235. ; shift_out_dat, etc.)
  236. ;***********************************************************************/
  237. void shotclock_to_7seg(){
  238.  
  239. }
  240.  
  241. void print_num(int num){
  242. char first = itoa(num/100);
  243. char second = itoa((num/10)%10);
  244. char third = itoa(num%10);
  245.  
  246. outchar(first);
  247. outchar(second);
  248. outchar(third);
  249. outchar(' ');
  250. }
  251.  
  252.  
  253. /***********************************************************************
  254. ; Character I/O Library Routines for 9S12C32
  255. ;***********************************************************************
  256. ; Name: inchar
  257. ; Description: inputs ASCII character from SCI serial port and returns it
  258. ;***********************************************************************/
  259. char inchar(void) {
  260. /* receives character from the terminal channel */
  261. while (!(SCISR1 & 0x20)); /* wait for input */
  262. return SCIDRL;
  263.  
  264. }
  265.  
  266. /***********************************************************************
  267. ; Name: outchar
  268. ; Description: outputs ASCII character passed in outchar()
  269. ; to the SCI serial port
  270. ;***********************************************************************/
  271. void outchar(char ch) {
  272. /* sends a character to the terminal channel */
  273. while (!(SCISR1 & 0x80)); /* wait for output buffer empty */
  274. SCIDRL = ch;
  275. }
Add Comment
Please, Sign In to add comment