Нужна помощь с шиной SPI ADXL345 для AtMega644

Привет, я пытаюсь подключить шину SPI на AtMega644 к акселерометру ADXL345. Я всегда получаю 0 обратно, и я не ошибаюсь. Любая помощь приветствуется. Я использую avr-gcc, а не кодовую базу Arduino. Спасибо

#define F_CPU 18000000
#define BAUDRATE 115200
#define UBRRVAL (F_CPU/(BAUDRATE*16UL)) -1
#define SPI_CS      PB0
#define DDR_SPI     DDRB
#define DD_MOSI     PB5
#define DD_MIOS     PB6
#define DD_SCK      PB7

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#include "serial.h"

/**
 *
 */
void clear_spics(){
    PORTB |= _BV(SPI_CS); // high
    delay_ms(30);
}

/**
 *
 */
void set_spics(){
    PORTB &=~ _BV(SPI_CS); // low
    delay_ms(30);
}

/**
 *
 */
void InitSPI(){
    // make the MOSI, SCK, and SS pins outputs
    DDRB |= ( 1 << DD_MOSI ) | ( 1 << DD_SCK ) | ( 1 << SPI_CS );

    // make sure the MISO pin is input
    DDRB &= ~( 1 << DD_MIOS );

    /* Enable SPI, Master, set clock rate fck/128 */
    SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);
}

/**
 *
 */
unsigned char WriteByteSPI(unsigned char cReg, unsigned char cData){
    set_spics();
    /* Start transmission send register */
    SPDR = cReg;
    /* Wait for transmission complete */
    while(!(SPSR & (1<<SPIF)))
        { /* NOOP */ }

    clear_spics();
    set_spics();
    SPDR = cData;
    /* Wait for transmission complete */
    while(!(SPSR & (1<<SPIF)))
        { /* NOOP */ }

    clear_spics();
    return SPDR;
}


int  main(){
    char  data;
    USART_Init();
    InitSPI();
    //tell axdl345 use 4 wire spi communication
    WriteByteSPI(0x31,0x00);



    for(;;){
        data = WriteByteSPI(0x36,0);
        send_byte(data);
        delay_ms(2000);
    }

    //never get here
    return 0;
}

person Jim    schedule 16.06.2011    source источник


Ответы (1)


это то, что мне было нужно. Мне нужно было установить бит чтения с маской 0x80.

/**
 * This writes to a register with the data passed to the address passed
 * @param unsigned char cReg - the address of the register you wish to write to
 * @param unsigned char cData - the data you wish to write to the register
 */
unsigned char WriteByteSPI(unsigned char cReg, unsigned char cData){
   set_spics();
   /* Start transmission send register */
   SPDR = cReg;
   /* Wait for transmission complete */
   while(!(SPSR & (1<<SPIF)))
       { /* NOOP */ }
   SPDR = cData;
   /* Wait for transmission complete */
   while(!(SPSR & (1<<SPIF)))
      { /* NOOP */ }
   clear_spics();
   return SPDR;
   }

/**
 * This adds the read bit to the address and passes it to the Write command
 * @param cReg - unsigned char the register you wish to read
 */
unsigned char ReadByteSPI(unsigned char cReg){
    return WriteByteSPI( (cReg | ADXL345_SPI_READ),0xAA);
}
person Jim    schedule 17.06.2011