5 Jul 2012

General compiling steps & Library creation

General Steps of compilation of C file:

               \ /
                |                 .c    
                |                  |
      Preprocessing      |                gcc -E file.c
                |                 .i
                |                  |
        Compiling           |               gcc -c file.c
                |                 .s
                |                  |
       Assembler           |               gcc -S file.c
                |                 .o (asm)
                |                  |
          Linker              |      
                |                .exe
                |
          executable                       gcc  file.c  -o  foo



   General execution:
               
               gcc file.c                -->  gives executable file
               gcc  -c  file.c          -->  gives the object file

Creating the Dynamic and Static Libraries  :

 Static Library:
    Static library has the extention of  (.a) . You should know that when ever you call the function of this library the whole code is dumped into the main code while compilation time so that the whole size of the program increases but the execution speed increases. But the main requirement is the file should be small and the speed should be also fast right . so we go to the dynamic libraries.
   Ok lets discuss how to create a static library and how to link it to the required file.


1. All file first make them as object files using the command 
                gcc   -c  file1.c file2.c       -->   file1.o  file2.o
2. Now you create using this object files your static library file
               ar cr  libswap.a  file1.o file2.o   --> gives the output of libswap.a  
3. Now you have created the library now link it to the main program
               gcc -Wall main.c  libswap.a  -o  swap
      The above command tells that main in linked with libswap.a  library and executable is renamed as the swap if the library (.a) is in the same file other wise you should give the path of the library file where it is located. This i will dicuss in the dynamic library. ok just here i showed creating and linking the main and library in the same file.


Dynamic Library:
    The good thing about this is when a function is called then pointer goes to the location where it is located and execute over there and come back to the main when it is done with the function . It just saves lot of memory and execution speed is also good.That is why most of the operating systems like unix and linux uses such type of libraries mostly.


    So lets learn how to create the Dynamic Libraries:
1. gcc -shared  -fPIC  -I ../../include   swap1.c  -o  ../../library/libswap.so 
          Ya now i feel you feel some difficulty its very simple i will tell you 
-shared   -->  is used to produce shared object which can be linked with other objects to form an executable.
-fPIC      -->  compiler directive to output position independent   code

2. gcc  -I  ../include/   -lswap  -L ../Library/  swap_main.c  -o  swap_main

here it tells that including the folder name where the .h files are located and linking the library where it is located (the folder paths we should specify from current directory) thats it very simple na. 

Don't still deep into it you should know one thing in embedded field or any other field read only what is required . Don't try read the whole stuff from scratch it will be helpful but you will loose your valuable time which is precious if you don't have any work like targets then you can.
 
              

4 Jul 2012

Reading from Flash is little Endian style

Suppose  You have a hex line as show below
     : 10 0000 00 0C9434000C944F000C944F00 4F

The machine instruction 940C is saved in the .hex file as 0C94 ok

CODE:  in hex file
0x0000: 0C
0x0001: 94
0x0002: 34
0x0003: 00
0x0004: 0C
0x0005: 94
0x0006: 4F
0x0007: 00
0x0008: 0C
0x0009: 94
0x000A: 4F
0x000B: 00

How ever when you switch on the power to an avr it makes 16bit opcode fetch which is little- endian. Finally it reads the code in:
0x0000  : 0c
0x0001  : 94

as 0x940C  and passes it to the instruction decoder which identifies it as a jump op code . This again  make a second fetch as part of this same opcode this time it picks up

0x0002  :  34
0x0003  :  00

