📄 motor_28.asm
字号:
; motor_28.ASM - only 28 speed step with 20kHz PWM, no additional speed modifications
; Version: 2 Date: 03 March 2002
; Copyright (C) 1999 Georg Ziegler
; Calibration routine changed
; information about OSCCAL in pdf file rong!!
; linear speed increase if OSCCAL increment!
; BTFSS PC_OFFSET,EE_OK ;(BTFSC STATUS,ZERO) ERROR?
; NMRA-DCC decoder for core less motors based on PIC12CE519 with si9986 H-Bridge
; >18kHz PWM and 28us timestep for polling of DCC signal
; 14/28 speed step
; direct, paged and register mode programming
; EEPROM read/write routine from MICROCHIP PIC12CE519 documentation
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License as
; published by the Free Software Foundation; either version 2 of
; the License, or (at your option) any later version.
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
; Contact :
; Georg Ziegler
; Zwehrener Weg 23a
; 34121 Kassel
; georg.ziegler@wintershall.de
;--------------------------------------------------------------------
; 0AAA AAAA 01DL SSSS -> Lok Adress Direction Light & Speed
; 0AAA AAAA 100D DDDD -> Lok Function F1-F4 & FL
; 0AAA AAAA 1011 DDDD -> Lok Function F5-F8
; 0000 0000 -> Broadcast Adress for all Lok Decoder
; 10AA AAAA 1AAA CDDD -> 9 Bit Accessory on/off F1-F8
; 1011 1111 1000 CDDD -> Broadcast Command for all Accessory Decoder
; 1111 1111 0000 0000 -> Idle Packet for all Decoder
;--------------------------------------------------------------------
LIST P=12CE519, R=DEC
INCLUDE <P12CE519.INC>
__CONFIG _MCLRE_OFF & _CP_OFF & _WDT_ON & _IntRC_OSC
;--------------------------------------------------------------------
#DEFINE DCCIN 3 ; DCC input pin (GPIO,3 only input)
#DEFINE MOTOR1 00000001b ; motor
#DEFINE MOTOR2 00000010b ; output
#DEFINE LIGHT1 4 ; light
#DEFINE LIGHT2 5 ; output
#DEFINE LED 2 ; redlight, flickering firebox
;--------------------------------------------------------------------
CV8 EQU 0x0D ; Home Made Decoder
CV7 EQU 2 ; Version No. XX00 0000
;--------------------------------------------------------------------
INDF EQU 0x00 ; indirect adressing
tmr0 EQU 0x01
PC EQU 0x02
STATUS EQU 0x03 ; Status Register
FSR EQU 0x04 ;
OSCCAL EQU 0x05
GPIO EQU 0x06 ;
;--------------------------------------------------------------------
GPIOOUT EQU 0x07 ; 0=DIR 1=CONSIST 2=STEPOK/NOCV 5=FS126 6=BITHI 7=FLAG
CONFIG EQU 0x08 ;
SOLL EQU 0x09 ; speed to go
BT1 EQU 0x0A ; FSR auf 0x09
BT2 EQU 0x0B ;
BT3 EQU 0x0C ;
BT4 EQU 0x0D ;
BT5 EQU 0x0E ;
BT6 EQU 0x0F ; FSR auf 0x10
;--------------------
; EQU 0x10 ;
; EQU 0x11 ;
ACC EQU 0x12 ;
DEC EQU 0x13 ;
PWM_ON EQU 0x14 ;
PWM EQU 0x15 ;
PWM1 EQU 0x16 ;
PWM2 EQU 0x17 ;
XOR EQU 0x18 ;
NOADR EQU 0x19 ;
; EQU 0x1A ;
F1F8SET EQU 0x1B ;
; EQU 0x1C ;
PRE EQU 0x1D ; Preample 12 BIT
LO EQU 0x1E ; low bit counter
HI EQU 0x1F ; hi bit counter
;--------------------
CV1 EQU 0x30 ; Primary Adress
CV2 EQU 0x31 ; Vstart
CV3 EQU 0x32 ; Acceleration Rate
CV4 EQU 0x33 ; Deceleration Rate
CV29 EQU 0x34 ; 0=DIR 1=14/28 2=PWRSRC 3=ADVACK 4=SPDTAB 5=LADRE 6= 7=ACCDEC
MAPCV EQU 0x35 ; rom CV's copied to here
CV5 EQU 0x36 ; Vhigh
CV6 EQU 0x37 ; Vmid
CV17 EQU 0x38 ; Extended Adress Low Byte
CV18 EQU 0x39 ; Extended Adress High Byte
CV19 EQU 0x3A ; Consist Adress
CV49 EQU 0x3B ; Output F1-F8 to GP2 (CV49-64)
CV50 EQU 0x3C ; reserved for FL/RL to map F1-8
CV51 EQU 0x3D ; space (page register)
CV55 EQU 0x3E ; Vmax 2
CV64 EQU 0x3F ; configuration byte
;--------------------
#DEFINE FLOFF CV64,0 ; front light switch with cv50 (GP4)
#DEFINE RANGIER CV64,0 ; low activ, mapped with FLOFF
#DEFINE BLOFF CV64,1 ; back light switch with cv51 (GP5)
#DEFINE SPEED2 CV64,1 ; low activ, mapped with BLOFF
#DEFINE RDLTON CV64,2 ; red light on (back light)
#DEFINE BRKON CV64,3 ; brake light on
#DEFINE HI2LOFS CV64,4 ; convert 126FS to 28FS with Vmin/Vmid/Vmax
#DEFINE FLACKON CV64,5 ; flickering firebox
#DEFINE LITAUTO CV64,6 ; firebox open/close automatically
#DEFINE REDLT CV64,7 ; is set by program
;---------------------
CARRY EQU 0
ZERO EQU 2
SEITE EQU 5
#DEFINE DIR CONFIG,0 ;
#DEFINE CONSIST CONFIG,1 ;
#DEFINE STEPOK? CONFIG,2 ;
#DEFINE NOCV CONFIG,2 ; is mapped with STEPOK?
#DEFINE ROMCV CONFIG,3 ; is mapped with FLACKER
#DEFINE FLACKER CONFIG,3 ;
#DEFINE BRAKE CONFIG,4 ;
#DEFINE FS126 CONFIG,5 ;
#DEFINE BITHI CONFIG,6 ;
#DEFINE FLAGS2 CONFIG,7 ;
#DEFINE LITOPEN FLAG,6 ; for lit open/close (copy from CV64)
#DEFINE DIRINV CV29,0 ; direction inverted (+light)
#DEFINE FS28 CV29,1 ; 14/28 speed step
#DEFINE LIINV CV29,2 ; light inverted (PWRSRC never used)
#DEFINE STAB CV29,4 ; speedtab / CV2,CV5&CV6
#DEFINE LADRE CV29,5 ; long adress enable
#DEFINE PROGBIT CV29,6 ;
#DEFINE PROGBT1 CV29,7 ;
#DEFINE ACKBIT STATUS,6 ; never overwriten
;--------------------------------------------------------------------
ORG 0 ; Reset Vector
INIT MOVLW 00000000b ; start value (information about OSCCAL in pdf file rong!! linear speed increase!)
MOVWF OSCCAL ; write calibration value
MOVLW 11010001b ; no wakeup, no pullup, T0 int., hightolow, T0, 1:4
OPTION
;.....................................................................
CALI BTFSS GPIO,DCCIN ;-----------------
GOTO CALI ; Anfang
CALI1 BTFSC GPIO,DCCIN ; finden
GOTO CALI1 ;-----------------
CLRF tmr0 ; Timer auf 0
CALI2 BTFSS GPIO,DCCIN ;
GOTO CALI2 ;-----------------
MOVF tmr0,0 ; Timer
MOVWF BT5 ; sichern
MOVLW 34 ;-----------------
SUBWF BT5,0 ; LOW
BTFSC STATUS,CARRY ; BIT
GOTO CALI ;-----------------
MOVLW 30 ; <- Schleife + 2
SUBWF BT5,0 ;
BTFSC STATUS,CARRY ; Hi BIT > 28?
GOTO CALEX ;-----------------
MOVLW 00001000b ;
ADDWF OSCCAL,1 ;
GOTO CALI ;-----------------
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
; -----------------------------------
CALEX COMF XOR,0 ; inverted stored for reset conditions (all zero)
XORWF SOLL,0 ; for brown out to start
XORWF GPIOOUT,0 ; with same speed
BTFSC STATUS,ZERO ; all Bytes ok?
GOTO START ; -----------------------------------
CLRF LO ;
CLRF HI ;
CLRF GPIOOUT ;
CLRF PWM ;
CLRF PWM1 ;
CLRF PWM2 ;
CLRF CONFIG ; test
; EEPROM ............................................................
LOADREG MOVLW 0x30 ;
MOVWF FSR ; Pointer for indirect Adressing
CLRF EEADDR ;
EEREAD BSF STATUS,SEITE ; set to 200H begin EEPROM
CALL READ_RANDOM ;
BCF STATUS,SEITE ; auf ROM seite 0
BTFSS PC_OFFSET,EE_OK ;(BTFSC STATUS,ZERO) ERROR?
GOTO EEREAD ;
MOVF EEDATA,0 ;
MOVWF INDF ;
MOVLW 0x3F ;
XORWF FSR,0 ;
ANDLW 00111111b ;
BTFSC STATUS,ZERO ;
GOTO EEEXIT ;
INCF FSR,1 ;
INCF EEADDR ;
GOTO EEREAD ;
EEEXIT MOVF CV3,0 ;
BTFSC STATUS,ZERO ;
IORLW 1 ;
XORLW 0xFF ; complement W
BCF FSR,5 ; rampage back to 0x10..
MOVWF ACC ;
BSF FSR,5 ; rampage to 0x30..
MOVF CV4,0 ;
BTFSC STATUS,ZERO ;
IORLW 1 ;
XORLW 0xFF ; complement W
BCF FSR,5 ; rampage back to 0x10..
MOVWF DEC ;
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
ANALOG MOVLW 00111111b ; all Input
TRIS GPIO ; tristate port GPIO
START MOVLW 0xFF ; 15
MOVWF BT4 ; 16
MOVWF BT5 ; 17
MOVWF BT6 ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
CLRF HI ; 21
CLRWDT ; WatchDog Timer zur點ksetzen 22
MOVLW 0x09 ; Pointer f黵 ind. Adressierung 23
MOVWF FSR ; 24
MOVLW 11 ; 25
MOVWF PRE ; PRE auf 10 Bit f黵 Preample 26
;-------------
INTEST CALL IN ; 27-28
BTFSC STEPOK? ; 4.
GOTO STEPOK ; 5.-6
;......................................................................
#INCLUDE PWM.INC ; 6-28
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BTFSC GPIO,DCCIN ; ########### input ########## 1 (29)
BSF BITHI ; 2-3
GOTO $+1 ; 3-4
GOTO $+1 ; 5-6
STEPOK NOP ; 7
MOVLW 11111100b ; FF-03 8
MOVF HI,1 ; 9
BTFSC STATUS,ZERO ; 0? 10
GOTO LOWBIT ; low half of bit 11-12
ADDWF HI,1 ; BIT im Carry 12
CLRF HI ; 13
DECFSZ PRE,1 ; Pre=0? 14
GOTO PRETEST ; 15-16
NOP ; 16
NOP ; 17
NOP ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BTFSC STATUS,CARRY ; warte auf 0 21
GOTO BYTE ; Preample erf黮lt! 22-23
BCF STEPOK? ; 23
INCF PRE,1 ; 24
PREBACK GOTO INTEST ; n鋍hstes Bit 25-26
PRETEST NOP ; 17
NOP ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
NOP ; 21
BTFSS STATUS,CARRY ; 22
GOTO PREBACK ; 23-24
GOTO START26 ; Preample nicht erf黮lt! 24-25
LOWBIT NOP ; 13
NOP ; 14
NOP ; 15
NOP ; 16
BTFSC BITHI ; 17
INCF HI ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
NOP ; 21
BCF BITHI ; 22
NOP ; 23
INCFSZ LO,1 ; 24
GOTO INTEST ; 25-26
GOTO ANALOG ; >10000uS 26
PWMTAB ;..............................................................
#INCLUDE PWM.INC ; 6-28
RETLW 0 ; 1-2
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
IN BTFSS GPIO,DCCIN ; ########### input ########## 1. (29)
RETLW 0 ; 2.-3.
INCF HI ; 3.
CLRF LO ; 4.
BSF STEPOK? ; 5.
;......................................................................
#INCLUDE PWM.INC ; 6-28
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BTFSS GPIO,DCCIN ; ########### input ########## 1 (29)
RETLW 0 ; FF-03 2-3
INCF HI ; 3
NOP ; ----------------------------- 4
MOVF GPIOOUT,0 ; for brown out 5
XORWF SOLL,0 ; to start with 6
MOVWF XOR ; same speed 7
COMF XOR,1 ; ----------------------------- 8
NOP ; 9
NOP ; 10
NOP ; 11
NOP ; 12
NOP ; 13
NOP ; 14
NOP ; 15
NOP ; 16
NOP ; 17
NOP ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
NOP ; 21
NOP ; 22
NOP ; 23
NOP ; 24
BCF STEPOK? ; 25
INCFSZ HI,0 ; 26
GOTO IN ; 27-28
GOTO ANALOG ; 28-1
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;.....................................................................
NEXT RLF INDF,1 ; 1.Byte zusammenbauen 17
NOP ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
NOP ; 21
NOP ; 22
NOP ; 23
BCF STEPOK? ; 24
GOTO INTEST1 ; n鋍hstes Bit 25-26
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LOWBIT1 MOVLW 0x10 ; ----------------------------- 13
SUBWF FSR,0 ; 14
ANDLW 00000111b ; mehr als 6 BYTE gelesen 15
BTFSC STATUS,ZERO ; neu beginnen! 16
GOTO START19 ; ----------------------------- 17-18
BCF STEPOK? ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BTFSC BITHI ; 21
INCF HI ; 22
BCF BITHI ; 23
INCFSZ LO,1 ; 24
GOTO INTEST1 ; 25-26
GOTO ANALOG ; 26-27
BYTE MOVLW 9 ; PRE auf 8 Bit f黵 1.Byte 24
MOVWF PRE ; 25
INCF FSR,1 ; Pointer f黵 ind. Adressierung 26
INTEST1 CALL IN ; 27-28
BTFSC STEPOK? ; 4.
GOTO STEPOK1 ; 5.-6
;......................................................................
#INCLUDE PWM.INC ; 6-28
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BTFSC GPIO,DCCIN ; ########### input ########## 1 (29)
BSF BITHI ; 2
NOP ; 3
NOP ; 4
NOP ; 5
NOP ; 6
STEPOK1 NOP ; 7
MOVLW 11111100b ; FF-03 8
MOVF HI,1 ; 9
BTFSC STATUS,ZERO ; 0? 10
GOTO LOWBIT1 ; low half of bit 11-12
ADDWF HI,1 ; BIT im Carry 12
CLRF HI ; 13
DECFSZ PRE,1 ; Pre=0? 14
GOTO NEXT ; 15-16
NOP ; 16
NOP ; 17
NOP ; 18
;.PWM2................................................................
MOVF PWM2,0 ; 19
MOVWF GPIO ; 20
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
BTFSC STATUS,CARRY ; 21
GOTO BYTE ; 9.Bit=0 22-23
COMF BT1,1 ; 9.Bit=1 (BYTE invertieren) 23
COMF BT2,1 ; (BYTE invertieren) 24
COMF BT3,1 ; (BYTE invertieren) 25
COMF BT4,1 ; (BYTE invertieren) 26
COMF BT5,1 ; (BYTE invertieren) 27
COMF BT6,1 ; (BYTE invertieren) 28
MOVLW (1<<DCCIN) ; GP3 input, rest output 1.
TRIS GPIO ; set port GPIO 2.
CLRF NOADR ; for IB with M* 3.
;.....................................................................
CALL PWMTAB ; 4.-2
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;.....................................................................
; Auswertung der Fahrstufen und PWM_ON setzen.
;---------------------------------------------------------------------
MOVLW 00010111b ; (20+3) 3
SUBWF SOLL,0 ; 4
BTFSS STATUS,CARRY ; <20 5
GOTO PWM1_19 ; 6-7
IORLW 11111000b ; 8
MOVWF PWM_ON ; 9
COMF PWM_ON,1 ; timer 10
MOVLW 11100001b ; 11
ADDWF SOLL,0 ; 12
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -