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.
• 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.
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.
• 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
• 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.
• 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:
• 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
• 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
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.
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.
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: