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);
}
#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);
}
#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);
}
}
}
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);
}

See also
PYTHON