|
|
View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
MCP4725 problem |
Posted: Sat Jan 27, 2018 5:15 am |
|
|
I have interfaced 18F2620 with MCP4725 12 bit DAC. Below is the test program.
Code: | #include "18F2620.h"
#fuses INTRC_IO
#use delay(clock=4000000)
#use I2C(MASTER, scl=PIN_C3, sda=PIN_C4)
void dac_i2c(unsigned int16 sample)
{
i2c_start();
i2c_write(0b11000000); // Device address
i2c_write(0b1000000); // Internal Device address
i2c_write((sample & 0xFF0) >> 4); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4)
i2c_write((sample & 0xF) << 4); //lower bits
i2c_stop(); // Stop
}
void main()
{
unsigned int16 i;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_comparator (NC_NC_NC_NC); // Disable comparator
enable_interrupts(GLOBAL); // Enable Global Interrupt
while(1)
{
i=200;
if(i>=4096)
i=0;
dac_i2c(4000);
delay_ms(200);
}
}
|
But the output voltage is +2.5V.. Why is it so? Please help. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9255 Location: Greensville,Ontario
|
|
Posted: Sat Jan 27, 2018 5:51 am |
|
|
WIth every I2C device you should run PCM P 'I2C Scanner' program located in the code library. It will locate every I2C device attached to the PIC.
While I don't use that device I did download it's data sheet and saw that the address can be factory set at customers request. Running the scanner program will confirm it's true address.
I have to ask what value are the I2C bus pullup resistors as well as the DAC output load resistor?
I haven't looked deep at your code as I suspect an addressing problem and you do need to confirm what it is first.
'Play PIC' and confirm the data being sent to the device is correct ! You've got some 'maths' involved in selcting the device and DAC value so simply send that data to a PC terminal program, and confirm bit for bit , is correct! Sometimes 'casting' with alter the data
Try 3 or 4 fixed values. For a DAC I'd use 0, 1/4 scale,1/2 and full values and compare the results to expected. Simply copy your DAC function several times,with names like DAC_zero, DAC_half,DAC_full. Easy to remmeber and understand what the function is supposed to do !
EDIT !!!!
you've got global interrupts enabled ! NOT a good idea. Not too sure what happens, though it's never good to enable ANY interrupt without a 'handler'.
2.5 is 1/2 way of FS, so I suspect that might be a factory test value,as the DAC value is the last one saved.
Your 'internal device address' is only 7 bits long. When dealing with data in 0b form, it's best to show ALL bits. Bit 7 in this case is a 'command' bit.I'd hate to assume bit 7 is a '0' when it's 'unknown'. Best to have 0b01000000 instead of 0b1000000. Maybe the compiler doesn't care but for debugging by a 3rd person who hasn't had enough coffee( or too much ) it does.
Jay
Last edited by temtronic on Sat Jan 27, 2018 7:22 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19566
|
|
Posted: Sat Jan 27, 2018 6:58 am |
|
|
Obvious question. What is connected to the A0 pin?.
The default address is:
0b1100 00P0
Where 'P' is the value on the A0 pin.
If this is not pulled down to 0v, the address will be wrong.
The I2C scanner program is always a useful starting point.
Code: |
#define MCP4725ADDR 0xC0 //Check this is what the scanner says
#define WRITE_REG 0x40 //Write and select normal power
void dac_i2c(unsigned int16 sample)
{
i2c_start();
i2c_write(MCP4725ADDR); // Device address
i2c_write(WRITE_REG); // Internal Device address & command
i2c_write((sample & 0x0FF0) >> 4); //upper 2 nibbles
i2c_write((sample & 0x0F) << 4); //low nibble
i2c_stop(); // Stop
}
//Then test with something like:
while(1)
{
for (i=0; i<4095); i+=16)
{
dac_i2c(i);
delay_ms(200);
}
}
|
I'd be most suspicious the A0 pin is high or floating, so the address is wrong. The scanner program will tell you where it is. |
|
|
|
|
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
|