i bought a small board with an acceleration sensor (ADXL345) on it and now i try to write my functions to communicate with the IC. I use an STM8S103F3P6 as controller and ST Visual Develop as IDE. So at first i took a look at the "rising/falling edge schematic":
So at first i want to write the "writeCommand" function, it looks like this, currently:
void writeCommand(uint16_t cmd){
uint8_t i,k;
GPIOD->ODR |= (1<<SDIO); //turn SDIO HIGH to turn it LOW later
GPIOD->ODR &= ~(1<<CS); //turn CS LOW
delay_ms(2);
GPIOD->ODR &= ~(1<<SCLK); //turn SCLK LOW
for(k = 0; k < 2; k++){ //proceed with toggling R/W and multiple-byte bit
GPIOD->ODR ^= (1<<SDIO);
delay_ms(1);
GPIOD->ODR |= (1<<SCLK);
delay_ms(1);
GPIOD->ODR &= ~(1<<SCLK);
}
for(i = 0; i < 15; i++){ //start writing data
if(cmd & 128){
GPIOD->ODR |= (1<<SDIO);
} else {
GPIOD->ODR &= ~(1<<SDIO);
}
GPIOD->ODR |= (1<<SCLK);
cmd << 1;
GPIOD->ODR &= ~(1<<SCLK);
}
delay_ms(1);
GPIOD->ODR |= (1<<CS);
}
I am wondering why there are only 6 (A5...A0) address bits? I am not sure how to concatenate the register i want to write to and the data.
Would this be the right way to call my function?
writeCommand(0x2C << 8 | 0x09); //write 1001 to 0x2C register
I know that it is probably possible to do this with hardware SPI but i want to learn how to do it by software. So maybe a few questions sound retarded but i hope you will still answer them :)