1. 程式人生 > >中科之旅-------PIC16F18855 SPI初始化

中科之旅-------PIC16F18855 SPI初始化



void PIN_MANAGER_Initialize(void)
{
LATB = 0x03; //RB4 init value 0  0000 0010  MOSI MISO SS = 0 ,SLCK = 1;
    LATA = 0x0;
    //LATC = 0x1;
    LATC = 0x61;
   WPUA = 0xF0;
  //  WPUB = 0xE9;//1001
   WPUB = 0xE4;//0100 RB2 pull up
    WPUC = 0xE0;

    ANSELA = 0xFF;
    ANSELB = 0x00; //RB4 digital out put
   // ANSELC = 0xE5;
   ANSELC = 0x25;
    TRISB = 0xE4;  //RB4 output reset pin  RB0 spi_cs = input RB2 MISO = input 1110 0101
       TRISC = 0xBE;

    TRISA = 0xFF;
 /*

    LATB = 0x0;
    WPUE = 0x8;
    LATA = 0x0;
    LATC = 0x7F; //rc6,rc7


    WPUA = 0xFF;
    WPUB = 0xFF;
    WPUC = 0xFF;
   
    ANSELA = 0xFF;
    ANSELB = 0xFF;
    ANSELC = 0x00;



    TRISB = 0xFF;
    TRISC = 0x80;//rc6,rc7 output
    TRISA = 0xF0;
*/
    
    bool state = GIE;
    GIE = 0;
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS


    //RC0PPS = 0x10;   //RC0->EUSART:TX;
    // RXPPSbits.RXPPS = 0x11;   //RC1->EUSART:RX;
    RC3PPS = 0x16;   //RC3->MSSP2:SDA2;
    RC4PPS = 0x17;   //RC4->MSSP2:SCL2;
    SSP2DATPPSbits.SSP2DATPPS = 0x14;   //RC3->MSSP2:SDA2;
    SSP2CLKPPSbits.SSP2CLKPPS = 0x13;   //RC4->MSSP2:SCL2;


//SSP1DATPPSbits.SSP1DATPPS
    RC6PPS = 0x10;   //RC6->EUSART:TX;
    RXPPSbits.RXPPS = 0x17;   //RC7->EUSART:RX;




//spi config


   RB0PPS = 0x00;
   RB1PPS = 0x14; //spi_sck
   SSP1CLKPPSbits.SSP1CLKPPS = 0x09; //rb1
   
   SSP1DATPPSbits.SSP1DATPPS = 0x0A;  //miso
   RB3PPS = 0x15;   //mosi
 
    PPSLOCK = 0x55;
    PPSLOCK = 0xAA;
    PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS

    GIE = state;

}

mssp初始化

void SPI_Initialize(void) 
{
    // Set the SPI module to the options selected in the User Interface
 // BF RCinprocess_TXcomplete; UA dontupdate; SMP Sample At Middle; P stopbit_notdetected; S startbit_notdetected; R_nW write_noTX; CKE Active to Idle; D_nA lastbyte_address; 

    SSP1STAT = 0x80;
    // SSPEN enabled; WCOL no_collision; SSPOV no_overflow; CKP Idle:Low, Active:High; SSPM FOSC/4; 
    SSP1CON1 = 0x30;

    SSP1ADD = 0x00;
}

有幾個要點,首先是SPI的時序要根據slave來,然後CS管腳要自己去設定,注意clk的頻率。特別是CS在一次通訊中要一直拉低直到本次通訊結束

放個波形