AVR - Flash a single LED

Post here to discuss all things Arduino!

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

AVR - Flash a single LED

Post by bitfogav » Fri May 06, 2011 2:05 am

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;
 }

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: AVR - Flash a single LED

Post by brad » Sat May 07, 2011 10:52 am

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!

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: AVR - Flash a single LED

Post by odessa » Sat May 07, 2011 4:13 pm

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
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: AVR - Flash a single LED

Post by bitfogav » Sat May 07, 2011 8:33 pm

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 25563 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). :)

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: AVR - Flash a single LED

Post by odessa » Mon May 09, 2011 5:16 pm

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
(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: AVR - Flash a single LED

Post by bitfogav » Tue May 10, 2011 3:59 am

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 766 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 25553 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 755 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 25553 times

User avatar
odessa
I practically live here!
I practically live here!
Posts: 102
Joined: Thu Sep 09, 2010 6:06 am
Location: London
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: AVR - Flash a single LED

Post by odessa » Tue May 10, 2011 5:54 am

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,

(\_/)
(='.')
(")-(")
This is a bunny, copy bunny into your signature to help him achieve world domination.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: AVR - Flash a single LED

Post by bitfogav » Tue May 10, 2011 6:37 am

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 :)

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: AVR - Flash a single LED

Post by bitfogav » Tue May 10, 2011 7:04 am


User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: AVR - Flash a single LED

Post by brad » Tue May 10, 2011 10:21 pm

How have you found the learning curve of using AVR's with C as opposed to pics in basic?

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: AVR - Flash a single LED

Post by bitfogav » Wed May 11, 2011 4:46 am

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 :)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: AVR - Flash a single LED

Post by brad » Wed May 11, 2011 6:14 am

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 :)

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: AVR - Flash a single LED

Post by bitfogav » Wed May 11, 2011 6:44 am

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! :)

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 7 guests