Page 180 - Programming Microcontrollers in C
P. 180

Microcontroller Memory    165

                          code, 0xd7, to do a store the accumulator indexed with a 16-bit offset
                          is created at the location 0x56 in memory. The 16-bit offset is the
                          address passed to the function in the combination of the x register and
                          the accumulator. This address is placed in the memory locations 0x58
                          and 0x57, completing the store instruction. At the address 0x59, a re­
                          turn from subroutine instruction, 0x81, is placed to complete the
                          function. The index register is cleared, and this two-instruction sub­
                          routine is executed to store the appropriate data prior to the program
                          setting the latch bit.

                       void erase(int* x)
                   0052 {
                   0815 BF 52 STX $52
                   0817 B7 53 STA $53
                   0819 12 07 BSET 1,$07 EEPROM_CTL.E1LAT=1;
                   081B 14 07 BSET 2,$07 EEPROM_CTL.E1ERA=1;
                   081D B7 58 STA $58 *x=0;
                   081F 9F        TXA
                   0820 B7 57 STA $57
                   0822 4F        CLRA
                   0823 AE D7 LDX #$D7
                   0825 BF 56 STX $56
                   0827 AE 81 LDX #$81
                   0829 BF 59 STX $59
                   082B 5F        CLRX
                   082C BD 56 JSR $56
                   082E 10 07 BSET 0,$07 EEPROM_CTL.E1PGM=1;
                   0830 5F        CLRX delay(PROG_TIME);
                   0831 A6 0A LDA #$0A
                   0833 CD 00 00 JSR $****
                   0836 13 07 BCLR 1,$07 EEPROM_CTL.E1LAT=0;
                   0838 81        RTS }
                              This code sequence requires 36 bytes, plus 4 bytes of uncommit­
                          ted RAM space, to accomplish what required 22 bytes in the earlier
                          example of the same operation.
                              You must not avoid the use of pointers because of this one ex­
                          ample. There are cases when proper pointer usage will provide the
                          best code that you can generate.  When writing code for
                          microcontrollers, use many relatively small functions that you can
   175   176   177   178   179   180   181   182   183   184   185