- Tue Dec 28, 2010 12:28 pm
#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.
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.