Oct 14, 2013

PIC16F877A-HITECH C-Part1

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

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

Here i am blinking PORTB using timer interrupt
Basic Steps
1)Enable global interrupt (GIE)
2)EnableTIMER0 interrupt (TMR0IE)
3)select internal clock source (TOCS)
4)select prescaler in timer mode (PSA)
5)Select prescaler value if nedded (PS2, PS1, PS0)
6)Load the desired timer value to TMRO
7)clear the tmer flag in interrupt service routine


#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1)
{
}                                                                
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
 TMR0IF=0;
 }
Timer0 as Counter
External clock is at RA4 pin
when 2 external pulses are given  to RA4 pin, PORTB  will Negate
please pull down the RA4 pin

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
TMR0IE=1;
T0CS=1; // not needed this step (this register normally is at 1)
TRISB=0;
PORTB=0;  
TMR0 = 0xfe;
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR0IF=0;
TMR0 = 0xfe;
}


PORT B WILL BLINK ON TIMER INTERRUPT
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfff;
TMR1CS=0;
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
__delay_ms(500);
TMR1IF=0;
}

clock is given at the pin RC0
HERE COUNT = 4
PLEASE PULL DOWN
PORTB WILL NEGATE
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1=0xfffC;
TMR1CS=1;
TRISB=0;
PORTB=0;  
while(1){ }                                                                  
}
void interrupt jimmy(void)
{
PORTB= ~PORTB;
TMR1IF=0;
TMR1=0xfffc;
}

clock frequency = 16MHz

#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR1IE=1;
TMR1ON=1;
TMR1CS=0;
TMR1 = 0XFFFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR1IF=0;
 c=c+1;
if(c==61)
{
PORTB= ~PORTB;
c=0;
}
}
I am used 16MHz external clock
PORTB blinking after every 1 second
Time calculation
External clock frequency, FOSC = 16MHz
clock frequency for timer0 = =  FOSC/4 = 4MHz  (in datasheet clock frequency  of timer will be quarter of  clock frequency )
Here in the program pre-scale value = 256 and also TMR0= 0xFF (255-but the count is actually 256, ie interrupt is set when timer reaches ''0'' only)
Then clock frequncy = 4MHz/ 256 =15625Hz
then time for single excution = 1/15625Hz = 0.000064 second
Here timer will overflow at after every 256 counts
time taken between two overflow = 256 * 0.000064 second =0.016384 second
In program i have used a variable to count up-to 1 second
this variable is calculated by 1 second/0.016384 second = 61.03515625 =61

#include<pic.h>

#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
TMR0IE=1;
T0CS=0;
PSA=0;
PS2=1;
PS1=1;
PS0=1;
TRISB=0;
PORTB=0;
TMR0=0XFF;
while(1){}                                                                
}
void interrupt jimmy(void)
{
c++;
if(c == 61)
{
PORTB= ~PORTB;
c=0;
}
TMR0IF=0;
}
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}

void interrupt jimmy(void)
{
TMR2IF=0;
PORTB= ~PORTB;
__delay_ms(500);
}
#include<pic.h>
#define _XTAL_FREQ 16000000
int c=0;
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
TRISB=0;
PORTB=0;
while(1){}                                                              
}
void interrupt jimmy(void)
{
TMR2IF=0;
c=c+1;
if(c==15625)
{
PORTB=~PORTB;
c=0;
}
}
frequency generation using TIMER2
measured frequency = 7936.5079365079365079365079365079 (approximately)
calculated frequency = 7843.1372549019607843137254901961

#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
GIE=1;
PEIE=1;
TMR2IE=1;
TMR2ON=1;
TMR2 = 0XFE;
PR2=0XFE; //time perioud register
TRISB=0;
PORTB=0;  
while(1){}                                                                  
}
void interrupt jimmy(void)
{
TMR2IF=0;
PORTB=~PORTB;
}


ADC

//in this i have setted AN0 as input ADC port
// value after Anolog to digital conversion is stored at variable and
// also shown at portc and portb in combined form(10 bit)
#include<pic.h>
#define _XTAL_FREQ 16000000
__CONFIG(0x3f7a);
int a;
void main()
{
TRISB=0;//setting port B as output
PORTB=0;
TRISC=0;//setting port C as output
PORTC=0;
TRISA=255;//setting the PORTA as input
ADON=1;//enables ADC
ADCS0=0;//setting the clock fo ADC
ADCS1=0;//setting the clock fo ADC
ADCS2=0;//setting the clock fo ADC
ADFM=1;//sets the higher bits of conversion to right justified
PCFG3=0;//Configuring 8 pin as adc input channels
PCFG2=0;//Configuring 8 pin as adc input channels
PCFG1=0;//Configuring 8 pin as adc input channels
PCFG0=0;//Configuring 8 pin as adc input channels
CHS2=0;
CHS1=0;
CHS0=0;
while(1)
{
__delay_ms(100);
a=(ADRESH<<8)+ADRESL; //this only for further data processing
PORTC=ADRESH; //higher data byte
PORTB=ADRESL; //lower data byte
GO=1; // to restart conversion
}
}