0% found this document useful (0 votes)
3 views20 pages

Chapter 7 PDF

chapter 7 of the book worked
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Chapter 7 PDF

chapter 7 of the book worked
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

HCS12 Programming in C

Dec Hex Bin


7 7 00000111

ORG ;Chapter Seven


OBJECTIVES (cont)
this chapter enables the student to:

• Examine C data types for the HCS12.


• Code C programs for time delay and I/O operations.
• Code C programs for I/O bit manipulation.
• Code C programs for logic & arithmetic operations.
• Code C programs for ASCII & BCD data conversion.
• Code C programs for binary (hex) to decimal
conversion.
• Code C programs for data serialization.
• Understand C compiler RAM and ROM allocation.

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.0 Why Program the HCS12 in C?

• Compilers produce hex files we download into the


ROM of the microcontroller, but size of the hex file is
a concern of programmers for two reasons:
– microcontrollers have limited on-chip ROM
– code space for the HCS12 is limited to 512K
• Some major reasons for writing programs in C:
– easier/less time consuming to write in C than Assembly
– C is easier to modify and update
– available function code libraries
– C code is portable to other microcontrollers with
little or no modification
HCS12 Microcontroller and Embedded Systems:
Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.0 Why Program the HCS12 in C?

• Several third-party companies develop C compilers


for the HCS12 microcontroller.
– for this book we have chosen Freescale’s C
compiler to integrate with CodeWarrior IDE
• Freescale has a student version of the C compiler
available for download from their website.
– for tutorials see http://www.microdigitaled.com
• A goal of C programmers is to create smaller hex
files, so it is worthwhile to re-examine C data types.

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.1 Data Types and Time Delays in C

• Table 7-1 shows data types/sizes for the HCS12.

Table 7-1 Some Data Types Widely Used by C

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.1 Data Types and Time Delays in C
char

• Unsigned char - character data type is the most


natural choice for many applications.
– 8-bit data with value in the range of 0–255 (00–FFH)
– one of the most widely used data types for the HCS12
• Signed char - 8-bit data using the most significant
bit (D7 of D7–D0) to represent the – or + value.
– only 7 bits for the magnitude of the signed number
giving values from –128 to +127.

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.1 Data Types and Time Delays in C
int

• Unsigned int - 16-bit data with value in the range


of 0 to 65,535 (0000–FFFFH).
– in HCS12, unsigned int is used to define 16-bit
variables such as memory addresses
– as registers & memory accesses are 8-bit chunks,
misuse of int variables will result in a larger hex file
• Signed int - 16-bit data using the most significant
bit (D15 of D15–D0) to represent the – or + value.
– as a result, only 15 bits are available for the magnitude
of the number, or values from –32,768 to +32,767

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.1 Data Types and Time Delays in C

• Other data types - unsigned int is limited to values


0–65,535 (0000–FFFFH).
– the C compiler supports both short long and long
data types, if we want values greater than 16-bit
• Time delay - two ways to create a time delay in C:
– using a simple for loop
– using the HCS12 timer
• In either case, an oscilloscope is used to measure
the duration of time delay.

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.2 Logic Operations in C
bit-wise

• Bit-wise operators in C - widely used in software


engineering for embedded systems and control.

Table 7-2 Bit-wise Logic Operators for C

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.2 Logic Operations in C
bit-wise

• Bit-wise shift operators in C - two bit-wise shift


operators:
– shift right ( >>) and shift left (<<)
• Their format in C is as follows:
– data >> number of bits to be shifted right
– data << number of bits to be shifted left
• Examples of shift operators in C:

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.3 Data Conversion Programs in C
ASCII

• On ASCII keyboards, when the key “0” is activated,


“011 0000” (30H) is provided to the computer.
– similarly, 31H (011 0001) is provided for key “1”, etc.
• BCD numbers are
universal, ASCII is
standard in the US
and other countries.
• Keyboards, printers, &
monitors all use ASCII.
– data must be converted
from ASCII to BCD Table 7-3 ASCII and BCD Codes for Digits 0–9

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.3 Data Conversion Programs in C
packed BCD to ASCII

• Packed BCD to ASCII conversion - to convert


packed BCD to ASCII, it must first be converted to
unpacked BCD.
– the unpacked BCD is tagged with 011 0000 (30H)

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.3 Data Conversion Programs in C
ASCII to packed BCD

• ASCII to packed BCD conversion - first convert it


to unpacked BCD (to get rid of the 3), then combine
to make packed BCD.
• For example, for 4 and 7 the keyboard gives back
34 and 37; the goal is to produce 47h.
– or “0100 0111”, which is packed BCD

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.3 Data Conversion Programs in C
checksum

• Checksum byte in ROM - to ensure the integrity


every system must perform a checksum calculation
to detect any corruption of the contents of ROM.
– a cause of ROM corruption is current surge, either
when the system is turned on, or during operation
• To ensure data integrity in ROM, the checksum
process uses what is called a checksum byte.
– an extra byte tagged to the end of a series of data bytes
• A checksum generation and testing program was
given in Program 6-1.

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.3 Data Conversion Programs in C

• Binary (hex) to decimal/ASCII conversion in C -


printf function is part of the standard C I/O library.
– does many things, including converting data from
binary (hex) to decimal, or vice versa
– printf takes a lot of memory space increases
hex file size substantially
– it is better to write your own conversion function
• One of the most widely used conversions is binary
to decimal conversion.

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.4 I/O Bit Manipulation and Data
Serialization in C
• I/O ports of HCS12 are bit-addressable.
– AND and OR bit-wise operators access a single bit
of a port
– DDRx registers in the same way
• Serializing data in C - is a way of sending a byte
of data one bit at a time through a single pin of a
microcontroller.
– using the serial port.
– transferring data one bit a time and controlling the
sequence of data and spaces between them

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.5 Program ROM Allocation in C

• In the HCS12 are two memory address spaces in


which to store data:
– several kilobytes of data RAM space.
– several hundred kilobytes of code (program) space
• Allocating program space to data - to make the
C compiler use the program (code) ROM space for
the fixed data, we use the keyword const as shown
in the following lines of C code:

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.5 Program ROM Allocation in C

• The following code shows how to use program


space for data in C:

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
7.5 Programmable ROM Allocation in C

• Putting Data in Specific ROM Addresses: to


place data (constants, strings, and look-up tables)
at a specific address of program ROM, use the @
symbol directive:

HCS12 Microcontroller and Embedded Systems:


Using Assembly and C with CodeWarrior © 2010 Pearson Higher Education, Inc.
By Muhammad Ali Mazidi and Danny Causey Pearson Prentice Hall - Upper Saddle River, NJ 07458
HCS12 Programming in C

Dec Hex Bin


7 7 00000111

END ;Chapter Seven

You might also like