Page 369 - Programming Microcontrollers in C
P. 369

354    Chapter 7  Advanced Topics

                          Here the least significant byte of data is assigned to the smaller ad­
                          dress and the most significant byte goes to the larger address. Almost
                          all Motorola chips use big endian, and almost all Intel chips use little
                          endian. There can be some confusion when developing code to run
                          on one style of data storage on a machine with the opposite. This
                          problem is seen in the following program.
                   #include <stdio.h>


                   main()
                   {
                       unsigned array[25];
                       int i;

                       numbdup(“123456789098765”,array,25);
                       for(i=0;i<8;i++)
                        printf(“%x”,array[i]);
                       putchar(‘\n’);
                   }

                              Listing 7-2: Numeric Encode Test
                              If this program is compiled with a PC (Intel-based) compiler, the
                          result will not appear to be correct. However, if the program is com­
                          piled on an HC12, or 68HC16, or 683XX, or 68HC11, or 68HC05
                          compiler, it will seem to work correctly. In fact, both results are cor­
                          rect, only the numeric representation in memory is different.


            Numeric Decoding
                              Once the numeric data are encoded and stored, they must be de­
                          coded to be used by other parts of the program. The decode routine
                          is called putbcd(). This function is shown below.

                   void putbcd(char *s,char *number)
                   {
                       int c,i=0;
                       char *sa;
                       sa=s;
                       while(*sa!=’\0')
                       {
   364   365   366   367   368   369   370   371   372   373   374