which again it reads as little endian and therefore treats as WORD address 0x0034 (which is BYTE address 0x0068). So after reading the first 4 bytes in two 16 bits chunk it has determined it has "JMP 0x0068" and sets PC to be that address. (really 0x0034 in fact - it's just GCC that interprets things byte wise to be common across all architectures).

So, the bytes are programmed in exactly the order they appear in the .hex at increasing byte addresses but when the AVR core makes fetches (two bytes, 16bits at a time it treats them as  little endian using the lower addressed of the two bytes as the lower part of each 16bit fetch. So it's not when programming hex that little endian ness comes into play - it's when the data/code is later fetched back from the flash that the endianness issue applies.

So, finally you can say that data or code is fetched in little endian model.

Hex file format and how to read it?



Using Vi editor you could open this .hex file in previous section i showed you how to create a hex file and now we are going to learn how to read a hex file.
What all the things it consists you will learn.

$ vi file.hex

Now you could see the first line in the picture that is represented in the form

:10 0000  00 8F EF 87 BB 18 BA 60 E0 81 E0 90 E0 E7 E0 F0 E0 B6

RED : Indicates the number of bytes is being written

BLUE : Indicates the starting address of the data being written

yellow: indicates the type of record . These were of 5 types:
             1. 00  -->  data record
             2. 01  -->  end of the record
             3. 02  -->  extended segment address record
             4. 04  -->  extended linear address record
             5. 05  -->  start linear address record

Blue : indicates the data

Black: indicates the checksum .

Now lets discuss about the checksum how it is calculated.
suppose you have the hex line give below:
:02 0000 02 1400  E8
Its nothing but 2's complement of whole except the checksum because we are calculating it right.


    now 01h+(~(02h + 00h + 00h + 02h + 14h +00h))
                   0000 0010 = 02h
                   0000 0000 = 00h
                   0000 0000 = 00h
                   0000 0010 = 02h
                   0001 0100 = 14h
                   0000 0000 = 00h  (+)
                  -----------------------------
                ~(0001 1000 )  =  1110 0111
                                              0000 0001 (+)
                  -------------------------------------------------------
                                               1110 1000 = E8 checksum
                  -------------------------------------------------------
          For all the lines it is in the same way take the whole line add up all the bytes until checksum and do 1s compliment for the result and then add the 01h the complement of result so that you will get the checksum .
        
I hope everyone got how the checksum is calculated right .
--------------------------------------------------------------------------------------------------------------------
Now lets discuss in detail about the type of records in detail ok. Lets go ........

Type 00 Record
          
              As i told you this will indicate the record is of data type so the type tells that it is the data record .
               This thing no need to tell in detail . This basic information is enough to understand ok.

Type 01 Record
           
               This record tells that it is the end of the record so this will be the last line in all hex file what ever it matters until you do something wrong .
                A perfectly executed hex file will have perfect format what i am describing here.
                And here checksum is calculated in the form:
                                     :00000001 FF
        This is calculated like this 01h+(~(00+00+00+01) )= 0xFF
The address in the end of record is meaning less so we can ignore it.

Type 02 Record   (Extended segment address record)


         1. Extended segment address record is also called HEX-86 record.
           2. Always have two data bytes and appear as follows

                        :02 0000 02 1400   E8
           3. Address field is always 0000h
         
Now the real part comes when an extended segment address record is read, the extended segment address  stored in the data field is saved and is applied to subsequent records read from the Intel HEX file. The segment address remains effective until changed by another extended address record.

The absolute-memory address of a data record is obtained by adding the address field in the record to the shifted-address data from the extended segment address record. The following example illustrates this process.

Address from data record's address field  =           2340
Extended segment address rec data field   =        1400
                                                                        -------------------
Absolute memory address                                00016340
                                                                        -------------------


Type 04 Record: (Extended linear Address Record)
         
         1. it is called 32-bit address records and Hex386 records.
         2. The upper 16bits (16-31) contains address of data .
    The extended linear address record  always have two data bytes and appears as follows:
                         :02 0000 04 FFFF FC
          FFFF   --> upper 16 bits of address
When an extended linear address record is read, the extended linear address stored in the data field is saved and is applied to subsequent records read from the Intel Hex file. The linear address remains effective until changed by another extended address record.

The absolute-memory address of a data record is obtained by adding the address field in the record to the shifted address data from the extended linear address record. The following example illustrates this process.

Address from the data record's address field =           2360
Extended linear address record data field      =  FFFF
                                                                            --------------------
Absolute-memory address                               =  FFFF2360   (32 bit)
                                                                            --------------------


Type 05 Record : Start linear address record .
       
          1. Only for MDK-ARM
          2. Specify the start address of the application
          3. contains 32 bit address (always 4 data bytes ) appear as
                   :04 0000 05 000000CD 2A

         000000CD --> starting address of the application.

Start linear address specifies the address of the __main(pre-main) function but not the address of the startup code which usually calls __main after calling Systeminit().
                    An odd linear start address specifies that  __main is compiled for the Thumb instruction set.

                    The start linear address Record can appear anywhere in hex file. In most cases this record can be ignored because it does not contain information which is needed to program flash memory .





3 Jul 2012

Creating the binary file and see how it is represented

#include <avr/io.h>
int main()
{
       DDRB = 0xFF;
       while (1)
       {
              PORTB ^=0xFF;
        }

}

So this is your file.c and you wana create the binary format of this file use this command:
avr-objcopy    -I  binary   -O   ihex  file.c   file.hex

so that you can see the output in the hexadecimal but it represents the binary values only.

see this for clear understanding:
:10 0000  00  23 69 6E 63 6C 75 64 65 20 3C 61 76 72 2F 69 6F 3D
                      #    i    n   c   l    u    d  e   '  '  <   a   v   r    /    i    o

:10 0010  00  2E 68 3E 0D 0A 0D 0A 69 6E 74 50 6D 61 69 6E 28 A6
                       .    h    >  \r   \n   \r   \n   i   n    t   ' '   m  a   i    n    (

:10 0020  00  76 6F 69 64 29 20 7B 0D 0A 09 44 44 52 42 20 3D C1
                       v   o   i    d    )  ' '    {   \r   \n   \t  D  D   R   B  ' '   =

:10 0030  00  20 30 78 46 46 3B 0D 0A 09 77 68 69 6C 65 20 28 B0
                      '  '  0   x    F  F   ;     \r  \n   \t   w  h   i    l    e   ' '   (

:10 0040  00  31 29 20 7B 0D 0A 09 09 50 4F 52 54 42 20 5E 3D  50
                       1   )   '  '  {    \r   \n  \t   \t  P   O   R   T  B  '  '  ^   =

:10 0050  00  30 78 46 46 3B 0D 0A 09 7D 0D 0A 7D 0D 0A EB
                      

Steps for compilation using usbasp programmer

______________________________________________________________________
Compilation steps using avrdude:
----------------------------------------------------------------------------------------------------------------
avr-gcc   -g   -Os   -mmcu=atmega8  -c   file.c
avr-gcc   file.o    -o  file
avr-objcopy   -j .text  -j  .data   -O  ihex  file   file.hex
avrdude    -c  usbasp  -p  m8  -U  file.hex
option of gcc and objcopy:

-g          --> for debugging symbols
-Os        --> for optimisation of the program there are 4 levels in the place of ' s ' you can place 1, 2, 3 , 4.
-c          -->  for the object file creation
-o          -->  Renaming the elf file or executable
-j           -->  used for the seperation of the section from the elf file
ihex       -->  is used to create the hex file 
options for avrdude :

-c          -->  to tell the microcontrollerthat is the programmer  being used
-p          -->  to tell the microcontroller which is being used
-U          -->  to tell that perticular file to be burnt or written.
______________________________________________________________________
Setting the fuse bit commands :
----------------------------------------------------------------------------------------------------------------
Writing fuses :
      avrdude   -c  usbasp  -p  m8  -U   lfuse:w:0xD9:m  -U  hfuse:w:0xD1:m

Reading fuses :
avrdude -v   -c  usbasp  -p  m8  -U  lfuse:r:low.txt:r  -U  hfuse:r:high.txt:r

Never ever make SPI fuse to '1' if you make it you couldn't program your microcontroller as it dissables the function. Again using Parallel programmer you should make that pin as high which is very costly . so , never even try doing this ok. if you have parallel programmer try this otherwise no need.

Once you did this its no use until you reprogram it . you couldn't do this with your programmer because your programmer itself is not read by the microcontroller if you dissable that pin. so be carefull.
________________________________________________________________________
Setting the lock bits :
-------------------------------------------------------------------------------------------------------------------
Same as the fuse bits programming but these bits have special feature that it can protect your application section and bootloader section as well preventing others to see your hex file or not even to read the sections . Nice know but we don't use at present . so lets go into how to compile

avrdude   -c  usbasp  -p  m8  -U  lock:w:0x3f:m 
_________________________________________________________________________
EEPROM PROGRAMMING :
---------------------------------------------------------------------------------------------------------------------
In order to make eeprom perfectly work you should make EESAVE fuse bit programmed . EESAVE = '0'.
If you don't make it programmed and even though you store the values at the eeprom they doen't store they will also be get erased while flash write . everything will be cleared. so in order to tell the microcontroller to no clear the eeprom we use EESAVE fuse bit which will be present in high fuse. 

Keep this in mind while you are programming an eeprom program . 
Lets deal with this section someother time in detail.ok .i hope in this session you learnt how to compile the file .

Here are the Steps for creating the binary file of a perticular file see this POST


sample .hex written in microcontroller (format in which it is written)

      enable program mode
      signature bits 
      fuse and lock bits
     eeprom bytes  
      chip erase 
      write to flash 
      page write
 AC530000 30000000 30000100 30000200 50000000 50000000 50000000 58080000 58080000 58080000 A001FC00 A001FD00 A001FE00 A001FF00 AC800000 AC530000
chiperase pm-mode
----------------------------------------------------------------------------------------------------------------------------------------------------
 4000 0022 4800 00C0     4000 0134 4800 01C0 4000 0233 4800 02C0 4000 0332 4800 03C0 4000 0431
  wr flash addr0000         addr    0001           addr 0002       addr 0003
 4800 04C0 4000 0530 4800 05C0 40 00 06 2F 48 00 06 C0 40 00 07 2E 48 00 07 C0 40 00 08 2D 48 00 08 C0 40 00 09 2C 48 00 09 C0 40 00 0A 2B 48 00
 0A C0 40 00 0B 2A 48 00 0B C0 40 00 0C 29 48 00 0C C0 40 00 0D 28 48 00 0D C0 40 00 0E 27 48 00 0E C0 40 00 0F 26 48 00 0F C0 40 00 10 25 48 00 10 C0
 40 00 11 24 48 00 11 C0 40 00 12 23 48 00 12 C0 40 00 13 4A 48 00 13 C0 40 00 14 4B 48 00 14 C0 40 00 15 4C 48 00 15 C0 40 00 16 4D 48 00 16 C0 40 00
 17 4E 48 00 17 C0 40 00 18 4F 48 00 18 C0 40 00 19 50 48 00 19 C0 40 00 1A 51 48 00 1A C0 40 00 1B 52 48 00 1B C0 40 00 1C 53 48 00 1C C0 40 00 1D 54
 48 00 1D C0 40 00 1E 55 48 00 1E C0 40 00 1F 56 48 00 1F C0 --4C001F00--28001F00--

 40002057 4800 20C0 4000 2158 4800 21 C0 40 00 22 59 48 00 22 C0 40 00 23 11 48 00 23 24 40 00 24 1F 48 00 24 BE 40 00 25 CF 48 00 25 E5 40 00 26 D4 48 00 26 E0 40 00 27 DE 48 00 27 BF 40 00 28 CD 48 00 28 BF 40 00 29 10 48 00 29 E0 40 00 2A A0 48 00 2A E6 40 00 2B B0 48 00 2B E0 40 00 2C E2 48 00 2C EB 40 00 2D F1 48 00 2D E0 40 00 2E 02 48 00 2E C0 40 00 2F 05 48 00 2F 90 40 00 30 0D 48 00 30 92 40 00 31 A0 48 00 31 36 40 00 32 B1 48 00 32 07 40 00 33 D9 48 00 33 F7 40 00 34 4C 48 00 34 D0 40 00 35 A1 48 00 35 C0 40 00 36 C9 48 00 36 CF 40 00 37 9B 48 00 37 01 40 00 38 AC 48 00 38 01 40 00 39 60 48 00 39 5C 40 00 3A 7D 48 00 3A 4B 40 00 3B 80 48 00 3B 4F 40 00 3C 9F 48 00 3C 4F 40 00 3D F3 48 00 3D E0 40 00 3E 66 48 00 3E 0F 40 00 3F 77 48 00 3F 1F --4C003F00--28003F00-- 40 00 40 88 48 00 40 1F
                                                        prg memory   
 40 00 41 99 48 00 41 1F 40 00 42 FA 48 00 42 95 40 00 43 D1 48 00 43 F7 40 00 44 E4 48 00 44 E0 40 00 45 22 48 00 45 0F 40 00 46 33 48 00 46 1F 40 00
 47 44 48 00 47 1F 40 00 48 55 48 00 48 1F 40 00 49 EA 48 00 49 95 40 00 4A D1 48 00 4A F7 40 00 4B 69 48 00 4B D0 40 00 4C 21 48 00 4C 50 40 00 4D 30
 48 00 4D 40 40 00 4E 1B 48 00 4E B8 40 00 4F 88 48 00 4F E1 40 00 50 8A 48 00 50 B9 40 00 51 86 48 00 51 E8 40 00 52 80 48 00 52 BD 40 00 53 30 48 00
 53 BD 40 00 54 29 48 00 54 B9 40 00 55 08 48 00 55 95 40 00 56 E8 48 00 56 2F 40 00 57 F0 48 00 57 E0 40 00 58 E0 48 00 58 31 40 00 59 F1 48 00 59 05
 40 00 5A 10 48 00 5A F5 40 00 5B ED 48 00 5B 5E 40 00 5C FF 48 00 5C 4F 40 00 5D 09 48 00 5D 94 40 00 5E 80 48 00 5E E3 40 00 5F 1D 48 00 5F C0 --4C005F00=28005F00-- 40 00 60 81 48 00 60 E3 40 00 61 1B 48 00 61 C0 40 00 62 82 48 00 62 E3 40 00 63 19 48 00 63 C0 40 00 64 83 48 00 64 E3 40 00 65 17
 48 00 65 C0 40 00 66 84 48 00 66 E3 40 00 67 15 48 00 67 C0 40 00 68 85 48 00 68 E3 40 00 69 13 48 00 69 C0 40 00 6A 86 48 00 6A E3 40 00 6B 11 48 00
 6B C0 40 00 6C 87 48 00 6C E3 40 00 6D 0F 48 00 6D C0 40 00 6E 88 48 00 6E E3 40 00 6F 0D 48 00 6F C0 40 00 70 89 48 00 70 E3 40 00 71 0B 48 00 71 C0
 40 00 72 81 48 00 72 E4 40 00 73 09 48 00 73 C0 40 00 74 82 48 00 74 E4 40 00 75 07 48 00 75 C0 40 00 76 83 48 00 76 E4 40 00 77 05 48 00 77 C0 40 00
 78 84 48 00 78 E4 40 00 79 03 48 00 79 C0 40 00 7A 85 48 00 7A E4 40 00 7B 01 48 00 7B C0 40 00 7C 86 48 00 7C E4 40 00 7D 5D 48 00 7D 9B 40 00 7E FE
 48 00 7E CF 40 00 7F 8C 48 00 7F B9 --4C007F00=28007F00-- 40 00 80 08 48 00 80 95 40 00 81 60 48 00 81 E8 40 00 82 75 48 00 82 E2 40 00 83 80 48 00
                       
 83 E0 40 00 84 90 48 00 84 E0 40 00 85 B1 48 00 85 DF 40 00 86 8A 48 00 86 E0 40 00 87 CE 48 00 87 DF 40 00 88 8F 48 00 88 EF 40 00 89 94 48 00 89 E3
 40 00 8A AC 48 00 8A E0 40 00 8B 81 48 00 8B 50 40 00 8C 90 48 00 8C 40 40 00 8D A0 48 00 8D 40 40 00 8E E1 48 00 8E F7 40 00 8F 00 48 00 8F C0 40 00
 90 00 48 00 90 00 40 00 91 8C 48 00 91 E0 40 00 92 C3 48 00 92 DF 40 00 93 8F 48 00 93 EF 40 00 94 94 48 00 94 E3 40 00 95 AC 48 00 95 E0 40 00 96 81
 48 00 96 50 40 00 97 90 48 00 97 40 40 00 98 A0 48 00 98 40 40 00 99 E1 48 00 99 F7 40 00 9A 00 48 00 9A C0 40 00 9B 00 48 00 9B 00 40 00 9C 8B 48 00
 9C E0 40 00 9D B8 48 00 9D DF 40 00 9E 8F 48 00 9E EF 40 00 9F 94 48 00 9F E3 --4C009F00=28009F00-- 40 00 A0 AC 48 00 A0 E0 40 00 A1 81 48 00 A1 50

 40 00 A2 90 48 00 A2 40 40 00 A3 A0 48 00 A3 40 40 00 A4 E1 48 00 A4 F7 40 00 A5 00 48 00 A5 C0 40 00 A6 00 48 00 A6 00 40 00 A7 8D 48 00 A7 E0 40 00
 A8 AD 48 00 A8 DF 40 00 A9 8F 48 00 A9 EF 40 00 AA 94 48 00 AA E3 40 00 AB AC 48 00 AB E0 40 00 AC 81 48 00 AC 50 40 00 AD 90 48 00 AD 40 40 00 AE A0  48 00 AE 40 40 00 AF E1 48 00 AF F7 40 00 B0 00 48 00 B0 C0 40 00 B1 00 48 00 B1 00 40 00 B2 80 48 00 B2 E2 40 00 B3 A2 48 00 B3 DF 40 00 B4 D1 48 00  B4 CF 40 00 B5 A1 48 00 B5 E2 40 00 B6 1A 48 00 B6 2E 40 00 B7 AA 48 00 B7 1B 40 00 B8 BB 48 00 B8 1B 40 00 B9 FD 48 00 B9 01 40 00 BA 0D 48 00 BA C0  40 00 BB AA 48 00 BB 1F 40 00 BC BB 48 00 BC 1F 40 00 BD EE 48 00 BD 1F 40 00 BE FF 48 00 BE 1F 40 00 BF A2 48 00 BF 17 --4C00BF00=2800BF00-- 40 00

 C0 B3 48 00 C0 07 40 00 C1 E4 48 00 C1 07 40 00 C2 F5 48 00 C2 07 40 00 C3 20 48 00 C3 F0 40 00 C4 A2 48 00 C4 1B 40 00 C5 B3 48 00 C5 0B 40 00 C6 E4
 48 00 C6 0B 40 00 C7 F5 48 00 C7 0B 40 00 C8 66 48 00 C8 1F 40 00 C9 77 48 00 C9 1F 40 00 CA 88 48 00 CA 1F 40 00 CB 99 48 00 CB 1F 40 00 CC 1A 48 00
 CC 94 40 00 CD 69 48 00 CD F7 40 00 CE 60 48 00 CE 95 40 00 CF 70 48 00 CF 95 40 00 D0 80 48 00 D0 95 40 00 D1 90 48 00 D1 95 40 00 D2 9B 48 00 D2 01 40 00 D3 AC 48 00 D3 01 40 00 D4 BD 48 00 D4 01 40 00 D5 CF 48 00 D5 01 40 00 D6 08 48 00 D6 95 40 00 D7 F8 48 00 D7 94 40 00 D8 FF 48 00 D8 CF --4C00D800=2800D800--
------------------------------------------------------------------------------------------------------------------------------------------------------
 20 00 00 00 28 00 00 00 20 00 01 00 28 00 01 00 20 00 02 00 28 00 02 00 20 00 03 00 28 00 03 00 20 00 04 00 28 00 04 00 20 00 05 00
 28 00 05 00 20 00 06 00 28 00 06 00 20 00 07 00 28 00 07 00 20 00 08 00 28 00 08 00 20 00 09 00 28 00 09 00 20 00 0A 00 28 00 0A 00 20 00 0B 00 28 00
 0B 00 20 00 0C 00 28 00 0C 00 20 00 0D 00 28 00 0D 00 20 00 0E 00 28 00 0E 00 20 00 0F 00 28 00 0F 00 20 00 10 00 28 00 10 00 20 00 11 00 28 00 11 00
 20 00 12 00 28 00 12 00 20 00 13 00 28 00 13 00 20 00 14 00 28 00 14 00 20 00 15 00 28 00 15 00 20 00 16 00 28 00 16 00 20 00 17 00 28 00 17 00 20 00
 18 00 28 00 18 00 20 00 19 00 28 00 19 00 20 00 1A 00 28 00 1A 00 20 00 1B 00 28 00 1B 00 20 00 1C 00 28 00 1C 00 20 00 1D 00 28 00 1D 00 20 00 1E 00
 28 00 1E 00 20 00 1F 00 28 00 1F 00 20 00 20 00 28 00 20 00 20 00 21 00 28 00 21 00 20 00 22 00 28 00 22 00 20 00 23 00 28 00 23 00 20 00 24 00 28 00
 24 00 20 00 25 00 28 00 25 00 20 00 26 00 28 00 26 00 20 00 27 00 28 00 27 00 20 00 28 00 28 00 28 00 20 00 29 00 28 00 29 00 20 00 2A 00 28 00 2A 00
 20 00 2B 00 28 00 2B 00 20 00 2C 00 28 00 2C 00 20 00 2D 00 28 00 2D 00 20 00 2E 00 28 00 2E 00 20 00 2F 00 28 00 2F 00 20 00 30 00 28 00 30 00 20 00
 31 00 28 00 31 00 20 00 32 00 28 00 32 00 20 00 33 00 28 00 33 00 20 00 34 00 28 00 34 00 20 00 35 00 28 00 35 00 20 00 36 00 28 00 36 00 20 00 37 00
 28 00 37 00 20 00 38 00 28 00 38 00 20 00 39 00 28 00 39 00 20 00 3A 00 28 00 3A 00 20 00 3B 00 28 00 3B 00 20 00 3C 00 28 00 3C 00 20 00 3D 00 28 00
 3D 00 20 00 3E 00 28 00 3E 00 20 00 3F 00 28 00 3F 00 20 00 40 00 28 00 40 00 20 00 41 00 28 00 41 00 20 00 42 00 28 00 42 00 20 00 43 00 28 00 43 00
 20 00 44 00 28 00 44 00 20 00 45 00 28 00 45 00 20 00 46 00 28 00 46 00 20 00 47 00 28 00 47 00 20 00 48 00 28 00 48 00 20 00 49 00 28 00 49 00 20 00
 4A 00 28 00 4A 00 20 00 4B 00 28 00 4B 00 20 00 4C 00 28 00 4C 00 20 00 4D 00 28 00 4D 00 20 00 4E 00 28 00 4E 00 20 00 4F 00 28 00 4F 00 20 00 50 00
 28 00 50 00 20 00 51 00 28 00 51 00 20 00 52 00 28 00 52 00 20 00 53 00 28 00 53 00 20 00 54 00 28 00 54 00 20 00 55 00 28 00 55 00 20 00 56 00 28 00
 56 00 20 00 57 00 28 00 57 00 20 00 58 00 28 00 58 00 20 00 59 00 28 00 59 00 20 00 5A 00 28 00 5A 00 20 00 5B 00 28 00 5B 00 20 00 5C 00 28 00 5C 00
 20 00 5D 00 28 00 5D 00 20 00 5E 00 28 00 5E 00 20 00 5F 00 28 00 5F 00 20 00 60 00 28 00 60 00 20 00 61 00 28 00 61 00 20 00 62 00 28 00 62 00 20 00
 63 00 28 00 63 00 20 00 64 00 28 00 64 00 20 00 65 00 28 00 65 00 20 00 66 00 28 00 66 00 20 00 67 00 28 00 67 00 20 00 68 00 28 00 68 00 20 00 69 00
 28 00 69 00 20 00 6A 00 28 00 6A 00 20 00 6B 00 28 00 6B 00 20 00 6C 00 28 00 6C 00 20 00 6D 00 28 00 6D 00 20 00 6E 00 28 00 6E 00 20 00 6F 00 28 00
 6F 00 20 00 70 00 28 00 70 00 20 00 71 00 28 00 71 00 20 00 72 00 28 00 72 00 20 00 73 00 28 00 73 00 20 00 74 00 28 00 74 00 20 00 75 00 28 00 75 00
 20 00 76 00 28 00 76 00 20 00 77 00 28 00 77 00 20 00 78 00 28 00 78 00 20 00 79 00 28 00 79 00 20 00 7A 00 28 00 7A 00 20 00 7B 00 28 00 7B 00 20 00
 7C 00 28 00 7C 00 20 00 7D 00 28 00 7D 00 20 00 7E 00 28 00 7E 00 20 00 7F 00 28 00 7F 00 20 00 80 00 28 00 80 00 20 00 81 00 28 00 81 00 20 00 82 00
 28 00 82 00 20 00 83 00 28 00 83 00 20 00 84 00 28 00 84 00 20 00 85 00 28 00 85 00 20 00 86 00 28 00 86 00 20 00 87 00 28 00 87 00 20 00 88 00 28 00
 88 00 20 00 89 00 28 00 89 00 20 00 8A 00 28 00 8A 00 20 00 8B 00 28 00 8B 00 20 00 8C 00 28 00 8C 00 20 00 8D 00 28 00 8D 00 20 00 8E 00 28 00 8E 00
 20 00 8F 00 28 00 8F 00 20 00 90 00 28 00 90 00 20 00 91 00 28 00 91 00 20 00 92 00 28 00 92 00 20 00 93 00 28 00 93 00 20 00 94 00 28 00 94 00 20 00
 95 00 28 00 95 00 20 00 96 00 28 00 96 00 20 00 97 00 28 00 97 00 20 00 98 00 28 00 98 00 20 00 99 00 28 00 99 00 20 00 9A 00 28 00 9A 00 20 00 9B 00
 28 00 9B 00 20 00 9C 00 28 00 9C 00 20 00 9D 00 28 00 9D 00 20 00 9E 00 28 00 9E 00 20 00 9F 00 28 00 9F 00 20 00 A0 00 28 00 A0 00 20 00 A1 00 28 00
 A1 00 20 00 A2 00 28 00 A2 00 20 00 A3 00 28 00 A3 00 20 00 A4 00 28 00 A4 00 20 00 A5 00 28 00 A5 00 20 00 A6 00 28 00 A6 00 20 00 A7 00 28 00 A7 00
 20 00 A8 00 28 00 A8 00 20 00 A9 00 28 00 A9 00 20 00 AA 00 28 00 AA 00 20 00 AB 00 28 00 AB 00 20 00 AC 00 28 00 AC 00 20 00 AD 00 28 00 AD 00 20 00
 AE 00 28 00 AE 00 20 00 AF 00 28 00 AF 00 20 00 B0 00 28 00 B0 00 20 00 B1 00 28 00 B1 00 20 00 B2 00 28 00 B2 00 20 00 B3 00 28 00 B3 00 20 00 B4 00
 28 00 B4 00 20 00 B5 00 28 00 B5 00 20 00 B6 00 28 00 B6 00 20 00 B7 00 28 00 B7 00 20 00 B8 00 28 00 B8 00 20 00 B9 00 28 00 B9 00 20 00 BA 00 28 00
 BA 00 20 00 BB 00 28 00 BB 00 20 00 BC 00 28 00 BC 00 20 00 BD 00 28 00 BD 00 20 00 BE 00 28 00 BE 00 20 00 BF 00 28 00 BF 00 20 00 C0 00 28 00 C0 00 20 00 C1 00 28 00 C1 00 20 00 C2 00 28 00 C2 00 20 00 C3 00 28 00 C3 00 20 00 C4 00 28 00 C4 00 20 00 C5 00 28 00 C5 00 20 00 C6 00 28 00 C6 00 20 00 C7 00 28 00 C7 00 20 00 C8 00 28 00 C8 00 20 00 C9 00 28 00 C9 00 20 00 CA 00 28 00 CA 00 20 00 CB 00 28 00 CB 00 20 00 CC 00 28 00 CC 00 20 00 CD 00 28 00 CD 00 20 00 CE 00 28 00 CE 00 20 00 CF 00 28 00 CF 00 20 00 D0 00 28 00 D0 00 20 00 D1 00 28 00 D1 00 20 00 D2 00 28 00 D2 00 20 00 D3 00 28 00 D3 00 20 00 D4 00 28 00 D4 00 20 00 D5 00 28 00 D5 00 20 00 D6 00 28 00 D6 00 20 00 D7 00 28 00 D7 00 20 00 D8 00 28 00 D8 00
------------------------------------------------------------------------------------------------------------------------------------------------------
 --50000000=50000000=50000000=58080000=58080000=58080000-AC530000-30000000=30000100=30000200-50000000=50000000=50000000--58080000=58080000=58080000   - A001FC00=A001FD00=A001FE00=A001FF00--
------------------------------------------------------------------------------------------------------------------------------------------------------
 20 00 00 00 28 00 00 00 20 00 01 00 28 00 01 00 20 00 02 00 28 00 02 00 20 00 03 00
 28 00 03 00 20 00 04 00 28 00 04 00 20 00 05 00 28 00 05 00 20 00 06 00 28 00 06 00 20 00 07 00 28 00 07 00 20 00 08 00 28 00 08 00 20 00 09 00 28 00
 09 00 20 00 0A 00 28 00 0A 00 20 00 0B 00 28 00 0B 00 20 00 0C 00 28 00 0C 00 20 00 0D 00 28 00 0D 00 20 00 0E 00 28 00 0E 00 20 00 0F 00 28 00 0F 00
 20 00 10 00 28 00 10 00 20 00 11 00 28 00 11 00 20 00 12 00 28 00 12 00 20 00 13 00 28 00 13 00 20 00 14 00 28 00 14 00 20 00 15 00 28 00 15 00 20 00
 16 00 28 00 16 00 20 00 17 00 28 00 17 00 20 00 18 00 28 00 18 00 20 00 19 00 28 00 19 00 20 00 1A 00 28 00 1A 00 20 00 1B 00 28 00 1B 00 20 00 1C 00
 28 00 1C 00 20 00 1D 00 28 00 1D 00 20 00 1E 00 28 00 1E 00 20 00 1F 00 28 00 1F 00 20 00 20 00 28 00 20 00 20 00 21 00 28 00 21 00 20 00 22 00 28 00
 22 00 20 00 23 00 28 00 23 00 20 00 24 00 28 00 24 00 20 00 25 00 28 00 25 00 20 00 26 00 28 00 26 00 20 00 27 00 28 00 27 00 20 00 28 00 28 00 28 00
 20 00 29 00 28 00 29 00 20 00 2A 00 28 00 2A 00 20 00 2B 00 28 00 2B 00 20 00 2C 00 28 00 2C 00 20 00 2D 00 28 00 2D 00 20 00 2E 00 28 00 2E 00 20 00
 2F 00 28 00 2F 00 20 00 30 00 28 00 30 00 20 00 31 00 28 00 31 00 20 00 32 00 28 00 32 00 20 00 33 00 28 00 33 00 20 00 34 00 28 00 34 00 20 00 35 00
 28 00 35 00 20 00 36 00 28 00 36 00 20 00 37 00 28 00 37 00 20 00 38 00 28 00 38 00 20 00 39 00 28 00 39 00 20 00 3A 00 28 00 3A 00 20 00 3B 00 28 00
 3B 00 20 00 3C 00 28 00 3C 00 20 00 3D 00 28 00 3D 00 20 00 3E 00 28 00 3E 00 20 00 3F 00 28 00 3F 00 20 00 40 00 28 00 40 00 20 00 41 00 28 00 41 00
 20 00 42 00 28 00 42 00 20 00 43 00 28 00 43 00 20 00 44 00 28 00 44 00 20 00 45 00 28 00 45 00 20 00 46 00 28 00 46 00 20 00 47 00 28 00 47 00 20 00
 48 00 28 00 48 00 20 00 49 00 28 00 49 00 20 00 4A 00 28 00 4A 00 20 00 4B 00 28 00 4B 00 20 00 4C 00 28 00 4C 00 20 00 4D 00 28 00 4D 00 20 00 4E 00
 28 00 4E 00 20 00 4F 00 28 00 4F 00 20 00 50 00 28 00 50 00 20 00 51 00 28 00 51 00 20 00 52 00 28 00 52 00 20 00 53 00 28 00 53 00 20 00 54 00 28 00
 54 00 20 00 55 00 28 00 55 00 20 00 56 00 28 00 56 00 20 00 57 00 28 00 57 00 20 00 58 00 28 00 58 00 20 00 59 00 28 00 59 00 20 00 5A 00 28 00 5A 00
 20 00 5B 00 28 00 5B 00 20 00 5C 00 28 00 5C 00 20 00 5D 00 28 00 5D 00 20 00 5E 00 28 00 5E 00 20 00 5F 00 28 00 5F 00 20 00 60 00 28 00 60 00 20 00
 61 00 28 00 61 00 20 00 62 00 28 00 62 00 20 00 63 00 28 00 63 00 20 00 64 00 28 00 64 00 20 00 65 00 28 00 65 00 20 00 66 00 28 00 66 00 20 00 67 00
 28 00 67 00 20 00 68 00 28 00 68 00 20 00 69 00 28 00 69 00 20 00 6A 00 28 00 6A 00 20 00 6B 00 28 00 6B 00 20 00 6C 00 28 00 6C 00 20 00 6D 00 28 00
 6D 00 20 00 6E 00 28 00 6E 00 20 00 6F 00 28 00 6F 00 20 00 70 00 28 00 70 00 20 00 71 00 28 00 71 00 20 00 72 00 28 00 72 00 20 00 73 00 28 00 73 00
 20 00 74 00 28 00 74 00 20 00 75 00 28 00 75 00 20 00 76 00 28 00 76 00 20 00 77 00 28 00 77 00 20 00 78 00 28 00 78 00 20 00 79 00 28 00 79 00 20 00
 7A 00 28 00 7A 00 20 00 7B 00 28 00 7B 00 20 00 7C 00 28 00 7C 00 20 00 7D 00 28 00 7D 00 20 00 7E 00 28 00 7E 00 20 00 7F 00 28 00 7F 00 20 00 80 00
 28 00 80 00 20 00 81 00 28 00 81 00 20 00 82 00 28 00 82 00 20 00 83 00 28 00 83 00 20 00 84 00 28 00 84 00 20 00 85 00 28 00 85 00 20 00 86 00 28 00
 86 00 20 00 87 00 28 00 87 00 20 00 88 00 28 00 88 00 20 00 89 00 28 00 89 00 20 00 8A 00 28 00 8A 00 20 00 8B 00 28 00 8B 00 20 00 8C 00 28 00 8C 00
 20 00 8D 00 28 00 8D 00 20 00 8E 00 28 00 8E 00 20 00 8F 00 28 00 8F 00 20 00 90 00 28 00 90 00 20 00 91 00 28 00 91 00 20 00 92 00 28 00 92 00 20 00
 93 00 28 00 93 00 20 00 94 00 28 00 94 00 20 00 95 00 28 00 95 00 20 00 96 00 28 00 96 00 20 00 97 00 28 00 97 00 20 00 98 00 28 00 98 00 20 00 99 00
 28 00 99 00 20 00 9A 00 28 00 9A 00 20 00 9B 00 28 00 9B 00 20 00 9C 00 28 00 9C 00 20 00 9D 00 28 00 9D 00 20 00 9E 00 28 00 9E 00 20 00 9F 00 28 00
 9F 00 20 00 A0 00 28 00 A0 00 20 00 A1 00 28 00 A1 00 20 00 A2 00 28 00 A2 00 20 00 A3 00 28 00 A3 00 20 00 A4 00 28 00 A4 00 20 00 A5 00 28 00 A5 00
 20 00 A6 00 28 00 A6 00 20 00 A7 00 28 00 A7 00 20 00 A8 00 28 00 A8 00 20 00 A9 00 28 00 A9 00 20 00 AA 00 28 00 AA 00 20 00 AB 00 28 00 AB 00 20 00
 AC 00 28 00 AC 00 20 00 AD 00 28 00 AD 00 20 00 AE 00 28 00 AE 00 20 00 AF 00 28 00 AF 00 20 00 B0 00 28 00 B0 00 20 00 B1 00 28 00 B1 00 20 00 B2 00
 28 00 B2 00 20 00 B3 00 28 00 B3 00 20 00 B4 00 28 00 B4 00 20 00 B5 00 28 00 B5 00 20 00 B6 00 28 00 B6 00 20 00 B7 00 28 00 B7 00 20 00 B8 00 28 00
 B8 00 20 00 B9 00 28 00 B9 00 20 00 BA 00 28 00 BA 00 20 00 BB 00 28 00 BB 00 20 00 BC 00 28 00 BC 00 20 00 BD 00 28 00 BD 00 20 00 BE 00 28 00 BE 00
 20 00 BF 00 28 00 BF 00 20 00 C0 00 28 00 C0 00 20 00 C1 00 28 00 C1 00 20 00 C2 00 28 00 C2 00 20 00 C3 00 28 00 C3 00 20 00 C4 00 28 00 C4 00 20 00
 C5 00 28 00 C5 00 20 00 C6 00 28 00 C6 00 20 00 C7 00 28 00 C7 00 20 00 C8 00 28 00 C8 00 20 00 C9 00 28 00 C9 00 20 00 CA 00 28 00 CA 00 20 00 CB 00
 28 00 CB 00 20 00 CC 00 28 00 CC 00 20 00 CD 00 28 00 CD 00 20 00 CE 00 28 00 CE 00 20 00 CF 00 28 00 CF 00 20 00 D0 00 28 00 D0 00 20 00 D1 00 28 00
 D1 00 20 00 D2 00 28 00 D2 00 20 00 D3 00 28 00 D3 00 20 00 D4 00 28 00 D4 00 20 00 D5 00 28 00 D5 00 20 00 D6 00 28 00 D6 00 20 00 D7 00 28 00 D7 00
 20 00 D8 00 28 00 D8 00 20 00 D9 00 28 00 D9 00 20 00 DA 00 28 00 DA 00 20 00 DB 00 28 00 DB 00 20 00 DC 00 28 00 DC 00 20 00 DD 00 28 00 DD 00 20 00
 DE 00 28 00 DE 00 20 00 DF 00 28 00 DF 00 20 00 E0 00 28 00 E0 00 20 00 E1 00 28 00 E1 00 20 00 E2 00 28 00 E2 00 20 00 E3 00 28 00 E3 00 20 00 E4 00
 28 00 E4 00 20 00 E5 00 28 00 E5 00 20 00 E6 00 28 00 E6 00 20 00 E7 00 28 00 E7 00 20 00 E8 00 28 00 E8 00 20 00 E9 00 28 00 E9 00 20 00 EA 00 28 00
 EA 00 20 00 EB 00 28 00 EB 00 20 00 EC 00 28 00 EC 00 20 00 ED 00 28 00 ED 00 20 00 EE 00 28 00 EE 00 20 00 EF 00 28 00 EF 00 20 00 F0 00 28 00 F0 00
 20 00 F1 00 28 00 F1 00 20 00 F2 00 28 00 F2 00 20 00 F3 00 28 00 F3 00 20 00 F4 00 28 00 F4 00 20 00 F5 00 28 00 F5 00 20 00 F6 00 28 00 F6 00 20 00
 F7 00 28 00 F7 00 20 00 F8 00 28 00 F8 00 20 00 F9 00 28 00 F9 00 20 00 FA 00 28 00 FA 00 20 00 FB 00 28 00 FB 00 20 00 FC 00 28 00 FC 00 20 00 FD 00
 28 00 FD 00 20 00 FE 00 28 00 FE 00 20 00 FF 00 28 00 FF 00 --58000000--

Steps how a programmer writes the hex file into microcontroller

Do You know how microcontroller writes .hex file into microcontroller most of us don't know how it writes right . There is a pattern how it writes into microcontroller . It follows a sequence while writting .hex into microcontroller there are some SPM (store program memory) commands which should be send and then send address and then data .

Some microcontrollers write data in the form of word and some write in the form of bytes. you should see this while you are developing something new like bootloader or something els.

If mc is word addressable first it loads the lower byte with data and then higher byte with next data of same address as it is word addressable. (remember).

Generally avr-gcc writes the code in byte form so what ever the address we need to change in the microcontroller you need to double the address and give it to gcc .

Suppose you want to write the code from 0xC00 in the flash which is word addressable how you should give this in avr-gcc is
                                                    0xC00 * 2 = 0x1800
in the chang section if you need to change the address location where to write.else if microcontroller is byte addressable you can write it directly 0xC00. There is no need to manipulate the addresses .

Ok leave this is brief i will explain you in the later sessions.

Now lets know what a Programmer does in the Microcontroller if it wants to write .hex file into the flash .

Steps it follows :

1. program enable mode (0)

2. check signature bits

3. read low fuse bits

4. read high fuse bits

5. read last 4bytes of the eeprom because it stores number of times the chip erase has been done .

6. program enable mode again (1)
   (address is word address here so )

7. load the data into the flash first low add (40) 1st data and then to high of the same address (48) 2nd data into the flash.

8. load until whole page is loaded and then send the command Write PAGE the flash page loading last addersss

9. now read the page with last address

10. now reads each word in flash from starting address

11. and again reads the fuse bits and lock bits

This was the mechanism followed by the USBASP programmer of any other programmer to write program into the flash .

In order to understand clearly what this means see this Post
which tells you how it is writting.

How a .hex file is written in the microcontroller

AC530000  --> program enable
AC800000  --> chip erase
40000ioo  --> load program memory page low  
010000000 0000xxxx xxx 0 0000 iiiiiiii
                                              |          |__________data in
                                              |__________________load at address

Write data i to Program memory page at word address b. Data Low byte must be loaded before Data High byte is applied within the same address
                                      
48000ioo  --> load program memory page high

4c00 3f 00--> write program memory page       
01001100 0000 0000 001 xxxxx xxxxxxxx
                                 |_______ write at this address
                            
280 03f 00--> read program meory at word address
00101000 0000 aaaa bbbbbbbb  oooooooo
                                     |                        |______ data out
                                     |___________________ at word address

300000000-->read signature bits
300000100                                
300000200                        
00110000 00xxxxxx xxxxxx aa oooooooo                                 
                                              |          |____ out signature
                                              |________address of the signature

Programming Enable                                     $AC $53 $00 $00
Chip Erase (Program Memory/EEPROM)     $AC $80 $00 $00

 Poll RDY/BSY                                                $F0 $00 $00 data byte out
Load Extended Address byte(1)                     $4D $00 Extended adr $00

Load Program Memory Page, High byte       $48 adr MSB adr LSB high data byte in
Load Program Memory Page, Low byte        $40 adr MSB adr LSB low data byte in
Load EEPROM Memory Page (page access)(1)$C1 $00 adr LSB data byte in

Read Program Memory, High byte                   $28 adr MSB adr LSB high data byte out
Read Program Memory, Low byte                    $20 adr MSB adr LSB low data byte out
Read EEPROM Memory     $A0 adr MSB adr LSB data byte out
Read Lock bits                    $58 $00 $00 data byte out
Read Signature Byte           $30 $00 0000 000aa data byte out
Read Fuse bits                     $50 $00 $00 data byte out
Read Fuse High bits            $58 $08 $00 data byte out
Read Extended Fuse Bits    $50 $08 $00 data byte out
Read Calibration Byte         $38 $00 $0b00 000bb data byte out

Write Program Memory Page   $4C 000a aaaa aa00 0000 $00
Write EEPROM Memory           $C0 adr MSB adr LSB data byte in
Write EEPROM Memory Page (page access)  $C2 adr MSB adr LSB $00
Write Lock bits                          $AC $E0 $00 data byte in
Write Fuse bits                          $AC $A0 $00 data byte in
Write Fuse High bits                 $AC $A8 $00 data byte in
Write Extended Fuse Bits         $AC $A4 $00 data byte in

you can check the sample writting of the programmer into the microcontroller:

click here 

You won't get this until you see the above link . if you don't get please put a comment ok. i will be more specific. 

2 Jul 2012

PINS

PIN TERMINOLOGY:

             Same way as Ports . Remember the number of Ports you have is equal to number of pins because there are fixed number of pins for a microcontroller right.

Do you remember i told you you should access whole port even you want to access only one pin. In the same way you should access whole PINX for even you need only one pin to access. X = A, B, C ,D.

Lets take an example so that it makes you clear all the things.

You use PINB when you are checking if something is coming to microcontroller . eg: consider switch when you press a switch it sends a signal of 5v right so you need a application if you press a switch you should do some task lets assume toggle led. so you should be checking that pin right whether the pin is getting signal or not so at that time you use PINB (for checking incoming signal to microcontroller).

ACCESS 2:
PROGRAM:
             #include <avr/io.h>
              int main()
              {
                    DDRB = 0x00;     //configuring as inputs
                    DDRC = 0xff;      //configuring as outputs
                    while(1)
                     {
                           if(PINB & (1 << PB0)) //switch is connected to pinb 0
                           {
                                 PORTC = (1 << PC0);
                            }
                            PORTC = 0x00;
                      }
                }
as we connected switch to the Pinb0 so we are checking that pin continuoesly. 

And you may be thinking which i made PORTC = 0x00; in the program because in order to see whether it is blinking or not you should make it 0. try commenting that .you will get the output once.
ACCESS 3:
                    Replace (1<< PB0) with _BV(PB0).
Now i think you are quite familier with the access of ports.

some of the input devices to the microcontroller :
1. switch
2. Matrix keypad
3. Potency variation
4. ADC
5. Usart receiver etc..........
            _____________________________________________________
PINB  | PB7  | PB6  |  PB5  |  PB4  |  PB3|  PB2  | PB1  | PB0  |
           --------------------------------------------------------------------------------------
 This looks like this in the microcontroller. so , you got it right if you want to check that microcontroller is getting some input we use PINx .

 Though it may be looking as a simple concept for some of you but many don't understand at the beginning so this is for just them who are beginners.

ok, then lets meet in next session........................................

PORTS

PORTS : ACCESS OF PORTS
              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 !!!!!!!!!!!!!!



               

Ports & Pins

PORTS & PINS:
         1. PORTS
         2. PINS

When ever you here these terms you should get into mind is these are  access to the external devices which act as interface between them and microcontroller.

                                                   [ Photo]

In Atmel they are termed as Ports and pins and should be accessed as whole port . i.e whole port is used for accessing even one pin. At the beginning it will be some what odd but while you will be programming na you will feel better. But i promise you if you learn this you can do any of the family like atmel, Pic , anything . Like if you learn this you will play with other microcontrollers .

Accessing Port in the Pic is very easy like if you are accessing PORTB assume ok. Now in order to access one pin just like PB0 = 1; like this is enough but when you come to Atmel if you want to access one pin i.e 0th pin of PORTB then you should do something like PORTB |= (1 << PB0). ya dont worry i will explain in detail ok.

In order to do port access first you should know what is data direction registers.
It means nothing but telling the microcontroller that port or pin is used for output or input. It is mandatory to tell the microcontroller what function the perticular pin of the port should do whether it should act as output or input.

In Atmel controllers we tell the microcontroller
                                     DDRB = 0xFF;   -->  1 indicates the PORT is designed to function as output if the same
                                     DDRB = 0x00;   -->  0 indicates that the PORT is designed to function as input of microcontroller.

Finally Atmega8 has 4 PORTS :
                     1. PORTA
                     2. PORTB
                     3. PORTC
                     4. PORTD
In the comming section i will explain you how to access the ports and pins in different styles.............................:-)

In order to get fast access in microcontroller you should be well prepared with bitwise operators. we use mostly these things in the microcontroller.