I'm trying to get the DAC of the PIC16F1704 to work, specifically I want it to output on pin RA2/DAC1OUT2. There's nothing connected to that pin, and I've reduced the test code to this:
#define _XTAL_FREQ 4000000
#define FCY 4000000ULL
#include <htc.h>
__CONFIG (FOSC_INTOSC & CP_OFF & MCLRE_OFF & BOREN_OFF & WDTE_OFF);
__CONFIG (PLLEN_OFF & LVP_OFF & ZCDDIS_ON);
void main(void)
{
WPUA = WPUC = 0; // disable weak pull-ups
nWPUEN = 1;
ZCD1CON = 0; // disable Zero-cross detect module
SLRCONA = SLRCONC = 0; // disable slew-rate limiting
ANSELA = 0xff; // all analog
TRISA = 0b00111111; // all inputs
OSCCON = 0b01101011; // 4 MHz internal oscillator, no PLL
DAC1CON0 = 0b10010000; // DAC on, Vref+ = Vdd, Vref- = Vss, output on DAC1OUT2
DAC1CON1 = 0; // Tap #0: output 0.0V on DAC1OUT2
while (1) {
__delay_ms(10);
DAC1CON1++;
}
}
This should output a ramp 0..Vdd in about 2.56 seconds, but instead what I see is this:
My Vdd is 3.3V. The skewed and inverted ramp is from 1.0 down to 0.4 V, with a plateau around 0.8V.
The problem seems to be with that particular pin, not the DAC itself. If I change the DAC config to
DAC1CON0 = 0b10100000;
I.e., output on DAC1OUT1 instead (RA0), I can read the ramp on that pin, it is perfectly linear, and not inverted.
It seems there is some peripheral that tries to keep pin RA2 on 0.8 volts, and does it in a weird non-linear fashion.
Any ideas what might this be? For completeness, here's what this pin is multiplexed with:
The Zero-cross detect circuitry fits the bill quite well, but as you can see, I've explicitly disabled it (btw changing the ZCDDIS_ON to ZCDDIS_OFF doesn't change anything).
The compiler is XC8 (1.31 I think) and I'm programming the chip with PicKit2.



Anyway, this seems like a remote possibility, since the similar approach with the device file editor worked before for other parts, but I'll borrow a PicKit3 to verify.
– anrieff Dec 22 '16 at 22:20TRISA &= 0x04;) Worth a shot if you haven't tried it. – slightlynybbled Feb 17 '17 at 19:24