形势:
我使用Pro (ATMega32u4)制作/开发了这个控制器(具有许多功能),并希望在触摸它时(使用触摸传感器)使用唤醒功能来扩展它。这一切都是为了节省一些能源时,不使用。因此,当设备被拾起/触摸时,设备就会醒来。当没有接触时,它会在一段时间后进入睡眠状态。触控传感器的触点距离将以一小块铜片围绕着机箱。
设备的图片:

我使用触摸传感器的第9引脚和红外传感器的第16引脚来唤醒,但它不起作用。两个设备功能都正常工作,我可以读取触摸状态(low=untouched或high=touched),并且可以接收IR-命令,因为它已经完全实现了。当接收到信号时,这些引脚将在一段时间内处于高位。
软件问题:
我的代码有什么问题(参数错误吗?)当执行此代码时,设备将进入休眠状态(但从未醒来):
#include <avr/sleep.h> // To enter sleep mode, save power
#include <LowPower.h> // To enter sleep mode, save power
......
#define TEP_IR_RECV_PIN 16
#define TEP_PIN_TOUCHSENSOR 9
......
......
void sleep()
{
attachInterrupt( TEP_PIN_TOUCHSENSOR, wakeup, HIGH );
attachInterrupt( TEP_IR_RECV_PIN, wakeup, HIGH );
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is high.
LowPower.powerDown( SLEEP_FOREVER, ADC_OFF, BOD_OFF );
// Disable external pin interrupt on wake up pin.
detachInterrupt( TEP_PIN_TOUCHSENSOR );
detachInterrupt( TEP_IR_RECV_PIN );
}我的EnjoyPad类的睡眠功能(当按下遥控器上的电源按钮或在没有触摸或做任何事情时达到超时时将触发):
void TEnjoyPad::setSleepMode()
{
// Notify user device entering sleep mode, beep twice
setBeep( 200 );
setBeep( 200, 100, false );
// To be sure: Trigger end / unhandled down events and reset states
reset(init);
// Handle sleep event
eventSleep();
// Stop all peripherals
end();
// Finally go to sleep, code stops here
TEP_LIB_FUNCS::sleep();
// Device woke up
setBeep( 500, 100, false );
// Restart all peripherals
begin();
// Handle wake up event
eventAwake();
}有什么不对的吗?如果我需要为触摸传感器更换一个引脚,这是可能的,但我想确定它是正确的引脚,因为我需要焊接它(而不是在面包板上)。左销: 10,14和A3
注意:我已经测试过了,只是延迟替换而不是睡觉,睡眠后的功能正常工作。这样我就能听到嘟嘟声和装置又响了。睡眠()后的代码没有问题,它只是不想唤醒。
发布于 2017-10-08 21:26:30
好吧,我自己想出来的。看来pin 9和pin 16在attachInterrupt()中是不可用的,因此它永远无法工作。而且这个用法是不正确的,一定是这样的:
attachInterrupt( digitalPinToInterrupt( TEP_PIN_TOUCHSENSOR ), wakeup, HIGH );想出了使用引脚改变中断来唤醒设备的想法,这似乎是可行的。但是,从睡眠模式中恢复无法恢复所有外围设备而不发生故障,因此需要硬件重置,因此必须从头开始。此外,由于睡眠时的变化,例如,电缆断开与计算机(它也有一个电池)和USB连接丢失。并不是我使用的所有类都是我的,也不是针对这些关机的东西进行优化的。
无论如何,我将代码改为这个,只是为了向您展示更改,也许它也可以帮助其他人:
#include <LowPower.h> // To enter sleep mode, save power
#include <avr/wdt.h> // For watchDog device reset
......
#define TEP_IR_RECV_PIN 16
#define TEP_PIN_TOUCHSENSOR 9
......
......
namespace TEP_LIB_FUNCS
{
void pciSetup(uint8_t iPin)
{
*digitalPinToPCMSK( iPin ) |= bit( digitalPinToPCMSKbit( iPin ) ); // enable pin
PCIFR|= bit( digitalPinToPCICRbit( iPin )); // clear any outstanding interrupt
PCICR|= bit( digitalPinToPCICRbit( iPin )); // enable interrupt for the group
}
#if defined( TEP_PIN_TOUCHSENSOR )
#if TEP_PIN_TOUCHSENSOR >= 8 && TEP_PIN_TOUCHSENSOR <= 13
ISR(PCINT0_vect)
{
#define TEP_SLEEP_WAKEUP_PIN_ENABLED 0
// Event handler for pins: D8 to D13
// Pin change wakeup event handler, does nothing but is required.
// Do not remove this.
}
#elif defined(A0) && defined(A5) && TEP_PIN_TOUCHSENSOR >= A0 && TEP_PIN_TOUCHSENSOR <= A5
ISR(PCINT1_vect)
{
#define TEP_SLEEP_WAKEUP_PIN_ENABLED 1
// Event handler for pins: A0 to A5
// Pin change wakeup event handler, does nothing but is required.
// Do not remove this.
}
#elif TEP_PIN_TOUCHSENSOR >= 0 && TEP_PIN_TOUCHSENSOR <= 7
ISR(PCINT2_vect)
{
#define TEP_SLEEP_WAKEUP_PIN_ENABLED 0
// Event handler for pins: A0 to A5
// Pin change wakeup event handler, does nothing but is required.
// Do not remove this.
}
#endif
#endif
void sleep()
{
// Possible pins Micro, Leonardo, other 32u4-based: 0, 1, 2, 3, 7.
// Because we use all pins already by other functions, we cannot
// use attachInterrupt(), it doesn't work.
// see also: https://www.arduino.cc/en/Reference/AttachInterrupt
// Instead of this we use a change event interrupt to wake up
// the device from sleep state.
//
// The device can be woke up by using:
// - The reset button
// - The touch sensor
// Okay, lets go
// We enable interrupts here to be sure it is going to work
interrupts();
#ifdef TEP_SLEEP_WAKEUP_PIN_ENABLED
// Set pin change interrupt enabled for sensor pin
// (See also https://playground.arduino.cc/Main/PinChangeInterrupt)
// old code: attachInterrupt( digitalPinToInterrupt( TEP_PIN_TOUCHSENSOR ), wakeup, HIGH );
// old code: attachInterrupt( digitalPinToInterrupt( TEP_IR_RECV_PIN ), wakeup, HIGH );
pciSetup( TEP_PIN_TOUCHSENSOR );
#endif
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin has changed.
LowPower.powerDown( SLEEP_FOREVER, ADC_OFF, BOD_OFF );
// Disable external pin interrupt on wake up pin.
// old code: detachInterrupt( TEP_PIN_TOUCHSENSOR );
// old code: detachInterrupt( TEP_IR_RECV_PIN );
}
void softReset()
{
wdt_enable(WDTO_15MS);
while(true) {}
}
// Function Implementation
void init(void)
{
MCUSR = 0;
wdt_disable();
}
} // end namespace TEP_LIB_FUNCS此外,我的EnjoyPad类的睡眠功能(当按下遥控器上的电源按钮或在未触摸或做任何事情时达到超时时将触发):
void TEnjoyPad::setSleepMode()
{
// Notify user device entering sleep mode, beep twice
setBeep( 200 );
setBeep( 200, 100, false );
// To be sure: Trigger end / unhandled down events and reset states
reset(init);
// Shut down any
broadcastSleepMode();
// Handle sleep event
eventSleep();
// Stop all peripherals
end();
// Turn off onboard led
digitalWrite( 13, LOW );
pinMode( 13, INPUT );
// Finally go to sleep, code stops here
TEP_LIB_FUNCS::sleep();
// Device woke up
setBeep( 500, 100, false );
// Handle wake up event
eventAwake();
// Restart whole device, start from scratch.
// This seems to be the best way to guarantee all
// peripherals will be initialized properly
// and without errors.
TEP_LIB_FUNCS::softReset();
}注意:重新启动这种类型的单片机是快速的,没有显着的速度差异时,你醒来和继续或醒来和重置。
暂时就是这样,坦克是用来看的;-)
编辑:当添加pciSetup( TEP_IR_RECV_PIN );时,它似乎工作得很完美(因为已经为它定义了一个中断处理程序)。因此,当接收到IR命令时,该设备也可以唤醒。没想到这是可能的,但尝试了,整洁的特性。
https://stackoverflow.com/questions/46626201
复制相似问题