#include // flag to indicate if SPI is currently involved in a transfer or not // stops a write to the SPI bus while a write is in progress static bit mspibusy; unsigned char daten_send = 10; unsigned char daten_empf; sbit LED = 0xA0; /*********************************************************************** DESC: Writes a byte to the SPI bus. spi_init must be called first RETURNS: Nothing ************************************************************************/ void spi_write (unsigned char dat) // byte to write { // wait for previous transfer to complete while (mspibusy); // SPI now busy mspibusy = 1; // write data to SPI bus SPDAT = dat; } // spi_write /*********************************************************************** DESC: Writes a byte to the SPI bus and returns the byte received from the slave. spi_init must be called first RETURNS: Byte received from slave ************************************************************************/ unsigned char spi_transfer( unsigned char dat ) // byte to write { // wait for previous transfer to complete while (mspibusy); // SPI now busy mspibusy = 1; // write data to SPI bus SPDAT = dat; // wait for transfer to complete while (mspibusy); // return new data received return SPDAT; } // spi_transfer /*********************************************************************** DESC: SPI Interrupt Service Routine Called when byte transmitted and/or byte received or if mode changed to slave by Slave Select pin RETURNS: Nothing ************************************************************************/ void spi_isr( void) interrupt 9 using 1 { // clear SPIF bit by writing 1 to it SPSTAT |= 0xFF; // SPI not busy mspibusy = 0; } // spi_isr /*********************************************************************** DESC: Initializes the SPI peripheral SPI Clock frequency = 93.75 kHz Set EA to 1 after calling RETURNS: Nothing ************************************************************************/ void spi_init (void ) { // set MOSI, MISO, /SS and SPICLK as quasi-bidirectional P2M1 = 0; P2M2 = 0; // configure SPI SPCTL = 0xD7; // SPI initially not busy mspibusy = 0; // set SPI interrupt priority to 0 IP1 &= ~0x08; IP1H &= ~0x08; // enable SPI interrupt ESPI = 1; } // spi_init void led () interrupt 2 { static unsigned char i=0; if( i%2 == 0) { daten_send = 10; SPDAT = daten_send; } else { daten_send = 8; SPDAT = daten_send; } i++; } void main() { spi_init(); SPDAT = daten_send; EX1 = 1; IT1 = 1; EA=1; while(1) { } }