Set the PortA RA0 as input and PortB RB0 as output of the PIC 16F84A device as output Port. Create a never ending while loop and inside the while loop read the PortA RA0 bit.
Check whether the Push Button is pressed or not. If the Button is Pressed means PortA RA0 = 1 or RA0 goes High and it enters into the while loop and starts the Led Blinking by sending 1 and 0 Alternately to RB0 with a 0.25 second delay and again checks for button is pressed if it is pressed again then stops the Led Blinking and the control comes out of the second while loop and the same process is repeated. If the Button is not Pressed means it will go to while loop and again checks for switch press.Flowchart and Circuit Diagram:
Circuit Description:
PIC 16F84A microcontroller device is programmed for Blinking Led with Push Button when the button is pressed. This device uses 4MHz crystal/Resonator oscillator to drive the oscillator1 and oscillator2. The Push Button is connected to the PortA RA0 and +5V and pull down Resistor R2(10k) is connected to the PortA RA0 and Gnd.
In this circuit Active High Input is used. Resistor R2(10k) is used to pull down the PIC input to Logic '0' or Low or Pull down the Voltage down to zero. when you press the switch it pulls the voltage from 0 to 5V and makes the input to Logic '1' or High.Resistor R1 is connected to the PIC pin and Led. the other negative terminal of the Led is connected to the ground.Resistor R1 is a Current Limiting Resistor R1(1K) is used to prevent the damage to both the PIC and the Led. Resistor R1 reduces the current to the sufficient level.Embedded C Program: Push Button for Blinking Led
Compiler: PIC CCS C compilerEmbedded C program for Push Button for Blinking Led. its very easy to learn and you can able to understand the concept and logic of the program from the flowchart and comment lines of the program.
/* Project Name: Push Button for Blinking Led */ #include<16F84A.h> /*defines type of device to include device files*/ #use delay(clock=4mhz) /* defines crystal frequency 4MHz */ #fuses xt, nowdt, noprotect, noput /* xt oscilator used and no watch dog timer, no code protection, no power up timer is used */ #byte portb=5 /* defines memory location of register */ #byte portb=6 /* defines memory location of register */ void main() { set_tris_a(0x01); /* it sets the portA RA0 as input port */ set_tris_b(0x00); /* it sets the portB RB0 as output port */ while(true) { if(porta == 0x01) /* if switch pressed Led starts Blinking*/ { while(true) { portb = 0x01; /*assigns portB RB0 to 0x01 for turn-on Led*/ delay_ms(250); portb = 0x00; /*assigns portB RB0 to 0x00 for turn-off Led*/ delay_ms(250); if(porta == 0x01) /* if switch pressed again Led stops Blinking*/ { break; /* comes out of the loop*/ } } } } } |
No comments:
Post a Comment