Same thing as an Arduino Uno but smaller. Let's look at the ATtiny13 MCU. 64bytes of usable RAM and 1kByte flash program storage. Only 6 IOs from PORTB.
It's all you need for a security dongle or a USB reader. Thermostats and washing machines use these.
Sample CodeThis code will run on an Arduino or other board using Atmega processors such as the ATtiny. It toggles bits on PORTB.
#define reps 3
#include <avr/io.h>
#include <util/delay.h>
int main() { DDRB = 0xFF; // PORTB output
while(1) {
// decimal version of PORTB = 1 then PORTB = 0
PORTB = 1; _delay_ms(500); // led on
PORTB = 0; _delay_ms(500); // led off
// decimal version of PORTB = 255 then PORTB = 0
PORTB = 255; _delay_ms(500);
PORTB = 0; _delay_ms(500);
// binary version of PORTB = 1 then PORTB = 0
PORTB = 0b00000001; _delay_ms(500); // led on
PORTB = 0b00000000; _delay_ms(500); // led off
// binary version of PORTB = 255 then PORTB = 0
PORTB = 0b11111111; _delay_ms(500);
PORTB = 0b00000000; _delay_ms(500);
// hexadecimal version of PORTB = 1 then PORTB = 0
PORTB = 0x01; _delay_ms(500); // led on
PORTB = 0x00; _delay_ms(500); // led off
// hexadecimal version of PORTB = 255 then PORTB = 0
PORTB = 0xFF; _delay_ms(500); // led on
PORTB = 0x00; _delay_ms(500); // led off
// shifts single bit from PORTB.0 to PORTB.7
for (int j=0;j < reps;j++) {
for (int i=0; i < 8 ;i++) { PORTB = (1<<i); _delay_ms(4000);}
for (int i=0; i < 8 ;i++) { PORTB = (128>>i); _delay_ms(4000);}
}
// masking of bits with |= and &= operators
for (int j=0;j < reps;j++) {
PORTB |= 0b00000001; _delay_ms(500); // mask to force PORTB.0 = 1
PORTB &= 0b11111110; _delay_ms(500); // mask to force PORTB.0 = 0
}
// alternate pattern
for (int j=0;j < reps;j++) {
PORTB = 0x55; _delay_ms(500); // PORTB = 0b01010101
PORTB = 0xAA; _delay_ms(500); // PORTB = 0b10101010
}
// mathematical inverse of PORTB is put into PORTB
for (int j=0;j < reps;j++) {
PORTB = !PORTB; _delay_ms(500);
}}}
Our ATtiny13 development board has LEDs on pin PB3 and PB4. You may wish to change this code to use those numbers.
Xpress IDE onlineOpen an account at MPLab Xpress IDE online. Create a New Project named at13 and select ATtiny13 MCU. Create a main.c file and enter this code. It will make LEDs blink.
Menu Run->Run Project and a hex file of this code will be delivered to your downloads folder. The first hex file will be at13.hex and the second will be at13(1).hex and so forth. You will have to clean up extra files in your downloads folder.
Download and run SimulIDE electronic simulator program. Donations are welcome.
Open a blank circuit. Find and add an ATtiny13 chip. Find and add buffers and an LED bar. Ground the cathode of the LED bar and connect the 6 pins of the ATtiny to LEDs on the bar until you have something like this.
This circuit simulator works when I add buffers to amplify the current. A real circuit may work with just LEDs.
Load FirmwareRight click directly on the ATtiny processor chip and look at the commands for Load firmware.
Find the hex file we generated from the Online compiler. We can use hex files from Arduino IDE, MPLab, Atmel Studio and many other compilers.
SimulIDE will power off as a safety feature when you make changes to the circuit or software. Click the On/Off power button and Pause button at the top of the screen.
Right click on the MCU and open MCU Monitor. This looks inside the memory of the simulated processor.
Flash shows our demo code. PC is the Program Counter a dedicated register that points to the active line of program code.
Many development boards do not contain a programmer circuit. USBtiny and USBasp are inexpensive devices that work with AVR processors.
Loose wires are an option. If you are using a cable pay attention to pin numbering, connector orientation and keying.
The HW-260 development board provides this 6pin programming header.
Open a terminal screen and enter
avrdude -p t13 -c usbasp -U flash:w:at13.hex:i
Avrdude program will upload a hex file to an ATtiny13 using a USBasp programmer. You should see this on your screen, you may see blinking lights on the programmer.
Since the IC integrated circuit package is a complete SOC system on a chip all we need is to add power and the ATtiny will start up and run the program it has in flash. We did not need to add any buffer circuits for this to work.
We can connect to a programmer with short, loose wires but we may need to use a slower baudrate speed parameter like -b 1200.
Notice that the LED on PB5 does not fully light. It is in pinMode(input) and a resistor pulls the lead up to 5v without enough current to light the LED.
PB5 is a reset input that we need to for device programming. We can recover the pin and use it as a normal IO but we may lose our ability to change the program.
Burn Bootloader?Let us talk about it. I don't think we need to.
- A bootloader program takes up flash in a device that is limited
- A bootloader program is only needed for Arduino IDE
- Without a bootloader the device can still talk through a serial or USB interface
- Many MCUs will only ever be programmed once
Set Arduino IDE for ATTiny13 device, select programmer, yes for bootloader menu Tools-> Burn Bootloader. Look in the verbose output for two lines of white text.
This is the command to burn fuse circuits in the device.
This shows a hex file uploaded to the chip. It is the compiled boot program.
Comments