📄 smtkit.asm
字号:
list p=12C509, ;Enter device name
include <p12c509.inc>
__CONFIG _CP_OFF & _WDT_ON & _IntRC_OSC & _MCLRE_OFF
GP0 equ 0
GP1 equ 1
GP2 equ 2
GP3 equ 3
GP4 equ 4
GP5 equ 5
TxD equ 0x04 ;Transmitter pin position
TxD_Direction equ B'00001111' ;GPIO direction
BitDuration equ .52 ;Bit duration x2 ms (not more than 128)
;*************************************************************
;* WORK ZONE *
;*************************************************************
ORG 0x07 ;FREE_RAM
HIGH_0_CNT res .1 ;Low level (logical 0) of the signal - High Byte
LOW_0_CNT res .1 ;Low level (logical 0) of the signal - Low Byte
HIGH_1_CNT res .1 ;High level (logical 1) of the signal - High Byte
LOW_1_CNT res .1 ;High level (logical 1) of the signal - Low Byte
OLD res .1 ;Old TMR0 value - used in the Masurement routine
NEW res .1 ;New TMR0 value - used in the Masurement routine
LOOP_C res .1 ;Repetitions of the non-calculated measurements counter
Counter res .1 ;Send a character via RS232 routine
;8 bit Binary to BCD conversion routine
RS232_OUT res .1 ;Send a character via RS232 routine
BIN_IN res .2 ;8 bit Binary to BCD conversion routine
BCD_OUT res .2 ;8 bit Binary to BCD conversion routine
INTEGRATION_C res .1 ;Repetitions of the calculated measurements counter
Temperature res .2 ;Calculated measurements integration accumulator
temp res .1 ;Help sell for different routines
MASK res .1 ;Present sensors mask
Pointer res .1 ;Sensor measurement sequence pointer
CheckPoint res .1 ;Sensor find sequence pointer
RS232Delay res .1 ;RS232 bit transmission delay
;Calculation routine working sels - Non-overlapping sels from the other routines are used!
AARGB0 equ HIGH_1_CNT
AARGB1 equ LOW_1_CNT
AARGB2 equ OLD
AARGB3 equ NEW
BARGB0 equ HIGH_0_CNT
BARGB1 equ LOW_0_CNT
REMB0 equ BIN_IN
REMB1 equ BIN_IN+1
TEMP equ temp
LOOPCOUNT equ Counter
MSB equ 7
LSB equ 0
;Carry/Borrow and Zero bit definitions
#define CARRY STATUS,C
#define ZERO STATUS,Z
org 0x00
;Calibrate Osc
movwf OSCCAL
bsf STATUS,PA0
goto EndOfProgramm
xorlw 0xFF
xorlw 0xFF
xorlw 0xFF
;************************************************************************
;* Send a character in W (RS232_OUT) via ASIA routine *
;************************************************************************
SendChar
movwf RS232_OUT
movlw .11 ;Initialize Bit Counter
movwf Counter
bcf CARRY ;Start bit
clrwdt
NextBit
btfss CARRY
bcf GPIO,TxD
btfsc CARRY
bsf GPIO,TxD
movf RS232Delay,w
movwf TMR0
bsf CARRY ;Prepare Stop bit
rrf RS232_OUT,f
Wt_del btfss TMR0,0x07
goto Wt_del
decfsz Counter
goto NextBit
bsf GPIO,TxD ;Stop bit
retlw 0
;********************************************************************
; Binary To BCD Conversion Routine
; This routine converts a 16 Bit binary Number to a 4 Digit
; BCD Number.
;
; The 16 bit binary number is input in locations BIN_IN and
; BIN_IN+1 with the high byte in BIN_IN.
; The 4 digit BCD number is returned in BCD_OUT,BCD_OUT+1 with BCD_OUT
; containing the MSD in its left most nibble.
;
;*******************************************************************;
BIN_BCD
bcf CARRY
movlw .16
movwf Counter
clrf BCD_OUT
clrf BCD_OUT+1
loop16 rlf BIN_IN+1
rlf BIN_IN
rlf BCD_OUT+1
rlf BCD_OUT
decfsz Counter
goto adjDEC
return
adjDEC
movlw BCD_OUT+1
movwf FSR
call adjBCD
movlw BCD_OUT
movwf FSR
call adjBCD
goto loop16
adjBCD movlw 3
addwf 0,W
movwf temp
btfsc temp,3 ; test if result > 7
movwf 0
movlw 30
addwf 0,W
movwf temp
btfsc temp,7 ; test if result > 7
movwf 0 ; save as MSD
RETLW 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Send in ASCII Routine
; The number in BCD_OUT is send via RS232 in ASCII format
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SND_ASCII
;Convert in ASCII and send
swapf BCD_OUT,W ;Load
andlw 0x0F ;Mask
btfsc ZERO ;Check for leading 0
goto LEAD_0
movwf temp
movlw 0x30 ;Convert in ASCII
addwf temp,w
call SendChar
LEAD_0
movf BCD_OUT,W ;Load
andlw 0x0F ;Mask
movwf temp
movlw 0x30 ;Convert in ASCII
addwf temp,w
call SendChar
retlw 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Send in ASCII with Decimal Point Routine
; The number in BCD_OUT is send via RS232 in ASCII format - Decimal Point between the digits
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SND_ASCII_2
;Convert in ASCII and send
swapf BCD_OUT,W ;Load
andlw 0x0F ;Mask
movwf temp
movlw 0x30 ;Convert in ASCII
addwf temp,w
call SendChar
movlw '.'
call SendChar
movf BCD_OUT,W ;Load
andlw 0x0F ;Mask
movwf temp
movlw 0x30 ;Convert in ASCII
addwf temp,w
call SendChar
retlw 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Sensor measurements - wait for 0, measure the Low level, measure the High level
; The routines are repeared for every input ( 4 times)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SENSOR0A
;wait for 0 in order to begin
init_a0 btfss GPIO,GP0 ;Wait for 1
goto init_a0
init_b0 btfsc GPIO,GP0 ;Wait for 0
goto init_b0
movf TMR0,W
movwf OLD
retlw 0
SENSOR0B
btfss GPIO,GP0 ;Wait for 1
goto SENSOR0B
movf TMR0,W
movwf NEW
retlw 0
SENSOR0C
btfsc GPIO,GP0 ;Wait for 0
goto SENSOR0C
movf TMR0,W
movwf NEW
retlw 0
SENSOR1A
;wait for 0 in order to begin
init_a1 btfss GPIO,GP1 ;Wait for 1
goto init_a1
init_b1 btfsc GPIO,GP1 ;Wait for 0
goto init_b1
movf TMR0,W
movwf OLD
retlw 0
SENSOR1B
btfss GPIO,GP1 ;Wait for 1
goto SENSOR1B
movf TMR0,W
movwf NEW
retlw 0
SENSOR1C
btfsc GPIO,GP1 ;Wait for 0
goto SENSOR1C
movf TMR0,W
movwf NEW
retlw 0
SENSOR2A
;wait for 0 in order to begin
init_a2 btfss GPIO,GP2 ;Wait for 1
goto init_a2
init_b2 btfsc GPIO,GP2 ;Wait for 0
goto init_b2
movf TMR0,W
movwf OLD
retlw 0
SENSOR2B
btfss GPIO,GP2 ;Wait for 1
goto SENSOR2B
movf TMR0,W
movwf NEW
retlw 0
SENSOR2C
btfsc GPIO,GP2 ;Wait for 0
goto SENSOR2C
movf TMR0,W
movwf NEW
retlw 0
SENSOR3A
;wait for 0 in order to begin
init_a3 btfss GPIO,GP3 ;Wait for 1
goto init_a3
init_b3 btfsc GPIO,GP3 ;Wait for 0
goto init_b3
movf TMR0,W
movwf OLD
retlw 0
SENSOR3B
btfss GPIO,GP3 ;Wait for 1
goto SENSOR3B
movf TMR0,W
movwf NEW
retlw 0
SENSOR3C
btfsc GPIO,GP3 ;Wait for 0
goto SENSOR3C
movf TMR0,W
movwf NEW
retlw 0
;****************************************
;* Send LF, CR routine *
;****************************************
SEND_LF_CR
movlw 0x0D
call SendChar
movlw 0x0A
call SendChar
retlw b'10000'
;****************************************
;* 32 bit / 16 bit routine *
;****************************************
include <calc.asm>
;****************************************
;* Send U (0x55) routine *
;****************************************
CheckRS232
movlw 0x55
call SendChar
goto CheckRS232
;****************************************
;* System configuration *
;****************************************
Begin
movlw .136-BitDuration
movwf RS232Delay
bsf GPIO,TxD ;Inactive RS232
movlw TxD_Direction
tris GPIO
CLRF TMR0
MOVLW b'10000000' ;Internal Pull-ups enabled, Prescaler attached to TMR0,
;Internal clock / 2 for TMR0
OPTION
;************************************************************************
;* Find the position of the sensors connected in the moment *
;************************************************************************
;If RESET preload mask value and wait WDT reset (initial delay)
btfsc STATUS,NOT_TO
goto FirstPower
CLRWDT
;WDT time-out occur
btfsc CheckPoint,GP0
goto CheckGP0
btfsc CheckPoint,GP1
goto CheckGP1
btfsc CheckPoint,GP2
goto CheckGP2
btfsc CheckPoint,GP3
goto CheckGP3
movf MASK,f
btfsc ZERO
goto CheckRS232
goto CheckEnd
FirstPower
clrwdt
clrf MASK
movlw b'0001'
movwf CheckPoint
wt_wdt goto wt_wdt ;Initial delay
CheckGP0
bcf MASK,GP0
movlw b'0010'
movwf CheckPoint
btfsc GPIO,GP0
goto CheckGP0
;There is a sensor on GP0
bsf MASK,GP0
CheckGP1
bcf MASK,GP1
movlw b'0100'
movwf CheckPoint
btfsc GPIO,GP1
goto CheckGP1
;There is a sensor on GP1
bsf MASK,GP1
CheckGP2
bcf MASK,GP2
movlw b'1000'
movwf CheckPoint
btfsc GPIO,GP2
goto CheckGP2
;There is a sensor on GP2
bsf MASK,GP2
CheckGP3
bcf MASK,GP3
movlw b'10000'
movwf CheckPoint
btfsc GPIO,GP3
goto CheckGP3
;There is a sensor on GP3
bsf MASK,GP3
CheckEnd
CLRWDT
movlw b'0001'
movwf CheckPoint
clrf Pointer
;****************************************************************
;* Sensors connected in the moment measurement cycle *
;****************************************************************
new_sensor_M
bcf CARRY
rrf Pointer,w
andlw 0x0F ;Test Z bit
btfsc ZERO
; movlw b'1000'
call SEND_LF_CR
movwf Pointer
andwf MASK,w
btfsc ZERO
goto new_sensor_M
movlw .32
movwf INTEGRATION_C
clrf Temperature
clrf Temperature+1
;****************************************************************
;* Raw measurement of the Low and High level pulses *
;****************************************************************
new_32_M
; Low Level=0;
; High Level=0;
clrf LOW_0_CNT
clrf HIGH_0_CNT
clrf LOW_1_CNT
clrf HIGH_1_CNT
; LOOP_C=42;
movlw .41
movwf LOOP_C
btfsc Pointer,GP0
call SENSOR0A
btfsc Pointer,GP1
call SENSOR1A
btfsc Pointer,GP2
call SENSOR2A
btfsc Pointer,GP3
call SENSOR3A
new_check
btfsc Pointer,GP0
call SENSOR0B
btfsc Pointer,GP1
call SENSOR1B
btfsc Pointer,GP2
call SENSOR2B
btfsc Pointer,GP3
call SENSOR3B
; OLD=NEW-OLD;
movf OLD,w
subwf NEW,w
; Low Level=Low Level+OLD;
addwf LOW_0_CNT,f
btfsc CARRY
incf HIGH_0_CNT,f
; OLD=NEW;
movf NEW,w
movwf OLD
btfsc Pointer,GP0
call SENSOR0C
btfsc Pointer,GP1
call SENSOR1C
btfsc Pointer,GP2
call SENSOR2C
btfsc Pointer,GP3
call SENSOR3C
; OLD=NEW-OLD;
movf OLD,w
subwf NEW,w
; High Level=High Level+OLD;
addwf LOW_1_CNT,f
btfsc CARRY
incf HIGH_1_CNT,f
; OLD=NEW;
movf NEW,w
movwf OLD
CLRWDT
decfsz LOOP_C,f
goto new_check
;Calculation routine
;High Level = High Level * $10000
clrf AARGB2
clrf AARGB3
;T=Low Level+High Level
movf AARGB1,w
addwf BARGB1,f
btfsc CARRY
incf BARGB0
movf AARGB0,w
addwf BARGB0,f
CALL FXD3216U
movf AARGB2,w
movwf AARGB1
movf AARGB3,w
movwf AARGB2
clrf AARGB3
movlw .31
movwf BARGB0
clrf BARGB1
CALL FXD3216U
;Substract 680 ($2A8)
movlw 0xA8
subwf AARGB3,f
btfss CARRY
decf AARGB2
movlw 0x02
subwf AARGB2,f
;The result is ready in AARGB2,AARGB3
;Temperature=Temperature + New
movf AARGB3,w
addwf Temperature+1,f
btfsc CARRY
incf Temperature
movf AARGB2,w
addwf Temperature,f
decfsz INTEGRATION_C,f
goto new_32_M
;Temperature=Temperature/32
rrf Temperature,f
rrf Temperature+1,f
rrf Temperature,f
rrf Temperature+1,f
rrf Temperature,f
rrf Temperature+1,f
rrf Temperature,f
rrf Temperature+1,f
rrf Temperature,f
rrf Temperature+1,f
;If positive, the first 5 bits should be 0; If negative, the first 5 bits should be 1 (sign bit extention).
bsf STATUS,PA0
btfss Temperature,2
goto Positive
btfsc Temperature,1
goto Neg_Result
Positive
movlw 0x07 ;Sign bit extention
andwf Temperature,f
call Coef_correct
movlw 0x04 ;Add 0.4 degree
addwf AARGB3,f
btfsc CARRY
incf AARGB2
goto Pos_Result
Neg_Result
movlw 0xF8 ;Sign bit extention
iorwf Temperature,f
comf Temperature,f ;Covert the negatiive value from complementary code
comf Temperature+1,f
incfsz Temperature+1,f
goto Correction
incf Temperature,f
Correction
call Coef_correct
movlw 0x04 ;Substract 0.4 degree
subwf AARGB3,f
btfss CARRY
decf AARGB2
btfss AARGB2,7
goto wr_min
comf AARGB2,f ;Covert the negatiive value from complementary code
comf AARGB3,f
incfsz AARGB3,f
goto Pos_Result
incf AARGB2,f
goto Pos_Result
wr_min
;if 0 do not write
movf AARGB2,f
btfss ZERO
goto wr_min_OK
movf AARGB3,f
btfss ZERO
goto wr_min_OK
goto Pos_Result
wr_min_OK
movlw '-' ;Write "-" on the display
bcf STATUS,PA0
call SendChar
Pos_Result
bcf STATUS,PA0
movf AARGB2,w ;Temperature,w
movwf BIN_IN
movf AARGB3,w ;Temperature+1,w
movwf BIN_IN+1
call BIN_BCD
call SND_ASCII
movf BCD_OUT+1,w
movwf BCD_OUT
call SND_ASCII_2
movlw ' '
call SendChar
goto new_sensor_M
Coef_correct
movf Temperature,w
movwf AARGB0
movf Temperature+1,w
movwf AARGB1
clrf AARGB2
clrf AARGB3
movlw 0xFE
movwf BARGB0
movlw 0x38
movwf BARGB1
bcf STATUS,PA0
call FXD3216U
bsf STATUS,PA0
retlw 0
EndOfProgramm
bcf STATUS,PA0
goto Begin
End
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -