Monday, 23 July 2018

Interface a Switch 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 input from a PIC microcontroller. This learning would also help in interfacing of external devices with the controller. Here theinput from the microcontroller is taken on a set of switch.
10pcs-each-x-12-font-b-models-b-font-Push-button-font-b-switch-b-font

Description:


Fig. 1 shows how to interface the switch to microcontroller. A simple switch has an open state and closed state. However, a microcontroller needs to see a definite high or low voltage level at a digital input. A switch requires a pull-up or pull-down resistor to produce a definite high or low voltage when it is open or closed. A resistor placed between a digital input and the supply voltage is called a “pull-up” resistor because it normally pulls the pin’s voltage up to the supply.
Interfacing switch to Microcontroller
Fig. 1 Interfacing switch to Microcontroller
We now want to control the LED by using switches in PIC Development board. It works by turning ON a LED & then turning it OFF when switch is going to LOW or HIGH

Block Diagram

3 switch

Schematic


Code

// ****************************************************
// Project: Interfacing single switch to PIC18F4520
// Author: Hack Projects India
// Module description: Operate array of LED's
// ****************************************************
#include<p18f452.h>
#pragma config OSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
#define SWITCH PORTCbits.RC0
#define LED PORTBbits.RB0
int main(void)
{
    ADCON1=0xF0;
   TRISB=0x00;
   TRISC=0X0F;
   while(1)
   {
       if(SWITCH)
       {
           LED=1;
       }
       else
       {
           LED=0;
       }
   }
}


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