You have no items in your shopping cart.

Subtotal:Rs.0.00
8051 BoardsMicro controller Boards

Learn 8051, PIC, AVR On-the-Go Part 2 -LED Blinking

learn-8051-pic-avr-on-the-go-part-2-led-blinking

Pre-Requisites:

 

  1. How to create Projects in KEIL IDE, MPLAB IDE and CodeVisionAVR IDE
  2. How to burn Programs into Microcontroller using a Programmer or Boot loader

 

These pre-requisites are located at the following links,

8051 and Keil:

https://www.pantechsolutions.net/microcontroller-boards/user-manual-for-8051-development-kit

 

PIC and MPLAB-IDE:

https://www.pantechsolutions.net/microcontroller-boards/user-manual-for-pic16f-18f-development-kit

 

CodeVisionAVR and ATMEGA16:

https://www.pantechsolutions.net/microcontroller-boards/user-manual-for-atmega-development-kit

 

The IDE’s are used to write and modify the source codes along with various interactive features.

The Programmers are special hardware and software combinations which are used to burn the compiled Hex or object files into the microcontroller chip.

 

No. of Ports and their Names

Before going to write a program to blink a LED, We have to know the no. of ports available and their corresponding pins as well as how to configure it for GPIO. The details will be very helpful whenever we are supposed to write programs. So be keen to list out the details carefully, slowly and memorize it as much as possible. Here my sincere advice is do it just like that, there must be no tension or anxiety.

 

S.No

8051(4 Ports)

PIC16F877A(5 Ports)

ATEGA8535 (4 Ports)

1

PORT0(8)

PORTA(6)

PORTA(8)

PORT1(8)

PORTB(8)

PORTB(8)

3

PORT2(8)

PORTC(8)

PORTC(8)

4

PORT3(8)

PORTD(8)

PORTD(8)

5

PORTE(3)

 

The table describes the no of ports and their bits. The numbers specified within bracket are bits.

 

Port Configuration, Direction Registers

 

S.No

8051

PIC16F877A

ATMEGA16

 

8051 Has No direction Registers for I/O Ports

PORT

DIR-REG(Out, In)

PORT

DIR-REG(Out, In)

1

PORTA

TRISA(0,1)

PORTA

DDRA(1,0)

2

PORTB

TRISB(0,1)

PORTB

DDRB(1,0)

3

PORTC

TRISC(0,1)

PORTC

DDRC(1,0)

4

PORTD

TRISD(0,1)

PORTD

DDRD(1,0)

5

PORTE

TRISE(0,1)

 

  • 8051 has no direction register; we can use I/O Ports Directly as Input and Output.
  • Moving a value 0xFF to a Port will be considered as Input
  • Moving a Value 0x00 will be considered as Output

 

  • PIC16F877A Microcontroller has direction registers for each Port
  • Setting a bit of Direction register will make the corresponding PORT pin an input
  • Clearing a bit of Direction register will make the corresponding PORT pin an output

 

  • AVR ATMEGA16 is direct opposite to PIC microcontroller
  • Clearing a bit of Direction register will make the corresponding PORT pin an input
  • Setting a bit of Direction register will make the corresponding PORT pin an output

 

Examples:              P0=0x00                                8051:  P0 configured as output

TRISB=0x0f            PIC   :  PORTB lower bits configured as input, upper bits as output

DDRA=0xff            AVR: PORTA configured as output

 

Writing to the Port

 

Set the direction registers to Output and the Move the value to the Ports

 

8051 Ports:

Direction:              P0=P1=P2=P3 = 0x00

Ex. Values:             P0 = 0x42, P1=0x00, P2=0x10, P3=0xAA

 

PIC Ports:

Direction:              TRISA=TRISB=TRISC=TRISD=TRISE = 0x00

Ex. Values:             PORTA = 0x3f, PORTB = 0x34, PORTC = 78, PORTD = 55, PORTE = 0x07

 

AVR Ports:

Direction:              DDRA=DDRB=DDRC=DDRD = 0xff

Ex. Values:             PORTA = 0x00, PORTB = 0xff, PORTC = 0x11, PORTD = 0x25

 

Making a LED blink in 8051

 

LEDs are output devices, so we have to set all the port pins as output. Send a high to turn ON and Send a low to turn OFF a LED or vice versa.

 

 #include<reg51.h>                                                                              //Header File void Delay_ms(unsigned int);                                                           //Delay Function Prototype

 

void main ()

{

          P0=0x00; P1=0x00; P2 = 0x00; P3=0x00;                         //All Ports Output (Direction)

          while(1)

          {

                 P0=0xff; P1=0xff; P2 = 0xff; P3=0xff;                          //All high

                 Delay_ms(500);

                 P0=0x00; P1=0x00; P2 = 0x00; P3=0x00;                 //All low

                 Delay_ms(500);

         }

}

 

void Delay_ms(unsigned int val)                                                   //Delay Function

{

         unsigned char i;

         while(–val>0)

           for(i=0;i<110;i++);

}

 

 

Change the LED Code to PIC and AVR

PIC Micro Code

AVR Code

 #include<pic.h>                    void Delay_ms(unsigned int);                                

                                                                        

void main ()

{

 

ADCON1=0x07**;

TRISA=0x00; TRISB=0x00;                           

TRISC = 0x00; TRISD=0x00;

TRISE=0x00;     

 

while(1)

{

       PORTA=0x3f; PORTB=0xff;

       PORTC=0xff; PORTD=0xff;

       PORTE=0x07;

 

       Delay_ms(500);

 

       PORTA=0x00; PORTB=0x00;                  

       PORTC=0x00; PORTD=0x00;

       PORTE=0x00;

 

       Delay_ms(500);

}

}

 

 

void Delay_ms(unsigned int val)      

{

       unsigned char i;

       while(–val>0)

       for(i=0;i<110;i++);

}

 

 #include<mega8535.h>                  void Delay_ms(unsigned int);                      

 

void main ()

{

 

DDRA=0xff; DDRB=0xff;                           

DDRC = 0xFF;

DDRD=0xff;    

    

                                                                   

while(1)

{

       PORTA=0xff; PORTB=0xff;                  

       PORTC=0xff;

       PORTD=0xff;

 

       Delay_ms(500);

 

       PORTA=0x00; PORTB=0x00;               

       PORTC=0x00;

       PORTD=0x00;

 

       Delay_ms(500);

}

}

 

void Delay_ms(unsigned int val)      

{

       unsigned char i;

       while(–val>0)

       for(i=0;i<110;i++);

}

Note:     Did you see any changes between these microcontrollers ? yes but very small changes.

** PORTA and PORTE are multiplexed with analog inputs so we have to configure the pins as digital I/O by moving the value 0x07 to ADCON1 (Analog to digital control register 1)

Tags : 8051 tutorialAVR On-the-Go Part 2 -LED Blinkingavr tutorialLearn 8051PICpic tutorial
sukumar

The author sukumar