View previous topic :: View next topic |
Author |
Message |
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
MAX519 and i2c not working |
Posted: Thu Sep 11, 2008 9:49 am |
|
|
Hi I am trying to use the max419 dac to convert digital to analog values but it is does not seem to work. I double checked all my hardware and everything seems fine. Please advise if the problem could be with the software.
Thanks
Code: | #include <16F886.H>
#fuses INTRC,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3,FORCE_HW)
#define MAX519_ADDRESS 0x20
#define MAX519_DATA_COMMAND_DAC0 0x00 //Set channel 0 on dual dac
#define MAX519_DATA_COMMAND_DAC1 0x02 //Set channel 1 on dual dac
int8 channel0=100,channel1=100;
void max519_write_dac()
{
i2c_start();
i2c_write(MAX519_ADDRESS);
i2c_write(MAX519_DATA_COMMAND_DAC0);
i2c_write(channel0);
i2c_write(MAX519_DATA_COMMAND_DAC1);
i2c_write(channel1);
i2c_stop();
}
//=================================
void main()
{
while(1)
{
//channel0+=1;
//channel1+=20;
max519_write_dac();
delay_ms(100);
}
} |
Last edited by Gerhard on Thu Sep 11, 2008 10:30 am; edited 1 time in total |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Sep 11, 2008 10:16 am |
|
|
I have several problems in understanding your question.
As first point MAX 419 is an OP rather than a I2C DAC. Do you mean MAX 519?
The next point is, none of the MAX51x DACs could have a 0x20 slave address, they use 0x40 address base. |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 10:22 am |
|
|
Sorry. Its a 519. Been working with too many numbers.
Quote: | The MAX517/MAX518/MAX519 each have a 7-bit long
slave address (Figure 6). The first three bits (MSBs) of
the slave address have been factory programmed and
are always 010. In addition, the MAX517 and MAX518
have the next two bits factory programmed to 1s. The
logic state of the address inputs (AD0 and AD1 on the
MAX517/MAX518; AD0, AD1, AD2, and AD3 on the
MAX519) determine the LSB bits of the 7-bit slave
address. These input pins may be connected to VDD or
DGND, or they may be actively driven by TTL or CMOS
logic levels. The MAX517/MAX518 have four possible
slave addresses and therefore a maximum of four of |
I must be missing something then. I have AD0,AD1,AD2 and AD3 conected to ground so they would be 0's.
On page 8 of data sheet it quotes that first 3 are 010 and next are AD0-3 values so 0100000b=20H. Can you spot my mistake? |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 10:41 am |
|
|
Ok i saw what i did with the adress wrongly. It should be 0x40 as there are 8 and not seven bits. However it is still not working so can you spot any other mistakes? |
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Thu Sep 11, 2008 11:42 am |
|
|
Quote: | Code: | #define MAX519_DATA_COMMAND_DAC0 0x00 //Set channel 0 on dual dac
#define MAX519_DATA_COMMAND_DAC1 0x02 //Set channel 1 on dual dac |
|
DAC1 should be 0x01
I think your problem is that you need 'restarts' in your sequence, else the MAX will assume that you are just sending a command byte:
Code: | i2c_start();
i2c_write(MAX519_ADDRESS);
i2c_write(MAX519_DATA_COMMAND_DAC0);
i2c_start(); // need this to tell MAX there is more coming..
i2c_write(channel0);
i2c_start(); // need this to tell MAX there is more coming..
i2c_write(MAX519_DATA_COMMAND_DAC1);
i2c_start(); // need this to tell MAX there is more coming..
i2c_write(channel1);
i2c_stop(); |
This might not be exactly right, but I think the restarts are the problem |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 12:56 pm |
|
|
It is the first time using I2C write function so it is possible that i am completely misunderstanding something. In the following code the led on B5 goes on but not off so i know the pic gets stuck in the I2C function. Would someone please give some advice or just let me know if the code is correct as that i know to narrow my fault finding.
Code: | #include <16F886.H>
#fuses INTRC,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3,FORCE_HW)
#define MAX519_ADDRESS 0x40
#define MAX519_DATA_COMMAND_DAC0 0x00 //Set channel 0 on dual dac
#define MAX519_DATA_COMMAND_DAC1 0x01 //Set channel 1 on dual dac
int8 chan0=100,chan1=100;
void max519_write_dac()
{
i2c_start();
i2c_write(MAX519_ADDRESS);
i2c_write(MAX519_DATA_COMMAND_DAC0);
i2c_write(chan0);
i2c_write(MAX519_DATA_COMMAND_DAC1);
i2c_write(chan1);
i2c_stop();
}
//=================================
void main()
{
while(1)
{ output_high(pin_b5);
max519_write_dac();
delay_ms(500);
output_low(pin_b5);
delay_ms(500);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 11, 2008 1:01 pm |
|
|
Do you have pull-up resistors on the i2c lines ?
You need a pull-up on SDA and on SCL. You can use 4.7K ohm resistors.
Also, there are bugs in the MSSP in master i2c mode.
http://ww1.microchip.com/downloads/en/DeviceDoc/80302D.pdf
If you still have problems, I suggest removing the "FORCE_HW"
parameter. Let it do software i2c. |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 1:13 pm |
|
|
Thanks PCM. Will remember them next time. It is working fine now. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 11, 2008 1:18 pm |
|
|
One last thing you should do to clean up code, is to add parameters to
the function. Use parameters instead of globals. Make the first function
parameter be 'chan0', and the 2nd should be 'chan1'. (and delete your
global variables).
Then in your main(), call the function with your desired DAC settings:
Code: | max519_write_dac(100, 100); |
Your code will be safer and more reliable if you do it this way,
than if you use global variables to pass parameters. |
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 2:19 pm |
|
|
thanks. Is this what you recommend?
Code: | void max519_write_dac(chan0,chan1)
{
i2c_start();
i2c_write(MAX519_ADDRESS);
i2c_write(MAX519_DATA_COMMAND_DAC0);
i2c_write(chan0);
i2c_write(MAX519_DATA_COMMAND_DAC1);
i2c_write(chan1);
i2c_stop();
}
//=================================
void main()
{
int8 chan0=100,chan1=100;
while(1)
{
chan0+=1;
chan1+=5;
//output_high(pin_b5);
// delay_ms(100);
max519_write_dac(chan0,chan1);
// output_low(pin_b5);
delay_ms(100);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 11, 2008 2:26 pm |
|
|
Yes. Except that the function declaration should have the data types
of the parameters in it. The compiler defaults to int8, but it's safer to
always declare the data types.
Quote: |
void max519_write_dac(int8 chan0, int8 chan1)
{
i2c_start();
// etc.
}
|
|
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 2:39 pm |
|
|
Thanks.
I am busy building a system that can sample 2 signals of maximum 1khz with the adc on a pic then multiplex them, transfer the data over a 2wire 485 line, demultiplex and reconstruct the 2 signals using the DAC chip. I am still however trying to figure out how the I2C protocol works in CCS. If i need the I2C speed to be 400Kbps can i just declare it as follows.
Code: | #use i2c(Master, sda=PIN_C4, scl=PIN_C3,FORCE_HW,Fast=400000) |
I have compiled and tested it and it works fine. I just want to ensure that i am doing it correctly as i have no way of testing that the actual transfer speed is 400Kbps. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 11, 2008 2:55 pm |
|
|
Do you have an oscilloscope ? Create a test program similar to this
and put a scope probe on the SCL line.
Code: | while(1)
{
i2c_write(0x55);
} |
|
|
|
Gerhard
Joined: 30 Aug 2007 Posts: 144 Location: South Africa
|
|
Posted: Thu Sep 11, 2008 3:05 pm |
|
|
I am still studying so money to buy nice stuff such as osciloscopes are rare. No i dont have a scope and if i want to test something i can only do it during day time at the univ. labs. So i try to work as much as i can during the night and test everything between classes during daytime. I will hook it up on a scope tomorow but can you please just advice me on if the software setup is correct.
I saw on google that fast=400kbps and on ccs help that this seems correct. |
|
|
|