#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis;
unsigned long time1, time2;
float x_values, y_values, z_values;
void setup() {
Serial.begin(115200);
lis.begin(Wire1);
if (!lis) {
Serial.println("ERROR");
while(1);
}
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
lis.setFullScaleRange(LIS3DHTR_RANGE_2G); //Scale range set to 2g
time2 = micros();
}
void loop() {
time1 = micros();
x_values = lis.getAccelerationX();
y_values = lis.getAccelerationY();
z_values = lis.getAccelerationZ();
Serial.print(time1-time2);
Serial.print(" "); Serial.print(x_values,3);
Serial.print(" "); Serial.print(y_values,3);
Serial.print(" "); Serial.print(z_values,3);
Serial.println();
delay(96);
time2 = time1;
}
99656 -0.056 -0.720 -0.564
99556 -0.420 -0.888 -0.812
100069 0.484 -0.744 -0.624
100181 -0.056 -0.820 -0.560
99907 -0.012 -0.700 -0.644
100551 -0.152 -0.780 -0.936
99978 0.040 -0.780 -0.684
100634 0.004 -0.792 -0.676
100858 0.008 -0.676 -0.516
//#include "SAMD51_TC.h"
#include <samd.h>
#include <cstdint>
#include <array>
#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis;
unsigned long time1, time2;
float x_values, y_values, z_values;
#define GENERIC_CLOCK_GENERATOR_1M 5u
class SAMD51TCInterruptHelper;
class SAMD51TC
{
public:
/**
* @brief Create a new instance of SAM51TC for the TCx peripheral where x == tcUnit.
* @param [in] tcUnit index of TC peripheral.
*/
SAMD51TC(std::uint_least8_t tcUnit) : tcUnit(tcUnit), regs(nullptr), isrCallback(nullptr), microseconds(0)
{
switch(tcUnit)
{
case 0: {this->regs = &TC0->COUNT16; this->irqn = TC0_IRQn; break;}
case 1: {this->regs = &TC1->COUNT16; this->irqn = TC1_IRQn; break;}
case 2: {this->regs = &TC2->COUNT16; this->irqn = TC2_IRQn; break;}
case 3: {this->regs = &TC3->COUNT16; this->irqn = TC3_IRQn; break;}
case 4: {this->regs = &TC4->COUNT16; this->irqn = TC4_IRQn; break;}
case 5: {this->regs = &TC5->COUNT16; this->irqn = TC5_IRQn; break;}
case 6: {this->regs = &TC6->COUNT16; this->irqn = TC6_IRQn; break;}
case 7: {this->regs = &TC7->COUNT16; this->irqn = TC7_IRQn; break;}
}
}
void initialize(std::uint32_t microseconds = 1000000)
{
this->configureClock();
this->regs->CTRLA.bit.SWRST = 1;
while(this->regs->SYNCBUSY.bit.SWRST);
this->setPeriod(microseconds);
}
void setPeriod(std::uint32_t microseconds)
{
this->microseconds = microseconds;
}
void __attribute__((noinline)) start()
{
this->regs->CTRLA.bit.ENABLE = 0;
while(this->regs->SYNCBUSY.bit.ENABLE);
this->regs->COUNT.reg = 0;
while(this->regs->SYNCBUSY.bit.COUNT);
const std::array<uint_fast8_t, 8> prescaler_shifts = { 0, 1, 2, 3, 4, 6, 8, 10 };
for( std::uint_fast8_t index = 0; index < prescaler_shifts.size(); index++ ) {
const auto compare_value = this->microseconds >> prescaler_shifts[index];
if( compare_value <= 65535 ) {
this->regs->CTRLA.bit.PRESCALER = index;
this->regs->CC[0].reg = compare_value;
break;
}
}
while(this->regs->SYNCBUSY.bit.CC0);
this->regs->WAVE.bit.WAVEGEN = TC_WAVE_WAVEGEN_MFRQ_Val;
this->regs->INTENSET.bit.MC0 = 1;
NVIC_EnableIRQ(this->irqn);
this->regs->CTRLA.bit.MODE = TC_CTRLA_MODE_COUNT16_Val;
this->regs->CTRLA.bit.ENABLE = 1;
while(this->regs->SYNCBUSY.bit.ENABLE);
this->regs->CTRLBSET.bit.CMD = TC_CTRLBCLR_CMD_RETRIGGER_Val;
while(this->regs->SYNCBUSY.bit.CTRLB);
}
void stop()
{
this->regs->CTRLA.bit.ENABLE = 0;
while(this->regs->SYNCBUSY.bit.ENABLE);
this->regs->INTENCLR.bit.MC0 = 1;
NVIC_DisableIRQ(this->irqn);
NVIC_ClearPendingIRQ(this->irqn);
}
void restart()
{
this->regs->CTRLBSET.bit.CMD = TC_CTRLBCLR_CMD_RETRIGGER_Val;
while(this->regs->SYNCBUSY.bit.CTRLB);
}
void attachInterrupt(void (*isrCallback)())
{
this->isrCallback = isrCallback;
this->start();
}
void detachInterrupt()
{
this->stop();
this->isrCallback = nullptr;
}
private:
std::uint_least8_t tcUnit;
TcCount16* regs;
IRQn_Type irqn;
std::uint32_t microseconds;
void (*isrCallback)();
void configureClock()
{
switch(this->tcUnit) {
case 0: {MCLK->APBAMASK.bit.TC0_ = 1; GCLK->PCHCTRL[TC0_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 1: {MCLK->APBAMASK.bit.TC1_ = 1; GCLK->PCHCTRL[TC1_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 2: {MCLK->APBBMASK.bit.TC2_ = 1; GCLK->PCHCTRL[TC2_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 3: {MCLK->APBBMASK.bit.TC3_ = 1; GCLK->PCHCTRL[TC3_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 4: {MCLK->APBCMASK.bit.TC4_ = 1; GCLK->PCHCTRL[TC4_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 5: {MCLK->APBCMASK.bit.TC5_ = 1; GCLK->PCHCTRL[TC5_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 6: {MCLK->APBDMASK.bit.TC6_ = 1; GCLK->PCHCTRL[TC6_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
case 7: {MCLK->APBDMASK.bit.TC7_ = 1; GCLK->PCHCTRL[TC7_GCLK_ID].reg = GCLK_PCHCTRL_GEN(GENERIC_CLOCK_GENERATOR_1M) | GCLK_PCHCTRL_CHEN; break;}
}
}
public:
void processIsr()
{
if( this->regs->INTFLAG.bit.MC0 ) {
this->regs->INTFLAG.bit.MC0 = 1;
if( this->isrCallback != nullptr ) {
this->isrCallback();
}
}
}
};
SAMD51TC TimerTC3(3);
void TC3_Handler() { TimerTC3.processIsr(); }
bool isLEDOn = false;
char time = 0;
void setup()
{
TimerTC3.initialize(100000); // microseconds
TimerTC3.attachInterrupt(timerIsr);
Serial.begin(115200);
lis.begin(Wire1);
if (!lis) {
Serial.println("ERROR");
while(1);
}
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
lis.setFullScaleRange(LIS3DHTR_RANGE_2G); //Scale range set to 2g
time2 = micros();
}
void loop()
{
/*
time ++;
if(time == 10)TimerTc3.detachInterrupt();
else if(time == 20)
{
TimerTc3.attachInterrupt(timerIsr);
time = 0;
}
delay(1000);
*/
}
void timerIsr()
{
time1 = micros();
x_values = lis.getAccelerationX();
y_values = lis.getAccelerationY();
z_values = lis.getAccelerationZ();
Serial.print(time1-time2);
Serial.print(" "); Serial.print(x_values,3);
Serial.print(" "); Serial.print(y_values,3);
Serial.print(" "); Serial.print(z_values,3);
Serial.println();
time2 = time1;
}
97002 -0.104 -0.276 -1.088
98002 -0.044 -0.260 -0.856
97002 -0.100 -0.212 -0.932
97002 -0.036 -0.004 -1.020
97002 -0.164 -0.192 -0.980
97002 -0.104 -0.160 -0.928
97002 -0.228 0.000 -1.732
97002 -0.080 -0.212 -1.044
97002 -0.060 -0.184 -1.000
97002 -0.052 -0.096 -0.944
を入力した。https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis;
#include"TFT_eSPI.h"
#include <SPI.h>
TFT_eSPI tft;
void setup() {
lis.begin(Wire1);
if (!lis) {
Serial.println("ERROR");
while(1);
}
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
lis.setFullScaleRange(LIS3DHTR_RANGE_2G); //Scale range set to 2g
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK); //Black background
}
void loop() {
float x_values, y_values, z_values;
x_values = lis.getAccelerationX();
y_values = lis.getAccelerationY();
z_values = lis.getAccelerationZ();
char x_str[100], y_str[100], z_str[100];
dtostrf(x_values, 6, 2, x_str);
dtostrf(y_values, 6, 2, y_str);
dtostrf(z_values, 6, 2, z_str);
tft.drawString(x_str, 70, 60, 6);
tft.drawString(y_str, 70,110, 6);
tft.drawString(z_str, 70,160, 6);
delay(100);
}
を入力して、OKボタンをクリックした。https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |