#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 = 0; unsigned char daten_send = 8; 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; // 0x80;;;;;; 0xC0 ka!!!!!!!!!!! // 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; P2 = 0xFF; // 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 { if(mspibusy == 0) { SS=0; daten_empf = spi_transfer(daten_send); SS=1; } } void main() { spi_init(); IT1 = 1; IE1=0; EX1 = 1; EA=1; SS = 1; while(1) { if(daten_empf == 10) { LED = 1; } else { LED = 0; } } }