
Site Search
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: |
---|
|
Special Microcontroller Features: |
---|
|
Low-Power Features: |
---|
|
Peripheral Features: |
---|
|