Atraxz
Joined: 04 Jan 2008 Posts: 1
|
Problem with i2c and the TRIS settings |
Posted: Fri Jan 04, 2008 3:32 am |
|
|
Need help with a problem using i2c to communicate with either E2 or a RTC. The problem lies in getting the i2c to work properly.
Compiler ver. 4.064
using a PIC18F65j10
When this problem ocurred i created this small test program to rule out any side effects from the other code.
Code: | // Debugging I2C
#include "18F65J10.h"
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=8000000)
#use i2c(master,scl=PIN_C3,sda=PIN_C4,force_hw,fast=400000,stream=RTCE2)
void main(){ set_tris_b( 0xFC );
set_tris_c( 0x98 );
set_tris_d( 0x00 );
set_tris_e( 0xFF );
set_tris_f( 0x00 );
set_tris_g( 0x1c );
while(1){
i2c_start(RTCE2); // Pull bus low
i2c_write(RTCE2, 0xA2); // Send slave adress, write cmd
i2c_write(RTCE2, 0x03); // Adress where data will be written
i2c_write(RTCE2, 0xAA); // Test data
i2c_stop(RTCE2);
delay_ms(500);
}
} |
But even this simple program presents me with the same problem which is: The previous code will result in a totally silent i2c. To get the i2c up and running i must first set tris_c pin c3 & c4 ( SCL & SDA) to outputs and thereafter setting the pins to inputs again. Following code will get the i2c to work as it should
Code: | // Debugging I2C
#include "18F65J10.h"
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=8000000)
#use i2c(master,scl=PIN_C3,sda=PIN_C4,force_hw,fast=400000,stream=RTCE2)
void main(){
set_tris_b( 0xFC );
set_tris_c( 0x80 ); // SDA & SCL outputs
set_tris_c( 0x98 ); // SDA & SCL inputs
set_tris_d( 0x00 );
set_tris_e( 0xFF );
set_tris_f( 0x00 );
set_tris_g( 0x1c );
while(1){
i2c_start(RTCE2); // Pull bus low
i2c_write(RTCE2, 0xA2); // Send slave adress, write cmd
i2c_write(RTCE2, 0x03); // Adress where data will be written
i2c_write(RTCE2, 0xAA); // Test data
i2c_stop(RTCE2);
delay_ms(500);
}
}
|
By setting SDA & SCL to outputs then inputs will get the i2c to work. But once the i2c has started to work I can program it with the first code again and it will still work as long as the only warm reset ocurres. Unplugging the voltage to the PIC will force me to program the TRIS C as output and thereafter as inputs to get it to work again. According to MPLAB the trisc is set correctly with both programs but only the second works. The i2c communication problem is verified using oscilloscope. Any ideas? What am I missing? |
|