SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By brunocae
#116203
Hi,

I'm trying to make SPI communication between SCA3000 accelerometer and PIC16F877A, and I'm programming on MikroC IDE, from Microchip. I made a simple code, that reads x-axis acceleration data from the sensor, and shows it on a LCD 16x2 Display, but it doesn't work. All I get is "0". Here is my code:

// ========== Initial Declaration ==========
unsigned char LCD_text[10];
unsigned short buffer;

unsigned short X_MSB, X_LSB;
int x_axis;

// ========== main() function ==========
void main()
{
// ---> Ports' Initialization
TRISA = 0b000000;
TRISB = 0b00000000;
TRISC = 0b00010000;
ADCON1 = 0b00000111; // just to turn off A/D converter

// ---> LCD Initialization
Lcd_Config(&PORTB,4,5,6,3,2,1,0);
Lcd_Cmd(Lcd_CURSOR_OFF);

// ---> SPI Initialization
PORTA.F5 = 1; // Chip Select = 1
Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_END, CLK_Idle_HIGH, LOW_2_HIGH);

// ---> SCA3000 Initializes on measurement mode by defaut
Delay_ms(500); // Waits for SCA3000

// ---> SPI Communication Loop
while(1)
{
PORTA.F5 = 0; // Chip Select = 0
Spi_Write(0b00010100); // Register Address (0x05 = 0b000101) + Read bit(0) + 0
X_MSB = Spi_Read(buffer);
Spi_Write(0b00010000); // Register Address (0x04 = 0b000100) + Read bit(0) + 0
X_LSB = Spi_Read(buffer);
PORTA.F5 = 1; // Chip Select = 1

x_axis = (X_MSB<<8)+(X_LSB);

IntToStr(x_axis,LCD_text); // Convert it into string and show on LCD
Lcd_Out(1,1,LCD_text);

Delay_ms(500);
Lcd_Cmd(LCD_CLEAR);
}
}


Can anyone help me find what's wrong in my code??
thanks.
By waltr
#116211
MikroC IDE, from Microchip
MikroC is not from Microchip. HiTech C is from Microchip.
I don't know what the MikroC library functions do but the pins that are used by the SSP module for SPI need to be set to inputs (TRIS value is 1 for these pins). Maybe that is or is not taken care of by the Spi_Init_ function.

Are you sure you have the SPI SCK polarity correct? there are four different modes.
Code: Select all
Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_END, CLK_Idle_HIGH, LOW_2_HIGH);
According to the SCA3000 manual in 4.1.1 the SCK is IDLE LOW and data changes on the falling edge of SCK. Is that what the SPI init function sets up? Try all four modes if you don't have an O'scope.

Did you look at the SPI outputs with an O'scope to verify that clock polarity and if anything is actually being sent?
What is the SPI clock frequency? How long are the wires from the PIC to the SCA3000?

Have you double checked the connections from the PIC to the SCA3000?
PIC pins (40pin DIP) to SCA3000 breakout connector are:
SCK 18 -> SCK 6
SDI 23 <- MISO 5
SDO 24 -> MOSI 4
CS (PORTA.5) 7 -> CSB 7
GND 12 & 31 - 8

I'll guess that the SPI_READ function sends out eight clocks to clock in data. Verify with O'scope.
Code: Select all
Spi_Write(0b00010100); // Register Address (0x05 = 0b000101) + Read bit(0) + 0
This line looks correct according the the SCA3000 manual in 4.1.1.