/*
The spi6674 code bit bangs the thermocouple reading Max6674 and stores it in spihi (MSB)
and 3 bits in spilo (lsb).
*/
#include <system.h>
#include "spi6674.h"
void SPI_Setup(void)
{
clear_bit(trisb, 0); // Set up Relay bit
set_bit(trisb, SO); //SO Line input (1) RB1 2nd bit
clear_bit(trisb, SCK); //SCK Line is an output (0) RB2
clear_bit(trisb, CS); //CS Chip Select as output RB3
}
void SPI_Read(void)
{
clear_bit(portb, SCK); //Initialization
set_bit(portb, CS);
spihi = 0;
spilo = 0;
delay_ms(250);
clear_bit(portb, CS);
//0
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 7); //Read SO on RB2 and set bit value of spihi
clear_bit(portb, SCK); //This is bit 15 and is a dummy bit
// delay_ms(10);
//1
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 6); //This is bit 14 and is the MSB of reading
clear_bit(portb, SCK);
// delay_ms(10);
//2
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 5);
clear_bit(portb, SCK);
// delay_ms(10);
//3
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 4);
clear_bit(portb, SCK);
// delay_ms(10);
//4
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 3);
clear_bit(portb, SCK);
// delay_ms(10);
//5
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 2);
clear_bit(portb, SCK);
// delay_ms(10);
//6
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 1);
clear_bit(portb, SCK);
// delay_ms(10);
//7
set_bit(portb, SCK);
if (portb & 2) set_bit(spihi, 0);
clear_bit(portb, SCK);
// delay_ms(10);
//Write Low Bit
//0
set_bit(portb, SCK);
if (portb & 2) set_bit(spilo, 2);
clear_bit(portb, SCK);
// delay_ms(10);
//1
set_bit(portb, SCK);
if (portb & 2) set_bit(spilo, 1);
clear_bit(portb, SCK);
//delay_ms(10);
//2
set_bit(portb, SCK);
if (portb & 2) set_bit(spilo, 0); //This is bit 5 that Max6674 outputs and is the LSB
clear_bit(portb, SCK); //Bits 0-4 are ignored
//delay_ms(10);
}
Copyright © 2002-2006 SourceBoost Technologies