Just in case you were wondering, Here is my Google Plus URL
https://www.google.com/+ArduinobasicsBlogspot
Tuesday, November 19, 2013
Google Plus URL
Labels:
ArduinoBasics,
best arduino blog,
Google+,
Plus,
ScottC,
URL
Tuesday, August 13, 2013
Getting nRF24L01 working with attiny84
![]() |
| attiny84 |
After getting the attiny85 (8 pins) working with nRF24L01, I find that while this chip is nice and small, the lack of extra pins is a real hassle when I need to add any sensors/actuators to the MCU.
When I received my attiny84 ICs from Element14, now the fun begins...
![]() |
| attiny84 pinout |
As usual, I hook up the attiny84 and test a blink sketch to ensure the attiny84 is working correctly. The first thing I encounter is that the blink is very slow, about 8 to 10 secs slower and I was wondering why. By the way, I'm using this Arduino tiny-core https://code.google.com/p/arduino-tiny/ as there are other tiny-cores available on the net with different settings.
After posting my problems on Arduino forum, I found out I need to do a Tools->Burn Bootloader to set the correct speed and fuses on the attiny84. After the "Burn Bootloader", the attiny84 was operating correctly at 8Mhz without an external crystal.
![]() |
| attiny84 |
The "fun" starts when trying to get the SPI pinout for attiny84 for running the nRF24L01. Since it uses the same tiny core was the attiny85 and I got the attiny85 working, it should not be too difficult, I guess.
After hooking up the attiny84 according to the above image and flashed the hex into the attiny84, I did not get correct settings on the four register I was monitoring on the serial/debug monitor. For troubleshooting, I have enabled "TinyDebugSerial" to read the four register, namely RF_CH, RF_SETUP, TX_ADDR and RX_ADDR.
I always needed to verify these settings and proper communications to the nRF24L01 as I used many different data rate, channel/frequencies and TX/RX address on all these nRF24L01 radios all the time.
Mirf.readRegister(RF_CH, &rf_ch,sizeof(rf_ch));
Mirf.readRegister(RF_SETUP, &rf_setup, sizeof(rf_setup));
Mirf.readRegister(TX_ADDR, tx_addr, sizeof(tx_addr));
Mirf.readRegister(RX_ADDR_P1, rx_addr, sizeof(rx_addr));
I also hook up a Logic Analyzer to the SPI pins to "see" what is going on during those SPI transfers. A proper SPI transfer would look something like below, with Enable/Slave Select (SS) pin held low during active SPI with Clock (SCK) supplying the clockrate for the MOSI/MISO.
![]() |
| SPI transfers |
The issue seems to be SPI related as I do not get SPI patterns similar to the above, I immediately looked at SPI85 library that was downloaded from Arduino forum and it was working fine on the attiny85 on my earlier blog entry.
A bit about the SPI for the attiny, as the attiny does not have dedicated pins for SPI, it uses Universal Serial Interface (USI) for both SPI or I2C. See http://playground.arduino.cc/Code/USI-SPI for details on the USI-SPI on Arduino playground.
By viewing this link on Arduino Playground, it gives *hint* NOT to use the MOSI/MISO pins for attiny as they are ONLY used for In-Circuit Serial Programming (ICSP) only.
By looking at the SPI85.cpp codes, there are two lines that seems to differentiate between attiny85 and attiny84.
#if defined( __AVR_ATtiny85__ )
const static uint8_t SS = PB4;
const static uint8_t MOSI = PB1;
const static uint8_t MISO = PB0;
const static uint8_t SCK = PB2;
#endif
#if defined( __AVR_ATtiny84__ )
const static uint8_t SS = PA7;
const static uint8_t MOSI = PA6;
const static uint8_t MISO = PA5;
const static uint8_t SCK = PA4;
#endif
Here are some snapshot from the datasheets as like most of you, I only
will read through the datasheet as a last resort as they are not written for
normal people like myself. I always wonder if the author even understand what
he himself wrote if he reads it a few years later.
![]() |
| attiny x4 pinout |
![]() |
| attiny x4 pin alternate functions |
wrote the SPI85 library was just following putting support for attiny84 without actually
tested an attiny84 before as clearly it shows the pins was incorrect.
The pin PA5, while listed as MISO (for ICSP), was also USI-DO (data out) and PA6 while
listed as MOSI (for ICSP), was also USI-DI (data in). As stated above in Arduino
playground, for attiny, the MISO/MOSI is used only for ICSP and not for USI-SPI.
That was easy to solve, just swapped PA5 and PA6 and everything will work fine,
right.. but it turns out that the PA5 and PA6 is logically mapped to value of 5 and 6
and when the codes does an Arduino code of pinMode and digitalWrite, it execute
the wrong pins for the attiny. A Digital pin5 mapped to PA5 and Digital pin6 mapped
to PA4 (the pin used for the SCK/USCK).. no wonder the SPI display on the
logic analyzer goes haywire..
Once, I traced the issue and figure out the problems, I just had to hard code the Arduino digital pins
to the AVR_ATtiny84 defines as below and changed the MOSI/MISO source of confusing to
USI_DO and USI_DI :-
#if defined( __AVR_ATtiny84__ )
const static uint8_t SS = 3;
const static uint8_t USI_DO = 5;
const static uint8_t USI_DI = 4;
const static uint8_t SCK = 6;
#endif
After that, I was able to see the correct settings on the Serial Debug or the SPI patterns on the
logic analyzer. I do not know enough about how the tiny cores was written to put the
proper attiny84 pin names but it was finally working.
If you figure out on how to fix the attiny84 pins, please let me know. The nRF24L01 library
was located at the summary links below.
There is an example of attiny84 codes in the Mirf library at github repo.
Summary Links :-
Mirf (nRF24L01) libs for UNO/attiny84/attiny85 :- https://github.com/stanleyseow/arduino-nrf24l01
Arduino tiny-cores :- https://code.google.com/p/arduino-tiny/
Wednesday, July 24, 2013
PPM Output (Draft - Looking for testers)
If we have code capable of generating servo signals its very simple to adapt this to generate a PPM output instead.
In a previous post we looked at reading an incoming PPM signal and we saw how the PPM Signal describes the individual servo pulses -
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-receiver-ppm-stream.html
To generate the PPM Stream from a set of servo commands all we need to do is change our code to toggle a single pin instead of individual pins for each servo.
If we look at the point where the channel 1 pulse ends and the channel 2 pulse starts in the diagram above. The code needs to find which pin is associated with the current chanel, then set the pin low to end the pulse. It then finds the next channel, sets its pin high and then sets the timer compare register so that a interrupt routine will be called again when its time to end the new channels pulse and start the next channels pulse.
To output PPM its actually a lot simpler. We are always dealing with the same pin so all we need to do is set the pin high, set the compare register so that the interrupt is called again to when we need start the next channel pulse and then we immediately set the PPM pin low again and exit. Systems which expect a PPM Input are only interested in the rising edge of the PPM Pulse so once we have set it high (giving the external system the rising pulse edge it is looking for) we can immediately set it low again and get out.
Timers, Compare Registers, Service Routines
The following posts provide an indepth background to how the standard Arduino Servo library uses timers, compare registers and interrupt service routines to control upto 12 Servos. The RCArduinoFastLib uses the same approach with a number of optimizations including support for more servos, faster refresh rates, dual refresh rates and reading incoming PPM signals. The remainder of this post presents a small modification of the RCArduinoFastLib which will provide a PPM output instead of individual servo channels.
Background posts
Standard Arduino servo library overview
http://rcarduino.blogspot.ae/2012/01/can-i-control-more-than-x-servos-with.html
RCArduinoFastLib
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-channels-rcarduinofastlib.html
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-receiver-ppm-stream.html
The Modified RCArduinoFastLib with PPM Output
Looking for testers, PM me Duane B on the Arduino forum for a zip folder to build the library and test sketch.
In a previous post we looked at reading an incoming PPM signal and we saw how the PPM Signal describes the individual servo pulses -
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-receiver-ppm-stream.html
To generate the PPM Stream from a set of servo commands all we need to do is change our code to toggle a single pin instead of individual pins for each servo.
If we look at the point where the channel 1 pulse ends and the channel 2 pulse starts in the diagram above. The code needs to find which pin is associated with the current chanel, then set the pin low to end the pulse. It then finds the next channel, sets its pin high and then sets the timer compare register so that a interrupt routine will be called again when its time to end the new channels pulse and start the next channels pulse.
To output PPM its actually a lot simpler. We are always dealing with the same pin so all we need to do is set the pin high, set the compare register so that the interrupt is called again to when we need start the next channel pulse and then we immediately set the PPM pin low again and exit. Systems which expect a PPM Input are only interested in the rising edge of the PPM Pulse so once we have set it high (giving the external system the rising pulse edge it is looking for) we can immediately set it low again and get out.
Timers, Compare Registers, Service Routines
The following posts provide an indepth background to how the standard Arduino Servo library uses timers, compare registers and interrupt service routines to control upto 12 Servos. The RCArduinoFastLib uses the same approach with a number of optimizations including support for more servos, faster refresh rates, dual refresh rates and reading incoming PPM signals. The remainder of this post presents a small modification of the RCArduinoFastLib which will provide a PPM output instead of individual servo channels.
Background posts
Standard Arduino servo library overview
http://rcarduino.blogspot.ae/2012/01/can-i-control-more-than-x-servos-with.html
RCArduinoFastLib
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-channels-rcarduinofastlib.html
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-receiver-ppm-stream.html
The Modified RCArduinoFastLib with PPM Output
Looking for testers, PM me Duane B on the Arduino forum for a zip folder to build the library and test sketch.
Subscribe to:
Posts (Atom)





