22
Timer 0 operations prepared by : Maher Al-omari

4.6 timers

Embed Size (px)

DESCRIPTION

timer in pic microcontroller

Citation preview

Page 1: 4.6 timers

Timer 0 operations

prepared by : Maher Al-omari

Page 2: 4.6 timers

What is a timer

� a counter:– Counts independently while the PIC performs other tasks.

� can be driven by :� can be driven by :– the processor's instruction clock � a timer.

– an external signal � counting transitions on an input pin.

� commonly used: to drive interrupts to allow regularly timed

"background" tasks to run.

prepared by : Maher Al-omari

Page 3: 4.6 timers

Timer0 and TMR0

� three timers:– the simplest of these is referred to as Timer0.

� TMR0 :� TMR0 :– the visible part, an 8-bit register,

– holds the current value of the timer.

– It is readable and writeable.

� writing to TMR0:– starts incrementing from the written value.

– sets an "overflow flag“ (the TOIF bit in the INTCON register) to indicate that a rollover happened.

prepared by : Maher Al-omari

Page 4: 4.6 timers

configuration of Timer0

timer0 clock source0 : the instruction clock. 1 : the TOCKI pin.

The clock edge (counter mode)0 : rising edge of TOCKI. 1 : falling edge of TOCKI.

prepared by : Maher Al-omari

Page 5: 4.6 timers

configuring the pre-scaler of Timer0

Prescaler AssignmentTimer0 RateBit Value

Prescaler Assignment1: WDT0: TMR0

Timer0 RateBit Value

1 : 2000

1 : 4001

1 : 8010

1 : 16011

1 : 32100

1 : 64101

1 : 128110

1 : 256111

1. reduces the clock rate seen by the timer, by dividing it by a power of two

2. To use the prescalar, clear the PSA bit.

3. The ratio is set by the PS<2:0> bits, as shown

prepared by : Maher Al-omari

Page 6: 4.6 timers

Block diagram for Timer0.

Prescaler Assignment

timer0 clock source

timer0 source edge

prepared by : Maher Al-omari

Page 7: 4.6 timers

PROGRAMMABLE PRESCALER:

Given that the PIC is clocked at 4 MHZ, what is the maximum period that Timer0 can measure without the prescaler?

Given that the PIC is clocked at 4 MHZ , PSA = 0 and PS<2:0> = '111‘ What is the Maximum period that Timer0 can measure directly?

Given that the PIC is clocked at 8 MHZ , PSA = 0 and PS<2:0> = ‘011‘ What is the Maximum period that Timer0 can measure directly?

prepared by : Maher Al-omari

Page 8: 4.6 timers

SUMMARY OF REGISTERS ASSOCIATED WITH TIMER0

prepared by : Maher Al-omari

Page 9: 4.6 timers

SETUP_TIMER_0(mode)RTCC_INTERNAL, RTCC_EXT_L_TO_H RTCC_EXT_H_TO_LFunction:

configure timer 0 .

Mode:

One constant may be used from each group or'ed together with the | operator.

RTCC_DIV_1,RTCC_DIV_2, RTCC_DIV_4, RTCC_DIV_8, RTCC_DIV_16, RTCC_DIV_32, RTCC_DIV_64, RTCC_DIV_128, RTCC_DIV_256

prepared by : Maher Al-omari

Page 10: 4.6 timers

SETUP_TIMER_0(mode)

Example1:setup_timer_0 (RTCC_DIV_8 | RTCC_INTERNAL);

Example2:Example2:setup_timer_0 (RTCC_EXT_H_TO_L | RTCC_DIV_64);

Example3:setup_timer_0 (RTCC_INTERNAL);

Example4:setup_timer_0 (RTCC_EXT_L_TO_H);

Example4:setup_timer_0 (RTCC_EXT_L_TO_H | RTCC_DIV_1);

No prescaler

Prescaler=2

Prescaler=2

prepared by : Maher Al-omari

Page 11: 4.6 timers

set_timer0(value)

1. Sets the count value TIMER0.

2. All timers count up.

