piclist 2000\06\07\024600a >
Thread: Frequency Scaling - remapping
www.piclist.com/techref/microchip/devices.htm?key=pic
picon face BY : Peter Betts email (remove spam text)



> If you can show us the program we might be able to point it out.

Here it is (hope is doesn't break the list etiquette)....
It works but it's a bit "wobbly"

;***************************************************************************
***
;                            PIC Hardware?
;***************************************************************************
***
; Use PIC16F84 and set radix system to decimal
       LIST P=16F84, R=DEC

;***************************************************************************
***
; Include header file
;***************************************************************************
***
#include "p16f84.inc"

;***************************************************************************
***
;                        Configuration Settings
;***************************************************************************
***
; Use XTAL clock, watch dog timer off and power up timer on.
       __config _XT_OSC & _WDT_OFF & _PWRTE_ON


;***************************************************************************
***
;                               equates
;***************************************************************************
***
; WORDS
count_in                equ     0x0c    ; Input Counter
count_out               equ     0x0d    ; Output Counter
new_output_counter      equ     0x0e    ; Next Output Counter Threshold
count_status            equ     0x0f    ; Status Condition of Counters
output_state            equ     0x10    ; State of the output pin

;BITS
trigger                 equ     0       ; Bit 0 of count_status
overflow                equ     1       ; Bit 1 of count_status

;GENERAL
frequency_input         equ     1       ; Bit RA1 of PortA

;***************************************************************************
***
;                            START of CODE
;***************************************************************************
***
       org     0x0                     ; reset vector
       goto    init                    ; jump over subroutines

       org     0x4                     ; Timer Interrupt Vector
       goto    timer_int               ; Goto this service routine

;***************************************************************************
***
;                              Subroutines
;***************************************************************************
***
toggle_output
       movlw   0x1
       xorwf   output_state,1          ; XOR output bit with 1 and store
back again
                                       ; This TOGGLES the state of the pin
from high to low.

       movf    new_output_counter,0    ; copy new_output_counter to 'w'
       movwf   count_out               ; Reset output counter
       goto    timer_int_end           ; Finished!

;---------------------------------------------------------------------------
---
no_toggle_output
       movlw   0x0
       movwf   output_state            ; Set output low.

       movlw   0x1                     ; else reload count_out with 1
       movwf   count_out
       goto    timer_int_end           ; Finished!

;---------------------------------------------------------------------------
---
remap_frequency
       nop                             ; Remap the input frequency value to
a new value
                                       ; Could just be a fixed ratio or..
                                       ; Use a look up table to scale it.
                                       ; Only (8bit) 256 counter values so
only 256 mapping points.
       goto    change_output_level

;***************************************************************************
***
;                       Interrupt Service Routine
;***************************************************************************
***
timer_int
       bcf     INTCON,T0IF             ; Clear pending interrupt flag

output_pulse                            ; Set output pin to correct state.
       movf    output_state,0          ; Load output_state into 'w'
       movwf   PORTB                   ; Output that bit

input_pulse                             ; "count_in = count_in + 1"
       incfsz  count_in,1              ; Increment input counter,
       goto    test_input_pin          ; If counter didn't wrap round to
zero, no_overflow

set_overflow                            ; count_in > 256 therefore a slow
input frequency
       bsf     count_status,overflow   ; Set OVERFLOW condition flag
       movlw   0x0
       movwf   new_output_counter

test_input_pin
       btfss   PORTA,frequency_input   ; Test input pin, high or low.
       goto    test_trigger            ; Is low so test if just triggered
from a high to low transition
       bsf     count_status,trigger    ; Input pin is high so set TRIGGER
condition flag and
       goto    change_output_level     ; then set next output pin state

test_trigger                            ; Test if trigger flag was set in
previous int'
       btfss   count_status,trigger    ; i.e. is a falling edge, high to
low.
       goto    change_output_level     ; If not then skip the rest and set
the condition of the next output pin state

       movlw   0x0                     ; Set 'w' to 0 incase overflow
condition is true
test_overflow                           ; Test if input counter overflowed
at any time when low or high
       btfss   count_status,overflow
       movf    count_in,0              ; No overflow so load count_in into
'w'
       movwf   new_output_counter      ; Load 'w' as the new output counter
threshold
       movlw   0x0
       movwf   count_in                ; Reset the input counter
       bcf     count_status,trigger    ; Reset the TRIGGER condition
       bcf     count_status,overflow   ; Reset the OVERFLOW condition, if
there was one!

remapping
       goto    remap_frequency

change_output_level
       decf    count_out,1             ; Decrement output counter,
"count_out = count_out - 1"
       btfss   STATUS,Z                ; Test ZERO flag is set
       goto    timer_int_end           ; count_out > 0

       movlw   0xffff
       andwf   new_output_counter,1
       btfss   STATUS,Z                ; Is new_output_counter = zero
       goto    toggle_output           ; if it isn't toggle_output
       goto    no_toggle_output

timer_int_end                           ; That's it!
       retfie                          ; Return from interrupt service
routine

;***************************************************************************
***
;                            initalisation
;***************************************************************************
***

init    clrf    PORTB                   ; set = 0
       bsf     STATUS,RP0              ; BANK1
       movlw   0ffh                    ; set porta as inputs
       movwf   TRISA
       movlw   00h                     ; set port b as outputs
       movwf   TRISB
       bcf     STATUS,RP0              ; BANK0

       movwf   count_in                ; Clear Input Counter
       movwf   count_out               ; Clear Output Counter
       movwf   new_output_counter      ; Clear Next Output Counter
Threshold
       movwf   count_status            ; Reset Counter Status
       movwf   output_state            ; Clear output pin state

       bsf     STATUS,RP0              ; BANK1
       movlw   0x0
       movwf   TMR0                    ; Reset Timer0
       movlw   b'10000011'             ; Prescaler of 1:16 (i.e. interrupt
every 3.9ms approx)
       movwf   OPTION_REG              ; Set Timer ON and use prescaler of
256
                                       ; (currently tunning from 5MHz XTAL)


;***************************************************************************
***
;                             main program
;***************************************************************************
***

main
       bcf     STATUS,RP0      ; BANK0
       bsf     INTCON,T0IE     ; Turn Timer Interrupts On
       bsf     INTCON,GIE      ; Turn Global Interrupts On

forever goto    forever

       end

<FFD4860870E0D2119F8C0008C7894980023D39D8@caeis01nok>

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Frequency Scaling - remapping

month overview.

new search...