示例#1
0
void spi_init(void) 
{
ANSELA = 0;
ANSELC = 0;
RST_T = 0;
RST = 1;
dly_ms(20);
TRISC0 = 0;
TRISC2 = 0;
SSPSTAT = 0xC0;
SSPCON1 = 0x2A;
SSP1ADD = 100;
sck_dur = 1;
spi_set_sck_duration(sck_dur);
RST = 0;
spi_reset_pulse();
}
void spi_init(void) 
{
        SPICS=1; 		// pb2 as reset pin is output, this is also the ss pin which
                    	// must be output for master to work.
        SPICS_TRIS=0;	// reset = high = not active

		// now output pins low in case somebody used it as output in his/her circuit
		//MOSI, SCK is output/low
		SPI1STATbits.SPIEN = 0;//disable, just in case...

		#ifdef OUTPUT_HIGH_IMPEDANCE
			SPIMOSI_ODC=1;
			SPICLK_ODC=1; 
			SPICS_ODC=1;
		#else
			SPIMOSI_ODC=0;
			SPICLK_ODC=0; 
			SPICS_ODC=0;
		#endif
		// Inputs 
		RPINR20bits.SDI1R=7; 		//B7 MISO
		// Outputs
		RPOR4bits.RP9R=SDO1_IO; 	//B9 MOSI
		RPOR4bits.RP8R=SCK1OUT_IO; 	//B8 CLK	
		//pps configures pins and this doesn't really matter....
		SPICLK_TRIS=0; 			//B8 sck output
		SPIMISO_TRIS=1; 		//B7 SDI input
		SPIMOSI_TRIS=0; 		//B9 SDO output
		SPICLK=0; 			
		SPIMISO=0; 		
		SPIMOSI=0; 		

        delay_ms(20); // discharge MOSI/SCK

        //if (spi_in_sw==0){
                /* Enable SPI, Master, set clock rate fck/16 */
                spi_set_sck_duration(sck_dur);
                // now SCK and MOSI are under control of SPI
        //}

        SPICS=0; // reset = low, stay active
        delay_ms(20); // stab delay
        spi_reset_pulse();
}