1

我刚刚开始了需要将 ADXL345 连接到我的 Atmega16 的项目。我想将设备 ID 放在屏幕上并读取轴。到目前为止,我无法与 ADXL345 通信。ON LCD 仅出现 0 我将不胜感激。

#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include "hd44780.h"
#define F_CPU 1000000UL
/*-----------------------Rejestry ADXL345---------------------*/
char POWER_CTL = 0x2D;  //Power Control Register 0x08 measurement
char DATA_FORMAT = 0x31;
char BW_RATE = 0X2C;
char FIFO = 0x38; //fifo ctl 
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
char DEVICEID = 0x00;
#define  Read_Byte_SPI( addr )   ADXL_write( 0x80 | addr , 0xFF );
/*------------------PORTY SPI---------------*/
#define SPI_CS      PB4
#define DDR_SPI     DDRB
#define DD_MOSI     PB5
#define DD_MIOS     PB6
#define DD_SCK      PB7
/*vcc<->vs*/
//--------------------------------------Funkcje-------------------
unsigned int init_port(void) //ports 4 lcd
{DDRD =0x00; PORTD =0x07 ; }
/*function 4 lcd*/
static void lcd( int a){ LCD_LOCATE(2,0); char buff[2]; char tab[2]; /*itoa(a,buff,10);*/ sprintf(a, %d", buff); lcd_puts(buff); 
  } 
/**
 * 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 ADXL_write(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;
   }


   uint8_t ADXL_read(unsigned char addr) {  // read ADXL345 - 128+ addrs, back 8 bits - found this       in         another topic
  int8_t ans; 
  set_spics(); 
  SPDR = 0x08 | addr; 
  loop_until_bit_is_set(SPSR, SPIF); 
  SPDR = 0xFF;                    
  loop_until_bit_is_set(SPSR, SPIF); 
  ans = SPDR; 
  clear_spics(); 
  return(ans);}
   //inicjalizacjia SPI
   void Init_SPI(){
    // make the MOSI, SCK, and SS pins outputs
    DDRB |= ( 1 << DD_MOSI ) | ( 1 << DD_SCK ) | ( 1 << SPI_CS );


    DDRB &= ~( 1 << DD_MIOS );

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

//Control SS=CHIP SELECT ADXL345
void clear_spics(){
    PORTB |= _BV(SPI_CS); // high
    _delay_ms(30);}

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

void ADXL_init(void){
//init ADXL
ADXL_write(DATA_FORMAT,0x08); //pełna rozdzielczość DATASHEET STRONA 26 4SPI
ADXL_write(BW_RATE, 0x0A) ; //100hz
ADXL_write(POWER_CTL,0x08);
ADXL_write(FIFO, 0); //FIFO Wylacozny

}

/*------------------------główna petla---------------------------*/

int main(void)
{
    /* tab for strings */
    char str1[] = "Czujnik ADXL 345";

    /* init lcd*/
    lcd_init();
    /* LCD UP  */
    LCD_DISPLAY(LCDDISPLAY);
    LCD_CLEAR;         
LCD_LOCATE(1,1);
lcd_puts(str1);
init_port();/*port init*/
Init_SPI();//SPIUP
_delay_ms(50);
ADXL_init(); //ADXL345 config
_delay_ms(50);
while(1)
{   
int8_t k;   

k=Read_Byte_SPI( DEVICEID );
lcd(k);
_delay_ms(5000);

k=ADXL_read(DATAX0);
lcd(k);
_delay_ms(5000); 
  }     

`>

4

0 回答 0