Attiny85接口与ADXL362运动传感器

时间:2018-12-07 05:15:03

标签: arduino-uno i2c spi attiny

所以最近我一直在Attiny85 uC上做一些特定的任务。现在,我担心的是使用SPI接口控制ADXL362。我已经使用attiny通过I2C接口控制digiPOT抽头电阻。截至目前,attiny可以使用I2C通讯控制digiPOT正常工作。当我为此添加ADXL 362时,它会产生错误,指出未声明某些GIMSK和PCMSK。

我对此进行了深入研究,发现该库中的SPIE出现了一些问题。我不确定我的方向是否正确。

ADXL362在安装了基本库的Arduino Uno板上工作良好。 最初,我添加了引脚更改中断,只是为了让系统进入休眠状态以节省功耗。

我的总体目标是分别使用SPI和I2C接口控制ADXL362和MAX5479 digiPOT,使用attiny85来制造超低功耗系统以满足我的需要,该系统利用中断来节省功耗。

如果有人可以帮助我,如何使用Attiny85使其同时在I2c和SPI上工作,因为它们都可以正常工作。

作为参考,可以在[1]中说明Attiny中断:https://bigdanzblog.wordpress.com/2014/08/10/attiny85-wake-from-sleep-on-pin-state-change-code-example/

我用于我的项目的库是

#include <avr/io.h>
#include <avr/sleep.h>
#include <TinyWireM.h>
#include <LowPower.h>             
#include <SPI.h>
#include <ADXL362.h>

我得到的错误很长,附有摘要。

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

                 from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\pgmspace.h:90,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28,

                 from sketch\Revision-01.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src/SPI.h:310:55: error: 'SPIE' was not declared in this scope

   inline static void detachInterrupt() { SPCR &= ~_BV(SPIE); }

                                                       ^

exit status 1
Error compiling for board ATtiny25/45/85.

期待一些有用的回复,建议或有用的链接指导。

如果您正在寻找代码,希望此代码片段可能会对您有所帮助。

#define I2C_SLAVE_ADDRESS1 0x28
#define I2C_SLAVE_ADDRESS2 0xD0 
#define C_BYTEWA 0X11
#define C_BYTEWB 0X12
#define M_Byte 0x02
#define ST_P PB3
#define LED PB4
#define BODS 7                            //BOD Sleep bit in MCUCR
#define BODSE 2                           //BOD Sleep enable bit in MCUCR
#define KEEP_RUNNING 60000                //milliseconds

#include <avr/io.h>
#include <avr/sleep.h>
#include <TinyWireM.h>
#include <LowPower.h>                     // Optional
#include <SPI.h>
#include <ADXL362.h>

ADXL362 xl;
const unsigned long starttime=0;

void setup()
{
  xl.begin();
  delay(1000);
  TinyWireM.begin();                      // Initialize the i2c master library
  i2c();                                  // I2C communication mode initialization function calls
  for (byte i=0; i<5; i++)
  {
    pinMode(i, INPUT);
   }
   GIMSK |= _BV(PCIE);                    // Enable Pin Change Interrupts
   PCMSK |= _BV(PCINT1);                  // Use PB0 as interupt pin
   pinMode(LED, OUTPUT);
   digitalWrite(LED, HIGH);               // Drive it low so it doesn't source current
   delay(10);
   digitalWrite(LED, LOW);
   pinMode(ST_P, OUTPUT);
   digitalWrite(ST_P, HIGH);              // Make ST_P high all time
}
void loop()
{
  Sleep();                                // Sleep function calls
  do
  {
    digitalWrite(LED, HIGH);
    delay(KEEP_RUNNING);
  }
  while(millis()-starttime<KEEP_RUNNING); // keep system awake for 'Preset time' before it goes back to sleep
  digitalWrite(LED, LOW);
}
void Sleep(void)
{
  GIMSK |= _BV(PCIE);                     // Enable Pin Change Interrupts
  PCMSK |= _BV(PCINT1);                   // Use PB0 as interrupt pin

  ADCSRA &= ~_BV(ADEN);                   // ADC off
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();                         // Sets the Sleep Enable bit in MCUCR Register
  sei();                                  // Enable interrupts
  sleep_cpu();                            // sleep
  sleep_disable();                        // Clearthe Sleep Enable bit in MCUCR Register
  ADCSRA |= _BV(ADEN);                    // ADC on
  sei(); 
}
ISR(PCINT0_vect)                          // Interrupt service routine to execute specific task during interrupt
{
  //GIMSK = 0;                            // disable external interrupts (only need one to wake up)
}

void i2c(void)
{
  // MAX digiPOT initialization
  TinyWireM.begin();
  TinyWireM.beginTransmission(I2C_SLAVE_ADDRESS1);
  TinyWireM.send(C_BYTEWA);
  TinyWireM.send(0xff);                   // send max resistance value (max tap)
  int Byte1 = TinyWireM.endTransmission();
  TinyWireM.begin();
  TinyWireM.beginTransmission(I2C_SLAVE_ADDRESS1);
  TinyWireM.send(C_BYTEWB);
  TinyWireM.send(0xff);                   // send max resistance value (max tap)
  int Byte2 = TinyWireM.endTransmission();
}

谢谢

0 个答案:

没有答案