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 support@ccsinfo.com

problems with I2C in PIC16F877A

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



Joined: 05 May 2006
Posts: 1

View user's profile Send private message

problems with I2C in PIC16F877A
PostPosted: Fri May 05, 2006 2:47 am     Reply with quote

Hello!
I am making a i2c communication master - slave. The master connects itself to the PC by RS232. The problem is that i2c does not work well since the HyperTerminal receives 255 instead of 160(0xA0)

MASTER CODE
Code:

//////////////////////////////////////////////////
// I2C master                                   //
// PIC16F877A                                   //
//                                 PICmaster.C  //
// Crea una comunicacion entre varios PICS      //
// por i2c mandando lo leido al PC por el       //
// el puerto serie.                             //
//                                              //
// Realizado por Juan José [spam] Barrera        //
//////////////////////////////////////////////////


#include <16f877A.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
#use delay(clock = 10000000)       // reloj 10MHz
/*
   Configuracion principal del I2C
   Modo Maestro
   Tasa de transferencia rapida, aunq puede tener problemas
   I2c se hace por harwdware pa eso lo tenemos en el
*/
#use i2c(MASTER,SDA=PIN_C4,SCL=PIN_C3/*,FAST,FORCE_HW*/)
/*
   Configuracion principal del puerto serie
   velocidad de 9600 baudios
   sin paridad
   con un bit de parada
*/
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)



/*
   Rutina de ESCRITURA a un dispositivo conectado al i2c

   i2cdev --> Direccion del dispositvo
   i2cdat --> Dato que le quiero enviar
*/
void i2cw(int i2cdev, int i2cdat){

   i2c_start();                  // Inicializo comunicación I2C
   i2c_write(i2cdev);            // Envio Dirección de dispositivo I2C + R/W
   i2c_write(i2cdat);            // Envio byte a escribir
   i2c_stop();                   // Cierro comunicacion
}


/*
   Rutina de LECTURA a un dispositivo conectado al i2c

   i2cdev --> Direccion del dispositvo del que quiero leer
*/
int  i2cr(int i2cdev){           // Rutina de lectura I2C

   int r=0x00;

   i2c_start();
   i2c_write(i2cdev);
   r=i2c_read();
   i2c_stop();
   return(r);
}

unsigned int giro=80;
main()
{
     output_float(PIN_C3);         //I2C pin float
     output_float(PIN_C4);         //I2C pin float

     while(1)
     {
         //COMUNICACION CON EL ESCLAVO 1
         giro = i2cr(0x02);           //Leo del dispositivo esclavo y lo muestro por el puerto B
         printf("Prueba de i2c %U\n\r",giro);  //Lo muestro por el puerto serie
         delay_ms(1000);            //Retardo de 1 segundo
         giro = 0;                    //Reseteo la variable

     }


}



SLAVE CODE
Code:

//////////////////////////////////////////////////
// I2C slave                                    //
// PIC16F877A                                   //
//                                 PICslave1.C  //
// Crea una comunicacion entre varios PICS      //
// siendo una comunicacion muy basica           //
//                                              //
// Realizado por Juan José [spam] Barrera        //
//////////////////////////////////////////////////

#include <16f877A.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
#use delay(clock = 10000000)       // reloj 10MHz
#use fast_io(B)                    // cambio rapido de estados en los puertos
/*
   Configuracion principal del I2C
   Modo Esclavo
   Direccion 0b00000010  = 0x02  (Son 7 bits el bit que se desprecia es el de menor peso por tanto es 1)
   Tasa de transferencia rapida, aunq puede tener problemas
   I2c se hace por harwdware pa eso lo tenemos en el chip
*/

#use i2c(SLAVE,SDA=PIN_C4,SCL=PIN_C3,ADDRESS=0x02/*,FAST,FORCE_HW*/)

byte DATA = 69;  // antes era 0x0F

//Interrupcion provocada cuando algo ocurre en el i2c
#INT_SSP
void ssp_interupt (){
     output_b(0xFF);
     if(i2c_poll()){               //Si tenemos algo en el buffer de entrada
           i2c_read();   //lo mostramos por el puerto B
     }
     else{

           i2c_write(0xA0);    //Si no esq el master quiere q le mandemos la info
                                   //solamente la parte baja de un entero (1 byte)
           output_b(0x00);
     }
}

void main (){

     set_tris_b(0x00);             //RB 7-0:OUT

     output_b(0);        //en un principio esta apagado

     enable_interrupts(GLOBAL);      //Activamos las interrupciones
     enable_interrupts(INT_SSP);     //Activamos la interrupcion para el i2c

   while (1);  //bucle infinito
}


I am Spanish asi that the commentaries of the program is in Spanish
The pull-up resistor to i2c is 2K2

Somebody knows where I can have the error?
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