View previous topic :: View next topic |
Author |
Message |
buchtsucht
Joined: 14 May 2010 Posts: 20
|
XLP: 5-bit DAC not working ?? |
Posted: Thu Nov 18, 2010 1:19 am |
|
|
Has anyone tried to run the 5-bit-DAC inside the new XLP PICs?
My 16LF1823 don't want to do anything. Output is everytime 0,0V at PIN12 = DACOUT (no external load, debugger or programmer - only scope).
I tried several settings for PORT A, several clock settings and several settings for #use delay.
Code: |
#include <16LF1823.h>
#include <STDLIB.h>
#fuses INTRC,NOMCLR,NoBROWNOUT,PUT,NoIESO,NOCPD,NOPROTECT
#use fast_IO(A)
//#USE DELAY (clock=32k)
//#use delay(clock=32k,Aux: crystal=32k, int=32k)
#use delay(clock=4M, int=4M)
int x;
Main() {
setup_oscillator(OSC_4Mhz|OSC_INTRC);
output_a(0b11111100);
set_tris_A(0b00111011);
setup_dac(DAC_VDD | DAC_OUTPUT);
while(1){
delay_cycles(20);
dac_write(x);
++x;
if(x>31) x=0;
}
}
|
It seems, that you can't do anything wrong:
Datasheet says "Selecting the DAC reference voltage for output
on the DACOUT pin automatically overrides the digital output buffer
and digital input."
Registers in 16LF1823.h seem to be O.K.:
Code: |
////////////////////////////////////////////////////////////////// DAC
// Digital to Analog Functions: SETUP_DAC(), DAC_WRITE()
// Constants used in SETUP_DAC() are:
#define DAC_OFF 0 // off
#define DAC_VDD 0x80 // 100% = Ubat
#define DAC_VREF 0x81 // Voltage at ref. - PIN
#define DAC_VR 0x82 // internal voltage reference
#define DAC_OUTPUT 0x40 // DAC switched to PIN |
"#use delay" seems to be not very important, I can't find anything time relevant in datasheet and following post:
http://www.ccsinfo.com/forum/viewtopic.php?p=137620 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 18, 2010 1:25 pm |
|
|
All these features are version dependent. Always post your compiler
version. |
|
|
buchtsucht
Joined: 14 May 2010 Posts: 20
|
|
Posted: Thu Nov 18, 2010 10:23 pm |
|
|
Yes, this is true: compiler is CCS 4.106. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 19, 2010 2:20 pm |
|
|
Your version (vs. 4.106) has several things wrong with the setup_dac()
and dac_write() functions. The register addresses are swapped for
the dac registers and the constants in the 16F1823.h file are not correct
or complete.
I made a test program with some work-arounds to fix it for you.
I've used the dac constants from the 16F1823.h file for vs. 4.114 as
shown below. I've also made two macros to re-define the setup_dac()
and dac_write() functions so they will write to the correct register
addresses. I don't have a 16F1823 to test, but I think this program
will probably work:
Code: |
#include <16F1823.h>
#fuses INTRC_IO,NOWDT,BROWNOUT,PUT
#use delay(clock=4000000)
// Constants used in SETUP_DAC() are:
#define DAC_OFF 0
#define DAC_VSS_VDD 0x80
#define DAC_VREF_VDD 0x81
#define DAC_VSS_VREF 0x84
#define DAC_VREF_VREF 0x85
#define DAC_VSS_FVR 0x88
#define DAC_FVR_VREF 0x89
// The following may be OR'ed in with the above using |
#define DAC_OUTPUT 0x20
#define DAC_LVP_POS 0x40
#define DAC_LVP_NEG 0
#byte DACCON0 = 0x118
#byte DACCON1 = 0x119
#define setup_dac(value) DACCON0 = value
#define dac_write(data) DACCON1 = data
//===========================
void main(void)
{
int8 i;
// Turn on the DAC and enable output on the DACOUT pin, and use
// the Vdd and Vss as the limits for the DAC output voltage.
setup_dac(DAC_OUTPUT | DAC_VSS_VDD);
// Create a sawtooth waveform by ramping up the DAC
// from 0 to 31, one step at a time, continuously.
i = 0;
while(1)
{
for(i=0; i< 32; i++)
dac_write(i);
delay_us(10);
}
while(1);
}
|
|
|
|
buchtsucht
Joined: 14 May 2010 Posts: 20
|
|
Posted: Thu Nov 25, 2010 5:07 am |
|
|
wow! Thank you!!!
I had some other problems, but I will test it the next days and tell you if it works! |
|
|
buchtsucht
Joined: 14 May 2010 Posts: 20
|
|
Posted: Tue Dec 07, 2010 1:29 am |
|
|
Thanks PCM programmer, it works perfect without any problems - exactly as you wrote it! |
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Wed Apr 20, 2011 4:15 am |
|
|
I want to get the Vref voltage of 4.096 out of the processor, I thought it was the easiest to use the DAC. Because I also use V4.106 (the maintainance renewal is in progress) I tried PCM programmer's code and it's working, I modified it a bit, but when I try to change the SETUP_DAC() into the Vref it doesn't work anymore. What is wrong??
Code: |
#include <12F1822.H>
#FUSES INTRC_IO, NOMCLR, NOCPD, NOWDT, NOBROWNOUT, NOPUT, NOPROTECT, NOFCMEN, NOIESO
#use delay(clock=4000000)
// Constants used in SETUP_DAC() are:
#define DAC_OFF 0
#define DAC_VSS_VDD 0x80
#define DAC_VREF_VDD 0x81
#define DAC_VSS_VREF 0x84
#define DAC_VREF_VREF 0x85
#define DAC_VSS_FVR 0x88
#define DAC_FVR_VREF 0x89
// The following may be OR'ed in with the above using |
#define DAC_OUTPUT 0x20
#define DAC_LVP_POS 0x40
#define DAC_LVP_NEG 0
#byte DACCON0 = 0x118
#byte DACCON1 = 0x119
#define setup_dac(value) DACCON0 = value
#define dac_write(data) DACCON1 = data
void main(void)
{
setup_vref(VREF_4v096);
setup_dac(DAC_OUTPUT | DAC_VREF_VREF);
while(1)
{
dac_write(31);
delay_us(10);
}
while(1);
}
|
|
|
|
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Wed Apr 20, 2011 7:38 am |
|
|
Code: | #bit CDAFVR0 = 0X117.2
#bit CDAFVR1 = 0X117.3
#bit FVREN = 0X117.7
#bit DACPSS0 = 0X118.2
#bit DACPSS1 = 0X118.3
CDAFVR0 = 1;
CDAFVR1 = 1;
FVREN = 1;
DACPSS0 = 0;
DACPSS1 = 1;
|
Looking at the registers in the MPLAB simulator I noticed that a few bits aren't set at the right value. Put them manual on the right value solved my problem. |
|
|
|