水果机硬件与软件,共享
Altium Designer画的果汁机电路原理图和PCB图如下:(51hei附件中可下载工程文件)
芯海单片机源程序如下:
- ;********************************************************************************************
- ;#######################################################################################
- ;【文件名】 LT-Y47.asm:主体程序文件 *** 18.01.09 ***
- ;#######################################################################################
- ;codeoption:FB01 checksum:B26E
- ;#######################################################################################
- ;---------- all rights reserved , Chipsea technology 2018 -------------
- ;======================================================================
- ; 【说明】:
- ;****************************************************
- ; prj desp. : 幻想果汁机
- ; target chip : CSU8RP3215
- ; crystal : 16MHZ
- ; author : dream
- ; date : 2018.01.09
- ;
- ;****************************************************
- ;****************************************************
- ;【型号定义】:
- ;****************************************************
- ;****************************************************
- ;【编译选项】:
- ;_DEBUG_ equ 1
- ;****************************************************
- ;【OPTION选项】:
- ;
- ;****************************************************
- ;【修改记录】:
- ;
- ;****************************************************
- ;【目标母体选择】:
- include "CSU8RP3215.inc"
-
- ;【引用文件】:
- include "LT_Y47.h"
- ;**********************************************************************
- ;**********************************************************************
- org 000h
- nop
- nop
- nop
- goto start
- org 004h
- goto intService
- ;******************************************************************************
- ; org 010h
- ;******************************************************************************
- start:
- bcf gie
- call fnAllDefaultSet
- main:
- clrwdt
- call fnTaskMgr
- call fnKeyFunc
- call fnDispFunc
- call fnBatFunc
- call fnEnterSleep
- goto main
- ;******************************************************************************
- ;【说明】: 按键扫描程序
- ;【输入】: 无
- ;【输出】: fLongKey
- ;******************************************************************************
- fnTaskMgr:
- btfss fBase2ms
- goto lbTaskMgrExit
-
- bcf fBase2ms
- bcf led1_o
- btfsc ledBuf,0
- bsf led1_o
-
- bcf led2_o
- btfsc ledBuf,1
- bsf led2_o
-
- bcf led3_o
- btfsc ledBuf,2
- bsf led3_o
- bcf led4_o
- btfsc ledBuf,3
- bsf led4_o
-
- btfsc fHaveUsb
- goto lbChkUsbInfo
-
- btfss hall_i
- goto lbBottleLock
- lbBottleNotLock:
- clrf cHaveLock
- btfss fKGLock
- goto lbChkUsbInfo
- incf cNoLock,F
- movfw cNoLock
- sublw 29 ;30*2ms=60ms
- btfsc c
- goto lbChkKey
- bcf fKGLock
- btfsc fRunOn
- call lbChangeToOff
- bcf fBuzOn
- bcf buz_o
- bsf fEnWaitDly
- clrf tWaitDly
- clrf ledBuf
- goto lbChkUsbInfo
- lbBottleLock:
- clrf cNoLock
- btfsc fKGLock
- goto lbChkUsbInfo
- incf cHaveLock,F
- movfw cHaveLock
- sublw 29 ;30*2ms=60ms
- btfsc c
- goto lbChkUsbInfo
- bsf fKGLock
- btfsc fHaveUsb
- goto lbChkUsbInfo
- bsf fFstLock ;
- clrf tWaitDlyAssi ;延时1秒后关闭指示灯
- clrf tWaitDly ;
- bsf fBuzOn
- lbChkUsbInfo:
- btfss usb_i
- goto lbNoUsbInJug
- lbUsbInJug:
- clrf cUsbOut
- btfsc fHaveUsb
- goto lbTaskMgrExit
- incf cUsbExist,F
- movfw cUsbExist
- sublw 19 ;20*2ms=40ms
- btfsc c
- goto lbTaskMgrExit
- bsf fHaveUsb
- bcf fBat1ChgFull
- bcf fBat2ChgFull
- bcf fRunOn
- bcf motor_o
- bcf fBatLow
- bcf fEnWaitDly
- clrf cVolOffset
- bcf fMaskAlarm
- bcf fWorkedDly
- bcf fNeedCoolDown
- clrf cNormalWork
- movlw high(CHG_CUR_1000MA)
- movwf aimCurH
- movlw low(CHG_CUR_1000MA)
- movwf aimCurL
- goto lbTaskMgrExit
- lbNoUsbInJug:
- clrf cUsbExist
- btfss fHaveUsb
- goto lbChkKey
- incf cUsbOut,F
- movfw cUsbOut
- sublw 19 ;20*2ms=40ms
- btfsc c
- goto lbChkKey
- bcf fHaveUsb
- clrf ledBuf
- clrf cVolOffset
- bcf fEnWaitDly
- bcf pwm2out
- bcf pwm_o
- lbChkKey:
- btfsc fEnAlarmDly
- goto lbTaskMgrExit ;过载保护期间禁止按键
-
- movfw p_key
- andlw MASK_KEY_BIT
- xorlw MASK_KEY_BIT
- movwf var1
- btfsc z
- goto lbNoKeyPush ;没有按键按下
-
- clrf tKeyRel
- xorwf lastKey,W
- btfss z
- goto lbNewKeyPush ;有新按键按下
-
- btfsc fKeyPush
- goto lbLongKeyPushCnt
-
- incf tKeyPush,F
- movfw tKeyPush
- sublw 30 ;31*2ms=62ms
- btfsc c
- goto lbTaskMgrExit
- movfw lastKey
- movwf keyName
- bsf fKeyOk
- bsf fKeyPush
- bcf fKeyRel
- lbTaskMgrExit:
- return
-
- lbNewKeyPush:
- movfw var1
- movwf lastKey
- bcf fKeyPush
- clrf tKeyPush
- goto lbTaskMgrExit
- lbLongKeyPushCnt:
- goto lbTaskMgrExit
- lbNoKeyPush:
- clrf tKeyPush
- bcf fKeyEr
- btfsc fKeyRel
- goto lbTaskMgrExit
- incf tKeyRel,F
- movfw tKeyRel
- sublw 23 ;24*2ms=48ms
- btfsc c
- goto lbTaskMgrExit
-
- bsf fKeyRel
- bcf fKeyPush
- goto lbTaskMgrExit
- ;******************************************************************************
- ;【说明】: 按键功能
- ;******************************************************************************
- fnKeyFunc:
- btfss fRstRun
- btfss fKeyOk
- goto lbKeyFuncExit
-
- bcf fKeyOk
- movfw keyName
- xorlw CONS_ON_KEY
- btfsc z
- goto lbOnKeyPr
- bsf fKeyEr
- lbKeyFuncExit:
- return
- lbOnKeyPr:
- btfsc fHaveUsb
- goto lbKeyFuncExit
- btfss fKGLock ;KG开关没有锁紧时,禁止运转
- goto lbAlarmSet
-
- btfsc fNeedCoolDown ;10分钟散热期间,禁止开启
- goto lbSetCoolDownDly
- btfsc fRunOn
- goto lbChangeToOff
-
- bsf fRunOn
- bsf fEnWaitDly ;马达开启后等待300ms寻找最大值
- clrf tWaitDly ;
- clrf cEmptyMotor
- clrf tRunSec
- clrf tRunMSec
- clrf tMotorDly
- clrf motorMaxCurH
- clrf motorMaxCurL
- clrf cVolOffset
- bcf fEmptyMotor
- bsf motor_o ;开启马达
- bsf fWorkGood
- clrf tWaterFlow
- movlw 1
- movwf ledBuf
- bsf fEnDisp
- goto lbKeyFuncExit
- lbChangeToOff:
- bcf fRunOn
- bcf motor_o
- clrf tWaitDly
- clrf tWaitDlyAssi
- clrf tRunMSec
- clrf cVolOffset
- bcf fWorkGood
- bsf fEnDisp
- goto lbKeyFuncExit
- lbAlarmSet:
- bsf fEnWaitDly
- clrf tWaitDly
- goto lbKeyFuncExit
-
- lbSetCoolDownDly:
- bsf fMaskAlarm ;开启马达需要散热报警指示
- clrf tMaskSec
- clrf cMaskFlash
- bcf fEnDisp
- goto lbKeyFuncExit
- ;******************************************************************************
- ;【说明】: 显示处理程序
- ;******************************************************************************
- fnDispFunc:
- btfss fDisp2ms
- goto lbDispFuncExit
-
- bcf fDisp2ms
- btfss fHaveUsb
- goto lbDischargeDisp
- lbChargeDisp:
- incf tFlashTime,F
- movfw tFlashTime
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto $+4
- clrf tFlashTime
- movlw 0x04 ;fEnFlash
- xorwf sysFlag2,F
-
- movfw volume
- addpcw
- goto lbDispChgVol0
- goto lbDispChgVol25
- goto lbDispChgVol50
- goto lbDispChgVol75
- goto lbDispChgVol100
- lbDispChgVol0: ;0%时,第一个灯闪烁
- movlw 0
- btfss fEnFlash
- movlw 1
- movwf ledBuf
- goto lbDispFuncExit
- lbDispChgVol25: ;25%时,25%常亮,50闪烁
- movlw 01b
- btfss fEnFlash
- movlw 11b
- movwf ledBuf
- goto lbDispFuncExit
- lbDispChgVol50: ;50%时,25%和50%常亮,75%闪烁
- movlw 011b
- btfss fEnFlash
- movlw 111b
- movwf ledBuf
- goto lbDispFuncExit
- lbDispChgVol75: ;75%时,25%、50%和75%常亮,100%闪烁
- movlw 0111b
- btfss fBat1ChgFull
- btfss fEnFlash
- movlw 1111b
- movwf ledBuf
- goto lbDispFuncExit
- lbDispChgVol100: ;100%时,充饱全亮
- movlw 1111b
- movwf ledBuf
- goto lbDispFuncExit
- lbDischargeDisp:
- btfss fKGLock
- goto lbChkAlarmDly ;没有锁紧,报警显示
- btfsc fFstLock
- goto lbFirstLockDisp ;首次检测到锁紧显示
-
- btfsc fMaskAlarm
- goto lbChkMaskDly ;需要散热报警指示显示
-
- btfss fEnDisp
- goto lbDispFuncExit
- btfss fWorkGood
- goto lbDispCurVolume ;显示当前电量
-
- incf tWaterFlow,F
- movfw tWaterFlow
- sublw 149 ;150*2ms=300ms
- btfsc c
- goto lbDispFuncExit
- clrf tWaterFlow
- bcf c
- rlf ledBuf,F ;跑马左移显示
- movlw 1
- btfsc ledBuf,4
- movwf ledBuf
- goto lbDispFuncExit
-
- lbDispCurVolume:
- movfw volume
- addpcw
- goto lbDispDisVol0
- goto lbDispDisVol25
- goto lbDispDisVol50
- goto lbDispDisVol75
- goto lbDispDisVol100
- lbDispDisVol100:
- movlw 1111b
- movwf ledBuf
- goto lbDispDisVol25+2
- lbDispDisVol75:
- movlw 0111b
- movwf ledBuf
- goto lbDispDisVol25+2
- lbDispDisVol50:
- movlw 0011b
- movwf ledBuf
- goto lbDispDisVol25+2
- lbDispDisVol25:
- movlw 0001b
- movwf ledBuf
-
- incf tWaitDly,F
- movfw tWaitDly
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbDispFuncExit
- clrf tWaitDly
- incf tWaitDlyAssi,F
- movfw tWaitDlyAssi
- sublw 9 ;10*0.5s=5s
- btfsc c
- goto lbDispFuncExit
- clrf ledBuf
- bcf fEnDisp
- goto lbDispFuncExit
- lbDispDisVol0:
- incf tAlarmFlash,F
- movfw tAlarmFlash
- sublw 49 ;50*2ms=100ms
- btfsc c
- goto lbDispFuncExit
- clrf tAlarmFlash
- movlw 0x08 ;fAlarmFlash
- xorwf sysFlag2,F
-
- movlw 1111b
- btfss fAlarmFlash
- movlw 0000b
- movwf ledBuf
-
- incf tWaitDly,F
- movfw tWaitDly
- sublw 9
- btfsc c
- goto lbDispFuncExit
- clrf ledBuf ;关闭显示
- bcf fEnDisp
- goto lbDispFuncExit
- lbFirstLockDisp:
- movlw 0x0f
- movwf ledBuf
- incf tWaitDly,F
- movfw tWaitDly
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbDispFuncExit
- clrf tWaitDly
- incf tWaitDlyAssi,F
- movfw tWaitDlyAssi
- sublw 1 ;0.5s*2=1s
- btfsc c
- goto lbDispFuncExit
- bcf fFstLock
- clrf ledBuf ;初次锁紧满1秒,关闭显示
- bcf fBuzOn
- bcf buz_o
- goto lbDispFuncExit
- lbChkMaskDly:
- incf tMaskSec,F
- movfw tMaskSec
- sublw 49 ;50*2ms=100ms
- btfsc c
- goto lbDispFuncExit
-
- clrf tMaskSec
- movlw 0x0f
- xorwf ledBuf,F
- movlw 0x01 ;fBuzOn
- xorwf buzFlag,F
- incf cMaskFlash,F
- movfw cMaskFlash
- sublw 9 ;10次
- btfsc c
- goto lbDispFuncExit
- bcf fMaskAlarm
- clrf ledBuf
- bcf fBuzOn
- goto lbDispFuncExit
- lbChkAlarmDly:
- btfss fEnWaitDly
- goto lbDispFuncExit
- movlw 0xff
- movwf ledBuf
- incf tWaitDly,F
- movfw tWaitDly
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbDispFuncExit
- bcf fEnWaitDly
- clrf ledBuf ;关闭显示
- lbDispFuncExit:
- return
- ;==============================================================================
- fnReadAnalog:
- movfw channelNum
- addpcw
- goto lbJudgeMotorCur
- goto lbJudgeBatVol1
- goto lbJudgeBatVol2
- goto lbJudgeUsbDc
- lbJudgeMotorCur:
- call fnSelectMotorCurrent
- goto lbStartReadAd
- lbJudgeBatVol1:
- call fnSelectBatVoltage1
- goto lbStartReadAd
- lbJudgeBatVol2:
- call fnSelectBatVoltage2
- goto lbStartReadAd
- lbJudgeUsbDc:
- call fnSelectUSBDc
- lbStartReadAd:
- movlw 20 ;
- movwf var1 ;
- decfsz var1,F ;切换通道delay
- goto $-1 ;
- call fnReadADC
-
- ;取和
- movfw var2
- addwf averageL,F
- movfw var1
- addwfc averageH,F
-
- ;更新最小值
- movfw var2
- subwf tempMinL,W
- movfw var1
- subwfc tempMinH,W
- btfss c
- goto $+5
- movfw var1
- movwf tempMinH
- movfw var2
- movwf tempMinL
-
- ;更新最大值
- movfw var2
- subwf tempMaxL,W
- movfw var1
- subwfc tempMaxH,W
- btfsc c
- goto $+5
- movfw var1
- movwf tempMaxH
- movfw var2
- movwf tempMaxL
-
- decfsz cAdcCnt,F
- goto lbCurrentJudgeExit
-
- ;减去最大值、最小值
- movfw tempMaxL
- subwf averageL,F
- movfw tempMaxH
- subwfc averageH,F
-
- movfw tempMinL
- subwf averageL,F
- movfw tempMinH
- subwfc averageH,F
-
- movfw channelNum
- addpcw
- goto lbSaveMotorCur
- goto lbSaveBatVol1
- goto lbSaveBatVol2
- goto lbSaveUsbDc
- lbSaveMotorCur:
- movfw averageH
- movwf motorCurH
- movfw averageL
- movwf motorCurL
- goto lbGotoNextChannel
-
- lbSaveBatVol1:
- movfw averageH
- movwf totVolH
- movfw averageL
- movwf totVolL
- goto lbGotoNextChannel
- lbSaveBatVol2:
- movfw averageH
- movwf batVol2H
- movfw averageL
- movwf batVol2L
-
- subwf totVolL,W
- movwf batVol1L
- movfw batVol2H
- subwfc totVolH,W
- movwf batVol1H
- btfsc c
- goto lbGotoNextChannel
- movfw totVolH
- movwf batVol1H
- movfw totVolL
- movwf batVol1L
- goto lbGotoNextChannel
- lbSaveUsbDc:
- movfw averageH
- movwf usbVolH
- movfw averageL
- movwf usbVolL
-
- ; movlw low(USB_3000MV)
- ; subwf usbVolL,W
- ; movlw high(USB_3000MV)
- ; subwfc usbVolH,W
- ; btfss c
- ; goto lbNoUsbInJug
- ;lbUsbInJug:
- ; clrf cUsbOut
- ; btfsc fHaveUsb
- ; goto lbGotoNextChannel
- ; incf cUsbExist,F
- ; movfw cUsbExist
- ; sublw 19 ;20*2ms=40ms
- ; btfsc c
- ; goto lbGotoNextChannel
- ; bsf fHaveUsb
- ; bcf fBat1ChgFull
- ; bcf fBat2ChgFull
- ; bcf fRunOn
- ; bcf motor_o
- ; bcf fBatLow
- ; bcf fEnWaitDly
- ; clrf cVolOffset
- ; bcf fMaskAlarm
- ; bcf fWorkedDly
- ; bcf fNeedCoolDown
- ; clrf cNormalWork
- ; movlw high(CHG_CUR_1000MA)
- ; movwf aimCurH
- ; movlw low(CHG_CUR_1000MA)
- ; movwf aimCurL
- ; goto lbGotoNextChannel
- ;lbNoUsbInJug:
- ; clrf cUsbExist
- ; btfss fHaveUsb
- ; goto lbChkKey
- ; incf cUsbOut,F
- ; movfw cUsbOut
- ; sublw 19 ;20*2ms=40ms
- ; btfsc c
- ; goto lbGotoNextChannel
- ; bcf fHaveUsb
- ; clrf ledBuf
- ; clrf cVolOffset
- ; bcf fEnWaitDly
- ; bcf pwm2out
- ; bcf pwm_o
- lbGotoNextChannel:
- call fnInitSamp
- incf channelNum,F
- movfw channelNum
- sublw 3
- btfsc c
- goto lbCurrentJudgeExit
- clrf channelNum
- bcf fRstRun
- ifdef _DEBUG_
- ;{
- btfss fBase1s
- goto lbCurrentJudgeExit
- bcf fBase1s
- movlw 0x55
- movwf data0
- movfw motorCurL
- movwf data1
- movfw motorCurH
- movwf data2
- movfw batVol1L
- movwf data3
- movfw batVol1H
- movwf data4
- movfw batVol2L
- movwf data5
- movfw batVol2H
- movwf data6
- movlw 7
- movwf txdNum
- bsf fEnTxd
- ;}
- endif
- lbCurrentJudgeExit:
- return
- ;----------------------------------------------------------
- fnInitSamp:
- movlw 3
- movwf cAdcCnt
- clrf averageH
- clrf averageL
- clrf tempMaxH
- clrf tempMaxL
- movlw 0xff
- movwf tempMinH
- movwf tempMinL
- return
- ;=================================================================
- fnReadADC:
- bsf srads ;开启AD转换
- btfsc srads
- goto $-1
-
- movfw sradl
- movwf var2
- movfw sradh
- andlw 0x0f
- movwf var1
-
- bsf offex ;翻转输入信号
- bsf srads
- btfsc srads
- goto $-1
-
- movfw sradl
- addwf var2,F
- movfw sradh
- andlw 0x0f
- addwfc var1,F
-
- bcf c
- rrf var1,F ;得到两次输入信号的平均值
- rrf var2,F ;
- return
- ;******************************************************************************
- ;【说明】: 电池充放电管理程序
- ;******************************************************************************
- fnBatFunc:
- btfss fBat2ms
- goto lbBatFuncExit
-
- bcf fBat2ms
- btfss fHaveUsb
- goto lbSystemDischarge
- lbSystemCharge:
- call fnReadAnalog ;模拟信号读取
- call fnChgVolDet ;充电电量检测
- call fnBatChgFullJug ;充饱检测
- call fnChgVolControl ;充电电压控制
- goto lbBatFuncExit
- lbSystemDischarge:
- call fnReadAnalog ;模拟信号读取
- call fnDisVolDet ;放电电量检测
- call fnMotorCtrl ;马达控制
- call fnOverLoadCtrl
- call fnWaitCoolDown
- lbBatFuncExit:
- return
- ;==============================================================================
- ;充饱检测
- fnBatChgFullJug:
- movlw low(BAT_VOL_4200MV)
- subwf batVol1L,W
- movlw high(BAT_VOL_4200MV)
- subwfc batVol1H,W
- btfss c
- clrf cBat1FullCnt
-
- incf cBat1FullCnt,F
- incf cBat1Cycle,F
- movfw cBat1Cycle
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbChkBat2ChgFull
- clrf cBat1Cycle
-
- movfw cBat1FullCnt
- clrf cBat1FullCnt
- sublw 249*60/100
- btfss c
- bsf fBat1ChgFull
- lbChkBat2ChgFull:
- movlw low(BAT_VOL_4200MV)
- subwf batVol2L,W
- movlw high(BAT_VOL_4200MV)
- subwfc batVol2H,W
- btfss c
- clrf cBat2FullCnt
-
- incf cBat2FullCnt,F
- incf cBat2Cycle,F
- movfw cBat2Cycle
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbBatChgFullJugExit
- clrf cBat2Cycle
-
- movfw cBat2FullCnt
- clrf cBat2FullCnt
- sublw 249*60/100
- btfss c
- bsf fBat2ChgFull
- lbBatChgFullJugExit:
- return
- ;==============================================================================
- ;充电电压控制
- fnChgVolControl:
- ; movlw low(BAT_VOL_4200MV)
- ; subwf batVol1L,W
- ; movlw high(BAT_VOL_4200MV)
- ; subwfc batVol1H,W
- ; btfsc c
- ; goto lbBatChgFull
- ;
- ; movlw low(BAT_VOL_4200MV)
- ; subwf batVol2L,W
- ; movlw high(BAT_VOL_4200MV)
- ; subwfc batVol2H,W
- ; btfsc c
- ; goto lbBatChgFull
-
- call fnSelectMotorCurrent
- movlw 20
- movwf var1 ;
- decfsz var1,F ;切换通道delay
- goto $-1 ;
- call fnReadADC
- movfw var2
- movwf chgCurL
- movfw var1
- movwf chgCurH
- call fnReadADC
- movfw var2
- addwf chgCurL,F
- movfw var1
- addwfc chgCurH,F
- call fnReadADC
- movfw var2
- addwf chgCurL,F
- movfw var1
- addwfc chgCurH,F
- call fnReadADC
- movfw var2
- addwf chgCurL,F
- movfw var1
- addwfc chgCurH,F
- bcf c
- rrf chgCurH,F
- rrf chgCurL,F
- bcf c
- rrf chgCurH,F
- rrf chgCurL,F
-
- movfw batVol1H
- movwf var1
- movfw batVol1L
- movwf var2
-
- movfw batVol1L
- subwf batVol2L,W
- movfw batVol1H
- subwfc batVol2H,W
- btfss c
- goto $+5
- movfw batVol2H
- movwf var1
- movfw batVol2L
- movwf var2
- movlw low(USB_4600MV)
- subwf usbVolL,W
- movlw high(USB_4600MV)
- subwfc usbVolH,W
- btfss c
- goto lbPwmDec
-
- movfw aimCurL
- subwf chgCurL,W
- movfw aimCurH
- subwfc chgCurH,W
- btfsc c
- goto lbPwmDec ;实际电流大了,PWM--
-
- movlw low(BAT_VOL_4200MV)
- subwf var2,W
- movlw high(BAT_VOL_4200MV)
- subwfc var1,W
- btfsc c
- goto lbPwmDec
- lbPwmInc:
- movfw pwmBuf
- xorlw 0xff
- btfss z
- incf pwmBuf,F
- goto lbUpdateChargeCur
- lbPwmDec:
- movfw pwmBuf
- xorlw 0x0
- btfss z
- decf pwmBuf,F
- lbUpdateChargeCur:
- movfw pwmBuf
- movwf tm2r
- bsf pwm2out
- return
- lbBatChgFull:
- clrf pwmBuf
- clrf tm2r
- bcf pwm2out
- bcf pwm_o
- return
-
- ;******************************************************************************
- ;【说明】: 睡眠进入程序
- ;【输入】: 无
- ;【输出】: 无
- ;******************************************************************************
- fnEnterSleep:
- btfss fRunOn
- btfsc fHaveUsb
- goto lbEnterSleepExit
-
- btfsc fBatLow
- goto lbBatLowAlarm
-
- btfsc fEnAlarmDly
- goto lbEnterSleepExit
-
- btfss fWorkedDly
- btfsc fNeedCoolDown
- goto lbEnterSleepExit
-
- movfw ledBuf
- xorlw 0
- btfss z
- goto lbEnterSleepExit
- btfss fKeyPush
- btfss fSleep10ms
- goto lbEnterSleepExit
-
- bcf fSleep10ms
- incfsz tWaitSleep,F
- goto lbEnterSleepExit
- lbGotoSleepNow:
- clrwdt
- bcf fBuzOn
- bcf led1_o
- bcf led2_o
- bcf led3_o
- bcf led4_o
- bcf buz_o
- bsf pwr_o
- bcf sraden
- clrf sradcon0
- clrf sradcon1
- clrf sradcon2
- bcf pt3con,0
- bcf wdten
- movlw 0x13 ;使能按键和充电唤醒睡眠
- ; btfsc fBatLow
- ; movlw 0x03 ;电池低时,禁止按键唤醒,只可以充电唤醒
- movwf pt1con
- movlw 0x08
- movwf pt1con1 ;使能PT3.1状态改变时唤醒睡眠
- bsf e0ie
- bsf e1ie ;使能外部中断1
- clrf intf
- sleep
- nop
- nop
- nop
- bcf e0ie
- bcf e1ie
- clrf pt1con ;关闭PT1.3/PT3.0口中断唤醒功能
- clrf pt1con1 ;关闭PT3.1口中断唤醒功能
- bsf wdten
- bcf fBatLow
- movlw 00110000b ;ADC输入信号获取时间:2个ADC时钟,采样时间:CPUCLK
- movwf sradcon0
- movlw 00000000b ;参考电压:VDD
- movwf sradcon1
- clrf sradl
- clrf sradh
- clrf sroftl
- clrf srofth
- bsf sraden ;使能ADC
- call fnInitSamp
- bsf pwr_o
- lbEnterSleepExit:
- return
- lbBatLowAlarm:
- clrf var1
- clrf var2
- bcf led1_o
- bcf led2_o
- bcf led3_o
- bcf led4_o
- lbAlarmFlash:
- clrwdt
- btfss fBase2ms
- goto $-2
- bcf fBase2ms
- incf var1,F
- movfw var1
- sublw 49 ;50*2ms=100ms
- btfsc c
- goto lbAlarmFlash
- clrf var1
- movlw 0x80 ;led1_o
- xorwf pt1,F
- incf var2,F
- movfw var2
- sublw 9
- btfsc c
- goto lbAlarmFlash
- goto lbGotoSleepNow
-
- ;******************************************************************************
- ;【说明】: 充电电量检测
- ;【输入】: 无
- ;【输出】: 无
- ;******************************************************************************
- fnChgVolDet:
- movlw high(tblChgVolLevel)
- movwf eadrh
- movfw volume
- addlw low(tblChgVolLevel)
- movwf eadrl
- btfsc c
- incf eadrh,F
- movp
- subwf totVolL,W
- movfw edath
- subwfc totVolH,W
- btfss c
- clrf cVolOffset
-
- incfsz cVolOffset,F
- goto lbChgVolDetExit
- movfw volume
- xorlw BAT_VOLUME_100
- btfss z
- incf volume,F
- lbChgVolDetExit:
- return
-
- ;******************************************************************************
- ;【说明】: 放电电量检测
- ;【输入】: 无
- ;【输出】: 无
- ;******************************************************************************
- fnDisVolDet:
- btfsc fRunOn
- goto lbChkBatteryLow
-
- movlw high(tblDisVolLevel)
- movwf eadrh
- movfw volume
- addlw low(tblDisVolLevel)
- movwf eadrl
- btfsc c
- incf eadrh,F
- movp
- subwf totVolL,W
- movfw edath
- subwfc totVolH,W
- btfsc c
- clrf cVolOffset
-
- incfsz cVolOffset,F
- goto lbDisVolDetExit
- movfw volume
- xorlw 0
- btfss z
- decf volume,F
- movfw volume
- xorlw 0
- btfsc z
- bsf fBatLow
- lbDisVolDetExit:
- return
- lbChkBatteryLow:
- movlw low(TOT_VOL_5500MV)
- subwf totVolL,W
- movlw high(TOT_VOL_5500MV)
- subwfc totVolH,W
- btfsc c
- clrf cVolOffset
-
- incfsz cVolOffset,F
- goto lbDisVolDetExit
- bsf fBatLow
- clrf volume
- call lbChangeToOff ;电池低电,关机报警
- goto lbDisVolDetExit
- ;==================================
- fnSelectMotorCurrent:
- movlw 0x82 ;内部1.4V
- movwf sradcon1
- movlw CH_MOTOR_CUR
- movwf sradcon2
- movlw 00100000b
- movwf pt3con ;pt3.5为AD口
- return
- fnSelectBatVoltage1:
- movlw 0x80
- movwf sradcon1
- movlw CH_BAT_VOL1
- movwf sradcon2
- movlw 00010000b
- movwf pt3con ;pt3.4为AD口
- return
- fnSelectBatVoltage2:
- movlw 0x80
- movwf sradcon1
- movlw CH_BAT_VOL2
- movwf sradcon2
- movlw 01000000b
- movwf pt3con ;pt3.6为AD口
- return
- fnSelectUSBDc:
- movlw 0x80
- movwf sradcon1
- movlw CH_USB_DC
- movwf sradcon2
- movlw 00000001b
- movwf pt3con ;pt3.0为AD口
- return
- ;******************************************************************************
- ;【说明】: 马达控制
- ;【输入】: 无
- ;【输出】: 无
- ;******************************************************************************
- fnMotorCtrl:
- btfss fRunOn
- goto lbMotorCtrlExit
- ; 1、马达启动时,在刚启动300ms之内寻找最大值,300ms之后比较电流值和最大值,如果电流值比最大值小10MV以上,认为
- ; 马达工作正常,否则认为堵转;
- ; 2、在马达正常工作中,寻找最大值,如果最大值比正常值大于10MV以上时,认为发生堵转。
- movlw low(EMPTY_MIN_CUR)
- movwf var2
- movlw high(EMPTY_MIN_CUR)
- movwf var1
- btfss fEmptyMotor
- goto $+5
- movlw low(EMPTY_MAX_CUR)
- movwf var2
- movlw high(EMPTY_MAX_CUR)
- movwf var1
-
- movfw var2
- subwf motorCurL,W
- movfw var1
- subwfc motorCurH,W
- btfss c
- incf cEmptyMotor,F
-
- incf cMotorPeriod,F
- movfw cMotorPeriod
- sublw 99 ;100*2ms=200ms
- btfsc c
- goto lbChkBlockCur
-
- clrf cMotorPeriod
- movfw cEmptyMotor
- sublw 70 ;100*70%=70
- btfss c
- bsf fEmptyMotor ;>70%时,认为空载
-
- movfw cEmptyMotor
- sublw 30 ;100*30%=30
- btfsc c
- bcf fEmptyMotor ;<30%时,认为正常负载
-
- clrf cEmptyMotor
- btfss fEmptyMotor
- clrf tMotorDly ;没有空载时,清计数器
- lbChkBlockCur:
- btfsc fEmptyMotor
- goto lbMotorRunDly ;空载延时控制
- btfss fEnWaitDly
- goto lbNormalRunChk
- ;马达启动时,堵转监测
- movfw motorCurL
- subwf motorMaxCurL,W
- movfw motorCurH
- subwfc motorMaxCurH,W
- btfsc c
- goto $+5
- movfw motorCurH
- movwf motorMaxCurH
- movfw motorCurL
- movwf motorMaxCurL
-
- incfsz tWaitDly,F ;256*2ms=512ms
- goto lbMotorRunDly
-
- bcf fEnWaitDly
- movlw 2;5;10;40;87 ;13mV
- addwf motorCurL,F
- movlw 0
- addwfc motorCurH,F
- movfw motorCurL
- subwf motorMaxCurL,W
- movfw motorCurH
- subwfc motorMaxCurH,W
- btfss c
- goto lbMotorCurTooBig ;最大值-当前值 < 13mV时,认为堵转
- movlw 0xff
- movwf motorMinCurH
- movwf motorMinCurL
- goto lbMotorRunDly
- lbMotorCurRisingHigh:
- bsf fEnWaitDly
- clrf tWaitDly
- movfw motorCurH
- movwf motorMaxCurH
- movfw motorCurL
- movwf motorMaxCurL
- goto lbMotorRunDly
-
- lbNormalRunChk: ;正常运转时,堵转监测
- movfw motorCurL
- subwf motorMinCurL,W
- movfw motorCurH
- subwfc motorMinCurH,W
- btfss c
- goto $+5
- movfw motorCurH
- movwf motorMinCurH
- movfw motorCurL
- movwf motorMinCurL
- movlw 160 ;55mV
- addwf motorMinCurL,F
- movlw 0
- addwfc motorMinCurH,F
- movfw motorCurL
- subwf motorMinCurL,W
- movfw motorCurH
- subwfc motorMinCurH,W
- btfss c
- goto lbMotorCurRisingHigh ;lbMotorCurTooBig ;当前值-最小值 > 55mV以上时,认为堵转
- movlw 160
- subwf motorMinCurL,F
- movlw 0
- subwfc motorMinCurH,F
-
- movlw low(BLOCK_CURRENT)
- subwf motorCurL,W
- movlw high(BLOCK_CURRENT)
- subwfc motorCurH,W
- btfsc c
- goto lbMotorCurTooBig ;当前值太大,认为堵转
- lbMotorRunDly:
- incf tRunMSec,F
- movfw tRunMSec
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbMotorCtrlExit
-
- clrf tRunMSec
- incf tMotorDly,F
- movfw tMotorDly
- sublw 10 ;10*0.5s=5s
- btfss c
- goto lbCloseMotor
- incf tRunSec,F
- movfw tRunSec
- sublw 59 ;60*0.5s=30s
- btfsc c
- goto lbMotorCtrlExit
- ;开启一分钟延时,如果一分钟之内再次开启,则累加次数,
- ;连续工作5次后,需等待10分钟才能重新启动
- bsf fWorkedDly
- clrf tWorkedDlySec
- clrf tWorkedDlyMsec
-
- incf cNormalWork,F
- movfw cNormalWork
- sublw 4 ;5次
- btfsc c
- goto lbCloseMotor
- bsf fNeedCoolDown
- clrf tCoolMin
- clrf tCoolSec
- clrf tCoolMsec
- lbCloseMotor:
- call lbChangeToOff
- lbMotorCtrlExit:
- return
- lbMotorCurTooBig:
- call lbChangeToOff
- bsf fEnAlarmDly
- clrf tAlarmDly ;三秒内不能按键
- clrf tWaitDly
- bsf fBuzOn
- movlw 5
- movwf cBuzTime ;响三次
- clrf tBuzTime
- goto lbMotorCtrlExit
- ;********************************************************************
- fnOverLoadCtrl:
- btfss fEnAlarmDly
- goto lbOverLoadCtrlExit
-
- movfw cBuzTime
- xorlw 0
- btfsc z
- goto lbOverLoadDlyCnt
-
- incf tBuzTime,F
- movfw tBuzTime
- sublw 61 ;62*2ms=124ms
- btfsc c
- goto lbOverLoadDlyCnt
-
- clrf tBuzTime
- movlw 1 ;fBuzOn
- xorwf buzFlag,F
- btfss fBuzOn
- bcf buz_o ;暂停期间,输出低电平
- decfsz cBuzTime,F
- goto lbOverLoadDlyCnt
- bcf fBuzOn
- bcf buz_o
- lbOverLoadDlyCnt:
- incf tWaitDly,F
- movfw tWaitDly
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbOverLoadCtrlExit
-
- clrf tWaitDly
- incf tAlarmDly,F
- movfw tAlarmDly
- sublw 5 ;6*0.5s=3s
- btfsc c
- goto lbOverLoadCtrlExit
- bcf fEnAlarmDly
- lbOverLoadCtrlExit:
- return
- ;********************************************************************
- fnWaitCoolDown:
- btfss fWorkedDly
- goto lbChkCoolDownDly
-
- incf tWorkedDlyMsec,F
- movfw tWorkedDlyMsec
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbChkCoolDownDly
-
- clrf tWorkedDlyMsec
- incf tWorkedDlySec,F
- movfw tWorkedDlySec
- sublw 119 ;120*0.5s=60s
- btfsc c
- goto lbChkCoolDownDly
-
- clrf tWorkedDlySec
- bcf fWorkedDly ;满1分钟,重新累加次数
- clrf cNormalWork ;清除累加次数
- lbChkCoolDownDly:
- btfss fNeedCoolDown
- goto lbWaitCoolDownExit
-
- incf tCoolMsec,F
- movfw tCoolMsec
- sublw 249 ;250*2ms=500ms
- btfsc c
- goto lbWaitCoolDownExit
-
- clrf tCoolMsec
- incf tCoolSec,F
- movfw tCoolSec
- sublw 119 ;120*0.5s=60s
- btfsc c
- goto lbWaitCoolDownExit
-
- clrf tCoolSec
- incf tCoolMin,F
- movfw tCoolMin
- sublw 9 ;10*1min=10min
- btfsc c
- goto lbWaitCoolDownExit
- bcf fNeedCoolDown
- clrf cNormalWork
- lbWaitCoolDownExit:
- return
- ;********************************************************************
- ;【说明】: 初始化
- ;********************************************************************
- ;程序开始
- fnAllDefaultSet:
- ;====================================================================
- ;MCK寄存器说明:建议按位操作,默认值为1010uuu0
- ;bit7:CST: 1=外部晶振关闭
- ; 0=外部晶振打开
- ;bit6:CST_IN: 1=内部晶振关闭
- ; 0=内部晶振打开
- ;bit5:CST_WDT: 1=内部WDT晶振关闭
- ; 0=内部WDT晶振打开
- ;bit4:EO_SLP : 1=如果选择的是外部低速晶振,在sleep模式下不关闭外部晶振、
- ; 0=sleep模式下关闭外部晶振
- ;bit0:CLK_SEL: 1=外部晶振系统时钟
- ; 0=内部晶振系统时钟
- ;====================================================================
- bcf cst_in
- bsf cst
- bcf cst_wdt
- bcf eo_slp
- bcf clksel
-
- ;====================================================================
- ;INTF中断标志寄存器,硬件置位,软件清零
- ;bit7: 未定义
- ;bit6: TM2IF 1=12-Bit定时/计数器2发生定时中断
- ; 0=12-Bit定时/计数器2未发生定时中断
- ;bit5: 未定义
- ;bit4: TM0IF 1=8-Bit定时器0发生定时中断
- ; 0=8-Bit定时器0未发生定时中断
- ;bit3: SRADIF 1=AD发生中断
- ; 0=AD未发生中断
- ;bit2: 未定义
- ;bit1: E1IF 1=外部中断1发生中断
- ; 0=外部中断1未发生中断
- ;bit0: E0IF 1=外部中断0发生中断
- ; 0=外部中断0未发生中断
- ;====================================================================
- clrf intf ;清除中断标志
- ;====================================================================
- ;INTF2中断标志寄存器,硬件置位,软件清零
- ;bit7: 未定义
- ;bit6: 未定义
- ;bit5: TM4IF 1=12-Bit定时/计数器4发生定时中断
- ; 0=12-Bit定时/计数器4未发生定时中断
- ;bit4: TM3IF 1=12-Bit定时/计数器3发生定时中断
- ; 0=12-Bit定时/计数器3未发生定时中断
- ;====================================================================
- clrf intf2
-
- ;====================================================================
- ;INTF3中断标志寄存器,硬件置位,软件清零
- ;bit7: CMPIF 1=比较器发生中断
- ; 0=比较器未发生中断
- ;bit6~bit2: 未定义
- ;bit1: URTIF 1=串口通讯发生发送中断
- ; 0=串口通讯未发生发送中断
- ;bit0: URRIF 1=串口通讯发生接收中断
- ; 0=串口通讯未发生接收中断
- ;====================================================================
- clrf intf3
- ;====================================================================
- ;INTE中断使能寄存器:
- ;bit7: GIE 1=使能所有非屏蔽中断
- ; 0=不使能所有中断
- ;bit6: TM2IE 1=使能12-Bit定时/计数器2中断
- ; 0=禁能12-Bit定时/计数器2中断
- ;bit5: 未定义
- ;bit4: TM0IE 1=使能8-Bit定时器0中断
- ; 0=禁能8-Bit定时器0中断
- ;bit3: SRADIE 1=使能AD中断
- ; 0=禁能AD中断
- ;bit2: 未定义
- ;bit1: E1IE 1=使能外部中断1
- ; 0=禁能外部中断1
- ;bit0: E0IE 1=使能外部中断0
- ; 0=禁能外部中断0
- ;====================================================================
- clrf inte
- ;====================================================================
- ;INTE2中断使能寄存器:
- ;bit7: 未定义
- ;bit6: 未定义
- ;bit5: TM4IE 1=使能12-Bit定时/计数器4中断
- ; 0=禁能12-Bit定时/计数器4中断
- ;bit4: TM3IE 1=使能12-Bit定时/计数器3中断
- ; 0=禁能12-Bit定时/计数器3中断
- ;bit3~bit0: 未定义
- ;====================================================================
- clrf inte2
- ;====================================================================
- ;INTE3中断使能寄存器:
- ;bit7: CMPIE 1=使能比较器中断
- ; 0=禁能比较器中断
- ;bit6~bit2: 未定义
- ;bit1: URTIE 1=使能UART的发送中断
- ; 0=禁能UART的发送中断
- ;bit0: URRIE 1=使能UART的接收中断
- ; 0=禁能UART的接收中断
- ;====================================================================
- clrf inte3
- ;====================================================================
- ;PT1CON:外部中断1、0中断触发沿设定
- ;bit7: PT11OD 1=使能PT1.1漏极开路
- ; 0=禁能PT1.1漏极开路
- ;bit6: PT1W3 1=使能PT1.5外部中断1
- ; 0=禁能PT1.5外部中断1
- ;bit5: PT1W2 1=使能PT1.4外部中断1
- ; 0=禁能PT1.4外部中断1
- ;bit4: PT1W1 1=使能PT1.3外部中断1
- ; 0=禁能PT1.3外部中断1
- ;bit3: PT1W0 1=使能PT1.1外部中断1
- ; 0=禁能PT1.1外部中断1
- ;bit2: E1M 1=外部中断1为下降沿触发
- ; 0=外部中断1为状态改变时触发
- ;bit1~0:E0M 11=外部中断0为状态改变时触发
- ; 10=外部中断0为状态改变时触发
- ; 01=外部中断0为上升沿触发
- ; 00=外部中断0为下降沿触发
- ;====================================================================
- ;本例程没有使用该功能,若用户使用,请自行添加
- ;====================================================================
- ;PT1CON1:外部中断1设定
- ;bit7~bit4: 未定义
- ;bit3: PT1W23 1=使能PT3.1外部中断1
- ; 0=禁能PT3.1外部中断1
- ;bit2: PT1W22 1=使能PT1.7外部中断1
- ; 0=禁能PT1.7外部中断1
- ;bit1: PT1W21 1=使能PT1.6外部中断1
- ; 0=禁能PT1.6外部中断1
- ;bit0: PT1W20 1=使能PT1.2外部中断1
- ; 0=禁能PT1.2外部中断1
- ;====================================================================
- ;本例程没有使用该功能,若用户使用,请自行添加
- ;====================================================================
- ;-----------SRADCON0-----------:
- ;bit7/bit6: 未定义
- ;bit5/bit4: SRADACKS[1:0]ADC输入信号获取时间
- ; 00=16个ADC时钟 01=8个ADC时钟 10=4个ADC时钟 11=2个ADC时钟
- ;bit3/bit2: 未定义
- ;bit1/bit0: SRADCKS[1:0]ADC时钟
- ; 00=CPUCLK 01=CPUCLK/2 10=CPUCLK/4 11=CPUCLK/8
- ;-----------SRADCON1-----------:
- ;bit7: SRADEN :ADC使能位 1=使能 0=禁止
- ;bit6: SRADS :ADC启动位/状态控制位 1=开始,转换过程中 0=停止,转换结束
- ;bit5: OFTEN :转换结果选择控制位 1=存放在SROFT寄存器 0=存放在SRAD寄存器
- ;bit4: CALIF :矫正控制位(OFTEN为0时有效) 1=使能矫正 0=禁止矫正
- ;bit3: ENOV :使能比较器溢出模式(CALIF为1时有效) 1=使能 0=禁止
- ;bit2: OFFEX :OFFSET交换 1=比较器两端信号交换 0=不交换(正端为信号,负端为参考电压)
- ;bit1/bit0:VREFS[1:0]ADC参考电源选择,不同参考电源切换,建议延迟10us再做AD转换
- ; 00=VDD
- ; 01=PT3.0外部参考电源输入
- ; 10=内部参考电压
- ; 11=内部参考电压,PT3.0可外接电容做为内置参考电压滤波使用,以提高精度
- ;-----------SRADCON2-----------:
- ;bit7~bit4: CHS[3:0]ADC输入通道选择位
- ; 0000=AIN0 0001=AIN1 0010=AIN2 0011=AIN3
- ; 0100=AIN4 0101=AIN5 0110=AIN6 0111=AIN7
- ; 1000=AIN8 1001=内部1/8VDD 1010=内部参考电压
- ;bit3/bit2: 未定义
- ;bit1/bit0: REF_SEL[1:0] VREFS[1:0]配置为10或11,则可通过REF_SEL[1:0]选择如下
- ; 参考电压,若VREFS[1:0]不是配置为10或11,以下位无效
- ; 00=1.4V 01=2.0V 10=3.0V 11=4.0V
- ;-----------SRADH/SRADL--------:
- ;SRADH: SRAD[11:8]ADC数据高4位,只可读
- ;SRADL: SRAD[7:0]ADC数据低8位,只可读
- ;-----------SROFTH/SROFTL------:
- ;SROFTH:SROFT[11:8]矫正值数据的高4位
- ;SROFTL:SROFT[7:0]矫正值数据的低8位
- ;-----------METCH--------------:
- ;bit5: REFOEN: 内部参考电压从PT3.0输出使能位,输出REF_SEL[1:0]选择的内部参考电压
- ; 0=禁止内部参考电压输出
- ; 1=使能内部参考电压输出
- ;bit4: REFIN: 外部参考电压输入端口选择
- ; 0=选择PT3.0作为参考电压输入
- ; 1=选择PT3.2作为参考电压输入
- ;bit3: METCH[3]:0=禁止ERC时钟输出
- ; 1=使能ERC时钟通过PT1.5口输出
- ;bit2: METCH[2]:0=使能寄存器TRIM_EX的值来微调ERC频率
- ; 1=禁止ERC频率微调控制
- ;bit0: P13OD: PT1.3开漏输出使能
- ; 0=做为输入口 1=开漏输出
- ;====================================================================
- movlw 00110000b ;ADC输入信号获取时间:2个ADC时钟,采样时间:CPUCLK
- movwf sradcon0
- movlw 00000000b ;参考电压:VDD
- movwf sradcon1
- movlw 01000000b ;AIN4
- movwf sradcon2
- clrf sradl
- clrf sradh
- clrf sroftl
- clrf srofth
- bsf sraden ;使能ADC
- ;====================================================================
- ;SYSCFG0:输入逻辑电平电压配置
- ;bit7:VTHSEL --------------------------------------------------
- ; | |符号| 参数 |最小值 |典型值|最大值 |单位|
- ; |--+----+--------------+-------+------+-------+----|
- ; | | |数字输入高电平|0.75Vdd| | | V |
- ; | |VIH1|--------------+-------+------+-------+----|
- ; | | |复位输入高电平|0.8Vdd | | | V |
- ; | 0|----+--------------+-------+------+-------+----|
- ; | | |数字输入低电平| | |0.3Vdd | V |
- ; | |VIL1|--------------+-------+------+-------+----|
- ; | | |复位输入低电平| | |0.2Vdd | V |
- ; |--+----+--------------+-------+------+-------+----|
- ; | | |数字输入高电平|0.5Vdd | | | V |
- ; | |VIH2|--------------+-------+------+-------+----|
- ; | | |复位输入高电平|0.5Vdd | | | V |
- ; | 1|-------------------+-------+------+-------+----|
- ; | | |数字输入低电平| | |0.2Vdd | V |
- ; | |VIL2|--------------+-------+------+-------+----|
- ; | | |复位输入低电平| | |0.1Vdd | V |
- ; --------------------------------------------------
- ;bit5~4:VTHP30-----------------------------------------------
- ; | |符号| 参数 |最小值 |典型值 |最大值 |单位|
- ; |--+----+----------+-------+-------+-------+----|
- ; | |VIH1|输入高电平|0.75Vdd| | | V |
- ; |00|----+----------+-------+-------+-------+----|
- ; | |VIL1|输入低电平| | |0.3Vdd | V |
- ; |--+----+----------+-------+-------+-------+----|
- ; | |VIH |输入高电平| | 0.7 | | V |
- ; |01|----+----------+-------+-------+-------+----|
- ; PT3.0 | |VIL |输入低电平| | 0.7 | | V |
- ; |--+----+----------+-------+-------+-------+----|
- ; | |VIH |输入高电平| |Vdd-0.7| | V |
- ; |10|----+----------+-------+-------+-------+----|
- ; | |VIL |输入低电平| |Vdd-0.7| | V |
- ; |--+----+----------+-------+-------+-------+----|
- ; | |VIH2|输入高电平|0.5Vdd | | | V |
- ; |11|----+----------+-------+-------+-------+----|
- ; | |VIL2|输入低电平| | |0.2Vdd | V |
- ; -----------------------------------------------
- ;bit3:P30REFEN 0=关闭内部基准
- ; 1=打开内部基准
- ;====================================================================
- ;====================================================================
- ;定时器0配置,相关寄存器为:INTF/INTE/TM0CON/TM0IN/TM0CNT
- ;TM0CON:
- ;bit7(T0EN):0=禁止定时器0; 1=使能定时器0
- ;bit6~4(T0RATE2:0):定时器0时钟分频选择
- ; 000=CKT0 001=CKT0/2 010=CKT0/4 011=CKT0/8
- ; 100=CKT0/16 101=CKT0/32 110=CKT0/64 111=CKT0/128
- ;bit3:未定义
- ;bit2:(T0RSTB):定时器0复位 0=使能定时器0复位 1=禁止定时器0复位
- ;bit1~0(T0SEL[1:0]):时钟源选择 00=CPUCLK
- ; 01=MCK
- ; 10=外部32768Hz晶振时钟
- ; 11=内部32768 WDT时钟
- ;TM0IN:定时器0溢出值
- ;TM0CNT:定时器0计数寄存器
- ;====================================================================
- ifdef _DEBUG_
- ;{
- movlw 0x20
- movwf tm0con
- movlw 103 ;9600bps
- movwf tm0in
- ;}
- else
- ;{
- movlw SYS_BAS_CLK
- movwf tm0con
- movlw SYS_BAS_CNT
- movwf tm0in
- ;}
- endif
- bcf t0rstb
- bsf t0en
- bsf tm0ie
- ;====================================================================
- ;定时/计数器2配置,相关寄存器为:INTF/INTE/TM2CON/TM2IN/TM2CNT/TM2R
- ;/TM2INH/TM2CNTH/TM2RH/TMCON/TMCON2
- ;TM2CON:
- ;;bit7(T2EN):0=禁止定时器2; 1=使能定时器2
- ;bit6~4(T2RATE2:0):定时器2时钟分频选择
- ; 000=CKT2 001=CKT2/2 010=CKT2/4 011=CKT2/8
- ; 100=CKT2/16 101=CKT2/32 110=CKT2/64 111=CKT2/128
- ;bit3:T2CKS
- ; 1=PT3.0做为时钟源 0=CKT2的分频时钟做为时钟源
- ;bit2:(T2RSTB):定时器2复位 0=使能定时器2复位 1=禁止定时器2复位
- ;bit1:T2OUT PT1.4口输出控制 PWM2PO PWM2OUT T2OUT
- ;bit0:PWM2OUT 0 0 0 IO输出
- ; 0 0 1 蜂鸣器输出
- ; 0 1 0 PWM2输出
- ; 0 1 1 PWM2输出
- ; 1 x x IO输出
- ;TM2IN: 定时器2溢出值
- ;TM2CNT:定时器2计数寄存器
- ;TM2R: 定时/计数器2的PWM高电平占空比控制寄存器
- ;TM2INH:定时/计数器2溢出值高4位
- ;TM2CNTH:定时/计数器2计数寄存器高4位
- ;TM2RH :定时/计数器2的PWM高电平占空比控制寄存器高4位
- ;====================================================================
- bcf t2sel1 ;MCK做为时钟源
- bsf t2sel0 ;
- movlw 0x00 ;1:1,关闭PWM2输出
- movwf tm2con
- movlw 127
- movwf tm2in ;125kHZ
- clrf tm2inh
- clrf tm2r
- clrf tm2rh
- bcf t2rstb
- bcf pwm2po ;使能PT1.4口做为TM2的输出
- bsf t2en
- ;====================================================================
- ;定时/计数器3配置,相关寄存器为:INTF2/INTE2/TM3CON/TM3IN/TM3CNT/TM3R
- ;/TM3INH/TM3CNTH/TM3RH/TMCON/TM3CON2/TMCON2
- ;TM3CON:
- ;;bit7(T3EN):0=禁止定时器3; 1=使能定时器3
- ;bit6~4(T3RATE2:0):定时器3时钟分频选择
- ; 000=CKT3 001=CKT3/2 010=CKT3/4 011=CKT3/8
- ; 100=CKT3/16 101=CKT3/32 110=CKT3/64 111=CKT3/128
- ;bit3:T3CKS
- ; 1=PT3.1做为时钟源 0=CKT3的分频时钟做为时钟源
- ;bit2:(T3RSTB):定时器3复位 0=使能定时器3复位 1=禁止定时器3复位
- ;bit1:T3OUT PT1.5口输出控制 PWM3PO PWM3OUT T3OUT
- ;bit0:PWM3OUT 0 0 0 IO输出
- ; 0 0 1 蜂鸣器输出
- ; 0 1 0 PWM3输出
- ; 0 1 1 PWM3输出
- ; 1 x x IO输出
- ;TM3IN: 定时器3溢出值
- ;TM3CNT:定时器3计数寄存器
- ;TM3R: 定时/计数器3的PWM高电平占空比控制寄存器
- ;TM3INH:定时/计数器3溢出值高4位
- ;TM3CNTH:定时/计数器3计数寄存器高4位
- ;TM3RH :定时/计数器3的PWM高电平占空比控制寄存器高4位
- ;====================================================================
- ;====================================================================
- ;定时/计数器4配置,相关寄存器为:INTF2/INTE/TM4CON/TM4IN/TM4CNT/TM4R
- ;/TM4INH/TM4CNTH/TM4RH/TMCON/TMCON2
- ;TM4CON:
- ;;bit7(T4EN):0=禁止定时器4; 1=使能定时器4
- ;bit6~4(T4RATE2:0):定时器4时钟分频选择
- ; 000=CKT4 001=CKT4/2 010=CKT4/4 011=CKT4/8
- ; 100=CKT4/16 101=CKT4/32 110=CKT4/64 111=CKT4/128
- ;bit3:T4CKS
- ; 1=PT1.4做为时钟源 0=CKT4的分频时钟做为时钟源
- ;bit2:(T4RSTB):定时器4复位 0=使能定时器4复位 1=禁止定时器4复位
- ;bit1:T4OUT PT1.6口输出控制 PWM4PO PWM4OUT T4OUT
- ;bit0:PWM4OUT 0 0 0 IO输出
- ; 0 0 1 蜂鸣器输出
- ; 0 1 0 PWM4输出
- ; 0 1 1 PWM4输出
- ; 1 x x IO输出
- ;TM4IN: 定时器4溢出值
- ;TM4CNT:定时器4计数寄存器
- ;TM4R: 定时/计数器4的PWM高电平占空比控制寄存器
- ;TM4INH:定时/计数器4溢出值高4位
- ;TM4CNTH:定时/计数器4计数寄存器高4位
- ;TM4RH :定时/计数器4的PWM高电平占空比控制寄存器高4位
- ;====================================================================
-
- ;====================================================================
- bsf tm0ie
- bsf gie
-
- btfsc to
- goto lbAllDefaultSetEnd ;看门狗复位时不初始化RAM
- lbClrRam:
- ;--------清除RAM----------
- movlw 0x80
- movwf fsr0
- lbClrRamLoop:
- clrf ind0
- incfsz fsr0,F
- goto lbClrRamLoop
-
- bsf fRstRun
- call fnInitSamp
- lbPwrRstWait:
- btfss fBat2ms
- goto $-1
- bcf fBat2ms
- incf var1,F
- movfw var1
- sublw 49 ;50*2ms=100ms
- btfsc c
- goto lbPwrRstWait
- clrf var1
-
- ;====================================================================
- ;IO口初始化部分(x代表1/3)
- ;PTx............IO口寄存器
- ;
- ;PTxEN..........输入/输出控制寄存器:0=输入,1=输出
- ;
- ;PTxPU..........上拉电阻控制寄存器: 0=禁能,1=使能
- ;
- ;PT1CON[7...0]..见上描述
- ;
- ;PT3CON[7...0]..模拟/数字口使能控制:0=数字口 1=模拟口
- ;
- ;PT3OD[6.5].....PT3.6/PT3.5开漏设定:0=禁止开漏输出,1=使能开漏输出
- ;
- ;PT5[2...0]
- ;
- ;PT5EN[2...0]
- ;
- ;PT5PU[2...0]
- ;
- ;PT5CON[2...0]:bit2: 0=PT5.1禁止开漏输出,1=使能PT5.1开漏输出
- ; bit1: 0=PT5.0禁止开漏输出,1=使能PT5.1开漏输出
- ; bit0: 0=PT5.0为数字口,1=PT5.0为模拟口
- ;
- ;EX0CFG.........PT3.0口输入控制
- ;bit7~bit6......PWMRL[1:0]:外部中断0触发模式 00=外部中断0不受PWM2/PWM3/PWM4影响
- ; 01=外部中断0只受PWM2影响,只有在PWM2输出特定极性时有效
- ; 10=外部中断0只受PWM3影响,只有在PWM3输出特定极性时有效
- ; 11=外部中断0只受PWM4影响,只有在PWM4输出特定极性时有效
- ;bit5...........POLSEL:外部中断0触发极性选择,只在PWMRL[1:0]不为00时有效
- ; 0=PWM输出为高时外部中断0有效,PWM输出为低时,外部中断0被屏蔽
- ; 1=PWM输出为低时外部中断0有效,PWM输出为高时,外部中断0被屏蔽
- ;bit1~bit0......EX0FILT[1:0]:外部中断0滤波时间选择 00=滤波时间为0us
- ; 01=滤波时间为5us
- ; 10=滤波时间为10us
- ; 11=滤波时间为15us
- ;
- ;CURCON:端口输出电流选择寄存器
- ; bit7: P36CUR 0=PT3.6输出电流Ioh/Iol为20mA/20mA@5V
- ; 1=PT3.6输出电流Ioh/Iol为54mA/57mA@5V
- ; bit6: P35CUR 0=PT3.5输出电流Ioh/Iol为20mA/20mA@5V
- ; 1=PT3.5输出电流Ioh/Iol为54mA/57mA@5V
- ; bit5~4:P14CUR[1:0] 00=PT1.4输出电流Ioh/Iol为20mA/20mA@5V...\
- ; 01=PT1.4输出电流Ioh/Iol为54mA/57mA@5V... |com[0]为0时有效
- ; 1x=PT1.4输出电流Ioh/Iol为31mA/35mA@5V.../
- ; bit2~1:CURP17[1:0] 00=PT1.7输出电流Ioh/Iol为20mA/20mA@5V...\
- ; 01=PT1.7输出电流Ioh/Iol为1.5mA/20mA@5V...|com[3]为0时有效
- ; 1x=PT1.7输出电流Ioh/Iol为31mA/35mA@5V.../
- ;
- ;SYSCFG1:
- ; bit7: PDP13 0=PT1.3不接下拉电阻
- ; 1=PT1.3接400K下拉电阻
- ; bit6: PDP31 0=PT3.1不接下拉电阻
- ; 1=PT3.1接1K下拉电阻
- ; bit5: PDP34 0=PT3.4不接下拉电阻
- ; 1=PT3.4接10K下拉电阻(默认)
- ; bit4: PDP36 0=PT3.6不接下拉电阻
- ; 1=PT3.6接10K下拉电阻(默认)
- ; bit3~2: CURP16 00=PT1.6输出电流Ioh/Iol为20mA/20mA@5V...\
- ; 01=PT1.6输出电流Ioh/Iol为1.3mA/20mA@5V...|com[2]为0时有效
- ; 1x=PT1.6输出电流Ioh/Iol为31mA/35mA@5V.../
- ; bit1~0: CURP15 00=PT1.5输出电流Ioh/Iol为20mA/20mA@5V...\
- ; 01=PT1.5输出电流Ioh/Iol为1.3mA/20mA@5V...|com[1]为0时有效
- ; 10=PT1.5输出电流Ioh/Iol为2.6mA/20mA@5V...|
- ; 11=PT1.5输出电流Ioh/Iol为31mA/35mA@5V.../
- ;====================================================================
- clrf curcon ;关闭大电流
- clrf syscfg1 ;关闭内部下拉电阻
-
- movlw 0x00
- movwf pt1
- movlw 00001000b
- movwf pt1pu
- movlw 11110111b
- movwf pt1en ;led1/motor/led2/pwm/key/led4/led3/x
-
- movlw 0x08
- movwf pt3
- clrf pt3pu
- movlw 01110001b
- movwf pt3con
- movlw 10001100b ;x/bat2/cur/bat1/pwr/buz/hall/usb
- movwf pt3en
- lbFirstChkUsb:
- call fnSelectUSBDc
- call fnReadADC
- movlw low(USB_3000MV)
- subwf var2,W
- movlw high(USB_3000MV)
- subwfc var1,W
- btfss c
- goto lbNoUsbPlug
- lbUsbPlugIn:
- clrf var4
- incf var3,F
- movfw var3
- sublw 10
- btfsc c
- goto lbFirstChkUsb
- clrf volume
- goto lbContinueInit
- lbNoUsbPlug:
- clrf var3
- incf var4,F
- movfw var4
- sublw 10
- btfsc c
- goto lbFirstChkUsb
- movlw BAT_VOLUME_100
- movwf volume
- lbContinueInit:
- ;====================================================================
- ;WDTCON
- ;bit7(WDTEN) :0=禁止WDT 1=使能WDT
- ;bit2~0(WDTS) :当WDTIN=0xFF时
- ; :000=2048ms 001=1024ms 010=512ms 011=256ms
- ; :100=128ms 101=64ms 110=32ms 111=16ms
- ;====================================================================
- movlw 0x02
- movwf wdtcon
- movlw 0xff
- movwf wdtin
- bsf wdten
- bcf cst_wdt
- lbAllDefaultSetEnd:
- return
- ;******************************************************************************
- ;【说明】:中断处理程序,T0做为系统时钟的定时基准,为2ms
- ;******************************************************************************
- intService:
- push
- btfss tm0ie
-
- …………
- …………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
全部资料51hei下载地址:
V0.0.111.zip
(54.15 KB, 下载次数: 21)
果汁-V05.zip
(1 MB, 下载次数: 20)
|