THE DESIGN CYCLE
have to do is count down when the DAC0 data registers
get loaded with 0xFFF instead of resetting the DAC0 data
registers to 0x000 at that point. As it turned out, to get a
triangle wave to appear on the Cleverscope graph window,
I only had to add some triangle code to the Timer 2
interrupt handler:
static void tmr_isr (void) interrupt 5
{
//Triangle
if(phase == 0)
{
if(++low_counter == 0)
{
if(++high_counter == 0x10)
{
phase = ~phase;
high_counter = 0x0F;
low_counter = 0xFF;
}
}
Once again, I let the Configuration Wizard 2 application do the dirty work of initializing Comparator 1:
void Comparator_Init()
{
unsigned int i;
SFRPAGE = CPT1_PAGE;
CPT1CN = 0x80; // enable Comparator 1
for (i=0;i<60;i++) // Wait 20us for init
;
CPT1CN &= ~0x30;
CPT1MD = 0x30;
// clear the interrupt flags
// set mode to fastest
}
DAC1 does not need to do anything fancy. So, I simply
enabled it and it will only update its data registers on a write
to DAC1H (done by loading DAC1CN (DAC1 Control
Register with 0x80). Now all I need to do is turn the
sawtooth waveform loose and write the +1.2V voltage value
to the DAC1 data registers:
}
else
{
if(—low_counter == 0xFF)
{
if(—high_counter == 0xFF)
{
phase = ~phase;
high_counter = 0x00;
low_counter = 0x00;
void main()
{
Init_Device();
low_counter = 0x00;
high_counter = 0x00;
SFRPAGE = DAC1_PAGE;
DAC1L = 0xFF;
DAC1H = 0x07;
while(1);
}
}
}
}
DAC0L = low_counter;
DAC0H = high_counter;
}
The triangle interrupt handler code depends on the
value of the phase variable to determine whether it needs
to increment or decrement the value being fed to the
DAC0 data registers. This country boy guessed right again
as you can see in the Cleverscope display shown in
Screen Shot 4. We’re not done with DACs yet. I’ve got
another idea.
Take a look at Screen Shot 5. The modulated square
wave is actually the output of Comparator 1. As the
sawtooth wave voltage passes through +1.2V, the
Comparator 1 output trips from low to high and remains
high as long as the sawtooth wave voltage is above
+1.2V, which is what we dialed into Comparator 1 as its
reference voltage. If I load DAC1’s data registers with a
larger value, Comparator 1’s reference voltage will also
DARE TO COMPARE
What if I fired up DAC1 and set it to generate +1.2V
by writing 0x7FF to the DAC1 data registers, used the
DAC1 voltage as a reference voltage for Comparator 1,
and fed Comparator 1 with the sawtooth wave from
DAC0? I should get a square waveform that is modulated
by the Comparator 1 reference voltage versus the voltage
of the sawtooth waveform. Let’s code it up and see if
I’m right.
■ SCREEN SHOT 6. You can see that as I increase Comparator
1’s reference voltage, the high portion time of the
Comparator 1 output square waveform grows smaller. By
sweeping DAC1, we can create a PWM signal at the output
of Comparator 1.
June 2007 89