It is currently Wed Jun 19, 2013 9:10 am

All times are UTC + 10 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: AVR - Flash a single LED
PostPosted: Fri May 06, 2011 2:05 am 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
Heres a simple AVR code using C language to flash a single LED on PORTB :)

Code:
// 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;
}


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Sat May 07, 2011 10:52 am 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1897
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!


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Sat May 07, 2011 4:13 pm 
Offline
I practically live here!
I practically live here!
User avatar

Joined: Thu Sep 09, 2010 6:06 am
Posts: 109
Location: London
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.


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Sat May 07, 2011 8:33 pm 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
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)?.

Quote:
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.


Attachment:
bitwiseC1.jpg
bitwiseC1.jpg [ 38.49 KiB | Viewed 1293 times ]


You could also write C code like this:

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


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Mon May 09, 2011 5:16 pm 
Offline
I practically live here!
I practically live here!
User avatar

Joined: Thu Sep 09, 2010 6:06 am
Posts: 109
Location: London
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.


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Tue May 10, 2011 3:59 am 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
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? :)

Attachment:
C language help.rar [1.56 MiB]
Downloaded 85 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),
Attachment:
STK500.jpg
STK500.jpg [ 23.73 KiB | Viewed 1283 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?:
Attachment:
Introduction to the STK500.rar [3.08 MiB]
Downloaded 67 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.
Attachment:
ispMKII.jpg
ispMKII.jpg [ 61.63 KiB | Viewed 1283 times ]


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Tue May 10, 2011 5:54 am 
Offline
I practically live here!
I practically live here!
User avatar

Joined: Thu Sep 09, 2010 6:06 am
Posts: 109
Location: London
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.


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Tue May 10, 2011 6:37 am 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
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 :)


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Tue May 10, 2011 7:04 am 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
Heres a good tutorial for AVR :)

http://imakeprojects.com/Projects/avr-tutorial/


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Tue May 10, 2011 10:21 pm 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1897
How have you found the learning curve of using AVR's with C as opposed to pics in basic?


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Wed May 11, 2011 4:46 am 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
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 :)


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Wed May 11, 2011 6:14 am 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1897
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 :)


Top
 Profile  
 
 Post subject: Re: AVR - Flash a single LED
PostPosted: Wed May 11, 2011 6:44 am 
Offline
Moderator
Moderator
User avatar

Joined: Sun Mar 28, 2010 9:03 pm
Posts: 724
Location: United Kingdom
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! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 10 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme made by Keenen Wheeler