Page 280 - Programming Microcontrollers in C
P. 280
Timer Operations 265
31 0022 AE00 lds 0,x
32 0024 38 pulx
33 0025 39 rts
34 ; 6 }
35 ; 7
36 .public _count
37 .public _Register_Set
38 .external c_kents
39 .external c_ludv
40 .end
Note that this version differs from the first only in that the number
30000000 is not created in a declaration, but rather is created in line
when it is needed. This version requires 37 bytes of code.
The purpose of this exercise is to demonstrate that there are usually
many different ways that any part of a program can be approached.
If the programmer grabs the first idea and plods through without any
careful examination of the code being generated by the compiler, the
program will almost always suffer. The assembly listings of programs
should be examined, and where it seems as if the compiler is creating
clumsy code, a new approach should be considered. Writing C code
for a microcontroller is a joint exercise by the programmer and the
compiler to create the most efficient overall program.
Let’s return to the problem: we wish to set the speed of a motor and
the measurable control signal is the time of a revolution. Most servo type
devices work to position an output. Ours must set a speed which is a
different problem from most. The input will be an RPM which will be
converted to a time. Our system must compare the desired time with the
measured time and correct the speed to make the two times match. A
signal to drive the motor will be generated based on the desired time,
and this signal will be adjusted by the error signal calculated as the
difference between the measured time and the desired time. The following
pseudocode sequence will accomplish the desired operation.
FOREVER
{
do
convert to time;
calculate the required PWM output count;
apply calculated time error to PWM count;