0% found this document useful (0 votes)
27 views2 pages

EE313 - Tutorial-4 - Solution

Timers are used to generate time delays or pulses of precise time durations. Counters are used to count the number of events occurring like input pulses within a specific time period. Some key differences between timers and counters are: - Timers: - Depend on internal oscillator frequency - Can generate time delays and pulses of precise durations - Counters: - Independent of oscillator frequency - Count number of input pulses within a time period - Reset after reaching a preset value In summary, timers are used for time-based operations while counters are used for counting frequency or number of events.

Uploaded by

Valraj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

EE313 - Tutorial-4 - Solution

Timers are used to generate time delays or pulses of precise time durations. Counters are used to count the number of events occurring like input pulses within a specific time period. Some key differences between timers and counters are: - Timers: - Depend on internal oscillator frequency - Can generate time delays and pulses of precise durations - Counters: - Independent of oscillator frequency - Count number of input pulses within a time period - Reset after reaching a preset value In summary, timers are used for time-based operations while counters are used for counting frequency or number of events.

Uploaded by

Valraj Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial 4 – 2020 ---------------------Solution

Que -1

Using PIC C, write a program to generate a square wave output ( 0v-5v-0v-5v) at the output pin Port A, pin7. Set
the frequency of the square wave oscillation 2 Hz. Select the crystal internal frequency = 5 MHz. Use Timer 0
features of PIC16F877.

a. Show your calculations.


b. Write a C program in PIC, to generate the square wave signal at Pin A_7.
[ Refer the class note for more info]

Ans

Step 1: Required square wave frequency = 2 Hz. Therefore, time period of one cycle = 0.5 sec. Now we
are interested in the square wave output. Therefore the output will be high for 0.25 sec and low for 0.25
sec. We have to generate delay of 0.25 sec to measure a time for high 0.25 sec and low 0.25 sec.
Step 2: For given crystal frequency 5 MHz, 5MHz/4 = 1.25 MHz.
Step 3: So the Time Period = 0.8 µS (micro sec)
Step 4: If Prescaler is set to divide frequency by 256, we get Prescaler Period = 0.8 x 256 = 204.8 µSec.
Step 5: Overflow Period = 256 x 204.8 = 52428.8 uS ( since Timer0 is 8-bit)
Each overflow time = 0.052 sec

To get delay for 0.25 sec, we need 0.25/0.052 Overflows to count = 4.8 Overflows so approximately 5
overflows can be measured via ISR.

Try to find more accurate overflow count by changing the prescaler from 256 ->
128 -> 64- -> 32

Prescaler Overflow Exact round count

256 4.8 5

128

64 19.07 19

32

Take the final closed overflow count – as in this calculation 64 prescalar will give 19.07 = 19
--------------------------------------------------------------------------------------------------------------
#include <16F877.h> //16F877 Pic in use
int i=0;
#INT_TIMER0
void interrupt_tim0() // program automatically jump on ISR when overflow occurred
{
i++; // count for 5 to get 0.25 sec delay
if (i==5)
{if (input(PIN_A7)==true)
output_low(PIN_A7);
else
output_high(PIN_A7);
i=0; // reset the count
}
}
main() {
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
//inilz timer with internal clock with Prescaler = 256
enable_interrupts(INT_TIMER0); // Enable Timer 0 interrupt
enable_interrupts(GLOBAL); // Enable Global interrupt
output_low(PIN_A7); // Set first pin A_7 low
while(1); // Wait for the interrupt and this will generate continuous Square wave

}// end of program

Que - 2
Differentiate: Timers Vs Counters
 Basically, a timer can produce time delays (monostable operation) or continues pulses (astable
operation) and a counter can count from zero up to a number or from a number down to zero
every time it gets an input signal.

Timers depend on time/frequency whereas the counter is independent with frequency.

You might also like