Introducing the 68HC12

Download Report

Transcript Introducing the 68HC12

Example 9
Binary to ASCII String Conversion
Lecture L4.2
Steps for creating an ASCII number string
1234/10 = 123
Rem = 4
31
123/10 = 12
Rem = 3
32
12/10 = 1
Rem = 2
33
1/10 = 0
Rem = 1
34
PAD
Algorithms to convert a double number to an ASCII string
SHARP convert the next digit of a double number to ASCII
input: double number
output: double quotient after dividing by base
The digits are converted least significant digit first
and stored in memory starting at the end of the string.
Divide double number on data stack by base
If rem > 9 then add 7 to rem
Add '0' to rem to convert to ASCII
Store ASCII digit in string buffer
SHARPS convert all (remaining) digits in the double number
loop: call SHARP
repeat until double number quotient = 0
Example 9: C function calls for writing numbers to the LCD
C Function Call
Meaning
void write_int_lcd(int);
Display a 16-bit integer right-justified in a field of 5 digits
void write_long_lcd(long); Display a 32-bit integer right-justified in a field of 10 digits
00
40
14
54
01
41
15
55
02
42
16
56
03
43
17
57
04
44
18
58
05
45
19
59
06
46
1A
5A
07
47
1B
5B
08
48
1C
5C
09
49
1D
5D
0A
4A
1E
5E
0B
4B
1F
5F
0C
4C
20
60
0D
4D
21
61
0E
4E
22
62
Figure 7.3 Hex addresses of 20 x 4 LCD display
0F
4F
23
63
10
50
24
64
11
51
25
65
12
52
26
66
13
53
27
67
Example 9
// Example 9: Writing INTs and LONGs to LCD
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h" /* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
Example 9 (cont.)
int val16;
long val32;
void main(void) {
PLL_init();
// set system clock frequency to 24 MHz
lcd_init();
// enable lcd
val16 = 23456;
set_lcd_addr(0x0);
// display 5-digit int
write_int_lcd(val16);
val32 = 2345678123;
set_lcd_addr(0x40);
// display 10-digit long
write_long_lcd(val32);
val16 = 765;
set_lcd_addr(0x14);
// display 3-digit int
write_int_lcd(val16);
val32 = 23456;
set_lcd_addr(0x54);
write_long_lcd(val32);
// display 5-digit long
while(1) {
}
}