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

i2c and LTC2631 DAC

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



Joined: 05 Jan 2010
Posts: 5

View user's profile Send private message

i2c and LTC2631 DAC
PostPosted: Sun Mar 06, 2011 8:21 pm     Reply with quote

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
         
   }   

}
          
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 8:49 pm     Reply with quote

What connections do you have to pins CA0 and CA1 on the LTC2631 ?
temtronic



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

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 8:50 pm     Reply with quote

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..
niloc



Joined: 05 Jan 2010
Posts: 5

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 9:01 pm     Reply with quote

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
niloc



Joined: 05 Jan 2010
Posts: 5

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 9:10 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 9:20 pm     Reply with quote

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.
niloc



Joined: 05 Jan 2010
Posts: 5

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 9:30 pm     Reply with quote

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
niloc



Joined: 05 Jan 2010
Posts: 5

View user's profile Send private message

PostPosted: Sun Mar 06, 2011 11:07 pm     Reply with quote

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!!! Crying or Very sad

It is too easy to blame the code when there is a hardware problem. Wink

I'll update as soon as the new chips arrive.
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