timer interrupt help

Discussion in 'Assembly Language Programming (ALP) Forum' started by SENAN, Apr 3, 2008.

  1. SENAN

    SENAN New Member

    Apr 3, 2008
    Likes Received:
    Trophy Points:
    what im tryin to do is set up a timer to overflow every2 ms so this code will sample an ecg wave at 500Hz. Cant get my head around how to do this and any help would be greatly appreciated.

            rd1 equ P0.0           	;Read signal P0.0
            wr1 equ P0.1           	;Write signal P0.1
            cs equ P0.2           	;Chip Select P0.2
            intr equ P0.3         	;INTR signal P0.3
            adc_port EQU P2       	;ADC data pins P2
            adc_val EQU 30H       	;ADC read value stored here
            org 0H
    start:                    	             ;Start of Program
            acall conv            	;Start ADC conversion
            acall read            	            ;Read converted value
            mov P3,adc_val        	;Move the value to Port 3
            sjmp start            	             ;Do it again
    conv:                     	;Start of Conversion
            clr cs                	;Make CS low
            clr wr1                	;Make WR1 Low
            setb wr1               ;Make WR High
            setb cs               	;Make CS high
            jb intr,wait          	;Wait for INTR signal
            ret                   	;Conversion done
    read:                     	;Read ADC value
            clr cs                	;Make CS Low
            clr rd1                	;Make RD1 Low
            mov a,adc_port        	;Read the converted value
            mov adc_val,a         	;Store it in local variable
            setb rd1               	;Make RD1 High
            setb cs               	;Make CS High
            ret                   	;Reading done
    Last edited by a moderator: Apr 3, 2008
  2. erislover

    erislover New Member

    Aug 5, 2008
    Likes Received:
    Trophy Points:
    Timers are usually pretty complicated, loaded with features. For the most simple of uses, you will need to calculate how many clock cycles constitute two milliseconds. Depending on the processor, this might not be period*frequency. The 18F PICs, for example, operate at 1/4th the oscillator frequency. For a 32MHz oscillator, then, the PIC will operate at 8MHz. 0.002*8MHz is 16000 cycles. Choose a 16-bit timer, and you will need to load it with 0xFFFF - 16000 = 49535. Then it will overflow in 16000 counts, or 2 ms.

    Simple idea with polled interrupts:

    0) make sure the interrupt for this timer is not active. It should reset this way if your program isn't doing anything else you've not mentioned. If it is doing other things, be sure you're not using it for something else, or choose some other timer.
    1) stop the timer. Load 2ms_count into the timer. Clear the interrupt flag. Start the timer. (check your datasheet for the exact order of operations.)
    2) wait for overflow flag to be set. If not set, goto 2.
    3) start ADC conversion. This usually takes some time, maybe you can get away with a small nop-loop.
    4) read the value and do whatever with it.
    5) goto 1.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice