Last active
December 19, 2015 18:38
-
-
Save sourceperl/5999632 to your computer and use it in GitHub Desktop.
Lowpower Arduino test.
An ATmega328P on a breadboard powered under 3.3v.
Arduino bootloader at 8 MHz internal RC clock.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
An ATmega328P on a breadboard powered under 3.3v (2x AA battery) | |
Arduino bootloader at 8 MHz internal RC clock. | |
This example code is in the public domain. | |
Share it's happiness !!! | |
*/ | |
#include <SPI.h> | |
#include <avr/sleep.h> | |
#include <avr/power.h> | |
#include "nRF24L01.h" | |
#include "RF24.h" | |
// *** some define *** | |
#define sleep_bod_disable() \ | |
do { \ | |
uint8_t tempreg; \ | |
__asm__ __volatile__("in %[tempreg], %[mcucr]" "\n\t" \ | |
"ori %[tempreg], %[bods_bodse]" "\n\t" \ | |
"out %[mcucr], %[tempreg]" "\n\t" \ | |
"andi %[tempreg], %[not_bodse]" "\n\t" \ | |
"out %[mcucr], %[tempreg]" \ | |
: [tempreg] "=&d" (tempreg) \ | |
: [mcucr] "I" _SFR_IO_ADDR(MCUCR), \ | |
[bods_bodse] "i" (_BV(BODS) | _BV(BODSE)), \ | |
[not_bodse] "i" (~_BV(BODSE))); \ | |
} while (0) | |
// *** some const *** | |
unsigned long count = 0; | |
// *** some vars *** | |
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 | |
RF24 radio(9,10); | |
// *** ISR *** | |
ISR(WDT_vect) | |
{ | |
// do nothing | |
} | |
// link stdout (printf) to Serial object | |
// create a FILE structure to reference our UART output function | |
static FILE uartout = {0}; | |
// create a output function | |
// This works because Serial.write, although of | |
// type virtual, already exists. | |
static int uart_putchar (char c, FILE *stream) | |
{ | |
Serial.write(c); | |
return 0; | |
} | |
// the setup routine runs once when you press reset: | |
void setup() | |
{ | |
// disable ADC (enable by arduino core) | |
ADCSRA = 0x00; | |
// init serial | |
Serial.begin(9600); | |
// fill in the UART file descriptor with pointer to writer | |
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); | |
// standard output device STDOUT is uart | |
stdout = &uartout ; | |
printf("start\n\r"); | |
// setup radio | |
radio.begin(); | |
//radio.setRetries(15,15); | |
radio.setChannel(0x6A); | |
radio.setDataRate(RF24_250KBPS); | |
// reduce Payload size : improve reliability | |
radio.setPayloadSize(8); | |
// dump the configuration of the rf unit | |
radio.printDetails(); | |
// manage radio pipes | |
radio.openWritingPipe(0xF0F0F0F0F0LL); | |
//radio.openReadingPipe(1,pipes[1]); | |
// *** Setup the watch dog timer *** | |
// Clear the reset flag | |
MCUSR &= ~(1<<WDRF); | |
/* In order to change WDE or the prescaler, we need to | |
* set WDCE (This will allow updates for 4 clock cycles). | |
*/ | |
WDTCSR |= (1<<WDCE) | (1<<WDE); | |
/* set new watchdog timeout prescaler value */ | |
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */ | |
/* Enable the WD interrupt (note no reset). */ | |
WDTCSR |= _BV(WDIE); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() | |
{ | |
// check counter | |
count++; | |
// radio on | |
radio.powerUp(); | |
//printf("Now sending %lu...",count); | |
bool ok = radio.write( &count, sizeof(unsigned long) ); | |
// radio off | |
radio.powerDown(); | |
// sleep now... | |
do_sleep(); | |
} | |
/*** sleep routine ***/ | |
void do_sleep(void) | |
{ | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here | |
cli(); | |
sleep_enable(); | |
// disable BOD during sleep (auto-reset after wake-up) | |
sleep_bod_disable(); | |
sei(); | |
// !!! system sleeps here | |
sleep_mode(); | |
// system continues execution here (after watchdog timed out) | |
sleep_disable(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Simple code to receive and print data on console. | |
Useful to test "low_bread_rx.ino". | |
This example code is in the public domain. | |
Share it's happiness !!! | |
*/ | |
#include <SPI.h> | |
#include "nRF24L01.h" | |
#include "RF24.h" | |
// *** some vars *** | |
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10 | |
RF24 radio(9,10); | |
// link stdout (printf) to Serial object | |
// create a FILE structure to reference our UART output function | |
static FILE uartout = {0}; | |
// create a output function | |
// This works because Serial.write, although of | |
// type virtual, already exists. | |
static int uart_putchar (char c, FILE *stream) | |
{ | |
Serial.write(c); | |
return 0; | |
} | |
// the setup routine runs once when you press reset: | |
void setup() { | |
// init serial | |
Serial.begin(9600); | |
// fill in the UART file descriptor with pointer to writer | |
fdev_setup_stream(&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); | |
// standard output device STDOUT is uart | |
stdout = &uartout; | |
printf("start\n\r"); | |
// setup radio | |
radio.begin(); | |
//radio.setRetries(15,15); | |
radio.setChannel(0x6A); | |
radio.setDataRate(RF24_250KBPS); | |
// optionally, reduce the payload size. seems to | |
// improve reliability | |
radio.setPayloadSize(8); | |
// dump the configuration of the rf unit | |
radio.printDetails(); | |
// manage radio pipes | |
radio.openReadingPipe(1, 0xF0F0F0F0F0LL); | |
//radio.openReadingPipe(1,pipes[1]); | |
// Start listening | |
radio.startListening(); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() | |
{ | |
// if there is data ready | |
if ( radio.available() ) { | |
// Dump the payloads until we've gotten everything | |
unsigned long got_time; | |
bool done = false; | |
while (!done) { | |
// Fetch the payload, and see if this was the last one. | |
done = radio.read( &got_time, sizeof(unsigned long) ); | |
// print result | |
printf("(%lu) got payload %lu\n\r", millis(), got_time); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment