midsha1628
New Member
Hello all!
Thank you for viewing my question.
In my code, which is listed below, I can not seem to figure out why it does not work in the application(in the circuit, which spins a shaft w/ a magnetic sensor reading pulses) but sims fine with mplab v8.0. Note: Using PIC 16hv616.
I am using timer1 with gate to time the high time of the pulse, and compare this(count_5) to a threshold (average of counts 1-4). I have it branch to the three possible results (over, equal, or less) to light an LED telling me what it is doing based on the result.
In the application it always goes to the red LED even if the time is greater(green LED). As rpm increases the red led should light as time will get shorter when compared to the threshold and the green LED should come on as the rpm decreases since time is greater. Yellow LED should light when the rpm stays the same. Can anyone find something in the code that would keep this from working in the application?
Thank you.
-B
Thank you for viewing my question.
In my code, which is listed below, I can not seem to figure out why it does not work in the application(in the circuit, which spins a shaft w/ a magnetic sensor reading pulses) but sims fine with mplab v8.0. Note: Using PIC 16hv616.
I am using timer1 with gate to time the high time of the pulse, and compare this(count_5) to a threshold (average of counts 1-4). I have it branch to the three possible results (over, equal, or less) to light an LED telling me what it is doing based on the result.
In the application it always goes to the red LED even if the time is greater(green LED). As rpm increases the red led should light as time will get shorter when compared to the threshold and the green LED should come on as the rpm decreases since time is greater. Yellow LED should light when the rpm stays the same. Can anyone find something in the code that would keep this from working in the application?
Thank you.
-B
Code:
radix DEC
list p=16hv616 ; list directive to define processor
#include <p16hv616.inc> ; processor specific variable definitions
__config _CP_OFF & _WDT_OFF & _BOR_OFF & _MCLRE_ON & _PWRTE_ON & _IOSCFS_4MHZ & _INTRC_OSC_NOCLKOUT
;--------------------------------------------------------------------
FIRST_DIVIDE macro ;COUNT_1, COUNT_2 ;COUNT_1+1 COUNT_2+1
movf COUNT_1,W
addwf COUNT_2, W
movwf COUNTA
btfss STATUS, C
goto $+2
incf COUNT_1+1, F
movf COUNT_1+1,W
addwf COUNT_2+1,W ;RESULT OF THE ADD IS PLACED INTO THE WREG.
movwf COUNTA+1 ;WILL NOT TEST FOR HIGH BIT CARRY SINCE TIME PER PULSE WILL NOT BE THAT LONG.
rrf COUNTA+1, F
rrf COUNTA,F
endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;
;THIS CODE TAKES THE SUMS OF TWO 16 BIT NUMBERS AND DIVIDES BY TWO AND STORES THEM INTO REGISTER, 'COUNTB'.
SECOND_DIVIDE macro
movf COUNT_3,W
addwf COUNT_4, W
movwf COUNTB
btfss STATUS, C
goto $+2
incf COUNT_3+1, F
movf COUNT_3+1,W
addwf COUNT_4+1,W ;RESULT OF THE ADD IS PLACED INTO THE WREG.
movwf COUNTB+1 ;WILL NOT TEST FOR HIGH BIT CARRY SINCE TIME PER PULSE WILL NOT BE THAT LONG.
rrf COUNTB+1, F
rrf COUNTB,F
endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;
THRESHOLD_1 macro
;THIS CODE TAKES THE SUMS OF TWO 16 BIT COUNTS, (A+B), AND DIVIDES BY TWO AND STORES THEM INTO REGISTER, 'THRESHOLD1'and count0 and count1.
movf COUNTA,W
addwf COUNTB, W
movwf THRESHOLD1
btfss STATUS, C
goto $+2
incf COUNTA+1, F
movf COUNTA+1,W
addwf COUNTB+1, W
movwf THRESHOLD1+1
rrf THRESHOLD1+1, W
movwf THRESHOLD1+1
movwf COUNT1
rrf THRESHOLD1, W
movwf THRESHOLD1
movwf COUNT0
endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;
PULSE_SHIFT macro
movlw 00H
movwf COUNT_4
movf COUNT_3,W
movwf COUNT_4
movf COUNT_2,W
movwf COUNT_3
movf COUNT_1,W
movwf COUNT_2
endm
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
;VARIABLES
COUNT_1 equ 0X20
COUNT_2 equ 0X22
COUNT_3 equ 0X24
COUNT_4 equ 0X26
COUNT_5 equ 0X28
COUNTA equ 0X2A
COUNTB equ 0X2C
THRESHOLD1 equ 0X2E
COUNT equ 0X30
COUNT1 equ 0X32
COUNT0 equ 0X34
;----------------------------------------------------------------------------
;************************************************* *********************
;RESET_VECTOR CODE 0x000 ; processor reset vector
page
org 0
goto start
MAIN code
;-------------------------------------------------------
start
;--------------------------------------------------------------------
;INITIALIZATION (PORTA = GPIO)
errorlevel -302
bsf STATUS,5
clrf ANSEL
movlw B'00010000'
movwf TRISA
movlw B'10000011'
movwf OPTION_REG
movlw B'010000'
movwf WPUA
bcf STATUS,5
banksel PORTA
clrf PORTA
movlw B'11000001' ;USING TMR1 GATE. ;SET UP TIMER1 REGISTER FOR PRESCALER OF 1:1, INTERNAL CLOCK, BIT '0' STARTS/ STOPS TIMER1.
movwf T1CON
movlw B'00000010'
movwf CM2CON1
errorlevel +302
;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
;MAIN PROGRAM STARTS HERE
;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
;RESETS ALL 'COUNT' REGISTERS TO ZERO AT START OF PROGRAM.
movlw 00H
movwf COUNT
movlw 00H
movwf COUNT_1
movlw 00H
movwf COUNT_2
movlw 00H
movwf COUNT_3
movlw 00H
movwf COUNT_4
;-----------------------------------------------------------
clrf TMR1L
clrf TMR1H
HA
btfss PORTA,RA4
goto $-1
bsf PORTA,RA1
btfsc PORTA,RA4
goto $-1
bcf PORTA,RA1
movf TMR1H, W
movwf COUNT_1+1
movf TMR1L, W
movwf COUNT_1
clrf TMR1L
clrf TMR1H
;-----------------------------------------------------
btfss PORTA,RA4
goto $-1
bsf PORTA,RA1
btfsc PORTA,RA4
goto $-1
bcf PORTA,RA1
movf TMR1H, W
movwf COUNT_2+1
movf TMR1L, W
movwf COUNT_2
clrf TMR1L
clrf TMR1H
;---------------------------------------------------
btfss PORTA,RA4
goto $-1
bsf PORTA,RA1
;--------------------------------------------
FIRST_DIVIDE
;--------------------------------------------
btfsc PORTA,RA4
goto $-1
bcf PORTA,RA1
movf TMR1H, W
movwf COUNT_3+1
movf TMR1L, W
movwf COUNT_3
clrf TMR1L
clrf TMR1H
;----------------------------------------------
btfss PORTA,RA4
goto $-1
bsf PORTA,RA1
btfsc PORTA,RA4
goto $-1
bcf PORTA,RA1
movf TMR1H, W
movwf COUNT_4+1
movf TMR1L, W
movwf COUNT_4
clrf TMR1L
clrf TMR1H
;-------------------------------------------------------------
btfss PORTA,RA4
goto $-1
bsf PORTA,RA1
;--------------------------------------------
SECOND_DIVIDE
;--------------------------------------------
btfsc PORTA,RA4
goto $-1
bcf PORTA,RA1
movf TMR1H, W
movwf COUNT_5+1
movf TMR1L, W
movwf COUNT_5
;-----------------------------------------------------------------
DAL
;-----------------------------------------------------------------
THRESHOLD_1
PULSE_SHIFT
;-----------------------------------------------------------------
clrf TMR1L
clrf TMR1H
;************************************************* *************************
btfss PORTA,RA4
goto $-1
bsf PORTA,RA1
bcf PORTA,RA0
nop
bcf PORTA,RA2
nop
bcf PORTA,RA5
movf COUNT_5+1,W
subwf THRESHOLD1+1,W
btfss STATUS,Z
goto $+2
goto CAB
movf COUNT_5+1,W
subwf THRESHOLD1+1,W
btfsc STATUS,C
goto LESS ;LESS THAN,
goto GREATER ;GREATER THAN
BAB
movf THRESHOLD1,W
subwf COUNT_5,W
btfsc STATUS,C
goto GREATER
goto LESS
CAB
movf COUNT_5,W
subwf THRESHOLD1,W
btfss STATUS,Z
goto BAB
goto EQUAL
;------------------------------------------------------------------------------------
LESS ;RED LED
bsf PORTA,RA0
movf COUNT_5,W
movwf THRESHOLD1
movf COUNT_5+1,W
movwf THRESHOLD1+1
PULSE_SHIFT
goto WAKE
EQUAL ;YELLOW LED
bsf PORTA,RA2
movf COUNT_5,W
movwf THRESHOLD1
movf COUNT_5+1,W
movwf THRESHOLD1+1
PULSE_SHIFT
goto WAKE
GREATER ;GREEN LED
bsf PORTA,RA5
movf COUNT_5,W
movwf THRESHOLD1
movf COUNT_5+1,W
movwf THRESHOLD1+1
PULSE_SHIFT
goto WAKE
WAKE
btfsc PORTA,RA4
goto $-1
bcf PORTA,RA1
movf COUNT_5+1,W
movwf COUNT_1+1
movf COUNT_5+1,W
movwf COUNT_1+1
movf TMR1H, W
movwf COUNT_5+1
movf TMR1L, W
movwf COUNT_5
clrf TMR1L
clrf TMR1H
;---------------------------------------
FIRST_DIVIDE
SECOND_DIVIDE
;---------------------------------------
goto DAL
end
Last edited by a moderator: