Autor Tema: Ayuda comunicacion SPI con Dspic33fj128gp802  (Leído 1959 veces)

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

Desconectado efryend

  • PIC10
  • *
  • Mensajes: 1
Ayuda comunicacion SPI con Dspic33fj128gp802
« en: 09 de Mayo de 2011, 10:26:38 »
Hola a todos, bueno basicamente les  pido de su ayuda, ya que necesito realizar un proyecto pero para este necesito comunicar dos dspic33fj128gp802, he realizado la configuracion de los registros para el esclavo y el maestro pero solo he conseguido que el maestro mande informacion, mientras que el esclavo no se si no la resive o no la puede interpretar, no se si falla algo en el maestro o esclavo, si alguien sabe la respuesta porfavor.

para el maestro :

#include <p33FJ128GP802.h>
#include <SPI.h>

 //Configuracion inicial de los registros
_FBS ( RBS_NO_RAM & BSS_NO_FLASH & BWRP_WRPROTECT_OFF );
_FSS ( RSS_NO_RAM & SSS_NO_FLASH & SWRP_WRPROTECT_OFF );
_FGS ( GSS_OFF & GWRP_OFF );
_FOSCSEL ( IESO_OFF & FNOSC_PRIPLL );
_FOSC ( FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT );
_FPOR ( ALTI2C_OFF & FPWRT_PWR128 );
_FICD ( JTAGEN_OFF & ICS_PGD1 );

void escribe_SPI (short muestras)
{
   
   PORTBbits.RB6 = 0x00;
   while (PORTBbits.RB5 == 0x01)
   {   
      PORTBbits.RB12 = 0x01;
      asm("nop");
      WriteSPI1(0x09);
      if( DataRdySPI1() )
      PORTBbits.RB6 = 0x01;
      PORTBbits.RB12 = 0x00;
   }
   
}

void retardo (void)
{
   short cont;
   for (cont = 0; cont<255 ; cont++ );
}

void Iniciar_SPI(void)
{
   SPI1STAT = 0x0;
   SPI1STATbits.SPIROV = 0x00;
   SPI1CON1bits.DISSCK = 0x00;
   SPI1CON1bits.DISSDO =0x00;
   SPI1CON1bits.MODE16 = 0x01;
   SPI1CON1bits.SMP = 0x00;
   SPI1CON1bits.CKE = 0x00;
   SPI1CON1bits.SSEN = 0x00;
   SPI1CON1bits.CKP = 0x00;
   SPI1CON1bits.MSTEN = 0x01;
   SPI1CON1bits.SPRE = 0x07;
   SPI1CON1bits.PPRE = 0x01;
   SPI1CON2 = 0x00;
   SPI1STATbits.SPIEN = 0x01;
   SPI1STATbits.SPIRBF = 0x00;
   SPI1STATbits.SPITBF = 0x00;
}

int main ()
{
   short mues;
   RCONbits.SWDTEN = 0;
   CLKDIVbits.FRCDIV = 0;
   PLLFBD = 30;
   CLKDIVbits.PLLPOST = 0;
   CLKDIVbits.PLLPRE = 0;
   while ( OSCCONbits.LOCK != 1);
   
   TRISB = 0x0;
   OSCCONbits.IOLOCK = 0x00;
   
   RPOR5bits.RP10R = 0x08;
   RPOR5bits.RP11R = 0x07;
   RPOR6bits.RP12R = 0x09;
   RPINR20bits.SDI1R = 0x0f;

   while(1)
   {
      Iniciar_SPI();
      for ( mues = 0 ; mues < 500 ; mues++)
         {
            escribe_SPI(mues);
            retardo();
         }
   }
}


Esclavo:

#include <p33FJ128GP802.h>
#include <SPI.h>

// Configuracion inicial de los registros
_FBS ( RBS_NO_RAM & BSS_NO_FLASH & BWRP_WRPROTECT_OFF );
_FSS ( RSS_NO_RAM & SSS_NO_FLASH & SWRP_WRPROTECT_OFF );
_FGS ( GSS_OFF & GWRP_OFF );
_FOSCSEL ( IESO_OFF & FNOSC_PRIPLL );
_FOSC ( FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_XT );
_FPOR ( ALTI2C_OFF & FPWRT_PWR128 );
_FICD ( JTAGEN_OFF & ICS_PGD1 );


void retardo (void)
{
   short cont;
   for (cont = 0; cont < 255 ; cont++ );
}

void Iniciar_SPI(void)
{
   SPI1STAT = 0x0;
   SPI1BUF = 0X00;
   SPI1STATbits.SPIROV = 0x00;
   SPI1CON1bits.DISSDO =0x00;
   SPI1CON1bits.MODE16 = 0x01;
   SPI1CON1bits.SMP = 0x00;
   SPI1CON1bits.CKE = 0x01;
   SPI1CON1bits.SSEN = 0x00;
   SPI1CON1bits.CKP = 0x00;
   SPI1CON1bits.MSTEN = 0x00;
   SPI1CON1bits.SPRE = 0x06;
   SPI1CON1bits.PPRE = 0x02;
   SPI1CON2bits.FRMEN = 0x00;
   SPI1CON2bits.SPIFSD = 0x01;
   SPI1CON2bits.FRMPOL = 0x00;
   SPI1CON2bits.FRMDLY = 0x00;
   SPI1STATbits.SPIEN = 0x01;
}

int main ()
{   
   RCONbits.SWDTEN = 0;
   CLKDIVbits.FRCDIV = 0;
   PLLFBD = 30;
   CLKDIVbits.PLLPOST = 0;
   CLKDIVbits.PLLPRE = 0;
   while ( OSCCONbits.LOCK != 1);
   
   TRISB = 0x0;
   OSCCONbits.IOLOCK = 0x00;
   
   RPINR20bits.SCK1R = 0x10;
   RPINR20bits.SDI1R = 0x11;
   RPINR21bits.SS1R = 0x12;
   RPOR6bits.RP13R  = 0x07;
   int x;
   while(1)
   {   PORTBbits.RB4 = 0x00;
      
      x = ReadSPI1();
      if (x==0)
      PORTBbits.RB9 = 0x01;
      else
      PORTBbits.RB8 = 0x01;
      asm("nop");
      WriteSPI1(0xf0);
   
      PORTBbits.RB4 = 0x01;
   }
}