i new pic. know there lot of posts in forum on i2c , have tried methods given still doesn't solve problem. using pic16f1516 , external eeprom cat24c04tdi store data. have used 4k7 ohm resistors pull , code :
void i2cwrite(void); void waitmssp(void); void i2cread(void); void i2c_init(void); //void _delay_ms(unsigned int); void main() { _delay_ms(100); // give delay power i2c_init(); // initialize i2c _delay_ms(20); i2cwrite(); // sends data i2c eeprom _delay_ms(50); while(1) { i2cread(); // read data?s txreg='\n'; while(txif==0); txreg='\r'; _delay_ms(500); } } void i2cwrite() { sspcon2bits.sen=1; waitmssp(); // wait operation finished sspbuf=0xa0;//send slave address write command waitmssp(); sspbuf=0x00; // send starting address write waitmssp(); sspbuf=0x34; // rough data written waitmssp(); } pen=1; // send stop bit waitmssp(); } void i2cread() { sen=1; //send start bit waitmssp(); //wait operation finished sspbuf=0xa0;//send slave address write command waitmssp(); sspbuf=0x00; // send starting address write waitmssp(); rsen=1; // send re-start bit waitmssp(); sspbuf=0xa1; // slave address read command waitmssp(); rcen=1; // enable receive waitmssp(); ackdt=1; // acknowledge data 1: nack, 0: ack acken=1; // enable ack send pen=1; // stop condition waitmssp(); putch(sspbuf); // send received data pc _delay_ms(30); } pen=1; waitmssp(); } void waitmssp() { while(!sspif); // while sspif=0 stay here else exit loop sspif=0; // operation completed clear flag } void i2c_init() { triscbits.trisc3=1; // set i2c lines setting input triscbits.trisc4=1; sspcon1 = 0b00101000; // sspadd=(fosc / (4 * i2c_freq)) - 1; //clock 100khz sspadd = 0x18 ; //clock 100khz sspstat=80; // slew rate control disabled pir1bits.sspif = 0; // clear mssp interrupt request flag pie1bits.sspie = 1; // enable mssp interrupt enable bit }
when connect oscilloscope, nothing happens @ sda , scl ports after sending sen=1. please me problem. stuck since long time.
sspif
is not register: data sheet shows bit 3 in register pir1
instead of line
while(!sspif); // while sspif=0 stay here else exit loop sspif = 0; // operation completed clear flag
this means should test , clear
while ((pir1 & 0x08) == 0); pir1 = 0;
the assembler instruction clear status is
bcf pir1,sspif ;i2c done, clear flag
and more detail of code in forum please read data sheet more information.
Comments
Post a Comment