I'm trying to read data from MAX6675 with PIC16f1937. The bottom is my code
#define _XTAL_FREQ 4000000
#include <xc.h>
#include "MAX6675_header.h"
#include <stdbool.h>
#include <string.h>
volatile bool spi_rx_data_ready = false;
void __interrupt() interrupt_isr(){
INTCONbits.GIE = 0; //Disable interrupt
if(PIR2bits.BCLIF == 1){ //Checking to see if MSSP Bus Collision interrupt is triggered
PIR2bits.BCLIF = 0; //Reseting flag
}
else if(PIR1bits.SSPIF){ //Checking to see if MSSP Interrupt is triggered
spi_rx_data_ready = true; //A flag to indicate the data is received
PIR1bits.SSPIF = 0; //Reseting flag
}
INTCONbits.GIE = 1; //Enable interrupt
}
void PORT_configuration(){
TRISB = 0x00;
LATB = 0;
TRISCbits.TRISC3 = 0; //SCK as output
TRISCbits.TRISC4 = 1; //SDI as input
TRISCbits.TRISC5 = 0; //SDO as output
TRISCbits.TRISC6 = 0; //SS as output
LATCbits.LATC6 = 1; //SS is set to high to deselect slave
}
void SPI_Configuration(){
SSPCON1bits.SSPEN = 0; //Disabling serial port
ANSELA = 0x00; //Setting different PORTS as digital I/O
ANSELB = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
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 = 1; //Enabling serial port
PIE1bits.SSPIE = 1; //MSSP Interrupt Enable bit
PIE2bits.BCLIE = 1; //MSSP Bus Collision Interrupt Enable bit
}
void SPI_read(){
unsigned char Temp1=0,Temp2=0;
LATCbits.LATC6 = 0;
__delay_us(0.1); //Wait for 100 nano seconds
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;
LATCbits.LATC6 = 1;
__delay_ms(1000); //For better observation in PROTEUS (Remove for real_time application)
}
void main(void) {
//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();
}
}
But the problem is that the output data of the MAX6675 is 16 bits and the microcontroller's SPI buffer is 8 bit (MAX6675 uses SPI protocol). How can I receive the second 8 bits?
EDITED SPI_READ()
void SPI_read(){
unsigned char Temp1=0,Temp2=0;
LATCbits.LATC6 = 0;
__delay_us(0.1); //Wait for 100 nano seconds
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
LATD = 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
LATB = SSPBUF;
// LATD = Temp1;
// LATB = Temp2;
LATCbits.LATC6 = 1;
__delay_ms(1000); //For better observation in PROTEUS (Remove for real_time application)
}