You are on page 1of 5

1

EumpIe: 0enerofe repefifive inferrupfs usinq Timer0



Durofion of fime befween sforf of fhe counfinq from TMP0's preIooded inifioI voIue unfiI fimer overfIow fIoq is sef
fhof couses inferrupf, is qiven by (4/F
XT
)*A*(Zb-), where F
XT
is osciIIofor frequency, A is prescoIer voIue, is
inifioI voIue Iooded fo TMP0.

In fhe beIow proqrom, fhe moin funcfion confinuousIy furns on eoch bif of Porf8 one by one, ond of fime infervoIs
when fimer overfIow occurs PorfD bif0 is foqqIed. Timer overfIow condifion qenerofes fimer inferrupf: fhe
durofion befween fhese inferrupfs is defermined by fhe Timer0 seffinqs. With F
XT
=4MHz, A=1Z, =1b,
durution of time between timer overfIows is 1Zmsec As u mutter of fuct, time durution between euch
counter increment is (4/F
XT
)*A which is 01Zmsec

#include 16F877A.h~
#FUSES XT.PUT.NOWDT
#use delay(clock4000000)
#use Iastio(B)
#use Iastio(D)
int data1; //initialize 8-bit variable data to 1


#intTIMER0 //directive to inIorm compiler that Iollowing Iunction is the ISR Ior TMR0 overIlow interrupt
void TIMER0isr(void) //ISR (Interrupt Service Routine) Ior TIMER0 overIlow occurs

settimer0(156); //set TMR0 initial value to 156
outputtoggle(PIND0); //toggle logic level oI PortD bit0
}

void main(void)

settrisb(0x00); //PortB pins set as output
settrisd(0x00); //PortD pins set as output
outputb(0x00); //clear PortB data
outputd(0x00); //clear PortD data
settimer0(156); //set TMR0 initial value to 156
setuptimer0(RTCCINTERNAL'RTCCDIV128); //set timer0 as internal clock. prescaler128
enableinterrupts(INTTIMER0); //enable timer overIlow interrupt
enableinterrupts(GLOBAL); //enable global interrupt

while(TRUE) //loop Iorever and wait Ior TIMER0 interrupt
outputb(data); //output data to portb
datadata1; //leIt shiIt value oI data by one
iI (data~0b10000000) data1; //iI data~128 equate it to 1
}

} //end oI main Iunction












12.8ms
255
156

2



EumpIe: Obecf or evenf counfinq usinq Timer0
In fhe beIow proqrom, fhe moin funcfion Ioops forever. MeonwhiIe, in view of Timer 0 seffinqs, if on exfernoI evenf
occurs fhen TMP0 is oufomoficoIIy incremenfed. TMP0 wiII confoin fhe number of evenfs of mosf up fo Zbb
D
since
TMP0 is on 8-bif reqisfer. Affer Zbb
D
if wiII roII over ond sforf from zero oqoin occordinq fo fhe beIow Iisfinq. The
voIue of TMP0 is Iooded info o voriobIe counfed_evenfs ond dispIoyed on Porf8.

#include 16F877A.h~
#Iuses XT.PUT.NOWDT.NOBROWNOUT.NOLVP.NOPROTECT
#use delay(clock4000000)
#use Iastio(B)
#use Iastio(A)

int countedevents0; //initialize variable countedevents to zero

void main(void)

setupadcports(NOANALOGS); //No Analog input required at PortA
setupadc(ADCOFF); //Turn oII ADC
settrisa(0xFF); //porta pins set as input. RA4/T0CKI must be input Ior external clock
settrisb(0x00); //portb pins set as output
outputb(0x00); //clear PortB data
settimer0(0); //set TMR0 initial value to zero
setuptimer0(RTCCEXTHTOL'RTCCDIV1); //set timer0 as external clock. prescaler1

while(TRUE) //loop Iorever. TMR0 value will be incremented iI external event occurs
countedeventsgettimer0(); //equate countedevents to TMR0 value
outputb(countedevents); //output TMR0 value to PortB
//note that return value oI gettimer0() never exceeds 255!!!
}
} //end oI main Iunction





























3



EumpIe: Obecf or evenf counfinq usinq Timer0 ond inferrupfs
In fhe beIow proqrom, fhe moin funcfion Ioops forever. MeonwhiIe, in view of Timer 0 seffinqs, if on exfernoI evenf
occurs fhen ISP incremenfs fhe voriobIe counfed_evenfs ond TMP0 presef fo ifs inifioI voIue. The voIue of
counfed_evenfs is dispIoyed on Porf8.

#include 16F877A.h~
#Iuses XT.PUT.NOWDT.NOBROWNOUT.NOLVP.NOPROTECT
#use delay(clock4000000)
#use Iastio(B)
#use Iastio(A)
#use Iastio(D)

int countedevents0; //initialize variable countedevents to zero

