+ Reply to Thread
Results 1 to 12 of 12

Thread: timer0

  1. #1
    sahanaashwin Newbie
    Join Date
    Jul 2008
    Posts
    22

    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 07:06 AM. Reason: specify the language


  2. #2
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,839
    Blog Entries
    13

    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/

    Link to my Articles: http://www.electro-tech-online.com/a...icgibbs-55450/

  3. #3
    sahanaashwin Newbie
    Join Date
    Jul 2008
    Posts
    22

    Default

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

  4. #4
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,839
    Blog Entries
    13

    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.?
    Last edited by ericgibbs; 30th July 2008 at 07:05 AM.
    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/

    Link to my Articles: http://www.electro-tech-online.com/a...icgibbs-55450/

  5. #5
    sahanaashwin Newbie
    Join Date
    Jul 2008
    Posts
    22

    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

  6. #6
    sahanaashwin Newbie
    Join Date
    Jul 2008
    Posts
    22

    Default

    ok,i understand, now i am going to try,thank you

  7. #7
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,839
    Blog Entries
    13

    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.?
    Last edited by ericgibbs; 30th July 2008 at 07:48 AM.
    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/

    Link to my Articles: http://www.electro-tech-online.com/a...icgibbs-55450/

  8. #8
    sahanaashwin Newbie
    Join Date
    Jul 2008
    Posts
    22

    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 04:44 AM.

  9. #9
    futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent futz Excellent
    Join Date
    Sep 2007
    Location
    Vancouver, B.C.
    Posts
    1,980

    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.
    Last edited by futz; 31st July 2008 at 06:36 AM.
    =========================
    Futz's Microcontrollers & Robotics
    =========================

  10. #10
    ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent ericgibbs Excellent
    Join Date
    Jan 2007
    Location
    Hampshire. England.
    Posts
    10,839
    Blog Entries
    13

    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/

    Link to my Articles: http://www.electro-tech-online.com/a...icgibbs-55450/

  11. #11
    sahanaashwin Newbie
    Join Date
    Jul 2008
    Posts
    22

    Default

    thank you eric

  12. #12
    meguitarist Newbie
    Join Date
    Feb 2009
    Location
    Malaysia
    Posts
    5

    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.

+ Reply to Thread

Similar Threads

  1. Timer0 Prescale
    By lord loh. in forum Micro Controllers
    Replies: 10
    Latest: 8th October 2008, 05:14 PM
  2. Using timer0
    By mrfunkyjay in forum Micro Controllers
    Replies: 6
    Latest: 11th July 2008, 09:43 AM
  3. TIMER0 - Have I got this right?
    By UTMonkey in forum Micro Controllers
    Replies: 8
    Latest: 14th December 2007, 10:08 PM
  4. PIC Timer0
    By Hesam Kamalan in forum Micro Controllers
    Replies: 7
    Latest: 29th October 2007, 02:32 PM
  5. Cant get timer0 to interrupt
    By 2camjohn in forum Micro Controllers
    Replies: 5
    Latest: 16th June 2004, 12:06 PM

Tags for this Thread