CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Sorted: DAC not working on PIC16F753

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
duncangray



Joined: 27 Apr 2007
Posts: 14
Location: UK

View user's profile Send private message

Sorted: DAC not working on PIC16F753
PostPosted: Fri Nov 04, 2016 5:13 am     Reply with quote

Good morning CCS Forum,
Yet again I am trying something new and failing. Please can someone steer me in the right direction.

I want to make an approximation of a sine wave generator using a PIC DAC and my starter is supposed to be a ramp generator using the 16F753. Compiler is 5.062 and my code is
Code:
#include <16F753.h>
#use delay(internal=8000000)

void main()
{
   int8 value;
   setup_dac(DAC_VDD | DAC_OUTPUT);
   setup_opamp1(OPAMP_ENABLED | OPAMP_NI_TO_DAC);
   while(TRUE)
   {
      dac_write(value++);
      delay_ms(1);
      output_toggle(PIN_C2);
   }
}


The opamp may not be necessary as I am looking at A0 using a high impedance oscilloscope - it doesn't work with that line commented out either!
C2 is toggling so I know the code is executing but A0 is 0V (with the ICD disconnected).

Please can someone help as the CCS documentation seems slightly thin in the DAC department and I haven't found a DAC example in the examples directory.

Thanks Duncan


Last edited by duncangray on Fri Nov 04, 2016 1:14 pm; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 5:48 am     Reply with quote

OK, I've never used that PIC but looked at the datasheet..

things to consider..
1) other peripherals use that pin must be disabled

2) the DAC has an 'enable' pin, confirm that IS set...

3) add a 10K to gnd 'load' resistor on DAC out pin

4) dump the listing and compare code to the datasheet for proper register selection and setting of bits. Perhaps the compiler has a 'bug'.

5) can you just 'toggle' the pin ? Confirming it's not shorted or 'busted'?

Having zero output I suspect the DAC output is not being enabled.

Hopefully someone else who has either used this PIC or one similar will respond...

Jay
duncangray



Joined: 27 Apr 2007
Posts: 14
Location: UK

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 6:51 am     Reply with quote

Jay,

Thanks for the rapid response - you're an early bird - responding before 7am!

Your points in the wrong order...

5) A0 toggles so it's not bust - it's also one of the programming pins.
3) 10k to gnd does nothing (not sure why you suggested this one as A0 was already at zero volts but I'm sure there was a reason).
1) Ignoring A0's use as an input - it is used for ICSPDAT and GPIO as well as DAC. As it toggles it is working as GPIO but I had assumed that setting DAC_OUTPUT would reassign it exclusively for the DAC. I also tried to output to the opamp DAC_OPA1OUT and setup_opamp1 to OPAMP_ENABLED | OPAMP_NI_TO_DAC in the hope that the DAC output would appear on C2 (having removed my toggle). Still nothing.

I have dumped the lst file but I'm afraid I don't know how to do 2 or 4. Please can you guide me.

Duncan
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 8:06 am     Reply with quote

hmm..good news the pin ain't 'busted'...
Since it's used for programming, be sure to have the fuse NODEBUG.
Come to think of it, you don't have any #fuses, so it could be one of them has disabled the DAC...
Also if using MPLAB, it has to have the build option set to release...though if a pin toggles at the correct speed, that may not be the problem.
Yyou can send the listing to me at sparky.miller [at] cogeco.ca, it's only 3*C here and need an 'excuse' to stay inside !

You could send the 'value' variable to a PC to confirm it is incrementing(0,1,254,255,0).

However ZERO ouput on the DAC pin does seem to suggest it's not enabled.
Must be after lunch on your side of pond... Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 8:39 am     Reply with quote

I'll go 'backwards'. Smile

To setup the op-amp to give a voltage follower, it needs the extra define 'OPAMP_IN_UNITY_GAIN_MODE'. Currently the inverting input on the op-amp is not being connected. You have the DAC going to the non inverting input, but the other input undefined.

However you are not testing on the op-amps output pin anyway.

I'd remove the op-amp stuff, since some of the setting here do affect the DAC settings (DACOPA1OUT, is saying to use the op-amp and output on A1). Might be causing a problem.

With that done:
Code:

#include <16F753.h>
#use delay(internal=8000000)

void main()
{
   int16 value;
   setup_dac(DAC_VDD | DAC_OUTPUT | DAC_RIGHT_JUSTIFIED);
   while(TRUE)
   {
      dac_write(value++);
      delay_ms(1);
      output_toggle(PIN_C2);
      if (value==512)
         value=0;
   }
}   


Your chip requires a 16bit value for the DAC value (512 levels). In left justified mode (default), the top 9 bits of the 16bit value are used. Feeding it with an int8, in left justified mode, is only going to give an output of a few mV....
duncangray



Joined: 27 Apr 2007
Posts: 14
Location: UK

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 10:23 am     Reply with quote

Thanks Jay (it's getting dark again - time to start work) and big thanks too to Ttelmah.

I had tried left justified without knowing what I was doing but I grasped the logic of your code/comments and realised just how much I still have to learn!

My 16F753 now works as a 0 to 5V ramp generator and it's time to get started on a sine wave.

You haven't got time to write a book have you?

Thanks again, Duncan
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Fri Nov 04, 2016 12:35 pm     Reply with quote

Good. Smile

Left justified is what you get by default. Now, if you had tried right justified it'd have started working (but 0 to 2.5v). Smile

Have fun. Flag this thread as 'solved' (you just edit the title).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group