Basit spi problemi

Başlatan pikcimametusta, 22 Kasım 2016, 20:13:02

pikcimametusta

Yapmak istediğim masterdan slaveye bir emir göndermek slaveden de if else yapısıyla uygun emri seçip geri göndermek.Resimde slave2yi bosverin.Simdilik
Master ile slave1 arasında iletişim yapmaya çalısıyorum
MASTER:
#define FLAG SSPSTATbits.BF
void SpiInit(void)
{
    
    TRISCbits.TRISC3 = 0;   // SCLK => 0-MASTER MODE // 1-SLAVE MODE
    TRISCbits.TRISC4 = 1;   // MISO(Master oldugu için) 
    TRISCbits.TRISC5 = 0;   // MOSI (Master oldugu için) 
    TRISAbits.TRISA1 = 0;
    TRISAbits.TRISA2 = 0;
    PORTAbits.RA1    = 1;
    PORTAbits.RA2    = 1;
    
    SSPSTATbits.SMP = 1;    // 1-Master Mode (Input data sampled at end of data output time)
    SSPSTATbits.CKE = 0;    // Transmit occurs on transition from Idle to active clock state
    
    SSPCONbits.SSPEN = 1; // Enables serial port and configures SCK, SDO, SDI, and SS as serial port pins 
    SSPCONbits.CKP   = 0; // Idle state for clock is a low level
    
    //SPI Master Mode - Clock=Fos / 64
    SSPCONbits.SSPM3 = 0;
    SSPCONbits.SSPM2 = 0;
    SSPCONbits.SSPM1 = 1;
    SSPCONbits.SSPM0 = 0;
    
}

char str[10];

void main(void) {
    
    ADCON1 = 0x07;
    
    Lcd_Init();
    SpiInit();
    

    while(1)
    {
        Lcd_Clear();
        
        SS1 = 0;
        SSPBUF = 1;
        Lcd_Set_Cursor(1,1);
        Lcd_Write_String("Sending...");
        delay();
        while(!FLAG);
        _delay(10);
        sprintf(str,"%d", SSPBUF);
        SS1 = 1;
        Lcd_Set_Cursor(2,1);
        Lcd_Write_String("Received...");    
        delay();

    }
    
    return;
}



SLAVE:

#define FLAG SSPSTATbits.BF
void SpiInit(void)
{
    SSPCONbits.SSPEN = 0;
    TRISCbits.TRISC3 = 1;   // SCLK => 0-MASTER MODE // 1-SLAVE MODE
    TRISCbits.TRISC4 = 1;   // MOSI(Slave oldugu için) 
    TRISCbits.TRISC5 = 0;   // MISO(Slave oldugu için) 
    TRISAbits.TRISA5 = 1;
    
    SSPSTATbits.SMP = 1;    // 0-Slave Mode (SMP must be cleared when SPI is used in Slave mode)
    SSPSTATbits.CKE = 0;    // Transmit occurs on transition from Idle to active clock state
    
    SSPCONbits.SSPEN = 1; // Enables serial port and configures SCK, SDO, SDI, and SS as serial port pins 
    SSPCONbits.CKP   = 0; // Idle state for clock is a low level
    
    //SPI Slave Mode - Clock=SCK pin - SS pin control enabled
    SSPCONbits.SSPM3 = 0;
    SSPCONbits.SSPM2 = 1;
    SSPCONbits.SSPM1 = 0;
    SSPCONbits.SSPM0 = 0;
    
}

unsigned int _received_number;
void main(void) 
{
    ADCON1 = 0x07;
    SpiInit();
    
    SSPBUF = 0x06;
    
    while(1)
    {

       SSPBUF = 0xFF;
       while(!FLAG); 

    }
    
    return;
}