Physics 4DA3/4DB3/4D06
Freescale MC9S08QG8/4
Code Example #9


2012.01.01 - recent update  
  - added LCD info  
  - added Example 4.  
2012.01.13 - Introduction to Examples  
  - added explanations, various comments  
  - added example 5  
2012.01.15 - Global and local variables  
  - Stacks  
2012.01.16 - Interrupts  
2012.01.19 - 7-Segment LED interface  
2012.02.13 - Example #10 - Four 7-segment LED multiplexed  
2012.02.14 - Modifications to Example #10  
2012.03.14 - Examples on individual pages  

MC9S08 Resources

MC9S08 Getting Started Supplement

Example #1 Simple ASM program vs C program  
Memory I/O Model
Page Zero Addressing
Infinite Loop
External Crystal
Example #2 Flashing LED
  Software delay  
Example #3 Character Output  
  Serial Transmit Data
LCD Display
Function Prototypes
Example #4 Text Message  
  Characters, Strings and Pointers
Example #5 Timer test with oscilloscope
Example #6 Timer test with flashing LED  
  Gobal and Local variables
Example #7 Timer test with flashing LED using interrupts  
Example #8 Single Hexadecimal Display on 7-segment LED  
  Common Cathode Display  
Example #9 Single Hexadecimal Display on 7-segment LED  
  Common Anode Display  
Example #10 Four multiplexed 7-segment Common Anode LED  
  Displaying decimal digits
Switch/Case statements
Example #11 Four multiplexed 7-segment Common Anode LED  
  Displaying 8-bit and 16-bit integers as hexadecimal digits  


Displaying Numeric Data

Eventually, there comes the necessity and challenge to be able to determine and confirm that your program is generating correct numerical results. This can be accomplished using the DEBUG feature of your IDE (Integrated Development Environment). Sometimes the DEBUG feature cannot be used when the test must be conducted under real time operating conditions.

Multiple LEDs may not be a choice if there are no remaining input/output pins available. The solution calls for a means of displaying text (ASCII characters) transmitted to a suitable text display such as an LCD module or a PC screen. This solution is particularly efficient since it requires a single output pin on the MCU, the SCI TXD signal (Serial Communications Interface - Transmit Data), pin-11 on the MC9S08QG8 chip.

We can display 8-bit data in binary as a sequence of zeros and ones. We will leave this as an exercise.

Example #9 - Single Hexadecimal Display on 7-segment COMMON ANODE LED

added - 2012.01.19

Seven-segment displays are available in COMMON ANODE and COMMON CATHODE configurations. Either style can be used in this example. We will show both configurations in order to make clear the differences, COMMON CATHODE in the previous example. In either case, current limiting resistors are used to limit the segment currents to safe operating levels. Without these resistors it is possible to exceed the LED currents resulting in blown displays.

Set up conditions: Remove the external 8MHz crystal and 22M-ohm resistor.



  #include <MC9S08QG8.h>

// for COMMON ANODE LED Display

// Declare global constants here
#define COUNT_LIMIT 61

// Declare global variables
byte Count, TimerCount;
byte Display[16];

void EnableInterrupts(void)

void Init(void)
SOPT1 = 0x52; // disable COP watchdog
PTBDD = 0b11111111; // set PTB0-7 as output

// initialize MTIM (8-bit timer module)
// XTAL = 8MHz
// BUSCLK = 4MHz
// divide by 256, period = 64us
MTIMCLK = 0b00001000;
MTIMSC = 0b01000000; // Start the timer with interrupts enabled

Display[0] = 0b00111111;
Display[1] = 0b00000110;
Display[2] = 0b01011011;
Display[3] = 0b01001111;
Display[4] = 0b01100110;
Display[5] = 0b01101101;
Display[6] = 0b01111101;
Display[7] = 0b00000111;
Display[8] = 0b01111111;
Display[9] = 0b01101111;
Display[0xA] = 0b01110111;
Display[0xB] = 0b01111100;
Display[0xC] = 0b00111001;
Display[0xD] = 0b01011110;
Display[0xE] = 0b01111001;
Display[0xF] = 0b01110001;


// example of ISR for Modulo Timer Overflow
void interrupt 12 TimerISR(void)
byte status;
// toggle PortB bits
// read Timer Status
status = MTIMSC;
// restart Timer
MTIMSC = 0b01000000;
if (++TimerCount >= COUNT_LIMIT)
TimerCount = 0;
// Count from 0 to 15 only
Count = ++Count & 0x0F;
// Send to 7-segment display
// active LOW for COMMON ANODE LED
PTBD = ~Display[Count];

void main(void)
Init(); // Initialize hardware

// Endless loop, nothing to do here
{ }


What are the differences between the two exercises, one for COMMON CATHODE and the second for COMMON ANODE LED displays?


  1. The COMMON pin is connected to GROUND.
  2. The outputs are ACTIVE HIGH, meaning that a HIGH voltage turns ON the LED segment.


  1. The COMMON pin is connected to +V.
  2. The outputs are ACTIVE LOW, meaning that a LOW voltage turns ON the LED segment. In this case the data output is inverted.

MC9S08QG8 Pin Diagram

Connect Vss to GND

Lab Manual Chapter 2

MC9S08 Resources

2006.11.09 - 2012.03.14