Hey Guys
so i’ve been trying to get my ADXL 345 to talk back to me.
my code is given below.
the issue im having is that the code gets to the point where it pages the sensor using SPI and then configures the POWER_CNTRL register. i then try and ask the sensor to relay back to me the POWER_CNTRL registers contents.
i get the "Pwr= " looping to hyperterminal.
and on accasion i get a value for pcheck, if i do its a random ASCII charactor and not 0x28 which i believe translates to ‘(’ in ASCII. and as it never equals 0x28. it never leaves this loop.
any possible reasons? is my code wrong? is the chip damaged?
any help would be great.
#include <avr/io.h>
void delay_ms(uint16_t x)
{
uint8_t y, z;
for ( ; x > 0 ; x--){
for ( y = 0 ; y < 90 ; y++){
for ( z = 0 ; z < 6 ; z++){
asm volatile ("nop");
}
}
}
}
void SPI_Init() {
/* Set MOSI and SCK output, all others input */
DDRB = (1<<DDB3)|(1<<DDB5);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void SPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}
void send_byte(uint8_t byte) {
/* Start transmission */
SPDR = byte;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}
void Init_Accel() {
uint8_t pcheck = 0x00;
while (pcheck != 0x28) {
PORTC = 0b00000000;
send_byte(0x2D);
send_byte(0x28);
PORTC = 0b00110000;
delay_ms(5);
PORTC = 0b00000000;
send_byte(0x2D);
send_byte(0x00);
pcheck = SPDR;
PORTC = 0b00110000;
USART_Transmit('P');
USART_Transmit('w');
USART_Transmit('r');
USART_Transmit('=');
USART_Transmit(pcheck);
USART_Transmit('\n');
USART_Transmit('\r');
delay_ms(100);
}
}
void USART_Init(){
UCSR0A = 0x00;
UCSR0B = 0x18;
UCSR0C = 0x86;
UBRR0H = 0x00;
UBRR0L = 0x19;
}
void USART_Transmit( unsigned char data ) {
// Wait for empty transmit buffer */
while ( !( UCSR0A & 0x20) );
// Put data into buffer, sends the data */
UDR0 = data;
}
int main() {
DDRC = 0b00110000;
PORTC = 0b00110000;
delay_ms(10000);
USART_Init();
SPI_Init();
Init_Accel();
USART_Transmit('W');
USART_Transmit('o');
USART_Transmit('r');
USART_Transmit('l');
USART_Transmit('d');
USART_Transmit('\n');
USART_Transmit('\r');
return 0;
}