PIC18

PIC18, MPLAB X IDE, XC8, SPI, at45dbxx data flash

CoyoteUgly 2018. 9. 13. 22:05

PIC18, MPLAB X IDE, XC8, SPI, at45dbxx data flash



4MHz frequency 에서 spi clock으로 1MHz를 사용합니다.


- RC3 : CLK
- RC4 : SDI
- RC5 : SDO
- RA5 : CS


0. 참고 사항


1. HW 연결


2. 소스 설명

full 소스는 아래 github를 통해 확인하세요.




void init_spi( unsigned char sync_mode, unsigned char bus_mode, unsigned char smp_phase)
{
    TRIS_SDI = 1;               // SDI input
    TRIS_SDO = 0;               // SD0 output   
    
    SSPSTAT &= 0x3F;        // power on state
    
    SSPCON1 = 0x00;         // power on state
    SSPCON1 |= sync_mode;   // select serial mode       
                                // 0000 = SPI Master mode, clock = FOSC/4
   
    SSPSTAT |= smp_phase;   // select data input sample phase

    switch( bus_mode )
    {
        case 0:     // SPI bus mode 0,0
            SSPSTATbits.CKE = 1;    // data transmitted on rising edge
            break;    
        case 2:     // SPI bus mode 1,0
            SSPSTATbits.CKE = 1;    // data transmitted on falling edge
            SSPCON1bits.CKP = 1;    // clock idle state high
            break;
        case 3:     // SPI bus mode 1,1
            SSPCON1bits.CKP = 1;    // clock idle state high
            break;
        default:    // default SPI bus mode 0,1
            break;
   }
    
    switch( sync_mode )
    {
        case 4:             // slave mode w /SS enable
            TRIS_SCK = 1;   // define clock pin as input    
            TRIS_CS  = 1;   // define /SS1 pin as input
            break;
        case 5:             // slave mode w/o /SS enable
            TRIS_SCK = 1;   // define clock pin as input    
            break;        
        default:            // master mode, define clock pin as output
            TRIS_SCK = 0;   // define clock pin as output    
            break;
    }

    SSPCON1 |= 0x20;            // enable synchronous serial port,  0b00100000  Enable serial port and configures SCK, SDO, SDI
}


3. 소스


4. 결과