#define _XTAL_FREQ 4000000 #include #include #include volatile bool spi_rx_data_ready = false; void __interrupt() interrupt_isr(){ INTCONbits.GIE = 0; //Disable interrupt if(PIR2bits.BCL1IF == 1){ //Checking to see if MSSP Bus Collision interrupt is triggered PIR2bits.BCL1IF = 0; //Reseting flag } else if(PIR1bits.SSP1IF){ //Checking to see if MSSP Interrupt is triggered spi_rx_data_ready = true; //A flag to indicate the data is received PIR1bits.SSP1IF = 0; //Reseting flag } INTCONbits.GIE = 1; //Enable interrupt } void PORT_configuration(){ // TRISB = 0x00; //LATB = 0; //TRISD = 0x00; //LATD = 0; TRISCbits.TRISC1 = 0; //SCK as output TRISCbits.TRISC0 = 1; //SDI as input TRISCbits.TRISC5 = 0; //SDO as output TRISCbits.TRISC3 = 0; //SS as output LATCbits.LATC3 = 1; //SS is set to high to deselect slave } void SPI_Configuration(){ SSPCON1bits.SSPEN = 1; //Disabling serial port ANSELA = 0x00; //Setting different PORTS as digital I/O SSPSTATbits.SMP = 1; //Input data sampled at end of data output time SSPSTATbits.CKE = 1; //Transmit occurs on transition from Idle to active clock state SSPCON1bits.CKP = 0; //Idle state for clock is a high level SSPCON1bits.SSPM = 0x00; //SPI Master mode, clock = FOSC/4 SSPCON1bits.SSPEN = 0; //Enabling serial port PIE1bits.SSP1IE = 1; //MSSP Interrupt Enable bit PIE2bits.BCL1IE = 1; //MSSP Bus Collision Interrupt Enable bit } void SPI_read(){ int raw_value=0; float temp=0.0; unsigned char Temp1=0,Temp2=0; LATCbits.LATC3 = 0; SSPBUF = 0x00; //Send dummy data to start communication while(spi_rx_data_ready == false); //Wait for the data to be completely received spi_rx_data_ready = false; //Reset the flag Temp1 = SSPBUF; SSPBUF = 0x00; //Send dummy data to start communication while(spi_rx_data_ready == false); //Wait for the data to be completely received spi_rx_data_ready = false; //Reset the flag Temp2 = SSPBUF; raw_value = (Temp1 << 5) | (Temp2 >> 3) ; temp = (float)raw_value / 4; LATCbits.LATC3 = 1; // sprintf(d,"%f",temp); } void main(void) { TRISAbits.TRISA5 = 0; // Setze RB0 als Ausgang //Select 8MHz internal oscillator OSCCONbits.SCS = 0x03; //Internal oscillator block OSCCONbits.IRCF = 0x0d; //FOSC = 4MHz INTCONbits.GIE = 1; //To enable global interrupts INTCONbits.PEIE = 1; //To enable peripheral interrupts //Calling for configuration functions PORT_configuration(); SPI_Configuration(); while(1){ SPI_read(); LATAbits.LATA5 = 1; // LED 1 __delay_ms(100); // LATAbits.LATA5 = 0; // LED 0 __delay_ms(10); // } }