PIC16F877A-HITECH C-Digital Output & Input

Photo of author

By Jackson Taylor

Home » Programming » PIC16F877A-HITECH C-Digital Output & Input

As an Amazon Associate we earn from qualifying purchases.

Digital Output

1) All pin in the PORTB high

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=0;
while(1)
{
PORTB=0xFF;
}
}

2) All pin in the PORTB blinking

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=0;
while(1)
{
PORTB=0xFF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}

Digital input

1)input at RB0 and PORTB blinking

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;
while(1)
{
if((PORTB&1)==1)
{
PORTB=0xFF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

2)PULL UP enable on RB0 (3 PROGRAMS WITH SAME O/P AND DIFFERENT LOGIC)

basic steps

set RB0 as input

enable pull up bit(by writing 0 to nRBPU)

check RB0

a)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;       
nRBPU=0;                                      
while(1)
{
if(RB0==0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}}

b)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;       
nRBPU=0;                                      
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

c)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;               
OPTION_REG &=(0<<nRBPU);
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

EXTERNAL INTERRUPT at RB0

Basic Steps

1)Enable global interrupt(GIE)

2)Enable external interrupt (INTE)

3)select interrupt edge (INTEDG)

4)clear INTF flag in your interrupt service routine

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
INTE=1;
TRISB=1;
PORTB=0;                           
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}
void interrupt jimmy(void)
{
INTF=0;
PORTB=0XF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}

Additional Example

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=0;
while(1)
{
PORTB=0xFF;
}
}

2) All pin in the PORTB blinking

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=0;
while(1)
{
PORTB=0xFF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}

Digital input

1)input at RB0 and PORTB blinking

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;
while(1)
{
if((PORTB&1)==1)
{
PORTB=0xFF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

2)PULL UP enable on RB0 (3 PROGRAMS WITH SAME O/P AND DIFFERENT LOGIC)

basic steps

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

set RB0 as input

enable pull up bit(by writing 0 to nRBPU)

check RB0

a)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;       
nRBPU=0;                                      
while(1)
{
if(RB0==0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}}

b)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;       
nRBPU=0;                                      
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

c)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;               
OPTION_REG &=(0<<nRBPU);
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}
}

EXTERNAL INTERRUPT at RB0

Basic Steps

1)Enable global interrupt(GIE)

2)Enable external interrupt (INTE)

3)select interrupt edge (INTEDG)

4)clear INTF flag in your interrupt service routine

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
INTE=1;
TRISB=1;
PORTB=0;                           
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}
void interrupt jimmy(void)
{
INTF=0;
PORTB=0XF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}

Additional Example

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;
while(1)
{
if((PORTB&1)==1)
{
PORTB=0xFF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

2)PULL UP enable on RB0 (3 PROGRAMS WITH SAME O/P AND DIFFERENT LOGIC)

basic steps

set RB0 as input

enable pull up bit(by writing 0 to nRBPU)

check RB0

a)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;       
nRBPU=0;                                      
while(1)
{
if(RB0==0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}}

b)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;       
nRBPU=0;                                      
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

c)

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
TRISB=1;
PORTB=0;               
OPTION_REG &=(0<<nRBPU);
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}

Interrupt Example

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
INTE=1;
TRISB=1;
PORTB=0;                           
while(1)
{
if((PORTB &1)== 0)
{
PORTB=0xFe;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}
}
}
void interrupt jimmy(void)
{
INTF=0;
PORTB=0XF;
__delay_ms(1000);
PORTB=0;
__delay_ms(1000);
}

Frequently Asked Questions

What does TRISB=1 do in the input examples?

Each bit in TRISB controls one pin of PORTB. A 1 makes the pin an input and a 0 makes it an output. TRISB=1 is binary 00000001, so only RB0 becomes an input and RB1 to RB7 stay outputs. That is why writing PORTB=0xFF still lights the other pins. If you want all eight pins as inputs, use TRISB=0xFF.

See also
MICROWAVE OVEN TRANSFORMER CONVERTED TO WELDING / CUTTER TRANSFORMER BY EMERGING TECHNOLOGIES

Why do I need the pull-up on RB0?

A pin connected to a push button and ground floats when the button is not pressed, so the reading can be random. The internal weak pull-up holds the pin high until the button pulls it to ground. This is why the pull-up examples check for instead of 1. Clearing nRBPU in OPTION_REG turns on the pull-ups for PORTB pins that are set as inputs. Without it, you need an external pull-up or pull-down resistor.

Which of the three pull-up programs should I use?

All three give the same result. Version a) reads the RB0 bit directly and is the easiest to read. Versions b) and c) use bit masks, which is useful when you want to test several pins at once. For beginners, a) is the best starting point.

How do I choose the edge for the external interrupt?

The INTEDG bit in OPTION_REG sets the edge on RB0. Set it to 1 for a rising edge and clear it to 0 for a falling edge. After a reset, OPTION_REG has all bits set, so the default is the rising edge. With a pull-up and a button to ground, you usually want a falling edge, so clear INTEDG explicitly.

Why does my button seem to trigger more than once?

Mechanical switches bounce, which means the contact opens and closes several times within a few milliseconds. The microcontroller is fast enough to see each change. A short delay after detecting a press, or a small capacitor across the switch, usually fixes this.

Is it fine to use delays inside the interrupt routine?

The examples do this to make the result visible, but it is not good practice. While the routine runs, the main loop is blocked. In a real project, set a flag in the interrupt routine, clear INTF, and do the slower work in the main loop.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works