Author Topic: Looking for AVR programming tutorial  (Read 2910 times)

0 Members and 1 Guest are viewing this topic.

Offline apc3161Topic starter

  • Jr. Member
  • **
  • Posts: 36
  • Helpful? 0
Looking for AVR programming tutorial
« on: June 14, 2009, 09:45:37 AM »
Hello,

I'm looking for an ABSOLUTE BEGINNER tutorial for learning how to program in C for the AVR (i.e for the atmega8).

So far, every tutorial I've found (just like the $50 robot) doesn't actually teach you anything. They just say "copy this into your code and watch it work"

Example, I went to the AVR Freaks website, and they had a tutorial labeled "introduction to the AVR micro controller". When the programming section came up it just said copy this

Code: [Select]
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

// LED is connected to PB4 (pin 3 on the chip)
#define LEDPIN 4

int main(void)
{
DDRB = 0x1F; // PB0-PB4 output
PORTB = 0x00; // Set all pins low

// Turn LED on for 1 sec and then off for 1 sec forever
while(1)
{
PORTB |= _BV(LEDPIN); // Turn LED on
_delay_ms(1000); // Wait 1000 ms (1 sec)

PORTB &= ~(_BV(LEDPIN)); // Turn LED off
_delay_ms(1000); // Wait 1000 ms (1 sec)
}

return 0;

Now I'm totally new to this subject, and pretty much everything in the code looks like gibberish. Not one explanation is given as to why the code works as it does.

For example, no explanation is given as to what this is doing

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

or how this line works

PORTB |= _BV(LEDPIN);

Does anyone know of an absolute beginners tutorial, that assumes you know nothing. I want to learn how this stuff works, not just copy and paste code.

Any links would be greatly welcomed. Thanks

Offline RoboChan

  • Full Member
  • ***
  • Posts: 77
  • Helpful? 0
Re: Looking for AVR programming tutorial
« Reply #1 on: June 14, 2009, 11:53:11 AM »
So far, every tutorial I've found (just like the $50 robot) doesn't actually teach you anything. They just say "copy this into your code and watch it work"
Ya thats what im having problems with. but im kinda getting it...

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: Looking for AVR programming tutorial
« Reply #2 on: June 14, 2009, 12:24:25 PM »
The better C tutorials, those that carefully step you through the language, are most often those that are PC-centric. Those explain the language concept without being mired up in the "stuff" of embedded systems. I would say start there (with a PC primer) until you have read enough to be comfortable with the construct of the language.

Once you have a feel for the language elements, then you will start to identify the things that are different about using C with an embedded system.

You might find it best to look at the Arduino development system. It is C with helpful embedded libraries. The book "Getting Started with the Arduino" might be a lot of help to you:
http://www.circuitgizmos.com/products/cgbook/cgbook.shtml

Offline apc3161Topic starter

  • Jr. Member
  • **
  • Posts: 36
  • Helpful? 0
Re: Looking for AVR programming tutorial
« Reply #3 on: June 14, 2009, 01:39:36 PM »
The better C tutorials, those that carefully step you through the language, are most often those that are PC-centric. Those explain the language concept without being mired up in the "stuff" of embedded systems. I would say start there (with a PC primer) until you have read enough to be comfortable with the construct of the language.

Once you have a feel for the language elements, then you will start to identify the things that are different about using C with an embedded system.

You might find it best to look at the Arduino development system. It is C with helpful embedded libraries. The book "Getting Started with the Arduino" might be a lot of help to you:
http://www.circuitgizmos.com/products/cgbook/cgbook.shtml


Thanks for the advice. I'll write down what I'm doing now in the chance anyone ever runs across this problem.

I went to this link "Learn C programming languages in 21 days"

http://neonatus.net/C/index.html

And I downloaded this program which you need to program in C for computers (similar to AVR studio). I tried a few, and this definitely seems like the most user friendly program.

http://www.bloodshed.net/devcpp.html

Hopefully after learning the language well, I can come back to Robotics, sigh.

Offline awally88

  • Robot Overlord
  • ****
  • Posts: 212
  • Helpful? 0
Re: Looking for AVR programming tutorial
« Reply #4 on: June 14, 2009, 10:10:05 PM »

In all honesty you don't need to be a great programmer to build simple robots, If you learn C for a few weeks you will be able to understand the basic programming easily. Then as long as you keep learning more about the language as you go you'll be fine! As you get better at C you will make more complicated programs for the robot to follow.

Offline apc3161Topic starter

  • Jr. Member
  • **
  • Posts: 36
  • Helpful? 0
Re: Looking for AVR programming tutorial
« Reply #5 on: June 21, 2009, 07:28:45 PM »
Ok, after learning the basics of C, I'm still unfortunately lost. Every single microcontroller "tutorial" (and I've found dozens now) does the exact same thing. They simply have you copy code and then say "VUALA! see it works". This of course is useless.

So if someone can please explain to me code in the $50 robot I would appreciate it. I'll start with "SoR_utils.h"

Code: [Select]
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)
#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )

Code: [Select]
void configure_ports(void)
{
DDRC = 0x00;  //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF;  //configure all D ports for output
DDRB = 0xC7;  //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
}

Obviously I can read the comments in the code and understand in general what each part "does." However, I don't understand why or how it does it. Can someone out there please explain. Thanks

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Looking for AVR programming tutorial
« Reply #6 on: June 22, 2009, 10:52:44 AM »
Apparently your problem is not C but the AVR registers. And the answer for this is simple: datasheet (this is for the atmega8, check the atmel site for other datasheets.

Basically this code configure and manipulate the I/O ports through register (8 bits memory "boxes" that have a special meaning for the AVR). Every register of the AVR is documented in its datasheet. You use them to access peripherals on the AVR (ports, timers, uart...).

For instance, DDRD is the direction register for the ports D (8 ports). Each bit sets the direction of the port: 1 is for output, 0 for input. So the statement DDRD = 0xFF actually sets all D ports to output.

Chelmi.

Offline billhowl

  • Supreme Robot
  • *****
  • Posts: 375
  • Helpful? 32
  • Your success is in your hands.
Re: Looking for AVR programming tutorial
« Reply #7 on: June 30, 2009, 11:37:58 PM »

 


Get Your Ad Here