This forum is in archive mode. You will not be able to post new content.

Author Topic: [AVR] Tutorial 01  (Read 2088 times)

0 Members and 1 Guest are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
[AVR] Tutorial 01
« on: April 28, 2012, 02:18:53 AM »
  I've been meaning to write a tutorial series on AVR development. I was going to base it on the Arduino IDE and bootloader, but we are all men here, so I'll go forth with true AVR development with C and little spoon feeding.
Sorry before hand, about any spelling errors. It is not my best subject.
And since this is my 200th post this first comment will receive a new MP3 PLAYER!!!! Just kidding..... But I thought my 200th post should contribute to this fine community, instead of replies of empty opinions and a few LOLs.
 
 
 What are microcontrollers?
 A microcontroller is a computer on a chip. Just like modern 32/64bit computers, a microcontroller (mcu or uc from here on out) has a processor, RAM, and non-volatile memory EEPROM; ALL ON ONE TINY CHIP!!! There is a slight downside to this though. It is mostly in 8bit, and we will be sticking with 8bit mcu's in the rest of this tutorial. In particular Atmel's ATmegea328p. This chip will come with an Arduino Unu or Duemilanove which is the only AVR's I have on hand at the moment.
 
 
 Why do I care?
 Some of you might be asking, "Why the hell would I wanna know this?" or "Who gives a fuck?"
 Well, Mr. Grumpy Gills, you will no longer be tethered to the software world, and live in your computer. With uc's you can take your inner geek and touch the world around you. You can make things move, turn things on and off, sense temperature, pressure, humidity, water levels, impact, bending, etc... Really, it is limitless. Log the light levels in your room, and automatically turn lights on. Measure the temp in your server closet and auto trigger bigger fans when needed. Turn your music on as soon as you walk through your door. Use your imagination... Hell, make a bot that fixes you a sandwich and a beer when you send it a text.
 
 
 Holy shit batman, how do I start?
 Great question robbin, all you need is an AVR, programmer, and a computer. We now know what an AVR is and we know what a computer is; what's this programmer you speak of? Well, we have a uc and we have written some firmware for it on our computer, now we need to get the program from our computer the the mcu. That's why we need a programmer. The most popular programmers I've seen are ISP. Or In System Programming. For example this seems to be popular. For this tutorial, I will be using my Ardino as an ISP programmer, because I can lol.
 
 
 Setting up the Environment.
 Since I am on a Linux system (Debian), and only own Linux systems, you guessed it, we'll be doing dev on linux exclusively. You might be on windows, or worse, a Mac. The Mac setup isn't too much different than the way I'm going to show you here. Windows has it's own GUI's for this type of thing. The only thing different between any of these systems is the compiler and IDE's.I'm sure with your Googlefoo you can follow this just fine.
 
 
 First if /usr/local/bin isn't in your system path, add it now. 99% of distros already have this in the path.
 
 
 The toolchain is comprised of three things. The C compiler, binutils, C libs. You can use a debugger, and we will install one just because it will come in handy for later tutorials. Like mentioned above, this will be for Debian Linux only using apt-get.
 
 
 First is the C avr tool set:
Code: (bash) [Select]
sudo apt-get install gcc-avr
Code: (bash) [Select]
sudo apt-get install avr-libc
Code: (bash) [Select]
sudo apt-get install gdb-avr See, that wasn't so bad huh?
 
 
 Now, we get what we need for ISP programming:
 
Code: (bash) [Select]
sudo apt-get install uisp
 
 And lastly avrdude. This is pretty much like uisp, but can handle much more.
Code: (bash) [Select]
sudo apt-get install avrdude
 
 Now that is it for setting up the toolchain on which we will hack our way into embedded solid state euphoria.
 
 
 Arduino as an ISP programmer
 For this I am going to have you go here. Basically we flash the the board with the ISPprogrammer firmware that comes with the Arduino IDE.
 We will be using this setup:
 
 Since I am not going to be using an external clock source for the simple things we are going to do in this tutorial.
 
 
 The code...
 Now that we have everything set up, it's time to enter the matrix... Well, maybe not matrix yet, that will come with multiplexed leds and the binary clock build I'll host here when I have the money.
 
 
 
Code: (c) [Select]
#include <avr/io.h>
 #include <util/delay.h>
 
 
 void delay() {
    unsigned char c = 0;
    while (c != 85) {
        _delay_loop_2(40000);
        c++;
    }
 }
 
 
 int main(void) {
    DDRB |= (1<<PB5);
    while (true) {
        PORTB |= (1<<PB5)
        delay();
        PORTB &= ~(1<<PB5);
        delay();
    }
    return 0:
 }
 

 
 For simplicity, lets say DDRB controls if we're doing input or output. In our case it is output.
 _delay_ms() should be self explanatory; the built-in delay can only go for so long. So we created a function to expanded the delay to as long as we want(to around 4 seconds or so pending on clock speed). Just know that PORTB is the pins we are controlling. The "include"s are there so we can latch into the functions we want, such as the pins and the delay. NEVER, I repeat NEVER should you return from main().
 
 
 Conclusion
 This will get your feet wet. I didn't go over loading the chip with the program just yet, because I am assuming most all the community here doesn't know bitwise operators or what the program is actually doing. I'm sure most of you don't think on the bit level and throw around int's and long's like it's nothing.
 
 
 Do IT!!! Do it NOW!!!
 Further reading to keep up with the next tutorial. I am intentionally not going over what bit-wise operators do, or how they work. You will only learn by studying it for yourself, else you don't rightly give a shit. The openness is for the people who want to pursue this field. It is not for everyone. To fully understand future tutorials, it is vital that you know some of hardware theory. Transistors, caps, resistors, diodes, true logic (NAND, OR, XOR, etc..) to really grasp what this has to offer. Wiki and Google are your friend.
 
 
 If you want something a bit more softer for the n00b, then check this out.
 It is where I started, and if interest in the Arduino platform is welcomed above the core AVR dev with C, I'll start a new thread. Arduino is a lot easier than the core dev, but we don't really learn with sugar coats. The ports and registers and how it works to the core is a better lesson to me anyway. But if you don't know what I=R/V means, by all means stick with the bootloader and built-ins. It is where I had my start, but I want to start people here on the actual note, no the watered down.
 
 
 Hope you enjoyed the read. Now go blink some LED's and make the next doomsday device!!!! GO, no one is holding you back.

Sorry about any formatting errors. Tis all EZ. I changed all text to black because I was having some issues. If you need to color changed, let me know.

Further edit :
Maybe not black then, white?
« Last Edit: May 04, 2012, 10:28:37 AM by Factionwars »
>>>import this
-----------------------------

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: [AVR] Tutorial 01
« Reply #1 on: May 04, 2012, 10:29:00 AM »
I liked it so far :D, changed the code boxes :)
~Factionwars

th3g00n

  • Guest
Re: [AVR] Tutorial 01
« Reply #2 on: May 05, 2012, 10:13:20 PM »

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [AVR] Tutorial 01
« Reply #3 on: May 05, 2012, 10:28:18 PM »
Thanks Factionwars. The only peeve I have about the colored code is the grey letters are hard to see. The Bash looks great though.
>>>import this
-----------------------------

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.