Monday 23 July 2018

Interfacing Array of LED’s with PIC Microcontroller (PIC18F4520)

Aim:

It is necessary to understand basic I/O operations of PIC18F4520 before dealing with its complexities. This article presents a way to take simple output from a PIC microcontroller. This learning would also help in interfacing of external devices with the controller. Here the output from the microcontroller is taken on a set of LEDs which are made to blink in an alternate fashion.
images (1)

Description:

PIC18F4520 has a total of 35 I/O (input-output) pins which are distributed among 5 Ports. The following table shows the names and numbers of I/O pins of these 5 ports:
Port NameNumber of PinsPins
PORTA7RA0-RA6
PORTB8RB0-RB7
PORTC8RC0–RC7
PORTD8RD0-RD7
PORTE4RE0-RE3
As opposed to a basic 8051 microcontroller like AT89C51 which has most of the port pins serving single function, the port pins of a PIC microcontroller are multiplexed to serve more than one purpose.
The 35 I/O pins of PIC18F4520 are also multiplexed with one or more alternative functions of controller’s various peripherals. Each Port of a PIC microcontroller corresponds to three 8-bit registers which should be configured to use the Port for general I/O purpose. These registers are:
1.            TRISx: This is a data direction register which sets the direction of each port pin as input or output.
2.            PORTx: This register stores the input level of pins (High or Low). When a pin configured as input, the input signal from external source is read from PORTx register.
3.            LATx: This is output latch register. The data which has to be sent to external hardware as output is stored in LATx register.
Port Description:
PORTA:
PortA has 7 pins which can be used as both input as well as output pin. The 7th bit is missing from all the three registers. The input and output given to this port are of 8-bit but the 8th bit is internally masked.
Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TRISATRISA6TRISA5TRISA4TRISA3TRISA2TRISA1TRISA0
PORTARA6RA5RA4RA3RA2RA1RA0
LATALATA6LATA5LATA4LATA3LATA2LATA1LATA0
PORTB:
PortB has 8 pins which can all be used for both input and output operation.
Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TRISBTRISB7TRISB6TRISB5TRISB4TRISB3TRISB2TRISB1TRISB0
PORTBRB7RB6RB5RB4RB3RB2RB1RB0
LATBLATB7LATB6LATB5LATB4LATB3LATB2LATB1LATB0
PORTC:
PortC has 7 I/O pins. In PortC, Bit 3 is missing in hardware and Pins 4 & 5 can only be used as input pins. There are no 4th & 5th latch bits in LATC register, so these bits are internally masked during 8-bit write operation on PortC.
Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TRISCTRISC7TRISC6TRISC2TRISC1TRISC0
PORTCRC7RC6RC5RC4RC2RC1RC0
LATCLATC7LATC6LATC2LATC1LATC0
PORTD:
PortD has 8 pins which can all be used for both input and output operation.
Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TRISDTRISD7TRISD6TRISD5TRISD4TRISD3TRISD2TRISD1TRISD0
PORTDRD7RD6RD5RD4RD3RD2RD1RD0
LATDLATD7LATD6LATD5LATD4LATD3LATD2LATD1LATD0
PORTE:
PortE has 4 I/O pins. Pin3 can be used as input pin only. RDPU bit is used to enable/disable internal pull-ups of PortD.
Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
TRISETRISE2TRISE1TRISE0
PORTERPDURE3RE2RE1RE0
LATELATE2LATE1LATE0
I/O configuration:
The TRISx register is configured to set a pin as input or output. The High value (1) sets a pin as input pin and Low value (0) sets a pin as output. An easy way to remember this is to consider the resemblance of 1 with the letter I (for input) and 0 with the letter (for output).
For example suppose a switch is connected at RB0 and an LED is connected to RB7 of PortB. Now the pins 0 & 7 have to be configured as input and output respectively. So the bit TRISB0 is set to 1 to configure RB0 as input pin & bit TRISB7 is set to 0 to configure RB7 as output pin.
TRISBTRISB7TRISB6TRISB5TRISB4TRISB3TRISB2TRISB1TRISB0
Value01
The unused bits are set to 0.
TRISBTRISB7TRISB6TRISB5TRISB4TRISB3TRISB2TRISB1TRISB0
Value00000001
So the overall value of TRISB register becomes:
Using this knowledge, the objective of this article can be achieved which is to glow a set of LEDs in alternate blinking fashion. The LEDs here are connected to PORTB pins as shown in the circuit diagram.
Programming steps:
1.      Configure the TRISB register to make PortB as output port.
2.      Set all the bits of LATB register High (1) to glow all LEDs.
3.      Provide some delay.
4.      Set all the bits of LATB register Low (0) to turn off the LEDs.
5.      Provide some delay.
6.      Repeat the process from step 2.
Pin Assignment with atmega8
Point LEDsPIC18F4520  directionPIC18F4520 LinesLED SelectionConnections
DIGITAL OUTPUTSLED.0 TRISx=0xfePORTx=0x01dddConnect Any one PORT from PORTA,PORTB, PORTC, PORTD
LED.1TRISx=0xfdPORTx=0x02
LED.2TRISx=0xfbPORTx=0x04
LED.3TRISx=0xf7PORTx=0x08
LED.4TRISx=0xefPORTx=0x10
LED.5TRISx=0xdfPORTx=0x20
LED.6TRISx=0xbfPORTx=0x40
LED.7TRISx=0x7fPORTx=0x80

Block Diagram

2.interfacing array of leds

Schematic:

 2. array of leds

Code:

// **********************************************
// Project: Interfacing Single LED to PIC18F4520
// Author: Hack Projects
// Module description: Operate single LED
// **********************************************
#include<p18f4520.h>
#pragma config OSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
void delay(unsigned short long x);
void main(void)
{
ADCON1=0x0F;
TRISB=0;
for(;;)
{
PORTB=0xff;
delay(10);                 //Delay of 1sec
PORTB=0x00;
delay(10);
}
}
void delay(unsigned short long x)
{
while(x>0)
{
x--;
}
return;
}

Downloads:

The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.

Further Reading suggestions:

No comments:

Post a Comment