CodeCodeCode

ECE362 LAB06

Oct 19th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.89 KB | None | 0 0
  1. /***********************************************************************
  2.  * ECE 362 - Experiment 6 - Fall 2012                        
  3.  **********************************************************************/
  4. #include <hidef.h>          /* common defines and macros */
  5. #include "derivative.h"         /* derivative-specific definitions */
  6. #include <mc9s12c32.h>
  7.  
  8. // All funtions after main should be initialiezed here
  9. char inchar(void);    //Char input through serial
  10. void outchar(char x); //Char output to serial
  11. void tdisp(int,int);      //Display value on 7seg
  12. void BCD_to_7seg(int,int); //BCD -> 7seg data
  13. void seg_out(int,int);
  14.  
  15. // ASCII character definitions
  16. int CR = 0x0D;//Return                  
  17.                                                    
  18. //Variable declarations                
  19. int tenthsec = 0;  // One-tenth second flag
  20. int leftpb = 0;    // left pushbutton flag
  21. int rightpb = 0;   // right pushbutton flag
  22. int runstp = 1;    // run/stop flag (running on start-up)                        
  23. int rticnt = 0;    // RTICNT (variable)
  24. int prevpb = 0;    // previous state of pushbuttons (variable)
  25. int segA = 2;      // Value on 7-segment A
  26. int segB = 4;      // Value on 7-segment B
  27. int count_dec = 0; // Decimal counting flag (timer <10)
  28. int sec_sack = 0;  // Tracks # sec parts until full sec
  29. int first = 1;     // Primary run flag
  30. double temp = 0;
  31.                
  32. /***********************************************************************
  33. Initializations
  34. ***********************************************************************/
  35. void  initializations(void) {
  36.  
  37. // Set the PLL speed (bus clock = 24 MHz)
  38.     CLKSEL = CLKSEL & 0x80; // disengage PLL from system
  39.     PLLCTL = PLLCTL | 0x40; // turn on PLL
  40.     SYNR = 0x02;            // set PLL multiplier
  41.     REFDV = 0;              // set PLL divider
  42.     while (!(CRGFLG & 0x08)){  }
  43.     CLKSEL = CLKSEL | 0x80; // engage PLL
  44.  
  45. // Disable watchdog timer (COPCTL register)
  46.     COPCTL = 0x40; //COP off; RTI and COP stopped in BDM-mode
  47.  
  48. // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
  49.     SCIBDH =  0x00; //set baud rate to 9600
  50.     SCIBDL =  0x9C; //24,000,000 / 16 / 156 = 9600 (approx)  
  51.     SCICR1 =  0x00; //$9C = 156
  52.     SCICR2 =  0x0C; //initialize SCI for program-driven operation
  53.     DDRB   =  0x10; //set PB4 for output mode
  54.     PORTB  =  0x10; //assert DTR pin on COM port
  55. //----------------------------------------------------------------------------------        
  56. //  Initialize Port AD pins 7 and 6 for use as digital inputs
  57.     DDRAD = 0;      //program port AD for input mode
  58.     ATDDIEN = 0xC0; //program PAD7 and PAD6 pins as digital inputs
  59.  
  60. //  Add additional port pin initializations here  (e.g., Other DDRs, Ports)
  61.     DDRT = 0xFF;
  62.     PTT = 0;
  63.     CRGINT = CRGINT | 0x80; //set crgint
  64.     //interrupts enabled in main
  65.  
  66. //  Add RTI/interrupt initializations here
  67.     rticnt = 0;
  68.     RTICTL = 0x1A;
  69. }
  70.                                        
  71. /***********************************************************************
  72. Main
  73. ***********************************************************************/
  74. void main(void) {
  75.     initializations();                                 
  76.     EnableInterrupts;
  77.        
  78.     for(;;) {
  79.  
  80.  
  81.     //------Main program loop (state machine)-------------------------------
  82.     //------Start of main program polling loop------------------------------           
  83.    
  84.     /* If the "tenth second" flag is set, then
  85.      *   - clear the "tenth second" flag
  86.      *   - If the "run/stop" flag is set, then
  87.      *     + decrement the shot clock value by one-tenth of a second
  88.      *     + update the 7-segment displays
  89.      *   - Endif
  90.      * Endif */
  91.     if (first == 1) {    
  92.         tdisp(segA, 1);
  93.         tdisp(segB, 2);
  94.         first = 0;
  95.         PTT = PTT | 0x01;
  96.     }
  97.    
  98.  
  99.     //Check for a 0.1sec count complete
  100.     if ((tenthsec == 1) && (runstp == 1)) {
  101.         tenthsec = 0;  //tenthsec flag low
  102.         runstp = 1;    //runstp Flag on
  103.         sec_sack++;    //Add 0.1 to full second counter
  104.        
  105.         //if counter >= 10 (not using decimal)
  106.         if ((count_dec == 0) && (sec_sack == 10)) {
  107.             //if counter is 20 or 10
  108.             if ((segA != 0) && (segB == 0)) {
  109.                 //if counter is at 10, set to 9.9 and decimal flag high
  110.                 if (segA == 1) {
  111.                     count_dec = 1;
  112.                     segA = 9;
  113.                     segB = 9;
  114.                     sec_sack = 0;
  115.                 }
  116.                 //if counter is at 20
  117.                 if (segA == 2) {
  118.                     segA = 1;
  119.                     segB = 9;
  120.                     sec_sack = 0;
  121.                 }
  122.                 tdisp(segA,1); //Display both values
  123.             tdisp(segB,2);
  124.            
  125.             }
  126.             //if segB is not 0
  127.             else if ((segA != 0) && (segB != 0)) {
  128.                 segB--;
  129.                 tdisp(segB,2); //Display B
  130.                 sec_sack = 0;
  131.             }
  132.             ;              
  133.         }
  134.         //if counter <=9.9 (using decimal)
  135.     else if (count_dec == 1) {
  136.             //if #.0 (not 0.0)
  137.             if ((segA != 0) && (segB == 0)) {
  138.                 segA--;
  139.                 segB = 9;
  140.                 sec_sack--;
  141.             }
  142.             else if (segB != 0) {
  143.               segB--;
  144.               sec_sack--;
  145.             }
  146.             tdisp(segA,1);
  147.             tdisp(segB,2);
  148.     }
  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.     //Check for left btn push (reset clock)
  158.     if (leftpb == 1) {
  159.       count_dec = 0;
  160.       sec_sack = 0;
  161.       rticnt = 0;
  162.       tenthsec = 0;
  163.       segA = 2;
  164.       segB = 4;
  165.       tdisp(2, 1);
  166.       tdisp(4, 2);
  167.         runstp = 0;
  168.         PTT = PTT & 0xFC;//clear LED 1 & 2
  169.         leftpb = 0;
  170.         prevpb = 0;
  171.     }
  172.  
  173.     /* If the right pushbutton ("start/stop") flag is set, then
  174.      *  - clear the right pushbutton flag
  175.      *  - toggle the "run/stop" flag
  176.      *  - toggle the "run/stop" LED
  177.      * Endif */
  178.      
  179.     if (rightpb == 1) {
  180.         rightpb = 0; //clear right pushbutton flag
  181.  
  182.         //toggle run/stop flag
  183.         if (runstp == 0) {
  184.             runstp = 1;
  185.             PTT = PTT | 0x01;
  186.             PTT = PTT & 0xFD;
  187.             //count_dec = 0;
  188.             prevpb = 0;
  189.         } else if (runstp == 1) {
  190.             runstp = 0;
  191.             PTT = PTT & 0xFE;
  192.             prevpb = 0;
  193.         }
  194.     }
  195.  
  196.     /* If the shot clock has reached "00", then:
  197.      *  - clear the "run/stop" flag
  198.      *  - turn on the "time expired" LED
  199.      *  - turn off the "run/stop" LED
  200.      * Endif */
  201.  
  202.     if ((segA == 0) && (segB == 0)) {
  203.         runstp = 0;       //hault process (stop running)
  204.         PTT = PTT | 0x02;//set LED 2
  205.     }
  206.    
  207.  
  208.     _FEED_COP(); /* feeds the dog */
  209.   } /* loop forever */
  210.   /* please make sure that you never leave main */
  211. }
  212.  
  213.  
  214.  
  215. /***********************************************************************                      
  216. ; RTI interrupt service routine: rti_isr
  217. ;
  218. ;  Initialized for 1.408 ms interrupt rate
  219. ;
  220. ;  Samples state of pushbuttons (PAD7 = left, PAD6 = right)
  221. ;
  222. ;  If change in state from "high" to "low" detected, set pushbutton flag
  223. ;     leftpb (for PAD7 H -> L), rghtpb (for PAD6 H -> L)
  224. ;     Recall that pushbuttons are momentary contact closures to ground
  225. ;
  226. ;  Also, keeps track of when one-tenth of a second's worth of RTI interrupts go by,
  227. ;      and sets the "tenth second" flag                                                            
  228. ;***********************************************************************/
  229. interrupt 7 void RTI_ISR( void)
  230. {
  231.     CRGFLG = CRGFLG | 0x80; //set CRGFLG
  232.       rticnt++;
  233.      
  234.       if (rticnt == 71) {
  235.         tenthsec = 1;  
  236.          rticnt = 0;
  237.       }
  238.    
  239.     if ((PTAD_PTAD6 == 0) && (prevpb == 0)) {//pushed + used to be off//RIGHT
  240.       rightpb = 1;
  241.       prevpb = 1;//used to be on
  242.     }
  243.     if ((PTAD_PTAD7 == 0) && (prevpb == 0)) {//pushed + used to be off//LEFT
  244.       leftpb = 1;
  245.       prevpb = 1;//used to be on
  246.     }
  247. }
  248.  
  249. /***********************************************************************                        
  250. ;  Shot clock display routine "tdisp"
  251. ;
  252. ;  Displays the current count
  253. ;
  254. ;  NOTE: These values are in BCD and must be converted to
  255. ;        "7-segment display code" -- table lookup should be
  256. ;        used to perform this conversion
  257. ;***********************************************************************/
  258. void tdisp(int var_BCD, int pos)
  259. {
  260.       BCD_to_7seg(var_BCD, pos);   
  261. }
  262.  
  263. /***********************************************************************                      
  264. ; Add any additional subroutines needed here (e.g., count_down, BCD_to_7seg,
  265. ; shift_out_dat, etc.)
  266. ;***********************************************************************/
  267. void BCD_to_7seg(int var_BCD, int pos) {
  268.  
  269.     //edited for rolling right
  270.     if (var_BCD == 0) {
  271.       BCD_to_7seg(10,pos);
  272.       if ((count_dec == 1) && (pos == 2)) {
  273.         seg_out(1,pos);
  274.       } else {       
  275.         seg_out(0,pos);
  276.       }
  277.       seg_out(0,pos);
  278.     seg_out(1,pos);
  279.     seg_out(1,pos);
  280.     seg_out(1,pos);
  281.     seg_out(1,pos);
  282.     seg_out(1,pos);
  283.     seg_out(1,pos);
  284.         //var_7seg = 00000011;//11000000;//0x0C 11000000 !DR-!G-F-E-D-C-B-A
  285.     } else if (var_BCD == 1) {
  286.       BCD_to_7seg(10,pos);
  287.       if ((count_dec == 1) && (pos == 2)) {
  288.         seg_out(1,pos);
  289.       } else {       
  290.         seg_out(0,pos);
  291.       }
  292.       seg_out(0,pos);
  293.     seg_out(0,pos);
  294.     seg_out(0,pos);
  295.     seg_out(0,pos);
  296.     seg_out(1,pos);
  297.     seg_out(1,pos);
  298.     seg_out(0,pos);
  299.         //var_7seg = 00111111;//11111100;//0xFC 11111100 !DP-!G-!F-!E-!D-!C-B-A
  300.     } else if (var_BCD == 2) {//++
  301.       BCD_to_7seg(10,pos);
  302.         if ((count_dec == 1) && (pos == 2)) {
  303.         seg_out(1,pos);
  304.       } else {       
  305.         seg_out(0,pos);
  306.       }
  307.       seg_out(1,pos);
  308.     seg_out(0,pos);
  309.     seg_out(1,pos);
  310.     seg_out(1,pos);
  311.     seg_out(0,pos);
  312.     seg_out(1,pos);
  313.     seg_out(1,pos);
  314.         //var_7seg = 00100101;//10100100;//0x|| 10100100 !DP-G-!F-E-D-!C-B-A   -------
  315.     } else if (var_BCD == 3) {//++
  316.         if ((count_dec == 1) && (pos == 2)) {
  317.         seg_out(1,pos);
  318.       } else {       
  319.         seg_out(0,pos);
  320.       }
  321.       seg_out(1,pos);
  322.     seg_out(0,pos);
  323.     seg_out(0,pos);
  324.     seg_out(1,pos);
  325.     seg_out(1,pos);
  326.     seg_out(1,pos);
  327.     seg_out(1,pos);
  328.         //var_7seg = 00001101;//10110000;//0x06 10110000 !DP-G-!F-!E-D-C-B-A
  329.     } else if (var_BCD == 4) {//++
  330.       BCD_to_7seg(10,pos);
  331.         if ((count_dec == 1) && (pos == 2)) {
  332.         seg_out(1,pos);
  333.       } else {       
  334.         seg_out(0,pos);
  335.       }
  336.       seg_out(1,pos);
  337.     seg_out(1,pos);
  338.     seg_out(0,pos);
  339.     seg_out(0,pos);
  340.     seg_out(1,pos);
  341.     seg_out(1,pos);
  342.     seg_out(0,pos);
  343.         //var_7seg = 01100110;//10011001;//10011001;//0x4C 10011001 !DP-G-F-!E-!D-C-B-!A
  344.     } else if (var_BCD == 5) {//++
  345.         if ((count_dec == 1) && (pos == 2)) {
  346.         seg_out(1,pos);
  347.       } else {       
  348.         seg_out(0,pos);
  349.       }
  350.       seg_out(1,pos);
  351.     seg_out(1,pos);
  352.     seg_out(0,pos);
  353.     seg_out(1,pos);
  354.     seg_out(1,pos);
  355.     seg_out(0,pos);
  356.     seg_out(1,pos);
  357.         //var_7seg = 01001001;//10010010;//0x|| 10010010 !DP-G-F-!E-D-C-!B-A
  358.     } else if (var_BCD == 6) {//++
  359.         if ((count_dec == 1) && (pos == 2)) {
  360.         seg_out(1,pos);
  361.       } else {       
  362.         seg_out(0,pos);
  363.       }
  364.       seg_out(1,pos);
  365.     seg_out(1,pos);
  366.     seg_out(1,pos);
  367.     seg_out(1,pos);
  368.     seg_out(1,pos);
  369.     seg_out(0,pos);
  370.     seg_out(1,pos);
  371.         //var_7seg = 01000001;//10000010;//0x30 10000010 !DP-G-F-E-D-C-!B-A
  372.     } else if (var_BCD == 7) {//++
  373.       BCD_to_7seg(10,pos);
  374.       if ((count_dec == 1) && (pos == 2)) {
  375.         seg_out(1,pos);
  376.       } else {       
  377.         seg_out(0,pos);
  378.       }
  379.       seg_out(0,pos);
  380.     seg_out(0,pos);
  381.     seg_out(0,pos);
  382.     seg_out(0,pos);
  383.     seg_out(1,pos);
  384.     seg_out(1,pos);
  385.     seg_out(1,pos);
  386.         //var_7seg = 00011111;//11111000;//0x0F 11111000 !DP-!G-!F-!E-!D-C-B-A
  387.     } else if (var_BCD == 8) {//++
  388.         BCD_to_7seg(10,pos);
  389.         if ((count_dec == 1) && (pos == 2)) {
  390.         seg_out(1,pos);
  391.       } else {       
  392.         seg_out(0,pos);
  393.       }
  394.       seg_out(1,pos);
  395.     seg_out(1,pos);
  396.     seg_out(1,pos);
  397.     seg_out(1,pos);
  398.     seg_out(1,pos);
  399.     seg_out(1,pos);
  400.     seg_out(1,pos);
  401.         //var_7seg = 00000001;//10000000;//0x00 10000000 !DP-G-F-E-D-C-B-A
  402.     } else if (var_BCD == 9) {//++
  403.         BCD_to_7seg(10,pos);
  404.         if ((count_dec == 1) && (pos == 2)) {
  405.         seg_out(1,pos);
  406.       } else {       
  407.         seg_out(0,pos);
  408.       }
  409.       seg_out(1,pos);
  410.     seg_out(1,pos);
  411.     seg_out(0,pos);
  412.     seg_out(0,pos);
  413.     seg_out(1,pos);
  414.     seg_out(1,pos);
  415.     seg_out(1,pos);
  416.         //var_7seg = 00011001;//10011000;//0x0C 10011000 !DP-G-F-!E-!D-C-B-A
  417.     } else if (var_BCD == 10) {
  418.       seg_out(0,pos);
  419.       seg_out(0,pos);
  420.     seg_out(0,pos);
  421.     seg_out(0,pos);
  422.     seg_out(0,pos);
  423.     seg_out(0,pos); //clear with no-pin
  424.     seg_out(0,pos);
  425.     seg_out(0,pos);
  426.     }
  427.            
  428.     return;//(var_7seg);
  429. }
  430.  
  431. void seg_out(int out, int pos) {
  432.      
  433.   PTT = PTT & 0x03;
  434.   if (pos == 1) {
  435.        
  436.      if (out == 1) {    
  437.           PTT = PTT | 0x20; //00100000 Pin to A low and clock high
  438.      }
  439.      if (out == 0) {
  440.           PTT = PTT | 0x40; //01000000 Pin to A high
  441.           PTT = PTT | 0x20; //00100000 Pin to A and clock high      
  442.      }      
  443.   }
  444.   if (pos == 2) {
  445.       if (out == 1) {
  446.           PTT = PTT | 0x08; //00001000 Pin to B low and clock high
  447.       }
  448.       if (out == 0) {
  449.           PTT = PTT | 0x10; //00010000 Pin to B high
  450.           PTT = PTT | 0x08; //00001000 Pin to B and clock high
  451.       }
  452.   }
  453.  
  454.     return;
  455. }
  456.  
  457. /***********************************************************************
  458. ; Character I/O Library Routines for 9S12C32
  459. ;***********************************************************************
  460. ; Name:         inchar
  461. ; Description:  inputs ASCII character from SCI serial port and returns it
  462. ;***********************************************************************/
  463. char inchar(void) {
  464.   /* receives character from the terminal channel */
  465.     while (!(SCISR1 & 0x20)); /* wait for input */
  466.     return SCIDRL;
  467.  
  468. }
  469.  
  470. /***********************************************************************
  471. ; Name:         outchar
  472. ; Description:  outputs ASCII character passed in outchar()
  473. ;                  to the SCI serial port
  474. ;***********************************************************************/
  475. void outchar(char ch) {
  476.   /* sends a character to the terminal channel */
  477.     while (!(SCISR1 & 0x80));  /* wait for output buffer empty */
  478.     SCIDRL = ch;
  479. }
Advertisement
Add Comment
Please, Sign In to add comment