View previous topic :: View next topic |
Author |
Message |
niloc
Joined: 05 Jan 2010 Posts: 5
|
i2c and LTC2631 DAC |
Posted: Sun Mar 06, 2011 8:21 pm |
|
|
Could someone please help me with my code and please forgive my newbieness. I know the code looks untidy. I am using CCS compiler ver 4.112.
I thought I had this code running in my hardware but now it doesn't. In MPLAB IDE v8.56 DEBUG, the code will run thru but will not come out of the
#use i2c(Master,sda=PIN_C4,scl=PIN_C3) line.
If I remark out the out_DAC() function the laser turns on and off and the ADC values will change in a watch window. So the problem seems to be in the i2c function
My main problem is getting the i2c to work, i have 1k8 pullups on SCL and SDA. The i2c device is a LTC2631 DAC the PIC is master and the DAC is a write only slave.
Any advice is appeciated.
cheers
Code: | //+/- 0-5v from Photo detector is read by AN0 A/D on PIC18F2685 and output
//to LTC2631 D/A on I2C channel. Laser On switch is pressed to turn on Laser
//for reading of levels and reading is held when Laser is
//turned off. Reading retained and displayed on small voltmeter until
//Clear switch is pressed
//Code as was used in ver 5 but changed to give proper memory function
#include <18F2685.h>
#device ICD=TRUE
#device adc=10
#include <stdlib.h>
#FUSES NOWDT,INTRC_IO,NOMCLR,DEBUG,NOLVP
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use delay(clock=4000000)
#define Laser_Sw PIN_B0
#define Clear_Sw PIN_B1
#define Laser_Pwr PIN_C7
#define I2C_SCL PIN_C3
#define I2C_SDA PIN_C4
long value;
//long stored_value;
//long reading;
//int flag;
void out_DAC() //Function to write to D/A via I2C
{
value=value*15.044;
i2c_start(); //Start condition
i2c_write(0b00010000); //Device Address
//i2C_write(0b01110000); //select external reference 5v not needed?
i2c_write(0b00110000); //Device command
i2c_write(value>>8); //Device data MSB
i2c_write(value); //Device data LSB
i2c_stop(); //Stop condition
}
void main()
{
Output_low(Laser_Pwr);
SET_TRIS_B(0xFF);
SET_TRIS_C(0x00);
value = 0;
while(true) //start of main loop
{
setup_adc_ports(AN0_Analog);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_us(10);
while(input(Laser_Sw)==1) //if Laser switch is off
{
value = read_adc(); //Read ADC port and store in value
delay_us(10);
out_DAC(); //call I2C function
}
while(input(Laser_Sw)!=1) //If Laser Sw now on
{
Output_High(Laser_Pwr); //Turn on Laser
delay_ms(200); //Delay for laser to settle
value = read_adc(); //Read ADC port
delay_us(10);
out_DAC(); //call I2C Function
}
value=read_adc(ADC_READ_ONLY); //Laser Sw off again
Output_Low (Laser_Pwr);
do
{
setup_adc(ADC_OFF); //DAC will continue to
} //display stored reading until
while (input(Clear_Sw)==1); //clear button is pressed
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 06, 2011 8:49 pm |
|
|
What connections do you have to pins CA0 and CA1 on the LTC2631 ? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Sun Mar 06, 2011 8:50 pm |
|
|
Couple of quick comments...I make the assumption this is real hardware and NOT a simulation (Proteus,etc.)
1) In...
void out_DAC() //Function to write to D/A via I2C
{
value=value*15.044;
...
I think value will be multiplied by 15 , not the floating point 15.044....
best to check what really happens, maybe a weird number is being put into 'value'.
2) You'll have to change the program build option to 'release' from 'debug' and recompile BEFORE you burn a PIC with your code otherwise is will not work as expected.. |
|
![](templates/subSilver/images/spacer.gif) |
niloc
Joined: 05 Jan 2010 Posts: 5
|
|
Posted: Sun Mar 06, 2011 9:01 pm |
|
|
PCM programmer wrote: | What connections do you have to pins CA0 and CA1 on the LTC2631 ? |
CA0 and CA1 are at GND which according to data sheet gives address of 0010000 |
|
![](templates/subSilver/images/spacer.gif) |
niloc
Joined: 05 Jan 2010 Posts: 5
|
|
Posted: Sun Mar 06, 2011 9:10 pm |
|
|
temtronic wrote: | Couple of quick comments...I make the assumption this is real hardware and NOT a simulation (Proteus,etc.)
1) In...
void out_DAC() //Function to write to D/A via I2C
{
value=value*15.044;
...
I think value will be multiplied by 15 , not the floating point 15.044....
best to check what really happens, maybe a weird number is being put into 'value'.
2) You'll have to change the program build option to 'release' from 'debug' and recompile BEFORE you burn a PIC with your code otherwise is will not work as expected.. |
Yeah this is real hardware
Thanks, I'll check on the value*15.044 and I hadn't considered the release from debug option. Might be an hour or two before i can get back to it. Will post an update, cheers |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 06, 2011 9:20 pm |
|
|
Quote: | CA0 and CA1 are at GND which according to data sheet gives address of 0010000 |
That's correct, but in your code you have 3 leading zeros on the address.
It should be only 2. The slave address is left-justified.
Quote: | i2c_write(0b00010000);
|
Look at this diagram in the data sheet:
Quote: | Figure 2. Typical LTC2631 Write Transaction |
It shows the address is left-justified, with A6 as the top bit.
That means only 2 leading zeros. |
|
![](templates/subSilver/images/spacer.gif) |
niloc
Joined: 05 Jan 2010 Posts: 5
|
|
Posted: Sun Mar 06, 2011 9:30 pm |
|
|
PCM programmer wrote: | Quote: | CA0 and CA1 are at GND which according to data sheet gives address of 0010000 |
That's correct, but in your code you have 3 leading zeros on the address.
It should be only 2. The slave address is left-justified.
Quote: | i2c_write(0b00010000);
|
Look at this diagram in the data sheet:
Quote: | Figure 2. Typical LTC2631 Write Transaction |
It shows the address is left-justified, with A6 as the top bit.
That means only 2 leading zeros. |
I changed to i2c_write(0b0010000) but still have the problem. If I // out the "out_DAC()" function then the Laser turns on and off. With the "out_DAC()" retained the Laser will not power up.Seems as though there is a problem within the i2c settings |
|
![](templates/subSilver/images/spacer.gif) |
niloc
Joined: 05 Jan 2010 Posts: 5
|
|
Posted: Sun Mar 06, 2011 11:07 pm |
|
|
Seems as though the DAC chip is at fault, the SCL line, pin 2 has an internal fault to gnd at pin 4. New chips on order. Ah the joys of prototyping!!!
It is too easy to blame the code when there is a hardware problem.
I'll update as soon as the new chips arrive. |
|
![](templates/subSilver/images/spacer.gif) |
|