4.3 DAC Output as an Analog Comparator Input
This configuration involves the AC and DAC peripherals, where the PIC 8-bit microcontroller has the flexibility to route the DAC output to the noninverting input of AC, internally.
The comparator register CMxPCH must be used for selecting the DAC output as a noninverting channel. The polarity of the comparator output can be inverted by setting the CxPOL bit. Clearing the CxPOL bit results in a noninverted output. The CxPOL bit belongs to register CMxCON0 and the output of the comparator is listed in following table:
Input Condition | CxPOL | CxOUT |
---|---|---|
VIN+ > VIN- | 0 |
0 |
VIN+ < VIN- | 0 |
1 |
VIN+ > VIN- | 1 |
1 |
VIN+ < VIN- | 1 |
0 |
Figure 4-10 and Figure 4-11 show how the MCC can be used to generate code for this particular configuration.
void DAC1_Initialize(void)
{
// DAC1EN enabled; NSS VSS; PSS VREF+; OE1 disabled; OE2 disabled;
DAC1CON0 = 0x84;
// DAC1R 8;
DAC1CON1 = 0x08;
}
void CMP1_Initialize(void)
{
PIE2bits.C1IE = 0; // C1HYS disabled; C1EN enabled; C1POL not inverted; C1SYNC asynchronous;
CM1CON0 = 0x80;
CM1CON1 = 0x02; // C1INTN no_intFlag; C1INTP intFlag_pos
CM1NCH = 0x07; // NCH Vss;
CM1PCH = 0x05; // PCH DACOUT;
PIR2bits.C1IF = 0; // Clearing IF flag before enabling the interrupt.
PIE2bits.C1IE = 1; // Enabling CMP1 interrupt.
}