;RADLED.ASM #DEFINE PAGE0 BCF $03,5 #DEFINE PAGE1 BSF $03,5 INDF: .EQU 0 PCL: .EQU 2 STATUS: .EQU 3 FSR: .EQU 4 PORTA: .EQU 5 PORTB: .EQU 6 TRISA: .EQU 5 TRISB: .EQU 6 W: .EQU 0 ;Working F: .EQU 1 ;File C: .EQU 0 ;Carry Z: .EQU 2 ;Zero EEDATA: .EQU $08 ;eeprom data value register EECON1: .EQU $08 ;eeprom write register 1 EEADR: .EQU $09 ;eeprom data address register EECON2: .EQU $09 ;eeprom write register 2 WR: .EQU 1 ;eeprom write initiate flag WREN: .EQU 2 ;eeprom write enable flag RD: .EQU 0 ;eeprom read enable flag INTCON: .EQU $0B TEMP .EQU $0C COUNT .EQU $0D V0 .EQU $0E V1 .EQU $0F D0 .EQU $10 D1 .EQU $11 D2 .EQU $12 REF .EQU $13 Ch0Low .EQU $14 Ch0High .EQU $15 Ch1Low .EQU $16 Ch1High .EQU $17 Ch2Low .EQU $18 Ch2High .EQU $19 Ch3Low .EQU $1A Ch3High .EQU $1B MOT .EQU $1C REG .EQU $1D VAL .EQU $1E BIT .EQU $1F INTEN .EQU $20 AdcLsb .EQU $21 AdcMsb .EQU $22 RefType .EQU $23 DelayMs .EQU $24 TState .EQU $25 Stable .EQU $26 AdcLow0 .EQU $27 AdcHigh0 .EQU $28 AdcLow1 .EQU $29 AdcHigh1 .EQU $2A AdcLow2 .EQU $2B AdcHigh2 .EQU $2C AdcLow3 .EQU $2D AdcHigh3 .EQU $2E .ORG 4 .ORG 5 RESET: PAGE1 movlw $18 movwf TRISA movlw %11110010 movwf TRISB PAGE0 clrf PORTA ; movlw %00001000 movwf PORTB ; clrf RefType ; call READEE movf REF,W movwf AdcLow0 clrf AdcHigh0 ; clrf Ch0Low clrf Ch0High clrf Ch1Low clrf Ch1High clrf Ch2Low clrf Ch2High clrf Ch3Low clrf Ch3High ; clrf AdcLow1 clrf AdcHigh1 clrf AdcLow2 clrf AdcHigh2 clrf AdcLow3 clrf AdcHigh3 ; movlw $80 movwf MOT ; movlw 12 movwf INTEN ; movlw $FF movwf DelayMs clrf TState movlw 10 movwf Stable LP: call INITLED call WRITEREF call WRITETEM call WRITEMOT call Wait goto LP STOP: goto STOP GETSEG: andlw $F addwf PCL,F retlw %01111110 ; 0 retlw %00110000 ; 1 retlw %01101101 ; 2 retlw %01111001 ; 3 retlw %00110011 ; 4 retlw %01011011 ; 5 retlw %01011111 ; 6 retlw %01110000 ; 7 retlw %01111111 ; 8 retlw %01111011 ; 9 retlw %01110111 ; A retlw %00011111 ; B retlw %01001110 ; C retlw %00111101 ; D retlw %01001111 ; E retlw %01000111 ; F GETMOT10: andlw $F addwf PCL,F retlw 0 ; 0.0 retlw 25 ; 1.0 retlw 50 ; 2.0 retlw 75 ; 3.0 retlw 100 ; 4.0 retlw 125 ; 5.0 retlw 150 ; 6.0 retlw 175 ; 7.0 retlw 200 ; 8.0 retlw 225 ; 9.0 retlw 250 ; 10.0 retlw 255 GETMOT1: andlw $F addwf PCL,F retlw 0 ; 0.0 retlw 3 ; 0.1 retlw 5 ; 0.2 retlw 8 ; 0.3 retlw 10 ; 0.4 retlw 13 ; 0.5 retlw 15 ; 0.6 retlw 18 ; 0.7 retlw 20 ; 0.8 retlw 23 ; 0.9 retlw 255 SetVal: movf REG,W addwf PCL,F goto SetRef ; 0 return ; 1 goto SetMotor ; 2 return ; 3 return ; 4 goto SetRefType ; 5 return ; 6 goto GetAdc ; 7 GetVal: movf REG,W addwf PCL,F goto GetRef ; 0 goto GetTemp ; 1 return ; 2 goto GetTemp1 ; 3 goto GetLightLow ; 4 goto GetLightHigh ; 5 return ; 6 return ; 7 INITLED: movlw $C call SENDBYTE movlw 1 call SENDBYTE call LOADLED ; movlw $9 call SENDBYTE movlw 0 call SENDBYTE call LOADLED ; movlw $A call SENDBYTE movf INTEN,W call SENDBYTE call LOADLED ; movlw $B call SENDBYTE movlw 7 call SENDBYTE call LOADLED return WRITEREF: movf REF,W movwf V0 clrf V1 call DECODE ; movlw $1 call SENDBYTE movf D2,W btfss STATUS,Z goto WRREFSEG ; movlw 0 goto WRREFDO WRREFSEG: call GETSEG WRREFDO: call SENDBYTE call LOADLED ; movlw $2 call SENDBYTE movf D1,W call GETSEG addlw $80 call SENDBYTE call LOADLED ; movlw $3 call SENDBYTE movf D0,W call GETSEG call SENDBYTE call LOADLED return WRITETEM: movf AdcLow0,W movwf V0 movf AdcHigh0,W movwf V1 call DECODE ; movlw $4 call SENDBYTE movf D2,W btfss STATUS,Z goto WRTEMSEG ; movlw 0 goto WRTEMDO WRTEMSEG: call GETSEG WRTEMDO: call SENDBYTE call LOADLED ; movlw $5 call SENDBYTE movf D1,W call GETSEG addlw $80 call SENDBYTE call LOADLED ; movlw $6 call SENDBYTE movf D0,W call GETSEG call SENDBYTE call LOADLED return WRITEMOT: call DECMOT ; movlw $7 call SENDBYTE movf D1,W call GETSEG addlw $80 call SENDBYTE call LOADLED ; movlw $8 call SENDBYTE movf D0,W call GETSEG call SENDBYTE call LOADLED return AdcDelay: return ReadAdc0: movlw %10001000 goto ReadAdc ReadAdc1: movlw %10011000 goto ReadAdc ReadAdc2: movlw %10101000 goto ReadAdc ReadAdc3: movlw %10111000 ReadAdc: movwf TEMP movlw 7 movwf COUNT ; movlw %00001100 movwf PORTB call AdcDelay ; bcf PORTB,3 call AdcDelay AdcControlLoop: btfss TEMP,7 goto ResAdcControl SetAdcControl: bsf PORTB,2 goto NextAdcControl ResAdcControl: bcf PORTB,2 NextAdcControl: rlf TEMP,F ; bsf PORTB,0 call AdcDelay ; bcf PORTB,0 ; decfsz COUNT,F goto AdcControlLoop ; clrf AdcMsb movlw 4 movwf COUNT AdcMsbLoop: bcf STATUS,C rlf AdcMsb,F ; btfsc PORTB,1 bsf AdcMsb,0 ; bsf PORTB,0 call AdcDelay bcf PORTB,0 ; decfsz COUNT,F goto AdcMsbLoop clrf AdcLsb movlw 8 movwf COUNT AdcLsbLoop: bcf STATUS,C rlf AdcLsb,F ; btfsc PORTB,1 bsf AdcLsb,0 ; bsf PORTB,0 call AdcDelay bcf PORTB,0 ; decfsz COUNT,F goto AdcLsbLoop ; bsf PORTB,3 return GetAdc0: call ReadAdc0 movf AdcLsb,W addwf Ch0Low,F btfsc STATUS,C incf Ch0High,F movf AdcMsb,W addwf Ch0High,F return GetAdc1: call ReadAdc1 movf AdcLsb,W addwf Ch1Low,F btfsc STATUS,C incf Ch1High,F movf AdcMsb,W addwf Ch1High,F return GetAdc2: call ReadAdc2 movf AdcLsb,W addwf Ch2Low,F btfsc STATUS,C incf Ch2High,F movf AdcMsb,W addwf Ch2High,F return GetAdc3: call ReadAdc3 movf AdcLsb,W addwf Ch3Low,F btfsc STATUS,C incf Ch3High,F movf AdcMsb,W addwf Ch3High,F return GetAdc: call GetAdc0 call GetAdc1 call GetAdc2 call GetAdc3 return WaitClk: call Poll WaitClkHi: btfss PORTB,7 return btfsc PORTB,4 goto WaitClk WaitClkLow: call Poll btfss PORTB,7 return btfss PORTB,4 goto WaitClkLow return SetRef: movf VAL,W movwf REF return SetMotor: movf VAL,W movwf MOT return SetRefType: movf VAL,W movwf RefType call READEE return GetRef: movf REF,W movwf VAL return SaveAdc0: bcf STATUS,C rrf Ch0High,F rrf Ch0Low,F ; bcf STATUS,C rrf Ch0High,F rrf Ch0Low,F ; bcf STATUS,C rrf Ch0High,F rrf Ch0Low,F ; bcf STATUS,C rrf Ch0High,F rrf Ch0Low,F ; movf Ch0Low,W movwf AdcLow0 movf Ch0High,W movwf AdcHigh0 ; clrf Ch0Low clrf Ch0High return SaveAdc1: bcf STATUS,C rrf Ch1High,F rrf Ch1Low,F ; bcf STATUS,C rrf Ch1High,F rrf Ch1Low,F ; bcf STATUS,C rrf Ch1High,F rrf Ch1Low,F ; bcf STATUS,C rrf Ch1High,F rrf Ch1Low,F ; movf Ch1Low,W movwf AdcLow1 movf Ch1High,W movwf AdcHigh1 ; clrf Ch1Low clrf Ch1High return SaveAdc2: bcf STATUS,C rrf Ch2High,F rrf Ch2Low,F ; bcf STATUS,C rrf Ch2High,F rrf Ch2Low,F ; bcf STATUS,C rrf Ch2High,F rrf Ch2Low,F ; bcf STATUS,C rrf Ch2High,F rrf Ch2Low,F ; movf Ch2Low,W movwf AdcLow2 movf Ch2High,W movwf AdcHigh2 ; clrf Ch2Low clrf Ch2High return SaveAdc3: bcf STATUS,C rrf Ch3High,F rrf Ch3Low,F ; bcf STATUS,C rrf Ch3High,F rrf Ch3Low,F ; bcf STATUS,C rrf Ch3High,F rrf Ch3Low,F ; bcf STATUS,C rrf Ch3High,F rrf Ch3Low,F ; movf Ch3Low,W movwf AdcLow3 movf Ch3High,W movwf AdcHigh3 ; clrf Ch3Low clrf Ch3High return SetInten: movf AdcHigh3,W btfss STATUS,Z goto SetMaxInten ; movf AdcLow3,W movwf INTEN rrf INTEN,F rrf INTEN,F rrf INTEN,F rrf INTEN,W andlw $F addlw 2 movwf INTEN btfss INTEN,4 return SetMaxInten: movlw $F movwf INTEN return GetTemp0: movf AdcLow0,W movwf VAL return GetTemp1: movf AdcLow1,W movwf VAL return GetLightLow: movf AdcLow3,W movwf VAL return GetLightHigh: movf AdcHigh3,W movwf VAL return GetTemp: call SaveAdc0 call SaveAdc1 call SaveAdc2 call SaveAdc3 call SetInten call GetTemp0 return GetMotor: movf MOT,W movwf VAL return Wait: call Poll btfss PORTB,7 goto Wait ; PAGE1 movlw %10110010 movwf TRISB PAGE0 ; clrf VAL clrf REG call WaitClk btfss PORTB,5 goto WaitRec WaitSend: call WaitClk btfsc PORTB,5 bsf REG,0 call WaitClk btfsc PORTB,5 bsf REG,1 call WaitClk btfsc PORTB,5 bsf REG,2 ; call WaitClk btfsc PORTB,5 bsf VAL,0 call WaitClk btfsc PORTB,5 bsf VAL,1 call WaitClk btfsc PORTB,5 bsf VAL,2 call WaitClk btfsc PORTB,5 bsf VAL,3 call WaitClk btfsc PORTB,5 bsf VAL,4 call WaitClk btfsc PORTB,5 bsf VAL,5 call WaitClk btfsc PORTB,5 bsf VAL,6 call WaitClk btfsc PORTB,5 bsf VAL,7 btfss PORTB,7 goto WaitEnd call SetVal goto WaitEnd WaitRec: call WaitClk btfsc PORTB,5 bsf REG,0 call WaitClk btfsc PORTB,5 bsf REG,1 call WaitClk btfsc PORTB,5 bsf REG,2 ; call GetVal ; movlw 8 movwf BIT btfss PORTB,7 goto WaitEnd WaitRecLoop: btfss VAL,0 goto WaitRecReset bsf PORTB,6 goto WaitRecShift WaitRecReset: bcf PORTB,6 WaitRecShift: rrf VAL,F WaitRecHi: call Poll btfss PORTB,7 goto WaitEnd btfsc PORTB,4 goto WaitRecHi WaitRecLow: call Poll btfss PORTB,7 goto WaitEnd btfss PORTB,4 goto WaitRecLow decfsz BIT,F goto WaitRecLoop WaitEnd: PAGE1 movlw %11110010 movwf TRISB PAGE0 WaitHi: call Poll btfss PORTB,7 return goto WaitHi DECODE: clrf D1 clrf D2 movlw 100 DEC100: subwf V0,F btfss STATUS,C decf V1,F incf D2,F ; btfss V1,7 goto DEC100 ; decf D2,F addwf V0,F movlw 10 DEC10: incf D1,F subwf V0,F btfsc STATUS,C goto DEC10 ; decf D1,F addwf V0,W movwf D0 return DECMOT: movf MOT,W movwf V0 movlw 249 subwf V0,W btfss STATUS,C goto DECMOTIT ; movlw 9 movwf D1 movwf D0 return DECMOTIT: clrf D1 clrf D0 DECMOT10: incf D1,W call GETMOT10 subwf V0,W btfss STATUS,C goto DECMOTN incf D1,F goto DECMOT10 DECMOTN: movf D1,W call GETMOT10 subwf V0,F DECMOT1: incf D0,W call GETMOT1 subwf V0,W btfss STATUS,C return incf D0,F goto DECMOT1 DELAY: return SENDBYTE: movwf TEMP movlw 8 movwf COUNT SENDLOOP: movlw 0 btfsc TEMP,7 movlw 2 movwf PORTA call DELAY addlw 1 movwf PORTA call DELAY rlf TEMP,F decfsz COUNT,F goto SENDLOOP return LOADLED: movlw 5 movwf PORTA call DELAY clrf PORTA call DELAY return CheckRef: movf REF,W sublw 150 btfss STATUS,C goto CheckRefHi ; movlw 150 movwf REF return CheckRefHi: movf REF,W sublw 250 btfsc STATUS,C return ; movlw 250 movwf REF return READEE: movf RefType,W movwf EEADR PAGE1 bsf EECON1,RD PAGE0 movf EEDATA,W movwf REF call CheckRef return WRITEEE: movf RefType,W movwf EEADR PAGE1 bsf EECON1,WREN PAGE0 movf REF,W movwf EEDATA PAGE1 movlw $55 movwf EECON2 movlw $AA movwf EECON2 bsf EECON1,WR CHKWRT: btfss EECON1,4 goto CHKWRT bcf EECON1,WREN bcf EECON1,4 PAGE0 bcf INTCON,6 return Poll: return ; temporary to disallow manipulation decfsz DelayMs,F return ; movlw $FF movwf DelayMs ; movf PORTA,W andlw $18 xorwf TState,W btfss STATUS,Z goto RedoDebounc ; decfsz Stable,F return ; movlw 8 xorwf TState,W btfss STATUS,Z goto CheckRed CheckBoth: movlw $10 xorwf TState,W btfsc STATUS,Z return ; decf REF,F call CheckRef call WRITEEE call WRITEREF return CheckRed: movlw $10 xorwf TState,W btfss STATUS,Z return ; call CheckRef incf REF,F call WRITEEE call WRITEREF return RedoDebounc: movf PORTA,W andlw $18 movwf TState movlw 10 movwf Stable return .END