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

 

2011.12.31    
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  
  Watchdog
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
Stacks
Subroutines
 
     
Example #7 Timer test with flashing LED using interrupts  
  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  
     
     
     
     

 


Example #3 - Character Output

The purpose of this example is to output the letter 'A' once a second on the LCD display. Data is transmitted in the coded ASCII representation to the serial port at 9600 bits per second. The LEDs will continue to flash as in the previous example.

Skip this example if you have no means of displaying ASCII text sent via a TTL or RS-232 channel.

  #include <MC9S08QG8.h>

// Declare function protoypes here.
void Delay(unsigned long delay);
void putc(char ch);

void main(void)
{
SOPT1 = 0x52; // disable COP watchdog
PTBDD = 0b00111111; // // set PTB0-5 as output

// select external xtal
ICSC1 = 0b10000000;
ICSC2 = 0b00110110;

// initialise SCI for serial output
SCIBDH = 0;
SCIBDL = 26;
SCIC2 = 0x0C;

for(;;)
{
putc('A');
PTBD = 0b00111111; // pulse PTB0-5 high
Delay(6000);
PTBD = 0;
Delay(6000);
}
}

void Delay(unsigned long delay)
{
unsigned long i;
for (i=0; i<= delay;i++)
{
}
}

void putc(char ch)
// output one ASCII char to SCI TD (pin 11)
{
while (SCIS1_TDRE == 0);
SCID = ch;
}
 

Serial Transmit Data

Before sending data on the SCI (Serial Communications Interface, another term for UART), the transmitter flag must be checked to see if the transmitter is ready to accept new data. We have to wait for TDRE (Transmit Data Register Empty) to be 1 before writing the new data to the SCI Data register. The while( ) statement shown will loop back to itself as long as the TDRE bit is 0. Note that this is an optimizing compiler and it will select the most efficient asm code for this instruction. Disassemble the C code and learn how this is translated into asm code.

while (SCIS1_TDRE == 0);
SCID = ch;


LCD Display

- added 2012.01.01

The 2x16 LCD character display is a popular display using the commonly used Hitachi HD44780 Controller/Driver chip. The controller is designed to be used for 80-character displays in many popular configurations, for example, 1x16, 2x16, 2x20, 2x40, 4x16, 4x20.

The 2x16 display format actually uses the 2x40 format. Hence the first 16 characters will appear on the first line. The next 24 characters will not be visible. The second 40 characters will be assigned to the second line, only the first 16 characters being visible.

More information on interfacing directly to the LCD module will be provided at a later date.


Function Prototypes

What is a function prototype? Compilers can be designed as either a single-pass or muliple-pass compiler. In a single-pass compiler, the compiler needs to scan the program code only once and all variables and function calls can be resolved. In order to accomplish this, the program must provide information such as type of variable or function ahead of usage. In a multiple-pass compiler, the compiler can resolve unknown variables and usage on subsequent scans through the program code.

In a single-pass compiler, the program must declare the type of variables and the format of functions, i.e. how many parameters are passed to the function and what results are returned. This is the function prototype. The prototype is the same format as the header of the function and must end with a semi-colon. The void statement indicates that no parameter is passed. Note that the variable names are dummy names and are not important.

 

//Variable Declarations

char my_char;
char[20] my_message;
unsigned int counter, number;
byte hi_byte, lo_byte;
byte[40] my_data;


// Function Protoypes

void MyInit(void);
void Delay(unsigned long d);
void putc(char ch);
void message(char *string);
char getc(unsigned int COM);
double square_root(double x);

 

 


MC9S08QG8 Pin Diagram

Connect Vss to GND


Lab Manual Chapter 2

MC9S08 Resources



2006.11.09 - 2012.03.14