3. When a timer reaches the maximum value it will flip over to 0 and continue counting (254, 255, 0, 1,

When a timer reaches the maximum value it will flip over to 0 and continue counting (254, 255, 0, 1, 2...)

prepared by : Maher Al-omari

Page 12: 4.6 timers

value=get_timer0()

Parameters: None

Returns: Timer 0 returns a 8 bit int

Function: Returns the count value of a real time

clock/counter. RTCC and Timer0 are the

same.

prepared by : Maher Al-omari

Page 13: 4.6 timers

Delay using timer_0

#include "16F877A.H"

void main() {

setup_timer_0( RTCC_INTERNAL|RTCC_DIV_256 );

What is the function of this program segment ?

setup_timer_0( RTCC_INTERNAL|RTCC_DIV_256 );

set_timer0(0);

output_b(0x0f);

while ( get_timer0() < 200 ) ;

output_b(0xf0);

}

If the pic freq. is 4mhzHow long will program wait here?

prepared by : Maher Al-omari

Page 14: 4.6 timers

What is the function of this program?

#include "16F877A.H"

void main() {

setup_timer_0( RTCC_INTERNAL|RTCC_DIV_16 );setup_timer_0( RTCC_INTERNAL|RTCC_DIV_16 );

WHILE (1)

{ set_timer0(100);

output_b(0x0f);

while ( get_timer0() < 151 ) ;

output_b(0xf0);

while ( get_timer0() < 251 ) ; }

}

prepared by : Maher Al-omari

Page 15: 4.6 timers

Using timer0 to create a 1msec delay function

#include "16F877A.H"void del_1m() {#BIT T0IF = 0x0B.2 // map a bit to a c variablesetup_timer_0( RTCC_INTERNAL|RTCC_DIV_8 );

Given that

the freq. is

4mhz ;

setup_timer_0( RTCC_INTERNAL|RTCC_DIV_8 );set_timer0(130);while (!T0IF); //wait for timer0 interrupt flagclear_interrupt(int_timer0); } // must clear TOIF bit in INTCON

void main() { while (1) { del_1m(); output_b(0x0f);

del_1m(); output_b(0xf0); }}

prepared by : Maher Al-omari

Page 16: 4.6 timers

Count from 0-3 with 4 msec delay between1. # include "16F877A.h"2. void del_1m() {3. #BIT T0IF = 0x0B.2 4. setup_timer_0( RTCC_INTERNAL|RTCC_DIV_32 );5. set_timer0(130);6. while (!T0IF); 7. clear_interrupt(int_timer0);}

replace7. clear_interrupt(int_timer0);}8. void main(){9. int n;10. int8 anum[10]; 11. anum[0] =0x3f; anum[1] =0x06;12. anum[2] =0x5b;anum[3] =0x4f;13. while(1) 14. for ( n = 0; n < 4; n++ )15. { output_c(anum[n]);16. del_1m();} 17. }

Given that the

freq. is 4mhz ;

What is the

function of the

program?

prepared by : Maher Al-omari

Page 17: 4.6 timers

Given that PIC is running at 100khz ;

1. void main() {

2. #BIT flag = 0x0B.2

3. setup_timer_0( RTCC_INTERNAL|RTCC_DIV_128 );

4. set_timer0(59);

999 msec

4. set_timer0(59);

5. while (!flag);

6. Flag=0;} How much time

will the PIC take to

execute this

statement?

prepared by : Maher Al-omari

Page 18: 4.6 timers

Programming Timer0 as a Counteris used to count events or pulses/TOCKI4RA

# include "16F877A.h"//________________________________void main(){int n;int8 anum[16];

anum[0]=0x3f; anum[1]=0x06; anum[2]=0x5b;anum[3]=0x4f; anum[4]=0x66; anum[5]=0x6D;anum[6]=0x7D; anum[7]=0x07; anum[8]=0x7F;

No PSA

anum[6]=0x7D; anum[7]=0x07; anum[8]=0x7F;anum[9]=0x6f; anum[10]=0x77; anum[11]=0x7c;anum[12]=0x39; anum[13]=0x5e; anum[14]=0x79; anum[15]=0x71;

setup_timer_0( RTCC_EXT_H_TO_L | RTCC_DIV_1 );set_timer0(0);while(1)

{ n=get_timer0() & 0x0f ;output_d ( anum[n] ) ;}

}

prepared by : Maher Al-omari

Page 19: 4.6 timers

what is the output at the display after pressing SW1 7 times ?

# include "16F877A.h"//________________________________void main(){int n;int8 anum[16];

anum[0]=0x3f; anum[1]=0x06; anum[2]=0x5b;anum[3]=0x4f; anum[4]=0x66; anum[5]=0x6D;anum[6]=0x7D; anum[7]=0x07; anum[8]=0x7F;anum[6]=0x7D; anum[7]=0x07; anum[8]=0x7F;anum[9]=0x6f; anum[10]=0x77; anum[11]=0x7c;anum[12]=0x39; anum[13]=0x5e; anum[14]=0x79; anum[15]=0x71;

setup_timer_0( RTCC_EXT_H_TO_L);set_timer0(3) ;while(1)

{ n=get_timer0() & 0x0f ;output_d ( anum[n] ) ;}

}

prepared by : Maher Al-omari

Page 20: 4.6 timers

Stop watch-1connect a virtual terminal to the RS232 hardware in the PIC

Terminal screen

RS232 PIC connection Virtual

terminalprepared by : Maher Al-omari

Page 21: 4.6 timers

Stop watch-2

#use delay(clock=8000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define INTS_PER_SECOND 31

BYTE seconds;

BYTE int_count;

#int_rtcc

# of interrupts /sec

#int_rtcc

void clock_isr() {

if(--int_count==0) { ++seconds;

int_count=INTS_PER_SECOND;}}

prepared by : Maher Al-omari

Page 22: 4.6 timers

Stop watch-3void main() { BYTE start; int_count=INTS_PER_SECOND; set_timer0(0); setup_timer_0( RTCC_INTERNAL|RTCC_DIV_256); enable_interrupts(INT_RTCC); enable_interrupts(GLOBAL);

An interrupt every 32.6 msec enable_interrupts(GLOBAL);

do {printf("press any key to begin timming:( )\n\r");getc(); // waits for a key to be entered.

start=seconds; // the start time.

printf("Press any key to stop the watch:( )\n\r");getc(); // waits for a key to be entered.

printf("your time is %u seconds.\n\r", seconds-start ) ;while (TRUE); }

}

32.6 msec

Current time – start time

prepared by : Maher Al-omari