Outils pour utilisateurs

Outils du site


issue138:greatcowbasic

Ceci est une ancienne révision du document !


In FCM#136, I showed you how to use the analog to digital conversion (ADC) with a light-dependent resistor (LDR) to gather information about the ambient brightness and how to communicate to a remote device at distance. The reception with the standard receiver was bad, so I did some research, and ordered another receiver-module to improve the reception, it is called “RXB8 V2.0” and came at a reasonable price. In this issue we will enhance the Radio Frequency (RF) device interface to save some power - to do this I will introduce the Watchdog timer.

Introducing the Watchdog timer Imagine a situation where you program a microcontroller for a critical system. You would not just rely on the reliability of the software to avoid damage, would you? Software can always contain bugs, or maybe some environmental conditions arise that you did not think of in the first place. So, it would be important to restart the microcontroller to its initial state if something goes wrong. In such situations you surely will use the watchdog timer, which can restart the microcontroller completely. With respect to low ressources, another use case of the watchdog timer would be to implement a power saving mode to save battery life. The device drains as little battery as possible (whilst sleeping) and can then use the watchdog timer to wake up. So how does the watchdog timer (WDT) work internally?

The watchdog timer has a separate on-chip oscillator working at 128 KHz which works as a overflow timer. Thru a prescaler, you can set the time when the overflow occurs. The WDT can operate in three modes: interrupt, system reset or interrupt and system reset: • The interrupt mode can be used to wake up the device, or to timeout operations that last too long. • The system reset mode is quite self-explanatory, the device is restarted without further notice and all volatile data is erased. • The remaining mode combines the aforementioned ones, the interrupt occurs and you can implement a routine where you save your data or send some debugging information (eg, over the UART), and then the system is reset.

The WDT can be activated in two ways: • By setting a hardware fuse (WDTON): in this mode the WDT is set to ‘system reset’ mode and cannot be changed or switched off by software on the fly. Only the overflow of the WDT can be prevented by resetting it. • The WDT can be activated by software: then all three modes can be used. For the ATtiny13a, different timings can be set (to trigger overflow). For this article we need to know only two timing options.

LED flasher with the watchdog timer The WDT needs a timed sequence, without interruptions, to be changed or switched on and off: • First, make sure that Interrupts are off. • Reset the WDT. • The watchdog change register (WDTCR) must be set to logic 1 in the watchdog change enable bit (WDCE). • Enable the watchdog and set the prescaler of the watchdog timer. Compile the code shown right and upload it to the ATtiny13a, connect a LED to PortB.1, and see what happen.

The program starts, the LED is turned on and activates the WDT. After 64 ms the microcontroller restarts. The ‘boot time’ by default is about 64 ms according to the factory default. In this setup, the microcontroller is just reset - no power-save mode is used just yet. The LED flashes relatively fast because of the short time the LED is set to on (64 ms).

Powersave modes The ATTiny13a has three different power-save modes, each of them with more or less power consumption. In sequence by power-consumption (from highest to lowest) they are: Idle, ADC Noise Reduction and Power-down. The choice depends on the situation, and what you want to achieve with your device. For now, we’ll use power-down mode. The other sleep modes will be explained in a further article, when they are needed. To get an idea how low the power consumption can be, have a look at the site of Nick Gammon (Ref. No. 5), he did some measures for us.

Energy efficient transmitter Let’s extend the code from the last issue and use the power-save mode, and the watchdog timer to pause the transmission. The microcontroller takes around 64 ms to start, then we set the watchdog timer. After this, the program sends the LDR values over the air. The transmission itself now takes around 48 ms. The former version of the program waits 8 s without doing anything – but still consumes power! Here (next page, shown right) is how we optimize this by using the power-down sleep mode, and the watchdog timer.

On the first start after the microcontroller gets power, the microcontroller is initialised. Then the watchdog timer is set to the reset mode, and the main program measures the LDR and sends the data over the transmitter. Then all of the Timer and the ADC get switched off, and the sleep mode power-down is enabled. After that the microcontroller is sent to sleep. After 8 seconds it gets a restart, and so the same procedure gets repeated every 8 second. Here is the logged data from the serial line, with the new receiver in place, and the new software version on the microcontroller. The result (next page, top right) is good (see time 00:13:01 for the first dataset and 00:13:09 for the second dataset, as highlighted in black). Only a minimum of garbage is received, so a data logging program could easily pick up the lines with the wanted data and safely omit the unwanted bytes.

Breadboard circuitry The transmitter part is exactly the same as in the last issue, so I omit depicting it here again. The transmitter has two extra PINs to connect (than the former device): On the left side, there is a connector for the antenna (a wire ~ 17 cm is sufficient) and GND. On the right side, there are GND, DATA and Vcc (+5V). Both GND can be connected with a Y-wire to the same GND. The DATA pin goes to the Rx-Connector of the serial-to-usb-converter. Due to the shape of the new transmitter, I hooked up jumper wires to the connectors and let it rest on a safe area on my workplace. Maybe not what a serious electrician would do, but it worked just fine.

Conclusion The initial simple receiver module I got (without too deep research on the subject) did its job for our first experiments, but the replacement I found is better. So, if you are about to buy the hardware, I recommend the transmitter-modules (FS-1000A) from the last issue and the receiver-module (RXB8, v2) from this one. However, I highly recommend to do your own research; there are maybe better modules for both purposes. The newer program saves power, and, because some lines of code have been saved too, it has a little extra free space on the microcontroller for further optimisation and other ideas. For the next issue, I plan to prepare a little data logger program on the PC side, and expand the transmitter software to send data in larger time frames, and to gather some real life values.

Sources If you want to download the sources instead of copy-pasting it, you can now check it out with git or an SVN client. Have a look at goo.gl/aDvggr for more information.

References The AVR watchdog timer explained http://microchipdeveloper.com/8avr:avrwdt Application Note to the watchdog timer http://ww1.microchip.com/downloads/en/AppNotes/doc2551.pdf Fuses calculator with comprehensive information on the devices factory defaults http://www.engbedded.com/fusecalc/ Inspiration for the simple startcode https://sourceforge.net/p/gcbasic/discussion/579126/thread/f48c95af/ Power saving maths https://www.gammon.com.au/power RXB8 Datasheet https://p.globalsources.com/IMAGES/PDT/SPEC/508/K1045318508.pdf

Acknowledgement I wish to thank Evan Venn (Anobium) from the Great Cow BASIC Team for his insights and valuable hints.

issue138/greatcowbasic.1541835763.txt.gz · Dernière modification : 2018/11/10 08:42 de d52fr