Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************************
- * ECE 362 - Experiment 6 - Fall 2012
- **********************************************************************/
- #include <hidef.h> /* common defines and macros */
- #include "derivative.h" /* derivative-specific definitions */
- #include <mc9s12c32.h>
- // All funtions after main should be initialiezed here
- char inchar(void); //Char input through serial
- void outchar(char x); //Char output to serial
- void tdisp(int,int); //Display value on 7seg
- void BCD_to_7seg(int,int); //BCD -> 7seg data
- void seg_out(int,int);
- // ASCII character definitions
- int CR = 0x0D;//Return
- //Variable declarations
- int tenthsec = 0; // One-tenth second flag
- int leftpb = 0; // left pushbutton flag
- int rightpb = 0; // right pushbutton flag
- int runstp = 1; // run/stop flag (running on start-up)
- int rticnt = 0; // RTICNT (variable)
- int prevpb = 0; // previous state of pushbuttons (variable)
- int segA = 2; // Value on 7-segment A
- int segB = 4; // Value on 7-segment B
- int count_dec = 0; // Decimal counting flag (timer <10)
- int sec_sack = 0; // Tracks # sec parts until full sec
- int first = 1; // Primary run flag
- double temp = 0;
- /***********************************************************************
- Initializations
- ***********************************************************************/
- void initializations(void) {
- // Set the PLL speed (bus clock = 24 MHz)
- CLKSEL = CLKSEL & 0x80; // disengage PLL from system
- PLLCTL = PLLCTL | 0x40; // turn on PLL
- SYNR = 0x02; // set PLL multiplier
- REFDV = 0; // set PLL divider
- while (!(CRGFLG & 0x08)){ }
- CLKSEL = CLKSEL | 0x80; // engage PLL
- // Disable watchdog timer (COPCTL register)
- COPCTL = 0x40; //COP off; RTI and COP stopped in BDM-mode
- // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
- SCIBDH = 0x00; //set baud rate to 9600
- SCIBDL = 0x9C; //24,000,000 / 16 / 156 = 9600 (approx)
- SCICR1 = 0x00; //$9C = 156
- SCICR2 = 0x0C; //initialize SCI for program-driven operation
- DDRB = 0x10; //set PB4 for output mode
- PORTB = 0x10; //assert DTR pin on COM port
- //----------------------------------------------------------------------------------
- // Initialize Port AD pins 7 and 6 for use as digital inputs
- DDRAD = 0; //program port AD for input mode
- ATDDIEN = 0xC0; //program PAD7 and PAD6 pins as digital inputs
- // Add additional port pin initializations here (e.g., Other DDRs, Ports)
- DDRT = 0xFF;
- PTT = 0;
- CRGINT = CRGINT | 0x80; //set crgint
- //interrupts enabled in main
- // Add RTI/interrupt initializations here
- rticnt = 0;
- RTICTL = 0x1A;
- }
- /***********************************************************************
- Main
- ***********************************************************************/
- void main(void) {
- initializations();
- EnableInterrupts;
- for(;;) {
- //------Main program loop (state machine)-------------------------------
- //------Start of main program polling loop------------------------------
- /* If the "tenth second" flag is set, then
- * - clear the "tenth second" flag
- * - If the "run/stop" flag is set, then
- * + decrement the shot clock value by one-tenth of a second
- * + update the 7-segment displays
- * - Endif
- * Endif */
- if (first == 1) {
- tdisp(segA, 1);
- tdisp(segB, 2);
- first = 0;
- PTT = PTT | 0x01;
- }
- //Check for a 0.1sec count complete
- if ((tenthsec == 1) && (runstp == 1)) {
- tenthsec = 0; //tenthsec flag low
- runstp = 1; //runstp Flag on
- sec_sack++; //Add 0.1 to full second counter
- //if counter >= 10 (not using decimal)
- if ((count_dec == 0) && (sec_sack == 10)) {
- //if counter is 20 or 10
- if ((segA != 0) && (segB == 0)) {
- //if counter is at 10, set to 9.9 and decimal flag high
- if (segA == 1) {
- count_dec = 1;
- segA = 9;
- segB = 9;
- sec_sack = 0;
- }
- //if counter is at 20
- if (segA == 2) {
- segA = 1;
- segB = 9;
- sec_sack = 0;
- }
- tdisp(segA,1); //Display both values
- tdisp(segB,2);
- }
- //if segB is not 0
- else if ((segA != 0) && (segB != 0)) {
- segB--;
- tdisp(segB,2); //Display B
- sec_sack = 0;
- }
- ;
- }
- //if counter <=9.9 (using decimal)
- else if (count_dec == 1) {
- //if #.0 (not 0.0)
- if ((segA != 0) && (segB == 0)) {
- segA--;
- segB = 9;
- sec_sack--;
- }
- else if (segB != 0) {
- segB--;
- sec_sack--;
- }
- tdisp(segA,1);
- tdisp(segB,2);
- }
- }
- /* If the left pushbutton ("reset shot clock") flag is set, then:
- * - clear the "run/stop" flag
- * - reset the 7-segment display to "24"
- * - clear the left pushbutton flag
- * Endif */
- //Check for left btn push (reset clock)
- if (leftpb == 1) {
- count_dec = 0;
- sec_sack = 0;
- rticnt = 0;
- tenthsec = 0;
- segA = 2;
- segB = 4;
- tdisp(2, 1);
- tdisp(4, 2);
- runstp = 0;
- PTT = PTT & 0xFC;//clear LED 1 & 2
- leftpb = 0;
- prevpb = 0;
- }
- /* If the right pushbutton ("start/stop") flag is set, then
- * - clear the right pushbutton flag
- * - toggle the "run/stop" flag
- * - toggle the "run/stop" LED
- * Endif */
- if (rightpb == 1) {
- rightpb = 0; //clear right pushbutton flag
- //toggle run/stop flag
- if (runstp == 0) {
- runstp = 1;
- PTT = PTT | 0x01;
- PTT = PTT & 0xFD;
- //count_dec = 0;
- prevpb = 0;
- } else if (runstp == 1) {
- runstp = 0;
- PTT = PTT & 0xFE;
- prevpb = 0;
- }
- }
- /* If the shot clock has reached "00", then:
- * - clear the "run/stop" flag
- * - turn on the "time expired" LED
- * - turn off the "run/stop" LED
- * Endif */
- if ((segA == 0) && (segB == 0)) {
- runstp = 0; //hault process (stop running)
- PTT = PTT | 0x02;//set LED 2
- }
- _FEED_COP(); /* feeds the dog */
- } /* loop forever */
- /* please make sure that you never leave main */
- }
- /***********************************************************************
- ; RTI interrupt service routine: rti_isr
- ;
- ; Initialized for 1.408 ms interrupt rate
- ;
- ; Samples state of pushbuttons (PAD7 = left, PAD6 = right)
- ;
- ; If change in state from "high" to "low" detected, set pushbutton flag
- ; leftpb (for PAD7 H -> L), rghtpb (for PAD6 H -> L)
- ; Recall that pushbuttons are momentary contact closures to ground
- ;
- ; Also, keeps track of when one-tenth of a second's worth of RTI interrupts go by,
- ; and sets the "tenth second" flag
- ;***********************************************************************/
- interrupt 7 void RTI_ISR( void)
- {
- CRGFLG = CRGFLG | 0x80; //set CRGFLG
- rticnt++;
- if (rticnt == 71) {
- tenthsec = 1;
- rticnt = 0;
- }
- if ((PTAD_PTAD6 == 0) && (prevpb == 0)) {//pushed + used to be off//RIGHT
- rightpb = 1;
- prevpb = 1;//used to be on
- }
- if ((PTAD_PTAD7 == 0) && (prevpb == 0)) {//pushed + used to be off//LEFT
- leftpb = 1;
- prevpb = 1;//used to be on
- }
- }
- /***********************************************************************
- ; Shot clock display routine "tdisp"
- ;
- ; Displays the current count
- ;
- ; NOTE: These values are in BCD and must be converted to
- ; "7-segment display code" -- table lookup should be
- ; used to perform this conversion
- ;***********************************************************************/
- void tdisp(int var_BCD, int pos)
- {
- BCD_to_7seg(var_BCD, pos);
- }
- /***********************************************************************
- ; Add any additional subroutines needed here (e.g., count_down, BCD_to_7seg,
- ; shift_out_dat, etc.)
- ;***********************************************************************/
- void BCD_to_7seg(int var_BCD, int pos) {
- //edited for rolling right
- if (var_BCD == 0) {
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- //var_7seg = 00000011;//11000000;//0x0C 11000000 !DR-!G-F-E-D-C-B-A
- } else if (var_BCD == 1) {
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- //var_7seg = 00111111;//11111100;//0xFC 11111100 !DP-!G-!F-!E-!D-!C-B-A
- } else if (var_BCD == 2) {//++
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- //var_7seg = 00100101;//10100100;//0x|| 10100100 !DP-G-!F-E-D-!C-B-A -------
- } else if (var_BCD == 3) {//++
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- //var_7seg = 00001101;//10110000;//0x06 10110000 !DP-G-!F-!E-D-C-B-A
- } else if (var_BCD == 4) {//++
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- //var_7seg = 01100110;//10011001;//10011001;//0x4C 10011001 !DP-G-F-!E-!D-C-B-!A
- } else if (var_BCD == 5) {//++
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- //var_7seg = 01001001;//10010010;//0x|| 10010010 !DP-G-F-!E-D-C-!B-A
- } else if (var_BCD == 6) {//++
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- //var_7seg = 01000001;//10000010;//0x30 10000010 !DP-G-F-E-D-C-!B-A
- } else if (var_BCD == 7) {//++
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- //var_7seg = 00011111;//11111000;//0x0F 11111000 !DP-!G-!F-!E-!D-C-B-A
- } else if (var_BCD == 8) {//++
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- //var_7seg = 00000001;//10000000;//0x00 10000000 !DP-G-F-E-D-C-B-A
- } else if (var_BCD == 9) {//++
- BCD_to_7seg(10,pos);
- if ((count_dec == 1) && (pos == 2)) {
- seg_out(1,pos);
- } else {
- seg_out(0,pos);
- }
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- seg_out(1,pos);
- //var_7seg = 00011001;//10011000;//0x0C 10011000 !DP-G-F-!E-!D-C-B-A
- } else if (var_BCD == 10) {
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos);
- seg_out(0,pos); //clear with no-pin
- seg_out(0,pos);
- seg_out(0,pos);
- }
- return;//(var_7seg);
- }
- void seg_out(int out, int pos) {
- PTT = PTT & 0x03;
- if (pos == 1) {
- if (out == 1) {
- PTT = PTT | 0x20; //00100000 Pin to A low and clock high
- }
- if (out == 0) {
- PTT = PTT | 0x40; //01000000 Pin to A high
- PTT = PTT | 0x20; //00100000 Pin to A and clock high
- }
- }
- if (pos == 2) {
- if (out == 1) {
- PTT = PTT | 0x08; //00001000 Pin to B low and clock high
- }
- if (out == 0) {
- PTT = PTT | 0x10; //00010000 Pin to B high
- PTT = PTT | 0x08; //00001000 Pin to B and clock high
- }
- }
- return;
- }
- /***********************************************************************
- ; Character I/O Library Routines for 9S12C32
- ;***********************************************************************
- ; Name: inchar
- ; Description: inputs ASCII character from SCI serial port and returns it
- ;***********************************************************************/
- char inchar(void) {
- /* receives character from the terminal channel */
- while (!(SCISR1 & 0x20)); /* wait for input */
- return SCIDRL;
- }
- /***********************************************************************
- ; Name: outchar
- ; Description: outputs ASCII character passed in outchar()
- ; to the SCI serial port
- ;***********************************************************************/
- void outchar(char ch) {
- /* sends a character to the terminal channel */
- while (!(SCISR1 & 0x80)); /* wait for output buffer empty */
- SCIDRL = ch;
- }
Advertisement
Add Comment
Please, Sign In to add comment