SPI. C function:
The static void Spi_vSpi2SetPin (void)
{
/* Chose SPI2: so SDI2 - RG7 SDO2 - RG8 */
TRISGbits. TRISG7=1; RG7 - input/* */
TRISGbits. TRISG8=0;/* RG8 - output */
/* use RD12 for CS signal */
TRISDbits. TRISD12=0;/* RD12 - output */
return;
}
The static void Spi_vSpi2Write (unsigned char Data)
{
SPI2BUF=Data & amp; 0 XFF.
While (SPI2STATbits. SPITBF);
Data=https://bbs.csdn.net/topics/SPI2BUF; *//* avoiding overflow the when reading
return;
}
The static unsigned char Spi_u8Spi2Read (void)
{
Unsigned char Data=https://bbs.csdn.net/topics/0;
SPI2STATbits. SPIROV=0;
while(! SPI2STATbits. SPIRBF);/* Check for the Receive buffer full status bits of the status register */
If (SPI2STATbits. SPIRBF)
{
SPI2STATbits. SPIROV=0;
Data=https://bbs.csdn.net/topics/SPI2BUF;
}
Return (Data);
}
Void Spi_vSpi2Initial (void)
{
Spi_vSpi2SetPin ();
/* intial SPI2STAT SFR */
SPI2STATbits. SPIEN=0;/* bits 15: disable SPI */
SPI2STATbits. SPISIDL=0;/* bit 13: still work in free mode */
SPI2STATbits. SPIBEC=0;/* - 10-8: it will work in ESPI */
SPI2STATbits. SRMPT=0;/* bit 7: it will work in ESPI */
SPI2STATbits. SPIROV=0;/* bit 6: ont the receive Ooerflow */
SPI2STATbits. SRXMPT=0;/* bits 5: it will work in ESPI */
SPI2STATbits. SISEL=0;/* - 4-2: it will work in ESPI */
SPI2STATbits. SPITBF=0;/* bit 1: auto set or clear by the hardware */
SPI2STATbits. SPIRBF=0;/* bit 0: auto set or clear by the hardware */
/* initial SPI2CON1 SFR */
SPI2CON1bits. DISSCK=0;/* bit 12: use internal SPI clock */
SPI2CON1bits. DISSDO=0;/* bit 11: SDO2 control by SPI */
SPI2CON1bits. MODE16=0;/* bit 10: communication by 8 bits */
SPI2CON1bits. SMP=0;/* bit 9: input data during the data output */
SPI2CON1bits. CKE restaurants did=1;/* bit 8: change the data when mode from work to free */
SPI2CON1bits. SSEN=0;/* bit 7: SSx control by SPI */
SPI2CON1bits. CKP=0;/* bit 6: work - high level; Free - low level */
SPI2CON1bits. MSTEN=1;/* bit 5: enable the Master mode */
/* Fsck=Fcy/(SPRE * PPRE), Fsck=30 MHz/(3 * 4)=2.5 MHz */
SPI2CON1bits. SPRE=5;/* - 4-2: Auxiliary preassigned frequency 3:1 */
SPI2CON1bits. PPRE=2; 1-0:/* bit preassigned frequency 4:1 */
/* initial SPI2CON2 SFR */
SPI2CON2bits. FRMEN=0;/* bit 15: disable SPIx frame mode */
SPI2CON2bits. SPIFSD=0;/* bit 14: Frame synchronization pulse output */
SPI2CON2bits. FRMPOL=0;/* bit 13: Frame synchronization pulse low level */
SPI2CON2bits. FRMDLY=0;/* bit 1: use the same clock */
SPI2CON2bits. FRMEN=0;/* bit 0: use standed mode */
/* set the input and output pins */
RPOR14bits. RP120R=8;
RPOR13bits. RP118R=9;/* SCK2 to G6 */
SPI2STATbits. SPIEN=1;/* bits 15: enable SPI */
}
Void Spi_vSpi2WriteEnable (void)
{
LATDbits. LATD12=0;
Spi_vSpi2Write (SPI_WRITE_EN_COMMAND);
LATDbits. LATD12=1;
return;
}
Void Spi_vSpi2WriteCheck (void)
{
Unsigned char Status;
Do
{
LATDbits. LATD12=0;
Spi_vSpi2Write (SPI_READ_SR_COMMAND);/* Read the STATUS Register command */
Status=Spi_u8Spi2Read ();/* Read the STATUS Register */
LATDbits. LATD12=1;
} while (Status & amp; 0 x01);
}
Void Spi_vSpi2WriteStatusReg (unsigned char StatusData)
{
LATDbits. LATD12=0;
Spi_vSpi2Write (SPI_WRITE_SR_COMMAND);
Spi_vSpi2Write (StatusData);
LATDbits. LATD12=1;
return;
}
Void Spi_vSpi2WriteByte (unsigned char AddressH, unsigned char AddressL, unsigned char Data)
{
Spi_vSpi2WriteCheck ();
LATDbits. LATD12=0;
/* send BTTE WRITE SEQUENCE command */
Spi_vSpi2Write (SPI_WRITE_COMMAND);
16 bits *//* write address:
Spi_vSpi2Write (AddressH);/* send AddressH */
Spi_vSpi2Write (AddressL);/* send AddressL */
/* the write data */
Spi_vSpi2Write (Data);
LATDbits. LATD12=1;
Spi_vSpi2WriteCheck ();
return;
}
Unsigned char Spi_u8Spi2ReadSequence (unsigned char AddressH, unsigned char AddressL)
{
The static unsigned char Data;
LATDbits. LATD12=0;
/* send the write READ SEQUENCE command */
Spi_vSpi2Write (SPI_READ_COMMAND);
/* write the READ DATA address */
Spi_vSpi2Write (AddressH);/* send AddressH */
Spi_vSpi2Write (AddressL);/* send AddressL */
/* read data */
Data=(https://bbs.csdn.net/topics/Spi_u8Spi2Read);
LATDbits. LATD12=1;
Return (Data);
}
The Main c function:
Void InitPorts (void)
{
TRISA=0 x0000;
LATA=0 x00ff;/* LED is all ON */
}
Void delay (void)
{
Unsigned int I, j;
for(i=0; i<3000; I++)
{
for(j=0; j<3000; J++)
;
}
}
Int main (void)
{
PLLFBD=0 x003a;
CLKDIV=0 x0040;
InitPorts ();
delay();
nullnullnullnullnullnullnullnullnullnullnullnullnull