UMBC | CMSC 391 -- Programming Microcontrollers |
Time is more difficult. You must know how many times each instruction will be executed. That can be more difficult. The each time a loop is executed adds to the time required. When you know the number of times in advance, you can to the calculations. That is no small matter. For the sake of simplicity, we will assume a known number of times through each loop. (The formal analysis of algorithms is a separate course!)
Once again, you get the number of cycles for each instruction and add up the cycles (based on the number of times each instruction is really executed.)
Now you can compare the two results. Which is more important, time or amount of memory? When you have enough of either, it does not matter. In embedded systems, you can assume that if the task is finished in the allotted time, memory is more important. It does not matter if you finish with 30 milliseconds remaining or 100 nanoseconds remaining, you finished in time! You never have enought memory!
Each machine cyles has six states on the 8051 (not true for all variants, though). It takes two clock pulses for each state. Two times six is 12. (This must be adjusted for the chip you are working with.)
cycle = states per cycle x pulses per state
The published formula for the time of each instruction is
Cyclesnumber required x 12 Timeinstruction = ------------------------- crystal frequency
Note: An 11.0592 megaherz (or multiple thereof) crystal yields a cycle frequency of 921.6 kilohertz, which can be divided evenly by the standard communications baud rates of 19200, 9600, 4800, 2400, 1200, and 300 herz.
;delay for a number of ms (specified by acc) delay: mov r0, a dly2: mov r1, #230 dly3: nop nop nop ;6 NOPs + DJNZ is 4.34 us nop ;with the 22.1184 MHz crystal nop nop djnz r1, dly3 ;repeat 230 times for 1 ms djnz r0, dly2 ;repeat for specified # of ms ret