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

Problem with i2c on pic16f88

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







Problem with i2c on pic16f88
PostPosted: Wed Jul 21, 2004 8:53 am     Reply with quote

I am trying to do software i2c using scl=A3, SDA=A4. When an i2c write command is executed, it seems to hang. I recognize this part has a built in hardware i2c, which I am not implementing because of other reasons. The code below is a test routine for getting the i2c to work. I have been playing with turning off/on various hardware functions in order to get it to work. Could be wrong, but it seems like the TRIS settings are incorrect inside the i2c_write code. The i2c function you see below is a result of cutting it out of the memory chip driver to get down to the problem.

#include "C:\robspicc\ASTF88\ASTF88.h"
#include "C:\robspicc\astmemtest\at24c1024.c"
void Fast_Blink();
int mem_page;

void main()
{
long int a;
long int b;
// PORTB = 0; //HARDWARE CONTROL
// set_tris_A (0xFF);
setup_adc(ADC_OFF);
setup_comparator(NC_NC_NC_NC);
port_b_pullups(FALSE);
setup_spi(FALSE);
SETUP_CCP1(CCP_OFF);
enable_interrupts(GLOBAL);
SETUP_SPI(SPI_SS_DISABLED);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_4);
setup_timer_2(T2_DIV_BY_16,224,16);
setup_timer_0(RTCC_DIV_256 | RTCC_INTERNAL);
enable_interrupts(INT_EXT);
mem_page = 0;
// Fast_Blink();
output_high(PWR);
output_high(SCL);
output_high(SDA);
init_ext_eeprom();
while(1){
a=a+1;
delay_ms(1000);
Fast_Blink();
i2c_start();
i2c_write(0xAA);
i2c_stop();
// WRITE_EXT_EEPROM(mem_page,a,b);
// delay_ms(1000);
// printf("%d",READ_EXT_EEPROM(mem_page,a));

}
}

#include <16F88.h>
#device adc=10
#use delay(clock=38400)
//#fuses LP,NOWDT,NOBROWNOUT,NOLVP,NOMCLR
#use rs232(baud=300,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)
#use i2c(Master,sda=PIN_A4,scl=PIN_A3,slow)
#define PING PIN_A1
#define S_DOUT PIN_B1
#define S_SCLK PIN_B3
#define S_DIN PIN_B6
#define PWR PIN_B4
#define OSCPWR PIN_B7
#define IRRXPWR PIN_A0
#define LED PIN_B0
#define SDA PIN_A4
#define SCL PIN_A3
#byte PIR1 = 0x0c
#byte PORTB = 0x06
#byte PORTA = 0x05
Ttelmah
Guest







PostPosted: Wed Jul 21, 2004 10:52 am     Reply with quote

So, what is actually 'wrong', when you try a more complete code sequence (write a byte, and then read it).
The TRIS operation, in software I2C, is 'non intuitive'. The code toggles the TRIS bits, to simulate an open collector drive. Hence the data bit for the two lines wll be set to '0', and then when a '0' is to be sent, the TRIS bit will be set to an output, and when a '1' is to be sent, the TRIS bit will be set to an input.
The code as written will do nothing, except send a bit pattern down the data line, since you are just sending a 'control' byte, and then stopping. Remember you must have resistive pull-ups on both lines. If this is not present, or inadequate, on the data line, it will hang the code, where it looks for the 'ACK' at the end of the write operation. This sounds suspiciously like your problem. The same will apply, if the chip is not being properly addressed.
Also remember on a 'read', you need to use the 'NACK' option on the last transfer.

Best Wishes
tbsnake2
Guest







more on problems with 16f88 i2c
PostPosted: Wed Jul 21, 2004 1:55 pm     Reply with quote

the problem is that I am not seeing the data at all on the SDA or SCL lines. The single i2c_write was put in as I was trying to get something to appear on the SDA and SCL lines. (In other words, the lines are always at a '1' on the scope - they do not change).

Also doing a routine which toggles between output_high and output_low on the same lines I am doing the software sda and scl DOES work.

What is suspicious is that I use almost identical code on a 16f688 for another project and it works fine.
Ttelmah
Guest







Re: more on problems with 16f88 i2c
PostPosted: Wed Jul 21, 2004 2:55 pm     Reply with quote

tbsnake2 wrote:
the problem is that I am not seeing the data at all on the SDA or SCL lines. The single i2c_write was put in as I was trying to get something to appear on the SDA and SCL lines. (In other words, the lines are always at a '1' on the scope - they do not change).

Also doing a routine which toggles between output_high and output_low on the same lines I am doing the software sda and scl DOES work.

What is suspicious is that I use almost identical code on a 16f688 for another project and it works fine.

What you don't mention, is the compiler version?. The soft I2C, normally works now, but a while ago, did have a number of problems. If this is an old compiler, it could be causing the problem.
One comment, I notice you have enabled INT_EXT, but have not shown a handler for the interrupt. If there was a fault there, the code might never be ariving at the actual I2C routines.

Best Wishes
Guest








Re: more on problems with 16f88 i2c
PostPosted: Sun Jul 02, 2006 8:02 am     Reply with quote

hhhfjg
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