Hi all,
I'm programming a PIC micro-controller using C and there's a little problem with the code:
Code:
#include <pic.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);
// Variables
long int whatIwant;
#define FOSC 8000000L // Internal Oscillator to 8Mhz
#define CS RA4 // AD Converter
#define SDI RB4 // AD Converter
#define SCK RB6 // AD Converter
// Read AD Converter Function
long int Read3551(void)
{
short b;
long int result=0;
char byte[3], TrashByte; // Temp Bytes for reading
CS=0; // AD Chip Select low to read
while (SDI==1); // Wait for SDO/RDY to become low (AD internal conversion complete)
for (b=0; b<3; b++) // Loop for reading Byte 1, 2 and 3
{
SSPBUF=0b00001111; // Garbage send to start clocking in AD Data Byte 3
while (BF==0); // Wait for Buffer Full Status Bit to be set
byte[b]=SSPBUF; // get byte
}
if (byte[2] < 32) // Last 3 Bits must be zero (no overflow and no negative sign)
result = ((byte[0] <<16) + (byte[1] <<8 + (byte[2])); //Add the Bytes to get the whole Value
CS=1; // AD Chip select high to stop AD sending
return result; // return the Value
}
main() // ------- MAIN -------
{
// declaration
OSCCON = 0x75; // Oscillator to 8 Mhz
ANSEL = 0; // Turn off ADC
TRISB6= 0; // SCK (AD) is Output (Master Mode)
TRISA4= 0; // RA4 (CS from AD) is Output
CS=1; // CS high (AD stops sending)
SSPCON= 0b00110000; // 00, Enable SPI (SSPEN) , Idle State for SDI clock is high level (CKP), Fosc:4
SMP= 0; // SPI Input Data sampled at middle of data output time
CKE= 0; // SDI Data transmitted on rising Edge of SCK
// Infinite loop with main program
for (;;)
whatIwant=Read3551();
} // End of MAIN
This part of the program is supposed to read the data from the SPI interface and somehow it doesn't work.
Can someone please help me out here? Thanks!