單片機代理商管深圳英銳恩分享用PIC控制玩具車的整個制作方法。
英銳恩科技現提供服務產品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。
LIST P=12CE519, R=DEC
INCLUDE
__CONFIG _MCLRE_OFF & _CP_OFF & _WDT_ON & _IntRC_OSC
;--------------------------------------------------------------------
#DEFINE INOUT 00001000b ; GPIO port input/output definition
#DEFINE DCCIN GPIO,3 ; DCC input pin (GPIO,3 only input)
#DEFINE MOTOR1 GPIO,0 ; motor
#DEFINE MOTOR2 GPIO,1 ; output
#DEFINE LIGHT1 GPIO,4 ; light
#DEFINE LIGHT2 GPIO,5 ; output
#DEFINE LED GPIO,2 ; redlight, flickering firebox
;--------------------------------------------------------------------
CV8 EQU 0x0D ; Home Made Decoder
CV7 EQU 0x09 ; Version No. XX00 0000
; 00=Analog(0x00) 01=Stepper(0x40)
; 10=Fauli(0x80) 11=Other(0xC0)
TFLKON EQU 60 ; flickering firebox ontime
;--------------------------------------------------------------------
INDF EQU 0x00 ; Indirect Adressing
tmr0 EQU 0x01 ; Timer
PC EQU 0x02 ; Programm Counter
STATUS EQU 0x03 ; Status Register
FSR EQU 0x04 ; Special Funcion Register
OSCCAL EQU 0x05 ; Calibration Register
GPIO EQU 0x06 ; GP0-GP5 input/output
;--------------------
S31 EQU 0x07 ;
CONFIG EQU 0x08 ; 0=DIR 1=CONSIST 2=STEPOK/NOCV 5=FS126 6=BITHI 7=FLAG
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 ;
; EQU 0x14 ;
TFLKOFF EQU 0x15 ; flickering firebox offtime
FLAG EQU 0x16 ;
; EQU 0x17 ;
XOR EQU 0x18 ;
NOADR EQU 0x19 ; no adress since ...
PWM EQU 0x1A ; (Z1)
F1F8SET EQU 0x1B ;
IST EQU 0x1C ; actual speed
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 ; Output F1-F8 to GP4
CV51 EQU 0x3D ; Output F1-F8 to GP5
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
;.....................................................................
CALI BTFSS DCCIN ;-----------------
GOTO CALI ; Anfang
CALI1 BTFSC DCCIN ; finden
GOTO CALI1 ;-----------------
CLRF tmr0 ; Timer auf 0
CALI2 BTFSS DCCIN ;
GOTO CALI2 ;-----------------
MOVF tmr0,0 ; Timer
MOVWF BT5 ; sichern
MOVLW 34 ;-----------------
SUBWF BT5,0 ; LOW
BTFSC STATUS,CARRY ; BIT
GOTO CALI ;-----------------
MOVLW 27 ; <- Schleife +2
SUBWF BT5,0 ;
BTFSC STATUS,CARRY ; Hi BIT > 25?
GOTO CALEX ;-----------------
MOVLW 00100000b ;
ADDWF OSCCAL,1 ;
GOTO CALI ;-----------------
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
; -----------------------------------
CALEX MOVLW INOUT ; GP3 input, rest output
TRIS GPIO ; tristate port GPIO
MOVLW 11010110b ; no wakeup, no pullup, T0 int., hightolow, T0, 1:128
OPTION
COMF XOR,0 ; inverted stored for reset conditions (all zero)
XORWF SOLL,0 ; for brown out to start
XORWF IST,0 ; with same speed
BTFSC STATUS,ZERO ; all Bytes ok?
GOTO START ; -----------------------------------
CLEAR BCF ACKBIT ;
CLRF IST ; -----------------------------------
CLRF SOLL ; set all register to start condition
CLRF S31 ;
CLRF LO ;
CLRF HI ; set PWM to 1 to force a jump
CLRF PWM ; in STEP for stop condition
INCF PWM ; -----------------------------------
BEGIN
; 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 ;
;--------------------------------
BSF FSR,5 ;
MOVF CV64,0 ; copy config
BCF FSR,5 ; to
MOVWF FLAG ; lower ram page
;--------------------------------
MOVLW 0x09 ; adress = BT1 - 1
MOVWF FSR ; Pointer for indirect Adressing
GOTO START ;
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;..................................................................
SPDTAB MOVF S31,0 ;13
ADDWF PC ;14
RETLW 0 ;15-16
RETLW 0 ;
RETLW 0 ;
RETLW 0 ;
RETLW 2 ; 1
RETLW 3 ; 2
RETLW 5 ; 3
RETLW 7 ; 4
RETLW 10 ; 5
RETLW 13 ; 6
RETLW 18 ; 7
RETLW 22 ; 8
RETLW 28 ; 9
RETLW 34 ; 10
RETLW 41 ; 11
RETLW 48 ; 12
RETLW 56 ; 13
RETLW 65 ; 14
RETLW 74 ; 15
RETLW 84 ; 16
RETLW 95 ; 17
RETLW 106 ; 18
RETLW 118 ; 19
RETLW 131 ; 20
RETLW 144 ; 21
RETLW 158 ; 22
RETLW 173 ; 23
RETLW 188 ; 24
RETLW 204 ; 25
RETLW 220 ; 26
RETLW 237 ; 27
RETLW 255 ; 28
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
START CLRWDT ; 12
MOVLW 0xFF ; 13
MOVWF BT4 ; 14
MOVWF BT5 ; 15
MOVWF BT6 ; 16
MOVLW 0x09 ; Pointer for ind. Adressing 17
MOVWF FSR ; (BT1 - 1) 18
MOVWF MULT1 ; 9*8=72 76
RETLW 0 ; END MULTIP 77-78
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
START CLRWDT ; 12
MOVLW 0xFF ; 13
MOVWF BT4 ; 14
MOVWF BT5 ; 15
MOVWF BT6 ; 16
MOVLW 0x09 ; Pointer for ind. Adressing 17
MOVWF FSR ; (BT1 - 1) 18
STEPOK MOVLW 11111100b ; FF-03 7
MOVF HI,1 ; 8
BTFSC STATUS,ZERO ; 0? 9
GOTO LOWBIT ; low half of bit 10-11
ADDWF HI,0 ; BIT im Carry 11
DECFSZ PRE,1 ; Pre=0? 12
GOTO PRETEST ; 13-14
BTFSC STATUS,CARRY ; waiting for 0 14
GOTO BYTE ; Preample ok! 15-16
INCF PRE,1 ; 16
NOP ; 17
GOTO INTEST ; next Bit 18-19
PRETEST DECFSZ HI,0 ; HI=1? 15
BTFSC STATUS,CARRY ; 16
GOTO START19 ; no Preample! 17-18
GOTO INTEST ; 18-19
LOWBIT NOP ; 12
GOTO