Autor Tema: Problema I2c Slave multidato  (Leído 1809 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado mizzard

  • PIC10
  • *
  • Mensajes: 32
Problema I2c Slave multidato
« en: 14 de Octubre de 2010, 09:17:17 »
Hola a todos, estoy intentando comunicar un módulo GSM y un pic por i2c, el pic actuara como esclavo.

El caso es que el master que es el modulo gsm manda una serie de datos, pongamos 100 bytes, por ejemplo

Yo he implementado en el pic la siguiente rutina (100KHz para el i2c con reloj interno de 8MHz), en la que cada 15 seg aprox muestro el estado del buffer para ver si cambia. Por defecto este estara a FF.

Lo que me pasa es que la cadena no me llega completa y a veces se corta antes o despues (pruebas con 50 caracteres) y no se si estoy haciendo bien el tratamiento dentro de la interrupcion del i2c o si deberia hacerla de otra forma ya que los datos a recibir son bastantes.

Todos los ejemplos que he visto por aqui hacen referencia a un max para el valor de state=3... cuando yo voy a tener =50 o =100, que segun tengo entendido podre hasta el 7F.

Alguna idea o sugerencia??? muchas gracias!

include <18F2523.h>

//#device adc=12

#FUSES NOWDT,INTRC,PUT,NOPROTECT,NODEBUG,BROWNOUT,BORV21,NOLVP,NOCPD,NOWRT,STVREN,NOWRTD,NOPBADEN,NOWRTC,NOWRTB,NOEBTR,NOEBTRB,NOCPB,LPT1OSC,NOMCLR,NOXINST

#use delay(INTERNAL=8000000)

//NOTE: Must declare MASTER before SLAVE, i2c_isr_state() returns 0
//      when MASTER is the most recent #use i2c
//#use i2c(MASTER, sda=PIN_C4, scl=PIN_C3,SLOW, stream=I2CM)
#use i2c(SLAVE, sda=PIN_C4, scl=PIN_C3, address=0x20,SLOW, force_hw, stream=I2CS)
#use rs232(stream = DEBUGG, baud=9600, parity=N, xmit=PIN_B7, rcv=PIN_B6, bits=8)




//Library includes:
#include <limits.h>
#include <math.h>
#include <string.h>
#include <stdio.h>


//================================================================================================================================================
//-----------------------  DEFINICIONES  ---------------------------------------------------------------------------------------------------
//================================================================================================================================================

// TIMER 1
#define preEscalerTMR1 T1_DIV_BY_8
//#define preEscTMR1 4


//================================================================================================================================================
//-----------------------  VARIABLES GLOBALES  ---------------------------------------------------------------------------------------------------
//================================================================================================================================================

int i=0;

int estado=0;


//================================================================================================================================================
//-----------------------  DECLARACIÓN DE FUNCIONES  ---------------------------------------------------------------------------------------------
//================================================================================================================================================

void inicializaPIC(void);
void pruebaParpadeo(void);
void escrituraMI2c(void);
void lecturaMI2c(void);


//************************************************************************************************************************************************
//**************            INTERRUPCIONES            ********************************************************************************************
//************************************************************************************************************************************************

int rcv_buf[0x40];      
int wrt_buf[0x10];      
int cmd=0xFF;

int contador=0;
boolean bandera=false;

char sms[64];          
char tlf[64];





#int_TIMER1
void timer1_isr(void){
contador++;
if(contador==57){
bandera=true;
contador=0;
}
}



//================================================================================================================================================

#int_SSP // interrupt on i2c activity
void  i2c_isr(void)
{
    int state, incoming;
    state = i2c_isr_state();
  
    if(state < 0x80)
    {
        incoming = i2c_read(I2CS);
        if (state == 1)
        {
            cmd = incoming;  // El primer byte lo guardamos aquí
        }
      
        else if (state > 1)
        {
         rcv_buf[state]=incoming;

// aqui uso la de abajo ya que cuando state valga 2 yo quiero que me lo almacene en la posicion 0. Lo de arriba es una prueba
            //rcv_buf[state-2]=incoming; // El resto de los bytes los guardamos aquí. Podemos elegir el tamaño del buffer

      }
   //   if (state==54)
   //   {
    //      fprintf(DEBUGG,rcv_buf);
   //   }
    }

    else
    {
        i2c_write(I2CS,wrt_buf[state-0x80]);
    }
}

//================================================================================================================================================





//************************************************************************************************************************************************
//**************            FUNCIONES            *************************************************************************************************
//************************************************************************************************************************************************

//================================================================================================================================================

void inicializaPIC(void){
   SET_TIMER1(0);
   setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF); //Internal Oscillator block at 8MHz
   setup_timer_1(T1_INTERNAL|preEscalerTMR1);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(global);
   enable_interrupts(INT_SSP);
 for(i=0;i<64;i++){
   rcv_buf=0xff;  // Para borrar el buffer
 }
 for(i=0;i<32;i++){
   wrt_buf=0xff;  // Para borrar el buffer
 }
//wrt_buf[0]=0x0a;
//wrt_buf[1]=0x0b;
   SET_TIMER1(0);
   estado++;
   return;
}

//================================================================================================================================================

void escrituraMI2c(void){
}

//================================================================================================================================================

void lecturaMI2c(void){
   while(1)
   {
      if(bandera==true){
         //fprintf(DEBUGG,"hola");
         fprintf(DEBUGG,rcv_buf);
         bandera=false;
         SET_TIMER1(0);
      }
   }
}

//================================================================================================================================================



//================================================================================================================================================
//----------------------- PROGRAMA PRINCIPAL -----------------------------------------------------------------------------------------------------
//================================================================================================================================================

void main(){  

   estado = 0;

   while(TRUE){

      switch (estado){
  
         case 0:
            inicializaPIC();
            break;
         case 1:
            lecturaMI2c();
         //escrituraMI2c();      
            break;
         default:
            reset_cpu();
            break;
      }
   }  
}
« Última modificación: 14 de Octubre de 2010, 09:19:26 por mizzard »


 

anything