Application Note ATtiny32
Application Note ATtiny32
This application note is about how to make a Transmitter and receiver with the
Nordic nRF24L01 transceiver, an Atmel Avr microcontrollers “Atiny861v”, “Atiny44v”
and three different software application notes: AVR319: Using the USI module for SPI
communication, AVR136: Low-Jitter Multi-Channel Software PWM and Nordic
nRF24L01 with Bascom-Avr.
126 RF channels, only 84 channels legal in U.S.A @ 1 MHz and 42 channels @ 2 MHz
GFSK modulation
• Transmitter
Programmable output power: 0, -6, -12 or -18dBm
• Receiver
Integrated channel filters
12.3mA at 2Mbps
• RF Synthesizer
Fully integrated synthesizer
• Enhanced ShockBurst™
1 to 32 bytes dynamic payload length
• Power Management
Integrated voltage regulator
Idle modes with fast start-up times for advanced power management
• Host Interface
4-pin hardware SPI
Max 8Mbps
Note: 3 separate FIFO for TX and 3 separate FIFO for RX: 32 bytes each FIFO = 96 bytes for TX and 96
bytes for RX = a total of 48 R/C digital channels with 16 bit resolution
5V tolerant inputs
What do you need to make this application note to work? If you buy my TX and RX
board the only thing you need to buy is the assembled Transceiver MiRF nRF24L01
board from Sparkfun.com, also you need a high gain antenna; you can buy one from
sparkfun.com or from digikey. You need two of this board to complete the kit and a
Picco Z TX case, the board was made for this case because is very popular.
If you decide to make you own kit or reprogram mine, you will need to buy my TX
and RX boards or make you own boards, two Transceiver MiRF nRF24L01 board,
the antenna for the TX, the software to reprogram the AVR micros (AVR Studio ver.
4 from Atmel, $$free), AVR DRAGON debugger from Atmel about $49.00 from
Digikey, for the debugging the Avr this the most economical option you be able to
have. Also you need the application notes in PDF format and software; you get the
first 2 application notes from Atmel web site and the other one from Bascom web site
MCS Electronics @ www.mcselec.com.
The next photo show the transmitter board in a Silverlit Picco Z Mini Micro RC
Helicopter or the Air Hogs Havoc Heli transmitter plastic covers. I did not have a
PLANTRACO transmitter when I made this board@ the time of design. However the
Picco Z TX case is very popular choice. Also I did used some of the parts from the
Picco Z TX like the leds and on off switch. The pc board mounts in the TX case
mounting bolt arrangement of the Picco Z TX Board without modification. The only
modifications made to the case are for the joystick, switches and charging cover.
The transmitter boards have ten inputs for analog to digital converters and switches, have
a programming and debugging port, 5 volt voltage regulator, 10K ohms joystick for
rudder and elevator or aileron, 2 x 5K ohms trim pots for the joy stick, on-off switch, leds
for power on and for charging, a throttle potentiometer and a li-po battery charger @ this
moment the battery charger is not working. The chip for the charger is a Maxim max8808
however this chip is very small to be soldered in the board the easy way!!!!!! I will be
using a microchip MCP73833/4 or MCP73837/8 in 10-Lead MSOP package which will
be easier to replace in case of burnout.
The receiver boards have one Attiny44v, two Hex FET Mosfet Irlml2505, one Allegro
MicroSystems A3901 dual full-bridge motor driver for the actuators, this driver can
Output 400ma Current per Channel so you need to limit the amount of current passing
thru you coils or actuators, the easy way of doing this is to solder one or two resistor in
the holes of the actuators. At the rear of the board you have six holes to mount the battery
and motors and around the board many more holes for grounding. Next to the rear of the
Attiny84v is the connector for the transceiver 8 pins, in the middle of the microcontroller
to the right side of the board is the debug wire pin or hole. The yellow teardrop thing in
the front of the board is a 1uf Tantalum capacitor I use this cap when I need lot of current
moving around the board. Next I compare the receiver board to other ones:
The complete board measure about 37mm and weight about 3.8 grams. I do understand
that the board is little to fatty also too big in dimension; however my intentions is to
create a board that every body can work with and is easy to prototype with. Also I did
made these boards to replace more heavy boards like the ones you will see in the next
photos.
The upper board next to mine was from A Estes P40 Warhawk which I got from wal-mart
before Cox came out with the 3 channel R/C version; these boards have one output for
the actuators and a driver for the motor, is “AM” modulation 27.045 MHz and weight
more than 15 grams. I made my board to replace these types of r/c receiver and
transmitter boards. Look the next board.
This board is from an Air Hog plane named the red dragon I think is made by Spin
Master made in chin-chin country China. This board is “AM” modulation 27.145 MHz,
the board weights more than 9 grams the way you see it in the photo, with the battery
pack that came with the plane weights more than 22 grams and only have one channel;
left or right in a bit-bang system, the plane have two coil for the actuator which make
more noise than a rattle snake, the throttle is fix like about 60% to 75% throttle. I hope
you understand my point of view why I made my board this way. Ok let stop the chi-
Chad and get to work.
What you need to do to kick this pig and getting it working. First you need to choose
between use one of my boards the way it is or if you want to customized more the
software you way and second in which way you will customized the software to you
needs.
Part 1
Let say you choose the first one; buy my boards, now you will ask what is so especial
about this boards that I have to buy one, well to start they come fully assembled in other
words the only thing you need to do is buy and solder the transceiver board in the
transmitter and the receiver board and modify the Picco Z transmitter case for the
joystick, the throttle variable resistor or potentiometer and the switches for the battery
charger and the fail save on-off selection, install the antenna, batteries and test you set-up
before you burn something. Make sure you motor is connected correct, that include the
diode and cap, also check you actuators coil are no getting to hot, if they do connect some
resistor in the output of the A3901 motor driver to lower you current per channel. When
you are happy (like Mr. Happy remember nobody is more happy than Mr. Happy) with
you set up, you can go an try it in you plane (a cheap one first) and remember this is a
work in development even if I did eliminate every bug in the code anything can happen,
so double check you setup twice or more.
Part 2
So you decide to modify my board or make you own; if you take this route you will need
the AVR Studio 4, the application notes software, PDF files for micros and transceiver,
AVR Dragon debugger and you need to decide which language you want to develop the
software “C”, Basic (Bascom AVR, Bascom 8051 or Pro-Basic for pic micros) and if you
want shoot you self in the foot use assembly language. My software is completely written
in “C” for the compiler “Code Vision AVR”, if you use the Bascom application note is
written for the atmega8 which use the hardware SPI, is very easy to translate Bascom
AVR to Bascom 8051, however this do not apply to any other Basic Compiler for
microcontrollers each compiler is made different. Also you will need a development
platform which might be my TX and RX boards, the AVR Dragon, STK500 or any other
development tool for the microcontroller of you choice. @ One time Atmel send me an e-
mail in which they price the STK500 and the AVR Dragon for less than $60.00 together
from Digikey, I did buy one of these kits from Digikey, if I find the part number I will
post it. The AVR Dragon is a complete development platform in which you have an ISP
(in system programmer for low and high voltage) which supports all programming modes
for the Atmel AVR device family. It also includes full emulation support for devices with
32kB or less Flash memory. AVR Dragon can be used with an external target board.
However, the onboard prototype area, allow simple programming and debugging without
any additional hardware besides strapping cables. AVR Dragon is powered by the USB
cable, and can also source an external target with up to 300mA (from the VCC connector,
5V) when programming or debugging. If the target is already powered by an external
power source, the AVR Dragon will adapt and level convert all signals between the target
and the AVR Dragon. Note: If the target board is powered by external power source, no
connection should be made between the VCC connector and the external board. AVR
Dragon if fully supported by AVR Studio. This allows the AVR Dragon firmware to be
easily updated to support new devices and protocols. When connecting the AVR Dragon,
AVR Studio will automatically check the firmware and prompt the user if an updated
firmware is available. In order to use the AVR Dragon it is required to install the AVR
Studio and USB driver first. Please do not connect the AVR Dragon to the computer
before running the USB Setup in order to follow this procedure described in Software and
USB Setup. Next is a photo of the AVR Dragon with 40 pin ZIF I.C. socket.
You only need three lines to debug you AVR micro with the AVR Dragon VDD, GRND,
DEBUGWIRE, that is only possible if the debug interface is enable, if the debug
interface is disable you need to use the full six pin or cables for the ISP to enable the
debug wire, Most AVR micros come with this interface enable if the have less than 32
kilo bytes of memory and 32 I/O pin or less, all the ATtiny family, ATmega48 to
Atmega328, the new Atmega32_64C1_M1 with 32 pin I/O, if you AVR have 16 kilo
bytes of memory and more than 32 pin I/O lets say 40 pins I/O to more than 100 pin I/O
they use the JTAG (IEEE std. 1149.1 compliant) Interface. The AVR Dragon can debug
with the included JTAG interface but only up to 32 kilo bytes of memory. If you need to
debug more than 32 kilos of memory you will need the JTAGICEmkII which have no
limitations also is used to debug AVR32 micros. Atmel have a new family of AVR 8 bit
microcontrollers the XMEGA (atmega AVR turbocharged, supercharged and
nitrocharged in one microcontroller) family.
Ok stop panicking, let’s install AVR studio 4, get the newest version of Studio 4 and
WINAVR C compiler, also install the USB drivers, after you download AVR Studio 4
connect any development tool to you computer to see if need updating. Next is a photo of
AVR Studio with my project.
After you install Studio 4 and check for tools updates install WINAVR the C
compiler which is free. If you look closely AVR Studio 4 is a very complete software
development tool, the only thing you need is the debugger to be able to see any internal
register from the microcontroller when you run you software, also you can simulate you
Program with the build in simulator, however I constantly prefer the AVR Dragon
debugger.
After you install all the software you need to create a project in Studio 4, go to the
project menu selection and select Project Wizard, if you have any previous project open
Studio 4 will ask you to save the project files and will close the project, then will open a
new window where you have three choices; first open a previous created projects, second
open a file where you can look for a project made by another compiler like Bascom AVR
or Code Vision AVR and the last one is create a new project from the ground up. The
startup wizard are displayed every time you start AVR Studio 4. From within this dialog
you can quickly reopen the latest used projects, change debug platform/device setup or
create a new project. Just double-click on the wanted project and it will automatically
open and restore to its last settings.
Select create a new project; a new dialog will appear where you can choose two
options: Currently two project types are available listed in the project type list box. Atmel
AVR Assembler and AVR GCC. The assembler (AVRASM2) are distributed with AVR
Studio, but you have to download a GCC compiler to create and use an AVR GCC
project. Projects can also be created by loading supported object files. File->Open file
must be used to create such projects. Input the project name. Default the initial file will
have the same name (ASM or C) and will be created, but this can be changed. A folder
with the project name can be created, but this is not default selected. If project name and
project type are ok, press next to select platform and device to simulate/emulate. You can
also finish now, but then the debug platform and device must be selected when a debug
session is started. Click finish when your selection is done. Also remember all debug
sessions require that you load a debug object file that is supported by AVR Studio.
Usually a debug file contains symbolic information which is not included in a "thinner"
release file. The debug information enables AVR Studio to give extended possibilities
when debugging, e.g. source file stepping and symbolic watches. Debug platform and
device selection can be done by selecting debug->Select debug platform and device. All
on-system debug platforms and devices are listed. When selecting a platform name, all
supported devices for that platform are listed in black color, and unsupported are listed in
gray. AVR Studio can be targeted towards the built-in AVR Simulator, or an AVR In-
Circuit Emulator (bought separately). Check out device selection for how to select your
debug platform and device. Independent of which debug platform is running, the AVR
Studio environment will appear identical. When switching between debug platforms, all
environment options are kept for the new platform. Some platforms have unique features,
and new functionality/windows will appear. Although all debug platforms appear
identical in the debug environment there will be small differences between them. A real-
time emulator will be significantly faster than the simulator. An emulator will also allow
debugging while the system is connected to the actual hardware environment, while the
simulator only allow predefined stimulus to be applied. In the simulator, all registers are
always potentially available to be displayed, which might not be the case with an
emulator. While the AVR Studio User's Guide describes the general behavior of AVR
Studio, these differences are thoroughly described in the platform's User Guide. The
Status bars always indicate whether debugging is targeted at the AVR In-Circuit
Emulator or the built-in AVR Simulator. The name of the actual device and debug
platform is output on the lower status bar.
Now that we are set up with the software we can do some programming, now the
software model I going to give you only have one channel, no Fail save or any other
feature, just a simple software skeleton to get you in the air very fast. Remember my
software was compiled with “Code Vision AVR” so you have to translate it to AVR GCC
or Bascom AVR. With AVR GCC the translation is minimal rename some pin and I/O
etc... with Bascom you have to setup the analog to digital converter, the timers and any
other feature you want to include in you software for the transmitter board, for the
receiver board you have to setup the PWM, the timers fail save etc…if you use the
Atmega8 with Bascom you will be ok but if you use ATtiny861 or 44 you will need to
use the software SPI from Bascom. Here is the skeleton for the Transmitter, receiver and
supporting files.
Notes about the software for the transmitter and receiver, all my compiler files are fully
commented:
#include <tiny861.h> // this include file is complier dependent, for AVR GCC
you need to change it and include extra files:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include <delay.h> // also this one, you need to create you delay code or check
you compiler documentation for support
// Pin change 0-15 interrupt service routine, when you push the joystick push button will
create a interrupt and will take the microcontroller here; depending on the switch you
have move battery enable or fail save enable the code below will execute according to the
switches position and voltage polarities.
interrupt [PCINT] void pin_change_isr(void) // Compiler dependant
interrupt vector handler need to be changed for AVR GCC
{
if ((push_sw_1 == 0) && (Bat_Chg_sw == 0)) // if joystick push switch is equal
0(ground) and battery charger switch is equal 0 then the charger is on
{ // input you code here for what ever
you want to do when the joystick push button is press on
Bat_Chg_En = 0;
}
else if ((push_sw_1 == 0) && (Bat_Chg_sw == 1)) // if joystick push switch is equal
0(ground) and battery charger switch is equal 1(vdd) then the charger is off
{
Bat_Chg_En = 1;
};
if ( push_sw_1 == 0 & Fail_save_en == 0) // fail save not implemented however
you can do the same as the battery charger code, you can implement
{
__no_op(); // any thing when the button are
depress or push
}
else {
__no_op();
};
}// end of interrupt vector
// External Interrupt 0 service routine, in this routine the Transceiver MiRF nRF24L01
board pull down the external interrupt request # 0 each time a interrupt is generated by
nRF24L01 the microcontroller need to clear the interrupt flags or read the receiver pay
load and clear the flags disable. This interrupt service routine is the same for the
transmitter and the receiver the only different is that the transmitter clears the flags and
the receiver read the transmitted pay load and then clears the flags. NRF24L01 have more
interrupts which can trigger this External interrupt line, check the nRF24L01 PDF data
sheet.
// Timer 1 overflow interrupt service routine, this routine will execute about every 18 to
20 milli seconds, however can be changed from 1 milli seconds to any time you wish, for
the transmitter this routine control the repetition rate the transmitter send data to the
receiver, for the receiver this routine control the speed of the software PWM, both
ATtiny861 and 44 have a very powerful timer1.
// ADC interrupt service routine, will scan the ADC completely automatic from input #0
to #5
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void) //Compiler dependant interrupt
vector handler need to be changed for AVR GCC.
You need these two routines, they send data thru the “USI” or SPI interface, they are the
same for the transmitter and receiver, check application note AVR319: Using the USI
module for SPI communication for more information how to use these routines
W_usi_data( unsigned char byte_count) //Write register with USI
This two file have been modified with all the define bytes and bits names of these two
AVR microcontrollers; you only need this file if you are using Code Vision AVR
compiler which do not include some bits names in the two original #include <tiny861.h>
and #include <tiny44.h> files.
#include <tiny861.h> //this include file is complier dependent
#include <tiny44.h> //this include file is complier dependent
Well this is the end I hope you have learn something today.
If you want buy my boards send me a private messages.
Best regards from Marcial Adorno