[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 580: sizeof(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/session.php on line 636: sizeof(): Parameter must be an array or an object that implements Countable
Brads Electronic Projects Forum • AVR - Flash a single LED
Page 1 of 1

AVR - Flash a single LED

Posted: Fri May 06, 2011 2:05 am
by bitfogav
Heres a simple AVR code using C language to flash a single LED on PORTB :)

Code: Select all

// F_CPU tells util/delay.h our clock frequency
#define F_CPU 8000000UL	// Clock frequency (8MHz)

#include <avr/io.h>            // avr input and output header
#include <util/delay.h>       // avr delay header
 
 int main (void)
 {
		 DDRB = 0xFF;	// all PORTB Outputs

			 while(1) // do forever...
				 {
                PORTB |=  1 << PB0;	// LED on
                _delay_loop_2(40000);
				   PORTB &= ~(1 << PB0);	// LED off
                _delay_loop_2(40000);
				}
 return 0;
 }

Re: AVR - Flash a single LED

Posted: Sat May 07, 2011 10:52 am
by brad
I started looking into programming PIC's with C a couple of years ago and can see that the syntax is quite similar.

Be sure to keep us posted of your future endeavors!

Re: AVR - Flash a single LED

Posted: Sat May 07, 2011 4:13 pm
by odessa
Hi Gav,

What do the annotations |,& and ~ mean ?

PORTB |= 1

PORTB &= ~

I ask as this looks essentially C to me and may be a good way for me to learn it ( Damn ... I just know I'm going to get an arduino now )

Jay

Re: AVR - Flash a single LED

Posted: Sat May 07, 2011 8:33 pm
by bitfogav
Hi Jay

Has far as Im aware there is a slight difference between AVR and Arduino, the Arduino includes a cross platform IDE which will get you started quicker. Both use the C language but with some differences. (abit like Swordfish and Proton both Basic languages but slight differences)?.
PORTB |=

PORTB &= ~
The code that you pointed out Jay are bitwise operators in C as all I wanted to do was change bit0 of PORTB, the diagram below might help explain the bitwise operators in C better.

The one bitwise operator that confused me when I saw it was "~" bitwise operator but once you understand how it works its easy enough. The "~" is a bitwise complement (bitwise NOT), in other words this operator works like this:

So if we take the binary value 1.

00000001

Then if we ~1 using the "~" operator, this will flip the bits so the value is now.

11111110

Hope that helps you understand how the "~" operator works. the others which are "&" is a bitwise AND, and the "|" is bitwise OR operator.

bitwiseC1.jpg
bitwiseC1.jpg (38.49 KiB) Viewed 25934 times
You could also write C code like this:

Code: Select all

PORTB = 0x01;  (which is Hex)

or

PORTB = 0b00000001; (which is binary)

or

PORTB = 1; (which is decimal)

all will set PORTB bit 0 high (logic 1). :)

Re: AVR - Flash a single LED

Posted: Mon May 09, 2011 5:16 pm
by odessa
Thanks Gav thats very helpful, I've yet to find any really good tutorials on C, I find it's syntax quite confusing what with all the parenthesis brackets etc.

Also can you recommend a starter board for and AVR ?


Thanks

Jay

Re: AVR - Flash a single LED

Posted: Tue May 10, 2011 3:59 am
by bitfogav
odessa wrote:I've yet to find any really good tutorials on C, I find it's syntax quite confusing what with all the parenthesis brackets etc.
Your right Jay I found it difficult finding a good tutorial on C too, spent alot of time searching google and just picked up bits of the syntax along the way, I can share some PDF files Ive come across which might come useful? :)
C language help.rar
(1.56 MiB) Downloaded 778 times
odessa wrote: Also can you recommend a starter board for and AVR ?
My knowledge of AVR development/starter boards are very slim if im honest, a friend of mine has the AVR STK500 board (cheaper on ebay),
STK500.jpg
STK500.jpg (23.73 KiB) Viewed 25924 times
comes with a built in programmer and alot of options for different types of Atmel microchips, but the board only comes with 8 buttons/leds, but I think there is a replacement out for this board now called STK600.

Heres the introduction PDF file for the STK500 if you are interested?:
Introduction to the STK500.rar
(3.08 MiB) Downloaded 763 times
From a personal view I would recommend getting the AVR ISP mkII programmer, very cheap (abit like the Pickit2, hence the word "abit") but there is one down-side of this programmer :( it doesn't supply power to your projects unlike the pickit2.. but easy enough to connect to a bread board, One other note that I have found with this programmer is that all your projects need a on board power supply (a simple voltage reg would do the trick) As there is what is known as a power supply buffer on the programmer, basically so the programmer knows if your Atmel microchip is a 3.3volt or 5v microchip.
ispMKII.jpg
ispMKII.jpg (61.63 KiB) Viewed 25924 times

Re: AVR - Flash a single LED

Posted: Tue May 10, 2011 5:54 am
by odessa
Thanks Gav they are very very helpful and appreciated :D

I'll check out ebay and get a programmer this evening, any recommends for a good Atmel starter chip ?

With regards to the AVR programmer you might like this

Its by Dave Jones .... I love his channel on you tube ... He's a nutcase :lol: ... Very clever guy though,


Re: AVR - Flash a single LED

Posted: Tue May 10, 2011 6:37 am
by bitfogav
HaHa yeah Dave Jones, I like watching his Blogs.. Quite interesting! :) (funny enough I was going to post that video).

Ummm ive just been using a 28pin - ATmega168 (ATMEGA168-20PU) seems to be good enough for what ive been messing with :)

Re: AVR - Flash a single LED

Posted: Tue May 10, 2011 7:04 am
by bitfogav

Re: AVR - Flash a single LED

Posted: Tue May 10, 2011 10:21 pm
by brad
How have you found the learning curve of using AVR's with C as opposed to pics in basic?

Re: AVR - Flash a single LED

Posted: Wed May 11, 2011 4:46 am
by bitfogav
brad wrote:How have you found the learning curve of using AVR's with C as opposed to pics in basic?
Still prefer Basic (Swordfish) I have to say and PIC microchip, this was just an adventure. C language is harder to learn. The syntax of C as Jay pointed out is confusing, there seems alot of librarys out there for AVR C language to a company you with building your projects but a personal view I would'nt say I found one I was overwhelmed with, especially when trying to add a LCD to one of my project. In that case I guess you cant beat the support you get with Basic (Swordfish) and the librarys (modules) that you get.

I dont really want to go into the Pros and Cons of the two microchips but heres a good video :)

Re: AVR - Flash a single LED

Posted: Wed May 11, 2011 6:14 am
by brad
Thanks for that extra info Gav. I am certainly not someone who shuns Atmel microcontrollers or arduino's or anything. For me, I just started out with pics and it has worked very nicely! I am sure that if I had started out with AVR's that I would still be in the same sort of position :)

Re: AVR - Flash a single LED

Posted: Wed May 11, 2011 6:44 am
by bitfogav
brad wrote:Thanks for that extra info Gav. I am certainly not someone who shuns Atmel microcontrollers or arduino's or anything. For me, I just started out with pics and it has worked very nicely! I am sure that if I had started out with AVR's that I would still be in the same sort of position :)
Same here Brad I started with pics and its working nicely!, just thought I would try the grass on the other side as they say!! hehe! :)