SPI MAX6675
RB0 - SPI_DATA - SO - Pin 7
RD7 - SPI_CLOCK - SCK - Pin 5
RD6 - SPI_CS - CS - Pin 6
- T- (alumel) - Pin 2
- T+ (chromel)- Pin 3
*/
#include
#include
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);
int i, j, k, n; // Use Global Variables for Debug
// 1234567890123456
const char TopMessage[] = "Current: ";
const char BotMessage[] = "Setpoint: ";
const char Units[] = "F ";
#define E RA4 // Define the LCD Control Pins
#define RS RA5
#define SPI_CLOCK RD7 // Define the Max6675 pins
#define SPI_CS RD6
#define SPI_DATA RB2
#define Out RB5 // Define the output pin to control the SSR
const int Twentyms = 1250; // Declare a Constants for delays
const int Fivems = 300;
const int TwoHundredus = 10;
int SetPoint = 240; // Initialize the SetPoint
static unsigned int relay_timer; // timer value for relay driver
int GetTemp(void)
{
int CurrentTemp = 0; // Reset value of Current Temp
SPI_CS = 0; // Force CS low to stop conversion
SPI_CLOCK = 1; // discard bit 15
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 512;
SPI_CLOCK = 1; // bit 14
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 256;
SPI_CLOCK = 1; // bit 13
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 128;
SPI_CLOCK = 1; // bit 12
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 64;
SPI_CLOCK = 1; // bit 11
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 32;
SPI_CLOCK = 1; // bit 10
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 16;
SPI_CLOCK = 1; // bit 9
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 8;
SPI_CLOCK = 1; // bit 8
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 4;
SPI_CLOCK = 1; // bit 7
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 2;
SPI_CLOCK = 1; // bit 6
SPI_CLOCK = 0;
CurrentTemp = CurrentTemp + SPI_DATA * 1;
SPI_CLOCK = 1; // bit 5
SPI_CLOCK = 0;
SPI_CLOCK = 1; // discard bits 4 and 3
SPI_CLOCK = 0; // decimal values of temp
SPI_CLOCK = 1;
SPI_CLOCK = 0;
SPI_CLOCK = 1; // bit 2 -- fault bit
SPI_CLOCK = 0;
SPI_CLOCK = 1; // bit 1 -- always 0
SPI_CLOCK = 0;
SPI_CLOCK = 1; // bit 0 -- tri-state
SPI_CLOCK = 0;
SPI_CS = 1; // SPI bus is OFF
return(CurrentTemp);
}
main()
{
int CurrentTemp = 0;
// Initialize the I/O ports
PORTA = 0; // Start with Everything Low
CMCON0 = 7; // Turn off Comparators
ANSEL = 0; // Turn off ADC
TRISA = 0; // All of PORTA are Outputs
TRISB = 0b00011111; // Port B bits 7, 6, 5, 4, 3 are output
TRISD = 0;
T0IE = 1; // Enable interrupt on TMR0 overflow
INTEDG = 1; // falling edge trigger the interrupt
INTE = 1; // enable the external interrupt
GIE = 1; // Global interrupt enable
SetPoint = 240; // Reset the Setpoint
// Initialize LCD
j = Twentyms;
for (i = 0; i < j; i++); // Wait for LCD to Power Up
PORTA = 3; // Start Initialization Process
E = 1; E = 0; // Send Reset Command
j = Fivems;
for (i = 0; i < j; i++);
E = 1; E = 0; // Repeat Reset Command
j = TwoHundredus;
for (i = 0; i < j; i++);
E = 1; E = 0; // Repeat Reset Command Third Time
j = TwoHundredus;
for (i = 0; i < j; i++);
PORTA = 2; // Initialize LCD 4 Bit Mode
E = 1; E = 0;
j = TwoHundredus;
for (i = 0; i < j; i++);
// Main control loop
while (1 == 1)
{
j = Twentyms*10; // Wait 200ms between loops
for (i = 0; i < j; i++);
CurrentTemp = GetTemp() * 1.8 + 32; // Get current temp and convert to F
}
} // End Main