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

 

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 #4 - Text Message

- added 2012.01.01

The purpose of this example is to output a text message on the LCD display. This example demostrates the use of strings and pointers.

  #include <MC9S08QG8.h>

// Declare constants here
#define HOME1 0x01
#define HOME2 0x02
#define CLRS 0x03

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

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(CLRS);
putc(HOME1);
puts("Hello World");
PTBD = 0b00111111; // set PTB0-5 high
Delay(6000);

putc(HOME2);
puts("PHYSICS 4D6");
PTBD = 0; // set PTB0-5 low
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;
}

void puts(char *s)
{
while( *s != 0 )
{
putc(*s++);
}
}
 

 


Characters, Strings and Pointers

- added 2012.01.13

A character is a single 8-bit entity. It is very important to note that a character is still fundamentally an 8-bit byte and it is always binary. Eight bits can represent 256 items. Which letters, numerals, punctuation marks etc. we choose to represent is entirely arbitrary and constitute the code table know as the ASCII code (American Standard Code for Information Interchange). The C compiler will automatically substitute ASCII code 65 or binary 01000001 when it sees 'A'.

More often we need to output many letters or what we call an array of letters in order to transmit and display words and sentences. We call an array of letters a string. In the memory of a computer, we provide storage for arrays or strings by reserving consecutive memory locations each of one byte in length.

The C statement below will reserve 20 bytes of memory space for the array label ch. Each individual character can be accessed by an index value from 0 to 19, for example ch[4].

char ch[20];

In practice, we may not require all 20 bytes for our message. For example, if our message is simply the two words "HELLO WORLD", we only use the first 11 bytes of storage, that is, bytes ch[0] to ch[10]. (Note that the ASCII code for blank space is 32 or binary 00100000). It is accepted practice in the C programming language to follow or terminate the message with a zero byte in order to indicate the end of the message. In this example, we would set ch[11] = 0.

Address Operator &

When we need to pass a string from one subroutine to another there is no need to send all the characters in the message. All we need to send is the location of the first character, i.e., the memory location or address of ch[0]. We can do this with the address operator &. Thus &ch is the address of ch[0] and hence the entire string.

Pointer

A pointer is a varible type, much like byte, char, int, that is intended to hold an address.

Let us create a pointer called m. m will be used to store the address of our string. We declare m as follows:

char *m;

This statement says that m is a pointer. The address that will be stored is a variable of character type. Now that m is defined, we can store a character or string address to m using:

m = &ch;

Dereferencing Operator *

The pointer variable m from above contains the addess of a single character or byte. If we were to examine or access this variable we would get a memory address. In order to access the single character or byte stored at this address we use the dereferencing or indirection operator * as follows:

this_byte = *m;

Hopefully, you will get to appreciate here the importance and usage of pointers. The C language has a simple syntax for adding 1 to a variable (more about this later). For example, the following

number++;

will increment the variable number by one. If we apply this syntax to a memory pointer, we would be able to access consecutive byte locations of memory:

this_byte = *m++;

This is the technique we use in the puts(char *s) subroutine shown in Example 4.

Note also in Example 4, we test the character *s for a zero byte in order to determine the end of the text message.

Note that there is a difference between ++m and m++. The first usage (++m) is called pre-increment. The variable is incremented by one before being used. The second form (m++) is called post-increment where the value of m is used before it is incremented.


MC9S08QG8 Pin Diagram

Connect Vss to GND


Lab Manual Chapter 2

MC9S08 Resources



2006.11.09 - 2012.03.14