Page 264 - Programming Microcontrollers in C
P. 264
Timer Operations 249
The instruction
OC1D.OC1D5=ON;
indicates that when OC1 occurs, OC3 should be turned on. The next
instruction
TCTL1.OL3=ON;
causes OC3 to toggle when its time expires, and the instruction
PSCTL1.DDRA7=ON;
sets the DDRA7 bit so that signals from OC1 will be sent to the output
pin PA7. TOC1 and TOC3 are initialized by the next two instructions.
TOC1 is given a value of the contents of period greater than the
contents of the timer counter register TCNT. TOC3 is set equal to the
contents of TOC1 plus the time_on.
The routine then enters an endless loop. Within this loop, two
flags are examined and if they are set, they are reset. Also, new times
are calculated for when the next output compares should occur. The
OC1F flag in TFLG1 is set whenever an Output Compare 1 occurs.
This flag must then be reset. The instruction sequence
if(TFLG1 & OC1F)
{
TFLG1 = OC1F;
.
.
will accomplish the required assembly instructions to test the bit and
reset it if the bit is turned on.
The time of the next OC1 is calculated as TOC1+period. In
this particular instance, the bit OC1D.OC1D7 is complemented so
that the output observed on PA7 will toggle with the occurrence of
each OC1. TFLG1.OC3F is tested to determine if an Output Compare
3 has happened. If it has, this bit is reset, and TOC3 is set to a new
value of TOC1+time_on. With this setup, OC3—PA5—will go on
when each OC1 occurs, and will be reset an amount corresponding
to time_on after it goes on. Therefore, the period of this system
can be set and the on time can be any value less than the period.
Normally, the operation of a PWM is to generate an analog signal
that is present continuously. The above program does perform this