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 routine not working...need a set of experienced eyes!

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



Joined: 27 Jul 2006
Posts: 1

View user's profile Send private message

I2C routine not working...need a set of experienced eyes!
PostPosted: Thu Jul 27, 2006 10:31 am     Reply with quote

Hi,

I wrote this bit-bang I2C routine yesterday to read my LTC1400 ADC's. Unfortunately, it does not work. I am running a PIC18F4320 at 32MHz, the function is being called correctly and is returning e correctly (although the e it is returning is incorrect). I am using standard I/O (also tried with fast I/O), and the problem seems to lie somewhere in my input-reading procedure.

If I set e = any number before the end of the function, it returns e as that value to the main code (like it should) so it seems to me that none of the bits are being set as the function always returns 0 even with the PORTC.4 input at a constant 5V....so why might I be having this issue?

It is likely something simple, I am a student and have little experience in C, sorry if my coding style is wierd. If my whole routine is too wack to use, can someone suggest another? Or how to fix/improve this routine?

Any advice is greatly appreciated!


-Matt A.

Code:

adcread(void){
   int x, z;
   long int e = 0;               //Init and clear variable to store 12-bit result (within 16-bit word var)
   output_high(PIN_C3);         //Bit-bang clock line
   delay_cycles(30);
   output_high(PIN_C1);         //Bit-bang CS line high
   delay_cycles(30);
   output_low(PIN_C3);
   delay_cycles(60);
   output_high(PIN_C3);
   delay_cycles(20);
   output_low(PIN_C1);            //CS low
   delay_cycles(40);
   output_high(PIN_C3);         //Start filler clock cycle
   delay_cycles(60);
   output_low(PIN_C3);            //Finish filler clock cycle
   delay_cycles(50);            //Wait to preserve clock duty cycle
   for (z = 0; z < 12; z++){      //Loop to check for all bits   
      output_high(PIN_C3);      //Clock high
      x = 11 - z;               //Index for bit_set
      delay_cycles(20);         //Delay to make sure data input pin reads correctly
      if (input(PIN_C4) == 1){   //Check data input pin   
         bit_set(e, x);         //If true then set bit
      }
      delay_cycles(40);
      output_low(PIN_C3);
      delay_cycles(60);
      }
   return(e);                  //Return value e, which should be a 12-bit value in a 16-bit var
}
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Jul 27, 2006 11:26 am     Reply with quote

First, this is an SPI part not I2C which is a different protocol.

Quote:
adcread(void){


Try this instead:

Quote:
int16 adcread(void){


See if that will return a value for you.

Ronald
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