2014-12-19 08:45:50 -08:00
/*
_____ ____ __ _ ____ _ _ _ _
| __ \ / __ \ | \ | | / __ \ | | | | | | | |
| |__| | | / \_| | . \ | | / / \ \ | | | | \ \ / /
| _ / | | _ | |\ \| | | |__| | | | | | \ ' /
| | \ \ | \__/ | | | \ ' | | __ | \ \/ / | |
2014-12-23 13:07:19 -08:00
|_| \_\ \____/ |_| \__| |_| |_| \__/ |_| 2013/2014
2014-12-19 08:45:50 -08:00
http://p.loussouarn.free.fr
*******************************************************
* <TinyPinChange> library Demo *
* with debugging capabilities using *
* <SoftSerial> object as single wire serial interface *
*******************************************************
This sketch demonstrates how to use <TinyPinChange> library.
2014-12-23 13:07:19 -08:00
It counts all the FALLING edges on 2 different pins.
2014-12-19 08:45:50 -08:00
/!\CAUTION/!\: as <TinyPinChange> library can be shared (and it is with SoftSerial in this sketch) , the user shall test if the changes are related to the declared pins.
Trick: By connecting Pin#1 to Pin#0 or to Pin#5 through a 1K resistor, you can generate transitions for testing purpose.
Output results are sent to a software serial.
And the great thing is: using a <SoftSerial> object as a bi-directionnal software serial port (half-duplex) on a single pin to communicate with the outside world!
To display the sketch results on a PC (in a Terminal):
1) Build the "Serial One Wire Debug Cable" and plug it to the regular RS232 port as depicted below,
2) Open your favorite Terminal at 38400,n,8,1: HyperTerminal, Teraterm (Windows) or Minicom, GtkTerm (Linux) and CoolTerm (MAC) does the trick.
3) You can also use the Serial Monitor of the arduino IDE: Tools->Serial Port and select your RS232 port (may be an USB virtual port), Rate=38400.
4) To enable the display, type 1, to disable, type 0 in the Terminal/Monitor.
SERIAL ONE WIRE
DEBUGGING CABLE
_______________ ________________
/ \___/\___/ \
____
.--------. | \
| GND |--------------------------------+---o5 \
| | 47K | | 9o |
| | .--###--' | o4 |
| DEBUG | 4.7K | | 8o |
| TX_RX |-------------------###--+--|<|------o3 | ---> To regular RS232 SubD 9 pins Male of PC or Serial/USB adapter
| PIN | ^ | 1N4148 | 7o |
| | | '-----------o2 |
'--------' | | 6o |
ATtiny85 Single | o1 /
(Digispark) I/O |____/
SubD 9 pins
Female
*/
#include <TinyPinChange.h>
#include <SoftSerial.h>
#define LED_PIN 1
#define DEBUG_TX_RX_PIN 2
#define FIRST_INPUT 0
#define SECOND_INPUT 5
2014-12-23 13:07:19 -08:00
volatile uint16_t FirstInputChangeCount = 0 ; /* Volatile since the variable will be updated in interruption */
volatile uint16_t SecondInputChangeCount = 0 ; /* Volatile since the variable will be updated in interruption */
2014-12-19 08:45:50 -08:00
SoftSerial MySerial ( DEBUG_TX_RX_PIN , DEBUG_TX_RX_PIN , true ); /* Tx/Rx on a single Pin !!! (Pin#2) */
uint8_t VirtualPortNb ;
uint8_t VirtualPortNb_ ;
void setup ()
{
TinyPinChange_Init ();
2015-01-14 18:08:45 -08:00
MySerial . begin ( 57600 ); /* Trick: use a "high" data rate (less time wasted in ISR and for transmitting each character) */
2014-12-19 08:45:50 -08:00
2014-12-23 13:07:19 -08:00
VirtualPortNb = TinyPinChange_RegisterIsr ( FIRST_INPUT , InterruptFunctionToCall );
VirtualPortNb_ = TinyPinChange_RegisterIsr ( SECOND_INPUT , InterruptFunctionToCall );
2014-12-19 08:45:50 -08:00
/* Enable Pin Change for each pin */
TinyPinChange_EnablePin ( FIRST_INPUT );
TinyPinChange_EnablePin ( SECOND_INPUT );
MySerial . txMode ();
2014-12-23 13:07:19 -08:00
MySerial . println ( F ( " \n *** Tiny PinChange Demo (Falling Edge) ***" ));
2014-12-19 08:45:50 -08:00
MySerial . print ( F ( "Pin " )); MySerial . print (( int ) FIRST_INPUT );
MySerial . print ( F ( " is part of virtual port " )); MySerial . println (( int ) VirtualPortNb );
MySerial . print ( F ( "Pin " )); MySerial . print (( int ) SECOND_INPUT );
MySerial . print ( F ( " is part of virtual port " )); MySerial . println (( int ) VirtualPortNb_ );
MySerial . println ( F ( "As you can see, virtual port is always port 0 for ATtiny85" ));
2014-12-23 13:07:19 -08:00
MySerial . println ( F ( "Remember <TinyPinChange> is also designed for UNO, MEGA, ATtiny84 and ATtiny167 ;-)" ));
MySerial . println ( F ( "Type 1 to start display, 0 to stop display" ));
2014-12-19 08:45:50 -08:00
pinMode ( LED_PIN , OUTPUT );
MySerial . rxMode (); /* Switch to Rx Mode */
}
/* Function called in interruption in case of change on pins */
void InterruptFunctionToCall ( void )
{
2014-12-23 13:07:19 -08:00
if ( TinyPinChange_FallingEdge ( VirtualPortNb , FIRST_INPUT )) /* Check for FIRST_INPUT Rising Edge */
2014-12-19 08:45:50 -08:00
{
2014-12-23 13:07:19 -08:00
FirstInputChangeCount ++ ; /* Only Falling edges are counted */
2014-12-19 08:45:50 -08:00
}
2014-12-23 13:07:19 -08:00
if ( TinyPinChange_FallingEdge ( VirtualPortNb_ , SECOND_INPUT )) /* Check for SECOND_INPUT Rising Edge */
2014-12-19 08:45:50 -08:00
{
2014-12-23 13:07:19 -08:00
SecondInputChangeCount ++ ; /* Only Falling edges are counted */
2014-12-19 08:45:50 -08:00
}
}
void loop ()
{
2014-12-23 13:07:19 -08:00
static boolean State = HIGH , DisplayEnabled = true ;
static uint32_t LedStartMs = millis (), DisplayStartMs = millis ();
2014-12-19 08:45:50 -08:00
uint16_t LocalFirstInputChangeCount ;
uint16_t LocalSecondInputChangeCount ;
/* Blink the built-in LED */
2015-01-14 18:08:45 -08:00
if ( millis () - LedStartMs >= 500UL )
2014-12-19 08:45:50 -08:00
{
2014-12-23 13:07:19 -08:00
LedStartMs = millis ();
2014-12-19 08:45:50 -08:00
digitalWrite ( LED_PIN , State );
2014-12-23 13:07:19 -08:00
State = ! State ; /* State will be inverted at the next digitalWrite() */
2014-12-19 08:45:50 -08:00
}
/* Get command from single wire SoftSerial */
if ( MySerial . available ())
{
switch ( MySerial . read ())
{
case '0' :
2014-12-23 13:07:19 -08:00
DisplayEnabled = false ;
2014-12-19 08:45:50 -08:00
break ;
case '1' :
2014-12-23 13:07:19 -08:00
DisplayEnabled = true ;
2014-12-19 08:45:50 -08:00
break ;
}
}
/* Diplay Transition numbers every second */
2015-01-14 18:08:45 -08:00
if (( millis () - DisplayStartMs >= 1000UL ) && DisplayEnabled )
2014-12-19 08:45:50 -08:00
{
2014-12-23 13:07:19 -08:00
DisplayStartMs = millis ();
2014-12-19 08:45:50 -08:00
noInterrupts (); /* Mandatory since counters are 16 bits */
LocalFirstInputChangeCount = FirstInputChangeCount ;
LocalSecondInputChangeCount = SecondInputChangeCount ;
interrupts ();
MySerial . txMode ();
MySerial . print ( F ( "FirstInputChangeCount=" )); MySerial . println ( LocalFirstInputChangeCount );
MySerial . print ( F ( "SecondInputChangeCount=" )); MySerial . println ( LocalSecondInputChangeCount );
MySerial . rxMode ();
}
}