PIC16F877A-HITECH C-Digital Output & Input

Photo of author

By Jackson Taylor

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

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

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

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);
}
See also
BLINKING USING DSPIC30F2010 USING MIKRO C

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