#intTIMER0 //directive to inIorm compiler that Iollowing Iunction is the ISR Ior TMR0 overIlow interrupt
void TIMER0isr(void) //ISR Ior TIMER0 overIlow occurs

settimer0(255); //set TMR0 initial value 255 so that each time TMR0 rolls over ISR will execute
countedeventscountedevents1; //increment countedevents
}

void main(void)

setupadcports(NOANALOGS); //No Analog input required at PortA
setupadc(ADCOFF); //Turn oII ADC
settrisa(0xFF); //porta pins set as input. RA4/T0CKI must be input Ior external clock
settrisb(0x00); //portb pins set as output
settrisd(0x00); //portd pins set as output
outputb(0x00); //clear portb data
outputd(0x00); //clear portb data
settimer0(255); //set TMR0 initial value to 255
setuptimer0(RTCCEXTHTOL'RTCCDIV1); //set timer0 as external clock. prescaler1
enableinterrupts(INTTIMER0); //enable timer overIlow interrupt
enableinterrupts(GLOBAL); //enable global interrupt

while(TRUE) //loop Iorever and wait Ior TIMER0 interrupt
outputb(countedevents); //output countedevents to PortB
// note iI countedevents~255 PortB will not be able to indicate it. hence better to use LCD!!!
outputd(gettimer0()); //output TMR0 value to PortD
}
} //end oI main Iunction



















4



EumpIe: 0enerofe repefifive inferrupfs usinq TimerI
SimiIor fo previous exompIe fhof qenerofes repefifive inferrupfs usinq Timer0 moduIe buf usinq TimerI moduIe.
TimerI seffinqs, hence proqrom execufion is occordinq fo numericoIIy worked-ouf exompIe qiven in fhe expIonofion
of fhe buiIf-in funcfion in Iecfure sIides.

#include 16F877A.h~
#FUSES XT.PUT.NOWDT
#use delay(clock4000000)
#use Iastio(B)
#use Iastio(D)
int data1; //initialize 8-bit variable data to 1


#intTIMER1 //directive to inIorm compiler that Iollowing Iunction is the ISR Ior TMR1 overIlow interrupt
void TIMER1isr(void) //ISR (Interrupt Service Routine) Ior TIMER1 overIlow occurs

outputtoggle(PIND0); //toggle logic level oI PortD bit0
}

void main(void)

settrisb(0x00); //PortB pins set as output
settrisd(0x00); //PortD pins set as output
outputb(0x00); //clear PortB data
outputd(0x00); //clear PortD data
settimer1(0); //set TMR1 initial value to 0

setuptimer1(T1INTERNAL ' T1DIVBY8); //set timer1 as internal clock. prescaler8
enableinterrupts(INTTIMER1); //enable timer overIlow interrupt
enableinterrupts(GLOBAL); //enable global interrupt

while(TRUE) //loop Iorever and wait Ior TIMER1 interrupt
outputb(data); //output data to portb
datadata1; //leIt shiIt value oI data by one
iI (data~0b10000000) data1; //iI data~128 equate it to 1
}

} //end oI main Iunction





















104.847ms
65536
0

5




EumpIe: 0enerofe repefifive inferrupfs usinq TimerZ
SimiIor fo previous exompIe fhof qenerofes repefifive inferrupfs usinq Timer0 moduIe buf usinq TimerZ moduIe.
TimerZ seffinqs, hence proqrom execufion is occordinq fo numericoIIy worked-ouf exompIe qiven in fhe expIonofion
of fhe buiIf-in funcfion in Iecfure sIides.

#include 16F877A.h~
#FUSES XT.PUT.NOWDT
#use delay(clock4000000)
#use Iastio(B)
#use Iastio(D)
int data1; //initialize 8-bit variable data to 1


#intTIMER2 //directive to inIorm compiler that Iollowing Iunction is the ISR Ior TMR2 overIlow interrupt
void TIMER2isr(void) //ISR (Interrupt Service Routine) Ior TIMER2 overIlow occurs

outputtoggle(PIND0); //toggle logic level oI PortD bit0
}

void main(void)

settrisb(0x00); //PortB pins set as output
settrisd(0x00); //PortD pins set as output
outputb(0x00); //clear PortB data
outputd(0x00); //clear PortD data
settimer2(0); //set TMR2 initial value to 0

setuptimer2( T2DIVBY4. 0xc0. 2); //set timer2 as prescaler4. period0xc0.postscaler2
enableinterrupts(INTTIMER2); //enable timer overIlow interrupt
enableinterrupts(GLOBAL); //enable global interrupt

while(TRUE) //loop Iorever and wait Ior TIMER2 interrupt
outputb(data); //output data to portb
datadata1; //leIt shiIt value oI data by one
iI (data~0b10000000) data1; //iI data~128 equate it to 1
}

} //end oI main Iunction



















307.2us

You might also like