My expectation may be wrong, but I am trying to use an external Vref value to modify the range of output values produced by the DAC module. I’m using a PIC16F1503 with Vdd = 5V so voltage increments are around 0.16V through the range 0-5V when I use Vdd as the reference for DAC. I am trying to get a finer granularity in the voltage levels by using an external 2.5V reference and was expecting to get voltage increments of around 0.08V between 0 and 2.5V.
However, when I provide the 2.5V input, I still see the increments and range associated with the 5V Vdd reference.
In order to use the external Vref, my understanding is that the only configuration required is to set the input pin (RA1) as analogue input, and set the DACPSS bit in DACCON0. Perhaps I have misunderstood the purpose of using an external Vref with DAC? Either that, or my configuration is incorrect. I have included my assembly code for scrutiny. To add some context, the DAC outputs are joined to provide sufficient current to drive the gate of an N channel MOSFET that switches a string of christmas lights on/off with varying intensities. The program below simply increases and decreases the intensity of the lights by driving the MOSFET gate with voltages between the range of Vref+ and Vref-. The problem is that the voltage range is always between 0 and 5V and the associated increments mean that the MOSFET goes from fully off to fully on in just a few steps. By modifying the voltage range I was hoping to get finer increments in the voltage, which would result in a more subtle change in light intensity.
Thanks for any advice on what I might be doing wrong.
; TestDAC: Code to test the Digital-to-Analog Converter module library
;
;
; Pin summary
; 1 VDD +3.3V
; 8 RC2 Red LED
; 11 RA2 Output from DAC
; 12 RA1 External Vref+
; 13 RA0 Output from DAC
; 14 VSS Ground
;
; Assembled with pic-as (v2.32) under MPLAB X IDE (v6.15) 20 May 2024
;
; Add this line in the project properties box, pic-as Global Options -> Additional options:
; -Wa,-a -Wl,-pPOR_Vec=0h,-pISR_Vec=4h
;
; PIC16F1503
; +----------:_:----------+
; +3.3V <> 1 : VDD VSS : 14 <> GND
; <> 2 : RA5 RA0 : 13 <> DAC output
; <> 3 : RA4 RA1 : 12 <> VRef
; <> 4 : RA3/MCLR RA2 : 11 <> DAC output
; <> 5 : RC5 RC0 : 10 <>
; <> 6 : RC4 RC1 : 9 <>
; <> 7 : RC3 RC2 : 8 <> Red LED
; +-----------------------:
; DIP-14
PAGEWIDTH 132
RADIX DEC
#include <xc.inc>
#include "macros.inc"
; See respective data sheet for additional information on configuration word.
config FOSC = INTOSC ; Oscillator Selection bits (HS oscillator)
config WDTE = OFF ; Watchdog Timer (WDT disabled)
config PWRTE = OFF ; Power-up Timer Enable bit (Power-up Timer is disabled)
config CP = OFF ; Code Protection bit (Code protection disabled)
config MCLRE = ON
config BOREN = ON
config CLKOUTEN = OFF
config WRT = OFF
config STVREN = OFF
config LVP = OFF
config LPBOR = OFF
config BORV = LO
#define TMR0_ROLLOVER_COUNT 63 ; this gives close to 1 second interval
#define INCREMENTING_BIT 0
#define A_STRING_ON 1
#define UPPER_VALUE 31
#define LOWER_VALUE 0
TOGGLE_RED_LED MACRO
banksel LATC
movlw LATC_LATC2_MASK ; set bit for RC2
xorwf LATC, f
ENDM
DO_DELAY MACRO
call delay50ms
ENDM
;**********************************************************************
; Power-On-Reset entry point
;**********************************************************************
PSECT POR_Vec,global,class=CODE,delta=2
global start
start:
goto main
;objects in Common RAM - address 70h
psect udata_shr,global,class=COMMON,space=1,delta=1,noexec
programStatus: DS 1
d1: DS 1
d2: DS 1
d3: DS 1
currentVoltage: DS 1
flashCounter: DS 1
tmr0RolloverCount: DS 1
tmr0CountIndex: DS 1
tmr0CountValue: DS 1
tmr0SecondsCount: DS 1
TMR1H_val: DS 1
TMR1H_index: DS 1
WRegCopy: DS 1
shiftBuffer: DS 1
;**********************************************************************
; Interrupt vector and handler
;**********************************************************************
PSECT ISR_Vec,global,class=CODE,delta=2
global ISR_Vec
ISR_Vec:
retfie
END_ISR_Vec:
PSECT MainCode,global,class=CODE,delta=2
initialisation: ; setup peripherals, start timer
; clrf programStatus
call setupOscillator
call setupIOPins
call DAC_init_Vref
return
setupOscillator:
; initialise internal oscillator to 16MHz
banksel OSCCON
movlw 01111000B ; Int. osc. 16 MHz
movwf OSCCON
btfss HFIOFR ; Int. osc. running?
goto $-1 ; No, loop back
btfss HFIOFS ; Osc. stable?
goto $-1 ; No, loop back
return
setupIOPins:
banksel ANSELA
clrf ANSELA ; set all PORTA pins to digital I/O
bsf ANSELA, ANSELA_ANSA1_POSN ; set RA1 analogue
clrf ANSELC ; set all PORTC pins to digital I/O
banksel TRISA ; set all PORTA pins output
clrf TRISA
bsf TRISA, TRISA_TRISA1_POSN ; set RA1 input
banksel TRISC
clrf TRISC ; set all PORTC pins as output
banksel LATA
clrf LATA ; set all PORTA pins output low
clrf LATC ; set all PORTC pins output low
; set H-Bridge A-string on
bsf LATC, LATC_LATC0_POSN
return
DAC_init_Vref:
banksel DACCON0
movlw 10110100B
;1------- DACEN - enable bit
;-x------ unimplemented
;--1----- DACOE1 - output on RA0 - use both to provide sufficient current
;---1---- DACOE2 - output on RA2
;----x--- unimplemented
;-----1-- DACPSS - use RA1 as Vref
;------xx unimplemented
movwf DACCON0
return
;**********************************************************************
; main program
;**********************************************************************
main:
call initialisation
movlw LOWER_VALUE
movwf currentVoltage
bsf programStatus, INCREMENTING_BIT ; start incrementing
call DAC_enable
loop:
TOGGLE_RED_LED
btfss programStatus, INCREMENTING_BIT
goto decrementing
; value being incremented
movf currentVoltage, w
call DAC_write
incf currentVoltage, f
; check if value has passed upper limit
movlw UPPER_VALUE + 1
subwf currentVoltage, w
skipz
goto doTheDelay
; reset the value
movlw UPPER_VALUE - 1
movwf currentVoltage
bcf programStatus, INCREMENTING_BIT ; start decrementing
goto doTheDelay
; value being decremented
decrementing:
movf currentVoltage, w
call DAC_write
decf currentVoltage, f
; test if value has gone 'negative'
movlw 11111111B ; LOWER_VALUE - 1
subwf currentVoltage, w
skipz
goto doTheDelay
movlw LOWER_VALUE + 1
movwf currentVoltage
bsf programStatus, INCREMENTING_BIT ; start decrementing
doTheDelay:
call doDelay
goto loop
doDelay:
; call delay500ms
call delay1s
return
turnOnAString:
bsf programStatus, A_STRING_ON
bsf LATC, LATC_LATC0_POSN
bcf LATC, LATC_LATC1_POSN
return
DAC_enable:
banksel DACCON0
bsf DACCON0, DACCON0_DACEN_POSN
return
; 8 bit value to write is in WREG on entry.
; NB: Only DACR<4:0> bits are used to set the output voltage Dec 1-15
DAC_write:
banksel DACCON1
movwf DACCON1
return
delay1s:
movlw 0x23
movwf d1
movlw 0xB9
movwf d2
movlw 0x09
movwf d3
delay1s_0:
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto delay1s_0
goto $+1
return
END start