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

MCP4921 not working

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



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

MCP4921 not working
PostPosted: Wed Dec 08, 2010 12:22 am     Reply with quote

Hi,
I made this code wherein I get ADC input from a pot and display it on a LCD. This part works fine. However, I'm not able to get a DAC output. I have used mcp4921.c which is available.
My code is as follows:-
Code:

#include <18f2520.h>
#device ICD=TRUE
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#include <flex_lcd.c>
#include <stdlib.h>
#include <mcp4921.c>

void main()
{
   int16 a;
   lcd_init();
   init_dac();
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_64);
   set_adc_channel(0);
   delay_ms(100);

      while(1)
      {
         a=read_adc();
         delay_ms(1);
         printf(lcd_putc,"\f  %ld",a);
         write_dac(a);
         delay_ms(100);
      }
}


thanks,
Sid
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 12:13 pm     Reply with quote

Post a list of all connections between the PIC and the mcp4921.
Also, are you testing this in real hardware, or in Proteus ?
Sid2286



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

Connection details
PostPosted: Wed Dec 08, 2010 7:13 pm     Reply with quote

C4-> CLK
C5-> CS
C6->DIN
C7->LDAC
Vref to vcc
yes, i'm testing it it on proteus.
Thanks,
sid
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 08, 2010 7:49 pm     Reply with quote

Post the list of #define statements for the pins in mcp4921.c

This is the list in the CCS mcp4921.c driver:
Code:
#define DAC_CS     PIN_B0
#define DAC_CLK    PIN_B1
#define DAC_DI     PIN_B2
#define DAC_LDAC   PIN_B4

You have changed the pins to use Port C pins.
Post the #define statements that you now have in mcp4921.c.
Sid2286



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

Connection details
PostPosted: Wed Dec 08, 2010 9:05 pm     Reply with quote

I made changes on mcp4921.c file
Code:

#define DAC_CS PIN_C5
#define DAC_CLK PIN_C4
#define DAC_DI PIN_C6
#define DAC_LDAC PIN_C7
 

these are my #define on mcp4921 file
Sid2286



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

PostPosted: Thu Dec 09, 2010 1:31 am     Reply with quote

Ok! Finally i got it....

However I have a doubt... when I connect a ammeter in series with the pot and analog input of PIC. I don't get any reading.
But when i connect a voltmeter i get a reading which is 2.5V and a display as 512.
Ideally voltmeter shouldn't be working and ammeter should be working.
Is there anything wrong in my connections??

regards,
Sid
Ttelmah



Joined: 11 Mar 2010
Posts: 19509

View user's profile Send private message

PostPosted: Thu Dec 09, 2010 3:42 am     Reply with quote

Not sure why you would expect detectable current to flow.....
The input impedance of the PIC, is extremely high. Current flow into the ADC, will be in the nA area _except_ when the voltage is changing fast. The PIC input, is seen as a couple of resistors, connecting into a small capacitor (hence current flow when the signal is changing), and then a few nA of leakage.

Best Wishes
Sid2286



Joined: 12 Aug 2010
Posts: 119

View user's profile Send private message

PostPosted: Thu Dec 09, 2010 4:51 am     Reply with quote

Ok, i might have a confusion here.

Like in my program I want to see the input as well as the output from the PIC through the MCP4921.

So I was thinking that the analog input will be equal to the output. I guess its not the case.

I would like to know if I would have to add external circuit to calibrate with the input, like add another opamp.

Regards,
Siddhesh
temtronic



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

View user's profile Send private message

PostPosted: Thu Dec 09, 2010 7:07 am     Reply with quote

Well, at least we found out early this is yet another simulation !

The output of the DAC will never always be equal to the input to the PIC.

And there's the simulated voltmeter reading the simulated voltage on the simulated circuit problems...

Even in the real world, you must account for noise, ripple, adc ref. errors, impedance mismatches, etc. Every ADC and DAC is always 'off' +-1 bit, regardless of the number of bits in the device.
You can of course minimize the 'error' by filtering (hard or soft).

I figured I was doing really good with +-3 bit errors using 16 bit ADC within 2 feet of the ARL Spectrometers (20KV, 300ma, spark gap 1/4").
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