Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 30th July 2008, 07:25 AM   #1
Default timer0

" 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 by sahanaashwin; 30th July 2008 at 08:06 AM. Reason: specify the language
sahanaashwin is offline  
Old 30th July 2008, 07:30 AM   #2
Default

Quote:
Originally Posted by sahanaashwin View Post
" 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.?
__________________
Eric " Good enough is Perfect "
I will NOT answer PM's requesting technical help, please use the Forum
PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/
ericgibbs is offline  
Old 30th July 2008, 07:44 AM   #3
Default

i select prescaler 1:256
movlw B'10000111'
MOVWF OPTION_REG
is there any calculation to set 3sec or 5 sec delay?
sahanaashwin is offline  
Old 30th July 2008, 08:03 AM   #4
Default

Quote:
Originally Posted by sahanaashwin View Post
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.?
__________________
Eric " Good enough is Perfect "
I will NOT answer PM's requesting technical help, please use the Forum
PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

Last edited by ericgibbs; 30th July 2008 at 08:05 AM.
ericgibbs is offline  
Old 30th July 2008, 08:22 AM   #5
Default

"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
sahanaashwin is offline  
Old 30th July 2008, 08:31 AM   #6
Default

ok,i understand, now i am going to try,thank you
sahanaashwin is offline  
Old 30th July 2008, 08:47 AM   #7
Default

Quote:
Originally Posted by sahanaashwin View Post
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.?
__________________
Eric " Good enough is Perfect "
I will NOT answer PM's requesting technical help, please use the Forum
PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/

Last edited by ericgibbs; 30th July 2008 at 08:48 AM.
ericgibbs is offline  
Old 31st July 2008, 05:42 AM   #8
Default

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
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 by sahanaashwin; 31st July 2008 at 05:44 AM.
sahanaashwin is offline  
Old 31st July 2008, 06:22 AM   #9
Default

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.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 31st July 2008 at 07:36 AM.
futz is offline  
Old 31st July 2008, 08:27 AM   #10
Default

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
;...............
__________________
Eric " Good enough is Perfect "
I will NOT answer PM's requesting technical help, please use the Forum
PIC tutorials: Nigel's www.winpicprog.co.uk/ Bill's: www.blueroomelectronics.com/
ericgibbs is offline  
Old 6th August 2008, 07:35 AM   #11
Default

thank you eric
sahanaashwin is offline  
Old 27th March 2009, 01:47 PM   #12
Default

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.
meguitarist is offline  
Reply

Tags
timer0

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Timer0 Prescale lord loh. Micro Controllers 10 8th October 2008 06:14 PM
Using timer0 mrfunkyjay Micro Controllers 6 11th July 2008 10:43 AM
TIMER0 - Have I got this right? UTMonkey Micro Controllers 8 14th December 2007 11:08 PM
PIC Timer0 Hesam Kamalan Micro Controllers 7 29th October 2007 03:32 PM
Cant get timer0 to interrupt 2camjohn Micro Controllers 5 16th June 2004 01:06 PM



All times are GMT. The time now is 04:08 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker