• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to control the led like this?

Not open for further replies.


New Member

My PIC is PIC16F873.
This is the initial C program. I have to modify this thing like this;

Input push button on RB4, LED is initially ON. A press AND release of the button starts the LED flashing, and a subsequent press AND release freezes the LED ON .

I already hooked up the RB4, but I can't modify this code. Especially, how can I control the LED at the fist press and second press?

Please help me.

[email protected]

#include <pic.h>

#define XT_OSC 0x0001
#define DISABLE_DEBUG 0x0800

#define CP0 0x1010
#define CP1 0x2020

/* CP0,CP1 code protect off*/
/* other bits zero which means WDT disabled
Low Voltage prm disabled, brownout disabled
power up timer enabled */


#define bitset(var,bitno) ((var) |= (1 << (bitno)))
#define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))
#define bittst(var,bitno) (var & (1 << (bitno)))

/* a subroutine that implements a delay via loops
Note that the compile option used will alter this delay!
Always use -O compile option
unsigned int i,k;

/* change count values to alter delay */
for (k=1800; --k;) {
for(i = 200 ; --i ;);


/* just flash LED on port PB1 */
/* use this to test if PIC board is alive */

TRISB = 0; /* all bits output */
PORTB = 0x00; /* turn all off */
/* use PB1 for output */

for(;;) { /* note infinite loop, all PIC programs should
be an infinite loop of some kind. Why? If the
loop exited, what would the PIC do? */

a_delay(); /* a subroutine that implements a delay via loops */
bitset(PORTB,1); /* turn on PB1 */
bitclr(PORTB,1); /* turn off PB1 */
Not open for further replies.

Latest threads

EE World Online Articles