Sunday, April 12, 2009

Learn PIC Assembly Language Part - 5


Blinking LEDs

Now its the time to move to actual hardware. My primary task is to make 8 LEDS blink on PORTD. For blinking the LEDs, set up the hardware component as shown in the schematics.

Components List :

1. Microcontroller – PIC18F452

2. Crystal – 4 Mhz

3. Capacitor – 15pF ( 2 Nos)

4. Resistor – 1 K Ohms

5. Capacitor – 1 uF

6. Diode – IN4001

7. LEDs – 8 Nos

8. Power Supply – 5 Volts

Program6.asm

list p = 18F452

#include < P18F452.INC >

Count1 equ 0x00

Count2 equ 0x01

org 00000 h

goto Start

org 0002A h

Start :

MOVLW 0x00

MOVWF TRISD,0

Pos1 :

MOVLW 0x00

MOVWF PORTD,0

CALL Delay

CALL Delay

MOVLW 0xFF

MOVWF PORTD,0

CALL Delay

CALL Delay

goto Pos1

Delay :

MOVLW 0xFF

MOVWF Count1, 0

Pos2 :

DECFSZ Count1,1,0

goto Pos3

RETURN

Pos3 :

MOVLW 0xFF

MOVWF Count2,0

Pos4 :

DECFSZ Count2,1,0

goto Pos4

goto Pos2

end

For blinking the LEDs, you have to do the following steps.

1. Make the TRISD register as 0x00, so that PORTD is configured as digital output. For configuring PORTD as input, i.e for reading switches, then make TRISD as 0xFF. Here we are making TRISD as output as we need to glow the LEDs. So TRISD should be made 0x00. Every port is associated with a TRIS register, which controls the ports as digital output or digital input.

2. For turning ON the LEDs, you have to write 0xFF to PORTD

3. Call Delay. Delay routine allows the LEDs to glow for some period of time before turning it OFF.

4. After the delay ( Say 1 sec or 2 sec ), you have to make the LEDs OFF by writing 0x00 to PORTD. Now LEDS are OFF.

5. Then again you have to call Delay before making it ON again


The program logic is like this :

ON – > Delay -> OFF - > Delay -> ON -> Delay -> OFF ...... Infinite loop.

For a microcontroller to work, there should be one oscillator or clock crystal needed. We have connected here an external clock crystal of 4 MHz to the OSC1 and OSC2 pins of the PIC microcontroller. Some microcontrollers has the option of using internal oscillator. In that case, you dont have to use an external oscillator, thus saving the PCB board area.

As a part of the MPLAB set up, you have to set the configuration bits. These configuration bits specify the configuration of the PIC microcontroller. Like whether it is configured for external crystal oscillator or internal oscillator, watchdog timer, code protection, etc...

Go to Configure  Configuration Bits, Now a pop-up window will appear by the name ‘Configuration Bits’ . In that select, Oscillator = XT and Watchdog Timer = Disabled. Leave all other fields as such it is and close the window. Since you are using a 4 MHz crystal oscillator, so in the configuration bits, oscillator option should be selected as XT.

After Building the project, burn the program into a PIC18F452 microcontroller using a PIC programmer. As of now, my intention is to make you familiarize with assembly language programming of PIC, So I am not explaining here about PIC programming and bootloading. I will explain about those things in my forthcomming articles ( Inconvenience Regretted ).

After burning the program to the microcontroller, wire up the circuit as shown in the schematics in a breadboard. Switch on the power supply, you can see the LEDs blinking on PORTD........

No comments: