Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

timer0

Status
Not open for further replies.

sahanaashwin

New Member
" i would like to write a program using TMRO overflow interrupt to blink portb LED's at 5 second interval"(in assembly language)

my question is how to calculate(write) 5 sec delay code.pls help me

sahanaashwin
 
Last edited:
" i would like to write a program using TMRO overflow interrupt to blink portb LED's at 5 second interval"
my question is how to calculate(write) 5 sec delay code.pls help me

sahanaashwin


hi,
You set the tmr0 and its prescaler to give an integer roller interrupt value for timer0, increment a software counter on the interrupt.
Then you repeat that tmr0 loop the number of times that give 5 seconds.

Its also possible to do the same action without using the interrupt, just test the Tmr0 interrupt flag.

Do you follow.?
 
i select prescaler 1:256
movlw B'10000111'
MOVWF OPTION_REG
is there any calculation to set 3sec or 5 sec delay?

hi,
Assume you have a 4MHz crystal, giving a 1uSec cycle time.

So 256uSec to fill the prescaler,,, the tmr0 clock input would occur every 256uSec, as tmr0 is 8bit, [256 counts] = 65.536 mSec.
So the loop count would 65.536mSec *76= 4.98Seconds.

A simplier way is:
If you used a 3.2768MHz crystal, set the prescaler for 128, then tmr0 would roll over every 40mSec [tmr0 interrupt flag set],
do this 25 times and you get 1second, so 25 * 5 = 125 times around the loop will give exactly 5seconds.

Do you follow.?
 
Last edited:
"If you used a 3.2768MHz crystal, set the prescaler for 128, then tmr0 would roll over every 40mSec [tmr0 interrupt flag set], "

hi how do you calculate this 40msec.pls
 
ok,i understand, now i am going to try,thank you
hi,
Now that you can calculate the required time, its important to realise that when using a 'polling' method for timing using tmr0 in this way, that the MCU is not doing any other action.!

A more general method would be to use a interrupt service subroutine, in that way every 40mSec the program would jump to the ISR for doing the count.

The MCU could perform other functions while waiting for the tmr0 interrupt.

Have you considered using timer1 for the timing interrupt.?
 
Last edited:
timer0 is not working, i am trying to use timer1

this is the timer0 program, can you tell whats wrong in my coding?

;use timer0 overflow interrupt to blink portb LED's (one by one)
;file :timer0.asm

processor16c73b
#include<p16c73b.inc>


INTCON equ 0x0b
OPTION_REG equ 0x81
TMR0 equ 01
PORTB equ 06
TRISB equ 86
STATUS equ 03
BANK0RAM equ H'20'


CBLOCK 0x20
A1
A2
ENDC

org 0x0000

GOTO MAIN

org 0x0100
MAIN clrf STATUS
clrf A1
clrf A2
clrf PORTB
clrf TMR0

BSF STATUS,5 ;select bank1
BSF INTCON,5 ;enable the tmr0 interrupt
BSF INTCON,7 ;enable unmasked interrupt
MOVLW B'10000111' ;prescale 1:256
MOVWF OPTION_REG ; tmr0 control reg
CLRF TRISB ; potrb output
BCF STATUS,5 ; goto bank 0
BCF STATUS,0

movlw 0xFF
MOVWF A1
MOVLW 0XCD
MOVWF A2

MOVLW 0X70
MOVWF TMR0 :confused:
BTFSS INTCON,2 ;tmr0 overflow
; GOTO $-1

ORG 0x004
ISR BCF INTCON,2
DECFSZ A2,1
GOTO L1
RLF A1,1
MOVWF PORTB
L1 MOVLW 0X70
MOVWF TMR0
RETFIE ;return from interrupt
NOP
END
 
Last edited:
Please use Code tags when posting code here in the forums! Simply click the # icon immediately before pasting your code, or type [code] before your code and [/code] after it. This tells the forum software to not strip the formatting from your code and prevents it from becoming the unreadable lump seen in the above post.
 
Last edited:
hi sahanaashwin,

Your code dosnt make much sense.:(

Look at this:
Code:
;code fragment polling tmr0 intf
;16F877A, using a 3.2768MHz xtal 

tmr0_cnt equ 0x20

one_sec:
         bsf STATUS,RP0
         movlw 0x86  'timer0 prescaler 1:128, pu off
         movwf OPTION_REG
         bcf STATUS,RP0

init:
         movlw .25  ; .125 for 5secs
         movwf tmr0_cnt
         clrf TMR0
         bsf INTCON,T0IE
         bcf INTCON,T0IF
timer0:
         btfss INTCON,2
         goto timer0           'no timer0 overflow
         bcf INTCON,T0IF    'clr TMR0 INTF
         decfsz tmr0_cnt,F  'dec tmr0 cntr
         goto timer0           'Not zero so loop again
' done one sec
;...............
 
Good day everyone,

10f200 TMR0 question.

How do I use tmr0 to flash LED at 1hz with 20% on & 80% off duty cycle on GP1 & play a siren sound on GP0 at the same time?

Thank you.
 
Status
Not open for further replies.

Latest threads

Back
Top