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 12F675

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



Joined: 05 Nov 2009
Posts: 1

View user's profile Send private message

Problem with i2c on 12F675
PostPosted: Thu Nov 05, 2009 3:25 pm     Reply with quote

J'utilise les routines I2C sur un PIC12F675. Or la routine, I2C_write(adresse) ne fonctionne pas correctement. Sur les 7 bits d'adresse, uniquement 6 sont envoyés et le 7ème est toujours à 0.
Avez vous le même problème ?
Merci de vos aides

Translation:
I use i2c routines on a PIC12F675. However the routine, i2c_write(address) does not function correctly. Of the 7 bits of address, only 6 are sent and 7th is always to 0. Have you seen this problem? Thank you for your assistance.

Code:

**********************************
* Code utilisé
**********************************

#include <12F675.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BANDGAP_HIGH         
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_A3,rcv=PIN_A2,bits=8)

#define PIN_SDA PIN_A0
#define PIN_SCL PIN_A1

#use i2c(Master,sda=PIN_SDA,scl=PIN_SCL,force_sw)

//#use standard_io


BOOLEAN I2C_ready()
{
   int1 ack;
   delay_ms(10);
   i2c_start();
   ack = i2c_write(0x0F); 
   i2c_stop();
   return !ack;
}

void write_I2C(BYTE address, BYTE data) {
   while(!I2C_ready());
   i2c_start();
   i2c_write(0x0F);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
}


BYTE read_I2C(BYTE address)
{
   BYTE data;
   while(!I2C_ready());
   i2c_start();
   i2c_write(0x78);
   i2c_write(address);
   i2c_start();
   i2c_write(0x78);
   data=i2c_read();
   i2c_stop();
   return(data);
}


void main()
{

   BYTE data;
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_DISABLED);
   setup_comparator(NC_NC);
   setup_vref(FALSE);
   enable_interrupts(GLOBAL);
   init_capteur();
   
   // TODO: USER CODE!!
   
   while(true)
   {
   data=read_I2C(0x33);
   };
}
Ttelmah
Guest







PostPosted: Thu Nov 05, 2009 3:50 pm     Reply with quote

When you then try to talk to your device, you send an address of 0x78, which is a _write_ address. You then try to read from the same address.....
You do the opposite on your write routine, sending a 0xF, which is a read address, and then trying to write to this.
Is the device at address 0xE/0xF, or at 0x78/0x79?.

The first byte sent after an I2C start, is the device address coded as the seven upper bits followed by a single bit signifyng bus direction. This normally needs to be _low_ for the first transaction, so you can send the register number to be selected as the following byte. You then send data in subsequent bytes for the 'write'. For a read, you need to send a repeated start (you do), followed by the device _read_ address (low bit set), followed by the data reads.

With the wrong direction bit selected, there will be a bus conflict between the PIC, and the device....

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 05, 2009 5:38 pm     Reply with quote

Also, post the manufacturer and part number of the i2c slave device
that is connected to the 12F675.
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