- 8 LEDs
- 8 200-400 ohm resistors
- 5V voltage source
- ATMega1284P (or almost any other AVR chip)
- AVR Programmer (I'm using the Atmel ICE)
Take a look at the datasheet for the ATMega1284P so we can pick which port to use to hook the LEDs up to. On page 2 it shows the pinout for the AVR (I'm using a PDIP type). I'm going to use port B because they aren't separated and are on the same side of the chip which makes wiring a little bit easier.
Attach everything as shown in the breadboard image below:
Now all we have left to connect is a power source and our programming cable. Looking at the pinout for the 1284, we need to hook up pin 10 to 5V, pin 11 to Ground, pin 6 to the MOSI of the programming cable, pin 7 to the MISO of the programming cable, pin 8 to the SCK of the programming cable, and pin 9 to the RESET of the programming cable. One thing that originally confused me when coming from Arduino to just AVR's was that you need an external power supply to power the chip. At least with the Atmel ICE, there is no built in power source, unlike on the Arduino.
Both of these images are helpful for wiring up the programming cable.
When looking at the programming cable, the notch is on the left with the cable facing up (towards you)
Now that everything is connected we can finally open up Atmel Studio and start programming!
Create a new project from the start page when it first launches. Name it and save it wherever you want. The default location should be fine. Make sure that you're using a GCC C Executable File in the New Project wizard. Click Okay. Now select the chip you're using on the Device Selection window via the search bar or the list. I'm using the ATMega1284P so I selected that.
You will then have a blank program ready for your input!
First thing we need to do is setup PORTB as an output register. We would need to set the Data Direction for Register B (DDRB) to all 1's in main(). There's a few ways to do that:
I prefer to use the hex method because it's less typing. (0xFF = 0b11111111)
The while loop inside of main is where our code will continuously run, it's very similar to how void loop() works on an Arduino.
That is where we will turn on our LEDs and look at some shiny goodness! There are multiple ways to turn the LEDs on. You can turn the whole port on at a time, or each bit in the port individually. I'll show you the whole port on first, then individually:
To turn on and off LEDs individually we would approach it like this:
In the example above we are only turning on 3 bits from the port, bits 1, 2, and 7.
In the last example I'm going to show one more way of turning on individual bits, which is basically the same thing. In the example the goal is to have a single LED lit up moving up and down the port, it looks really cool.
In the above example on line 17 and 22, the (1 << x) is basically saying to only set high the x bit. Meaning that if you typed PORTB = (1 << 3), the 3rd bit (PB3) would be the only bit set high on that port.
Feel free to leave any questions in the comments!