Monday, September 2, 2013

Acquiring Button Inputs in PIC18 using C with Proteus Simulation

ButtonAnimation In this Tutorial we will demonstrate how  to acquire User Push Button Input  connected to one of the PIC18 digital  IO. As the user pushes the Button an  LED connected to a different IO will  glow as long as the button is kept      pressed.

 

 

 


A Little About Port Registers

There are three Registers associated with each Port of PIC18, LATx, PORTx, and TRISx. where ‘x’ represents A,B,C depending upon the number of Ports in a specific device. The TRISx register is used for setting a Port pin either input or output by writing a binary 1 or 0 respectively. The PORTx register is used to read the state of a Port pin,  and writing to it will write to the Port latch. The LATx register is the output latch register and it keeps the latched output value for PORTx.
For reading an input port pin we use PORTx register as it gives the current status of the port pin. For writing or reading the state of an output pin either LATx or PORTx is used. Both LATx and PORTx are read/write registers.

C Code for the Project

Below as an example code of the project in C which needs to be compiled using Microchip C18 Compiler and the MPLAB IDE environment. The code is almost the same as the previous tutorial with a little difference.  In the previous tutorial we used two pins as digital outputs, here one pin is used as digital input-to which the user button is connected, and the other is used as digital output and an LED is connected to it.
The input pin is polled in the main loop for button state, if the button is pressed the LED connected to another pin turns on as long as the the button is kept pressed. This is demonstrated with the Proteus simulation.
Copy the code below and compile it to generate .hex file.
/* 
   ################################################################################ 
   **** PIC18 Button Input Example
   **** IDE      : MPLAB Ver 8.91
   **** Compiler : Microchip C18 Ver 3.43
   ################################################################################
*/
 
// Includes
#include <p18cxxx.h>
#include <delays.h>
 
// Configurations
#pragma config WDT = OFF            // Watchdog Timer Disabled
#pragma config OSC = HS                // OSC Mode - High Speed Crystal/Resonator
#pragma config XINST = OFF            // Extended Instructions Disabled

// LED
#define LED1    LATDbits.LATD0
// Button
#define BUTTON1 PORTCbits.RC0

// main
void main(void)
{   
    // As the LED1 pin is also an analog input pin we have to declare it as
    // digital by writing the port configuration control bits(PCFG<3:0>) in 
    // ADCON1 Register - Refer to the datasheet for more details
       ADCON1 = 0x00001111;    
 
    // Set PORTD bit 0 as digital output, LED1 is connected on this pin
    TRISDbits.TRISD0 = 0;

    TRISCbits.TRISC0 = 1;     // Set the Button Pin as input

    // infinite loop    
    while(1)
    {
        if(BUTTON1 == 0)    // Poll for Button Press    
            LED1 = 1;    // Turn ON LED
        else
            LED1 = 0;    // Turn OFF LED
    }
}

Proteus Simulation

The Proteus circuit is also not so much different from the previous tutorial. the only change is one LED is replaced with a button.

circuit

After adding the hex file to the Proteus project click the play button to run the simulation, while the simulation is running push the button and the LED will glow release it and it will turn off.

This is a simple tutorial demonstrating the use of a push button with PIC18 for beginners hope you learn something from it.
Please comment for feedback.

Share This!



3 comments:

Popular Posts

Followers

Like Us

Follow me on Twitter