zagisrule!
Joined: 27 Jul 2006 Posts: 1
|
I2C routine not working...need a set of experienced eyes! |
Posted: Thu Jul 27, 2006 10:31 am |
|
|
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
} |
|
|