2 Jul 2012


              In ATmega8 there are four Ports which are
               1. PORTA
               2. PORTB
               3. PORTC
               4. PORTD
Now accessing this ports. you know it is a step which you should do what ever the microcontroller you are working with 1st thing you should do is see the input output files .h  in avr see the #include <avr/io.h> and their perticular microcontroller .h files for atmega8 #include <avr/iom8.h>. Mostly you will come to know what it is doing and to what they are #defined ok.

Here i will explain everthing , it doesn't change lets have a requirement you need to glow an led using one of the Port pin.

Accesses:  1. Directly using hex values but for manipulations its very confusing
                 2. using shift operators  like PORTB |= (1 << PB0)
                 3. using inbuild library functions like _BV(PB0)
                      _BV(PB0) is #defined as (1 << PB0) in the library so i prefer to use 2nd or 3rd access method.

Access-1:  using direct hexadecimal values
                   lets explain it is clearly using an example.
                #include <avr/io.h>
                void main()
                    DDRB = 0X01; //which is used to tell the micro controller 0th pin of PortB is used as output and remaining as inputs.
                    PORTB = 0X01; //giving 5v to 0th pin so that led glows
Though it looks easy now but if we go deep into condition where to access these type become some what complicated and difficulty you couldn't read after some span of time your code itself . you will get confuse . Lets see the second access method.

Access-2:  using the shift operators 
                   lets take an example to understand clearly.
                   #include <avr/io.h>
                 void main()
                        DDRB |= (1<< PB0);
                        PORTB |= (1<< PB0);
This is very easy to access and more readable also. your code can be read by anyone and it will be easy to you to access the code after long time also.

Access-3: using the built in functions that are given in the library.
                  example : to make it clear.
                    #include <avr/io.h>
                 void main()
                          DDRB = _BV(PB0);
                          PORTB = _BV(PB0);
This is similar as the above but this is inbuiltly defined see the avr-libc .h files you will come to understand what are the ports defined to how the isr is being accessed and what are the values given to #defines so i hope you will do this . lets discuss about the PIN in next session. Have a nice day !!!!!!!!!!!!!!


No comments:

Post a Comment