📄 qt2chgrdemo.asm
字号:
base $10
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Motorola Semiconductors (H.K.) Ltd.
* 8/16 bit MCU - Application
*
* FileName : QT2CHGRDEMO.asm
* Title : One Lithium Ion Cell Charger Reference Code
* MCU : MC68HC908QT2
* Assembler : P&E Microcomputer Systems - CASM08Z (v3.16)
* Include File : i) "QT2CHGR_INC.asm"
* ii) "MC68HC908QT4.equ"
* Author : Roger Fan
*
* DD/MM/YY Rev. Modified comments.
* History : 3/03/03 1.0 Initial release
* modified from QY_CGR0701.asm
* 3/05/03 1.1 Use PTA2/IRQ pin for battery ID detect
* LED on off control table as below
* A.) User Interfaces
* The following is a summary of the dual-colors LED display conditions:
* Dual-colors LED Indicator
* Item Condition
* RED GREEN
* ---------------
* Fast Charge in progress On Off ( Red )
* Battery is fully charged Off On (Green)
* Battery is not connected On On (Red/Green)
* Fault condition occurred --Flashing-- (Red/Green)
*
* B.) Battery insert detected by PTA2/IRQ pin voltage, a 10K pull up is connected to
* this pin. ==> Battery removed = 1
* Battery inserted = 0
*
* In this reference, the Li-ion battery under charging do not have a built-in
* battery sensor. And the T pin is connected to ground. So this reference demo
* use the QT2ˇs input only pin, IRQ/PTA2 as battery ID detection pin. A 10K
* pull-up is connected to this pin. When battery is not connected or removed,
* this pin is reading high = 1. When battery is inserted. It is reading low =0.
*
*
* NOTE: TCH0 IS USED AS PWM FUNCTION, NOT VREF NEED. THIS CHARGER NEED 1% VDD AS REFERENCE.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Disclaimer of All Warranties & Liabilities :
* This Program is a freeware to demonstrate the operation of HC08 micro-
* controller. In no event will Motorola be liable for any damages, or any
* incidental or consequential damages arising out of the use of or
* inability to use this program. User agrees that Motorola does not make
* any warranties of any kind that the program does not or will not
* infringe any copyright, patent, trade secret or other intellectual
* property right of any third party in any country.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Features:
* . HC08 CPU core
* . 3.2MHz Internal Bus Operation (using ICG, trimmed)
* . $EE00 ~ $FDFF ---- 4K bytes FLASH
* . $0080 ~ $00FF --- 128 bytes RAM
* . 4-channel ADC
* . 16-bit 2 channels Timer Interface Module(TIM)
*
* 908QT2 Pin Assignment (8-PDIP package):
* Pin Pin Name | Pin Pin Name
* --- -------------------- | --- ----------------
* 1 Vdd | 8 Vss
* 2 AD3/PA5 - Vbat | 7 TCH0/PA0 - PWM
* 3 AD2/PA4 - Isense | 6 AD1/PA1 - Tsense (VR)
* 4 RST/PA3 - LED Red/Gr | 5 IRQ/PA2 - Bat_ID
* --- ------------------------------------------
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Note:
* - /VPP & RST# pins equipped with internal pullup R & Schmitt trig. i/p
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
$include "MC68HC908QT4.equ"
ORG RamStart ;$0080
ADTOT DS 1
TEMP DS 1
COUNT DS 1
PWM_duty_H ds 1
PWM_duty_L ds 1
temp1 ds 1
temp2 ds 1
Port_CTRL equ PTA ; Control Port
DDR_CTRL equ DDRA ;
b_TSDta equ 3 ; RS232 Tx data line, PTA3
V_IRbyte ds 1 ; Tx byte
V_DlyX ds 1 ; used only in delay routines
data ds 1 ; data to be transmitted
VOLTAGE ds 1
CURRENT ds 1
TEMPERATURE ds 1
Temp_Buff ds 1 ; temperature buffer
chrg_tim_cnt_L ds 1 ; charging_time_count_min
chrg_tim_cnt_H ds 1 ; charging_time_count_hour
BufferH ds 1
BufferL ds 1
ADC_COUNT DS 1
tim_cnt_high ds 1
tim_cnt_low ds 1
ms500_cnt ds 1 ; 500ms
Green_LED equ 3 ; PTA
Red_LED equ 3 ; PTA
Flag ds 1
TEMP_FLAG equ 0 ; bit 0 of FLAG set = T fail, clear = T okay
CUR_FLAG equ 1 ; bit 1
VOLT_FLAG equ 2 ; bit 2
timer_tick equ 3 ; bit 3 of FLAG
time_overflow equ 4 ; bit 4 of FLAG
bat_ID equ 5 ; bit 5 of FLAG, 0=battery found. 1=not battery
trickle_fin equ 6 ; bit 6 of FLAG, trickle_charge_complete
bat_v_valid equ 7 ; bit 7 of FLAG, battery voltage valid check
FLAG1 ds 1
bat_full equ 0 ; bit 0 of FLAG1
trickle equ 1 ; 1 = in trickle charge mode
fast equ 2 ; 1 = fast charge mode
CC_error equ 3 ; charge current fault = 1
rs232_tick equ 4 ; output V, I, T data when this flag set
bat_remove_f equ 5 ; 1=bat_removed, 0=bat_not_removed
org FlashStart ; $EE00
$include "QT2CHGR_INC.asm"
LED_Flashing
BSET Green_LED,PTA ; turn on Green LED for 2 seconds
jsr DLY_500ms
BCLR Red_LED,PTA ; turn on Red LED for 2 seconds
jsr DLY_500ms
BSET Green_LED,PTA ; turn on Green LED for 2 seconds
jsr DLY_500ms
BCLR Red_LED,PTA ; turn on Red LED for 2 seconds
jsr DLY_500ms
rts
START: SEI
ldhx #$FF+1 ; initialize
txs ; the stack pointer
mov #%00000001,CONFIG1 ; disable COP,LVI enable(3V), disable STOP, long COP rate
mov #%10000000,CONFIG2 ; RST pin function disable, select int. oscillator,
; IRQ disable at PTA2 without internal pull-up
BSET 2,INTSCR ; CLEAR IRQF
jsr enable_int_clk ; enable ICG for device only
*********************************************************************
* Clear all RAM from $0080 to $00FF *
*********************************************************************
ClrRAM
sta $ffff ; CLEAR COP
ldhx #RamStart ; point to start of RAM
ClearRAM:
clr ,x ; clear RAM location
aix #1 ; advance pointer
cphx #RamLast+1 ; done ?
bne ClearRAM ; loop back if not
; Init the I/Os
clr PTA
mov #%00001001,DDRA ; PTA0,PTA3 as output, PTA3 for LED(Green & Red)
; PTA2 = input only pin
; Check Bus Frequency at PTA3
jsr TOGGLE_IO ; check bus frequency, (should be removed in real application)
JSR Init_TIM_dis_PWM ; timer init. with PWM disable and TCH0 output low
clr FLAG ; clear all bits of the FLAG
* Initialize the ADC module
MOV #$40,ADICLK ; ADC CLOCK = BUSCLOCK / 4 = 0.8MHz with 12.8MHz ICG
CLI
*************************************************************
Main jsr LED_FLashing
bclr 3,DDRA ; PTA3 in input state (turn on both Green and Red LEDs)
Main_1
mov #$19,ms500_cnt ; 500ms
clr chrg_tim_cnt_L ; charging_time_count_L
clr chrg_tim_cnt_H ; charging_time_count_H
bclr TOIE,TSC ; disable TOF interrupt
* check battery insert
lp1 jsr bat_ID_check ; check battery insertion
brclr bat_ID,FLAG,lp1 ; loop until battery inserted
* check temperature range
jsr TEMP_check ; check temperature range
brset TEMP_FLAG,FLAG,CHARGER_ERR_T ; if set means temperature out of range
* check battery valid
jsr bat_ID_check
brclr bat_ID,FLAG,CHARGER_ERR_ID ; if battery ID not valid then exit
* check battery voltage
jsr bat_valid_v_chk
brset bat_full,FLAG,charge_complete_V
bat_V_check
brclr bat_v_valid,FLAG,CHARGER_ERR_V ; if clear => not valid
JSR Init_TIM ; init timer and PWM
* check battery voltage for trickle charger if voltage in range 1.0 ~ 2.5V
lda VOLTAGE
cmp #$91 ; > 2.5V?
bhi fast_charge
jmp trickle_charge ; start trikle charge
Start_0 BRA START
charge_complete_V
jsr DLY_500ms
jsr DLY_500ms
jsr bat_ID_check
brclr bat_ID,FLAG,bat_V_check
charge_complete
clr TCH0H ; write Hi byte first
clr TCH0L
bclr bat_full,FLAG
mov #%00010000,TSC0 ; ch0 int. disable, TCH0 as I/O with o/p Low => disable PWM
bset 3,DDRA ; set PTA3 as output for control LEDs
bset Green_LED,PTA ; turn on Green LED
clr chrg_tim_cnt_L ; charging_time_count_L
clr chrg_tim_cnt_H ; charging_time_count_H
bclr TOIE,TSC ; disable TOF interrupt
bclr CH0IE,TSC0 ; disable TCH0 interrupt
CF_lp
jsr bat_ID_check ; check battery removed
brclr bat_ID,FLAG,Start_0 ; if removed then jump to start
jsr bat_V_check
LDA VOLTAGE
CMP #$EC ; 4.0V?
beq CF_lp
bhi CF_lp
bra Start_0 ; if battery voltage drop below 4.0V, then restart
;-------------------------------------------------------------------------
CHARGER_ERR_T
CHARGER_ERR_ID
CHARGER_ERR_V
CHARGER_ERR_I
jmp CHRG_Fault
;---------------------------------------------------------------------------
*******************************************************************************
fast_charge
* if temperature range okay, battery detection okay, battery voltage okay, then do follows
bset 3,DDRA
bclr Red_LED,PTA ; turn-on Red LED
mov #$00,TCH0H
mov #$02,TCH0L ; charging current starting = a few mA
mov #$07,chrg_tim_cnt_H ; $0718 => (7-1)*256+161=1697
mov #$A1,chrg_tim_cnt_L ; 1697*5.24288s=8897s=148mins =2 hours 28 mins=max charging time
; mov #$01,chrg_tim_cnt_H
; mov #15,chrg_tim_cnt_L ; 15*5.24 = 78.6s, for testing purpose
lda TSC0 ;
bclr CH0F,TSC0 ; clear CH0F flag
MOV #$1A,TSC0 ; prescaler, set up channel 0 for unbuffered PWM
bset CH0IE,TSC0 ; enable timer CH0 interrupt
bset TOIE,TSC ; enable tof interrupt for start charging time counting
bclr trickle,FLAG1
bset fast,FLAG1 ;
CC_loop
jsr CC_charging ; const. current charging control
brset CC_error,FLAG1,CHARGER_ERR_I ; charging current overrange
jsr bat_V_chk
CMP #$EE ; 4.1V?
beq CV_loop
bhi CV_loop
jsr TEMP_check ; check temperature range
brset TEMP_FLAG,FLAG,CHARGER_ERR_T ; if set, means temperature out of range
jsr check_tim_out ;
brset time_overflow,FLAG,CHARGE_FULL
jsr check_bat_remove
brset bat_remove_f,FLAG1,bat_remove ; battery removed? (high= battery removed)
;jsr Tx_data ; transmit the data out, for testing
bra CC_loop
CV_loop jsr bat_I_chk
jsr CV_charging ; const. voltage charging control
lda VOLTAGE
;------------
; if voltage > 4.21V, implies battery full
CMP #$F4 ; if >=4.21V, then battery full
beq CV_exit
BHI CV_exit
;------------
check_current
lda CURRENT
CMP #$01 ; < = 40mA?
; CMP #$00 ; <20mA?
beq CV_exit
blo CV_exit
jsr TEMP_check ; check temperature range
brset TEMP_FLAG,FLAG,CHARGER_ERR_T ; if set, means temperature out of range
jsr check_tim_out
brset time_overflow,FLAG,CV_exit ; time overflow
jsr check_bat_remove
brset bat_remove_f,FLAG1,bat_remove1 ; battery remove from socket?
;jsr Tx_data ; transmit the data out every 500ms
; jsr dly_500ms
bra CV_loop
CV_exit
jsr Tx_full ; tx the last V, I, T data
CHARGE_FULL
jmp charge_complete
bat_remove
jsr DLY_20ms
jsr check_bat_remove
brset bat_remove_f,FLAG1,bat_removed ; battery remove from socket?
bra CC_loop
bat_remove1
jsr DLY_20ms
jsr check_bat_remove
brset bat_remove_f,FLAG1,bat_removed ; battery remove from socket?
bra CV_loop
bat_remove2
jsr DLY_20ms
jsr check_bat_remove
brset bat_remove_f,FLAG1,bat_removed ; battery remove from socket?
bra trickle_loop
bat_removed
mov #%00001000,TSC0 ; disable PWM
bclr 0,PTA ; output low to PWM_ctrl pin
bclr 0,DDRA ; change the PWM_ctrl pin to input
clr TCH0L
clr TCH0H
clr PWM_duty_L
clr PWM_duty_H
mov PWM_duty_H,TCH0H ; write Hi byte first
mov PWM_duty_L,TCH0L
bclr 3,DDRA ; turn on both LEDs, PTA3 = input
jmp Main_1
*********************************************************************
* Trickle Charge
*********************************************************************
trickle_charge
lda TSC0
bclr CH0F,TSC0 ; clear CHOF flag
lda TSC ;
bclr TOF,TSC ; clear TOF flag
bset 3,DDRA ; set PTA3 output for control LEDs
bclr Red_LED,PTA ; turn-on Red LED
mov #$01,chrg_tim_cnt_H ; $018D => (1-1)*256+141=141
mov #$2D,chrg_tim_cnt_L ; 141*5.24288s=739s=12mins
; mov #$01,chrg_tim_cnt_H
; mov #$07,chrg_tim_cnt_L ; 7*5.24=36s, for testing purpose
mov #$00,PWM_duty_H
mov #$02,PWM_duty_L
mov #$00,TCH0H
mov #$02,TCH0L ; charging current starting = a few mA
MOV #$1A,TSC0 ; prescaler, set up channel 0 for unbuffered PWM
bset CH0IE,TSC0 ; enable timer CHO interrupt
bset TOIE,TSC ; enable tof interrupt for start charging time counti
bclr fast,FLAG1
bset trickle,FLAG1
bclr trickle_fin,FLAG
trickle_loop
jsr check_bat_remove
brset bat_remove_f,FLAG1,bat_remove2 ; battery remove from socket?
jsr TEMP_check ; check temperature range
brset TEMP_FLAG,FLAG,CHRG_FAULT ; if set, means temperature out of range
jsr bat_I_chk
CMP #$0B ; >380mA
bhi CHRG_FAULT ; error, over current
CMP #$02 ; 2=75mA?
BLO Inc_Current ; if current < 75mA
BEQ exit_CC_Trick ; if equal then let it be
Dec_Current ; if current > 75mA
lda PWM_duty_L
cmp #$00 ; 01/FF = 01/255 = 1% duty
beq exit_CC_Trick
dec PWM_duty_L
bra exit_CC_Trick
Inc_Current
lda PWM_duty_L
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -