Microchip PIC16F6284A Input Pin Code, RA0 Pin as an Input, RB5 Pin Output to LED

Microchip PIC16F6284A Input Pin Code, RA0 Pin as an Input, RB5 Pin Output to LED

Microchip PIC16F628A Input Pin Code,RA0 Pin as an Input, RB5 Pin Output to LED:

 /* Created:   Mon Oct 12 2015
 * Processor: PIC16F628A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 */

#include <htc.h>
//Configuration Word

__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & MCLRE_ON & BOREN_ON & LVP_OFF & CPD_OFF & CP_OFF);

//Define Pins

#define LED    RB5

//Define CPU Frequency
//This must be defined, if __delay_ms() or __delay_us() functions are used in the code

#define _XTAL_FREQ 20000000

//Main Function Start from here

void main(void)
 {
   // Write your code here
    TRISA0 = 1;        //Make this pin an input
    TRISB5 = 0;         //Make LED pin an output
    LED = 0;            //Turn LED off        
    
    while (1)
   {
      if(RA0)        //If RA0 is high
     LED = 1;    //Make LED pin high
      else        //otherwise if RA0 is low
     LED = 0;    //Make LED zero
   }
}

-----------------------------------------------------------------------------------------

Explanation:

In this figure above, PIC16F628A is running on external crystal of 20 MHz. RA0 pin is being used as the input pin. When the push button is in the pushed state then, RA0 pin is high, otherwise RA0 pin is low. Whenever RA0 pin is high, then RB5 pin (Attached with the LED) is also made high just to indicate correct reading of RA0 pin status in the microcontroller.

In the main function, firstly RA0 pin is made an input and RB5 is made an output. Using TRISx register, we can set the direction of any pin i-e if it is an input or output. So, TRISA0 = 1; makes RA0 pin an input pin. And TRISB5 = 0; makes RB5 pin an output pin. Also, using the statement LED = 0; RB5 pin is made low.

In the while(1) loop, status of RA0 pin is constantly being checked, if it becomes high then RB5 is also made high. If RA0 is low, then RB5 is also made low as well.

 Because every PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, when CPU frequency is defined to be 20MHz, then actual speed of this PIC microcontroller will be 5 MIPS (Million of instructions per second). You can attach any crystal from 0 to 20MHz with PIC16F628A.

PIC16F628A, 18-pin Flash-Based, 8-Bit CMOS Microcontrollers with nanoWatt Technology:

High-Performance RISC CPU:
  •  Operating speeds from DC – 20 MHz
  • Interrupt capability
  • 8-level deep hardware stack
  • Direct, Indirect and Relative Addressing modes
  • 35 single-word instructions:
  •  All instructions single cycle except branches
Special Microcontroller Features:
  • Internal and external oscillator options:
  •  Precision internal 4 MHz oscillator factory calibrated to ±1%
  • Low-power internal 48 kHz oscillator
  • External Oscillator support for crystals and resonators
  • Power-saving Sleep mode
  •  Programmable weak pull-ups on PORTB
  • Multiplexed Master Clear/Input-pin
  • Watchdog Timer with independent oscillator for reliable operation
  •  Low-voltage programming
  •  In-Circuit Serial Programming™ (via two pins)
  • Programmable code protection
  •  Brown-out Reset
  • Power-on Reset
  • Power-up Timer and Oscillator Start-up Timer 
  • Wide operating voltage range (2.0-5.5V)
  •  Industrial and extended temperature range
  • High-Endurance Flash/EEPROM cell:
  • 100,000 write Flash endurance
  •  1,000,000 write EEPROM endurance
  • 40 year data retention
Low-Power Features:
  • Standby Current: - 100 nA @ 2.0V, typical
  • Operating Current: - 12 μA @ 32 kHz, 2.0V, typical - 120 μA @ 1 MHz, 2.0V, typical
  • Watchdog Timer Current: - 1 μA @ 2.0V, typical
  • Timer1 Oscillator Current: - 1.2 μA @ 32 kHz, 2.0V, typical
  •  Dual-speed Internal Oscillator:
  • Run-time selectable between 4 MHz and 48 kHz
  •  4 μs wake-up from Sleep, 3.0V, typical
Peripheral Features:
  • 16 I/O pins with individual direction control
  • High current sink/source for direct LED drive
  • Analog comparator module with:
  • Two analog comparators
  • Programmable on-chip voltage reference (VREF) module
  • Selectable internal or external reference
  •  Comparator outputs are externally accessible
  • Timer0: 8-bit timer/counter with 8-bit programmable prescaler
  • Timer1: 16-bit timer/counter with external crystal/ clock capability
  • Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler
  • Capture, Compare, PWM module:
  • 16-bit Capture/Compare
  • 10-bit PWM
  •  Addressable Universal Synchronous/Asynchronous Receiver/Transmitter USART/SCI

 

More From Iamtechnical.com

Advertisement:

SiteLock