摘要:本文介紹了兩個(gè)固件,用作Teridian? 71M6521電表IC的演示程序,版本號(hào)為4.8p。這些固件有助于改善71M6521在功率模式之間轉(zhuǎn)換時(shí)的可靠性,并在器件恢復(fù)工作模式后提供適當(dāng)?shù)?a href="http://www.nxhydt.com/tags/實(shí)時(shí)時(shí)鐘/" target="_blank">實(shí)時(shí)時(shí)鐘(RTC)修正。文章討論了程序修改及設(shè)計(jì)細(xì)節(jié),便于用戶對(duì)現(xiàn)有固件進(jìn)行必要的更新。?
?
概述
微調(diào)“保險(xiǎn)絲”
微調(diào)“保險(xiǎn)絲”實(shí)際上指的是一組非易失(NV)存儲(chǔ)器,在Maxim的Teridian電表IC生產(chǎn)過程中用于調(diào)整IC的模擬和數(shù)字特征參數(shù)。器件出廠后,微調(diào)“保險(xiǎn)絲”內(nèi)存儲(chǔ)的內(nèi)容不再改變,用于后續(xù)產(chǎn)品中的參數(shù)補(bǔ)償,例如,用來微調(diào)VREF電壓,使其穩(wěn)定在1.195 VDC ,誤差不超過±1mV;還可用于調(diào)整VBIAS內(nèi)部電壓,使其穩(wěn)定在目標(biāo)值。
IC工作期間,按照固定的時(shí)間間隔讀取微調(diào)“保險(xiǎn)絲”的數(shù)值,由IC內(nèi)部的硬件電路進(jìn)行適當(dāng)?shù)膮?shù)調(diào)整。從“保險(xiǎn)絲”讀取的數(shù)據(jù)送入硬件控制電路和位于I/O RAM的保險(xiǎn)絲寄存器(圖1)。71M6521系列產(chǎn)品中共包含7個(gè)獨(dú)立的微調(diào)“保險(xiǎn)絲”數(shù)值,向I/O RAM寄存器0x20FD (TRIMSEL)寫入1-7之間的任何數(shù)值,可從I/O RAM寄存器0x20FF (TRIM)中讀取相應(yīng)的“保險(xiǎn)絲”數(shù)據(jù)。
圖1. 微調(diào)保險(xiǎn)絲工作原理
錯(cuò)誤讀取微調(diào)“保險(xiǎn)絲”
71M6521 IC支持三種低功耗模式(休眠模式、LCD單獨(dú)有效模式以及關(guān)斷模式)。低功耗模式下,無法支持IC正常工作的電流消耗。
在極端的電磁干擾(EMI)環(huán)境下,或者是當(dāng)電源超乎尋常地反復(fù)斷電的情況下,可能會(huì)影響到“保險(xiǎn)絲”數(shù)據(jù)的讀取和參數(shù)調(diào)整,錯(cuò)誤地讀取到某個(gè)“保險(xiǎn)絲”數(shù)據(jù)。發(fā)生這種情況時(shí),可能導(dǎo)致幾種錯(cuò)誤結(jié)果。當(dāng)電源發(fā)生高速“振蕩”,使得71M6521在工作模式與關(guān)斷模式之間反復(fù)切換時(shí),將會(huì)提高器件故障的概率,當(dāng)交流電緩慢爬升、下降,而后續(xù)使用的是“軟”電源時(shí),很容易發(fā)生這種狀況。電源振蕩導(dǎo)致71M6521在正常供電(正常工作模式)和低功耗(關(guān)斷)模式之間頻繁切換,由于“保險(xiǎn)絲”讀取錯(cuò)誤導(dǎo)致器件故障的可能性非常小,只有當(dāng)電表在長(zhǎng)達(dá)若干天,甚至幾個(gè)星期的時(shí)間內(nèi)頻繁斷電時(shí),才有可能看到故障狀況。
“保險(xiǎn)絲”讀取錯(cuò)誤導(dǎo)致IC工作故障可能有幾種不同的表現(xiàn)形式,具體取決于錯(cuò)誤讀取的“保險(xiǎn)絲”數(shù)據(jù)。故障現(xiàn)象可能是以下幾種情況之一:
MPU軟件運(yùn)行在未知狀態(tài),但依據(jù)固件的不同,有些情況下可能終止內(nèi)部時(shí)鐘,使看門狗定時(shí)器無法復(fù)位電表。
產(chǎn)生未知的內(nèi)部電源電壓和檢測(cè)門限,這種情況下可能導(dǎo)致外部電源作用到V1引腳,在喚醒時(shí)鐘尚未建立的前提下進(jìn)入休眠模式。有些情況下,不恰當(dāng)?shù)爻跏蓟瘍?nèi)部電源達(dá)到一個(gè)超時(shí)限制時(shí),可能導(dǎo)致RTC計(jì)時(shí)錯(cuò)誤。
在電表中從根本上消除EMI可能不太現(xiàn)實(shí),但是,在器件進(jìn)入關(guān)斷模式時(shí)通過測(cè)試IC的微調(diào)“保險(xiǎn)絲”,固件可以檢測(cè)并避免發(fā)生上述故障。
新版本演示程序解決的另一問題是RTC時(shí)間偏移的修正。早期軟件中所包含的RTC程序集成了長(zhǎng)期抖動(dòng)導(dǎo)致RTC偏差的修正,復(fù)位后進(jìn)行調(diào)整。
改進(jìn)程序的應(yīng)用示例
本應(yīng)用筆記介紹的程序支持所有版本的71M6521,包括71M6521BE、71M6521DE和71M6521FE。Maxim推薦在帶有備份電池的新設(shè)計(jì)中采用固件修正。
功率模式切換期間可能產(chǎn)生的失效
71M6521中的微調(diào)“保險(xiǎn)絲”由控制邏輯保證,重復(fù)上電或極端EMI可能影響微調(diào)“保險(xiǎn)絲”的數(shù)據(jù)讀取。失效原因包括:
電表頻繁斷電或出現(xiàn)極端EMI。
電表從工作狀態(tài)進(jìn)入關(guān)斷模式,新的鋰電池供電的情況下,錯(cuò)誤地讀取微調(diào)“保險(xiǎn)絲”的數(shù)據(jù)。電池電壓(典型值為3.6 VDC)與71M6521正常工作時(shí)的電壓(典型值為3.3 VDC)不同,導(dǎo)致失效。
配備電池的電表似乎更容易受到影響,因?yàn)椴粠щ姵氐碾姳碓谥麟娫磾嚯姴⒃俅紊想姾螅軌蚪?jīng)歷一次干凈的復(fù)位(POR)過程。
在供電模式轉(zhuǎn)換期間,可能引發(fā)上文提及的失效現(xiàn)象,但很難準(zhǔn)確定位具體故障,因?yàn)檫@取決于諸多因素:斷電或EMI、電表固件、電池選擇、電源設(shè)計(jì)等,這些都會(huì)影響到微調(diào)“保險(xiǎn)絲”的讀取錯(cuò)誤。大多數(shù)電表即使在極端測(cè)試條件下,也不會(huì)發(fā)生故障。
當(dāng)電表返回正常工作模式時(shí),電源能夠支持更大的工作電流,即可提供微調(diào)自校準(zhǔn)數(shù)據(jù)。但可能需要重新恢復(fù)電表的工作狀態(tài)。
RTC失效事件
RTC程序用于補(bǔ)償電表在低功耗模式下產(chǎn)生的時(shí)間偏差,但并非在啟動(dòng)時(shí)同步調(diào)整,而會(huì)累計(jì)抖動(dòng)產(chǎn)生的漂移。
4.8p演示程序
程序測(cè)試
實(shí)際測(cè)試時(shí)為演示板配備了新電池,配置顯示默認(rèn)值(顯示RTC的時(shí)鐘)。然后,在8周內(nèi)每隔10秒重復(fù)一次交流電的斷電/上電。簡(jiǎn)單地把電源穩(wěn)定在標(biāo)稱交流電壓,然后開始測(cè)試。好的設(shè)計(jì)應(yīng)該能夠正確地恢復(fù)系統(tǒng)工作,合理顯示時(shí)間信息。程序經(jīng)過8周測(cè)試后沒有出現(xiàn)失效。
軟件的影響
4.8p演示程序能夠在運(yùn)行main()之前出發(fā)PLL_OK中斷,保證在固件初始化期間能夠檢測(cè)到掉電故障并進(jìn)行相應(yīng)的處理。必須謹(jǐn)慎管理其它中斷操作,以避免出現(xiàn)嚴(yán)重的故障現(xiàn)象。
任何情況下,4.8p演示軟件都可以在喚醒定時(shí)器中保持一個(gè)非零數(shù)值。正常工作時(shí),喚醒定時(shí)器不會(huì)計(jì)數(shù)或觸發(fā)喚醒過程,除非IC意外進(jìn)入休眠或僅顯示LCD模式。發(fā)生這種事件時(shí),將觸發(fā)喚醒定時(shí)器工作,喚醒電表并從故障狀態(tài)下恢復(fù)電表工作。
程序存儲(chǔ)位置
目標(biāo)代碼應(yīng)該把修正程序放置在閃存的前8KB。Keil? IDE中,可以在項(xiàng)目平臺(tái)更改目標(biāo)程序,選擇<點(diǎn)擊鼠標(biāo)右鍵>optionsBL_51 Locate TabCode Space。演示程序中,該指令置于存儲(chǔ)器盡可能低的位置:
?C_C51STARTUP,?PR?_?PLL_ISR?BATMODES_20,?PR?_?BATMODE_CHANGE?BATMODES_20,
?C_C51STARTUP包含演示程序的啟動(dòng)代碼。
?PR?_?PLL_ISR?BATMODES_20SAFE,?PR?_?BATMODE_CHANGE?BATMODES_20SAFE執(zhí)行PLL_OK中斷例程時(shí)需要。
PLL_OK中斷
以下程序應(yīng)該置于PLL_OK中斷例程,檢測(cè)到關(guān)斷模式時(shí)運(yùn)行。4.8p mainatmodes_20.c中給出了經(jīng)過測(cè)試的程序代碼。
?
extern void trim_test(void) small reentrant; EA = 0; CONFIG0 = 0; // Make sure we are running as fast as we can. IFLAGS = (~IE_PLLFALL_) & (~IE_PLLRISE_); // force an edge to occur WAKE = 0x81; // force a wake timer wake if sleep is forced trim_test(); // From start-up code.
?
啟動(dòng)程序
以下啟動(dòng)程序用來替代startup.a51,Maxim提供經(jīng)過測(cè)試的軟件代碼(4.8p演示軟件中,啟動(dòng)程序位于utilstartup_n_safe.a51),以下列出了程序代碼。
第一次在工廠運(yùn)行程序時(shí),微調(diào)寄存器的內(nèi)容被復(fù)制到閃存內(nèi)。之后,對(duì)微調(diào)寄存器進(jìn)行測(cè)試。一旦測(cè)試中發(fā)生失效,程序?qū)⒑?jiǎn)單地把IC置于休眠模式,然后重新讀取微調(diào)數(shù)據(jù)。喚醒器件操作后,IC會(huì)自動(dòng)讀取微調(diào)“保險(xiǎn)絲”內(nèi)的數(shù)值。復(fù)位期間調(diào)用保險(xiǎn)絲測(cè)試程序,將重新讀取微調(diào)“保險(xiǎn)絲”的數(shù)據(jù)并進(jìn)行修正。
?
;/*************************************************************************** ; * This code and information is provided "as is" without warranty of any * ; * kind, either expressed or implied, including but not limited to the * ; * implied warranties of merchantability and/or fitness for a particular * ; * purpose. * ; * * ; * Copyright (C) 2011 Maxim Integrated Products, Inc. All Rights Reserved. * ; ***************************************************************************/ ;//************************************************************************** ;// ;// DESCRIPTION: 71M652x POWER METER - STARTUP Code. ;// ;//************************************************************************** ;// ;// File: STARTUP_N_SAFE.A51. ;// $NOMOD51 ;------------------------------------------------------------------------------ ; This file is part of the C51 Compiler package ; Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc. ;------------------------------------------------------------------------------ ; STARTUP.A51: This code is executed after processor reset. ; ; To translate this file use A51 with the following invocation: ; ; A51 STARTUP_N_SAFE.A51 ; ; To link the modified STARTUP_N_SAFE.OBJ file to your application use the following ; BL51 invocation: ; ; BL51, STARTUP_N_SAFE.OBJ ; ;------------------------------------------------------------------------------ ; ; User-defined Power-On Initialization of Memory ; With the following EQU statements the initialization of memory ; at processor reset can be defined: ; ; ; the absolute start-address of IDATA memory is always 0 IDATALEN EQU 100H ; the length of IDATA memory in bytes. ; XDATASTART EQU 0H ; the absolute start-address of XDATA memory XDATALEN EQU 000H ; the length of XDATA memory in bytes. ; PDATASTART EQU 0H ; the absolute start-address of PDATA memory PDATALEN EQU 0H ; the length of PDATA memory in bytes. ; ; Notes: The IDATA space overlaps physically the DATA and BIT areas of the ; 8051 CPU. At minimum the memory space occupied from the C51 ; run-time routines must be set to zero. ;------------------------------------------------------------------------------ ; ; Reentrant Stack Initialization ; ; The following EQU statements define the stack pointer for reentrant ; functions and initialized it: ; ; Stack Space for reentrant functions in the SMALL model. IBPSTACK EQU 1 ; set to 1 if small reentrant is used. IBPSTACKTOP EQU 0FFH+1 ; set top of stack to highest location+1. ; ; Stack Space for reentrant functions in the LARGE model. XBPSTACK EQU 0 ; set to 1 if large reentrant is used. XBPSTACKTOP EQU 07FFH+1; set top of stack to highest location+1. ; ; Stack Space for reentrant functions in the COMPACT model. PBPSTACK EQU 0 ; set to 1 if compact reentrant is used. PBPSTACKTOP EQU 07FFH+1; set top of stack to highest location+1. ; ;------------------------------------------------------------------------------ ; Page Definition for Using the Compact Model with 64 KByte xdata RAM ; ; The following EQU statements define the xdata page used for pdata ; variables. The EQU PPAGE must conform with the PPAGE control used ; in the linker invocation. ; PPAGEENABLE EQU 1 ; set to 1 if pdata object are used. ; PPAGE EQU 7 ; define PPAGE number. PUBLIC PPAGE_SFR PPAGE_SFR DATA 0BFH ; SFR that supplies uppermost address byte ; (most 8051 variants use P2 as uppermost address byte) ;------------------------------------------------------------------------------ ; Switch to M6520 when chip is available. ; Standard SFR Symbols ACC DATA 0E0H B DATA 0F0H SP DATA 81H DPL DATA 82H DPH DATA 83H USER1 DATA 90H DIR1 DATA 91H FCTRL DATA 0B2H IPH DATA 0B9H IPL DATA 0A9H IRCON DATA 0C0H IEN0 DATA 0A8H IEN1 DATA 0B8H NAME ?C_STARTUP ?C_C51STARTUP SEGMENT CODE ?STACK SEGMENT IDATA RSEG ?STACK DS 1 EXTRN CODE (?C_START) PUBLIC ?C_STARTUP PUBLIC _?TRIM_TEST CSEG AT 0 ?C_STARTUP: LJMP STARTUP1 RSEG ?C_C51STARTUP STARTUP1: CLR IEN0^7 ; Disable interrupts ; To enable secure mode, remove the semicolon of the next line ; ORL FCTRL,#40H ; set secure bit MOV 0E8h,#0FFh ; Refresh nonmaskable watchdog. ; Set system interrupt priorities; more frequent are higher priority. MOV IPH,#01DH ; From code in options_gbl.h, main.c MOV IPL,#00AH ; From code in options_gbl.h ; Clear PLL_OK interrupt (bit3), and others MOV IRCON,#0 ; Enable PLL_OK interrupt MOV DPTR,#2007H ; Set EX_PLL MOV A,#20H MOVX @DPTR,A ; Enable interrupts MOV IEN1,#08H ; Enable INT4, the PLL_OK interrupt MOV IEN0,#80H ; Enable all interrupts ACALL _?TRIM_TEST ; Test 6521's trims IF IDATALEN <> 0 MOV R0,#IDATALEN - 1 CLR A IDATALOOP: MOV @R0,A DJNZ R0,IDATALOOP ENDIF IF XDATALEN <> 0 MOV DPTR,#XDATASTART MOV R7,#LOW (XDATALEN) IF (LOW (XDATALEN)) <> 0 MOV R6,#(HIGH (XDATALEN)) +1 ELSE MOV R6,#HIGH (XDATALEN) ENDIF CLR A XDATALOOP: MOVX @DPTR,A INC DPTR DJNZ R7,XDATALOOP DJNZ R6,XDATALOOP ENDIF IF PPAGEENABLE <> 0 MOV PPAGE_SFR,#PPAGE ENDIF IF PDATALEN <> 0 MOV R0,#LOW (PDATASTART) MOV R7,#LOW (PDATALEN) CLR A PDATALOOP: MOVX @R0,A INC R0 DJNZ R7,PDATALOOP ENDIF IF IBPSTACK <> 0 EXTRN DATA (?C_IBP) MOV ?C_IBP,#LOW IBPSTACKTOP ENDIF IF XBPSTACK <> 0 EXTRN DATA (?C_XBP) MOV ?C_XBP,#HIGH XBPSTACKTOP MOV ?C_XBP+1,#LOW XBPSTACKTOP ENDIF IF PBPSTACK <> 0 EXTRN DATA (?C_PBP) MOV ?C_PBP,#LOW PBPSTACKTOP ENDIF MOV SP,#?STACK-1 LJMP ?C_START ; Read a trim whose index is in A. Return the trim in A. CSEG AT 100H FUSE_TABLE: DB 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff ; Trim test. Prefix "_?" tells Keil C it's reentrant. _?TRIM_TEST: PUSH IEN0 PUSH PSW ; Save registers, so it's reentrant. MOV PSW,00H ; Select registers before saving them PUSH DPL PUSH DPH PUSH ACC MOV A,R7 ; Save R7 from the current register set. PUSH ACC MOV A,R6 ; Save R6 from the current register set. PUSH ACC CLR IEN0.7 ; Wait for all fuses to be read once before checking fuses ; Complete fuse read takes 45 xtal clock cycles (1 fuse per xtal clock) MOV R7,#21 ; Wait 21x4 = 48 cycles loop_inner: DJNZ R7,loop_inner ; 4 cycles (in brownout, 1 cycle/clk) mov DPH, #HIGH(FUSE_TABLE) fuse_test_frst: ; check if values previously stored mov DPL,#LOW(FUSE_TABLE) clr a movc a,@a+dptr ; read fuse table xrl a,#55H jnz fuse_save MOV DPTR,#2003H ; Check if in brownout mode MOVX A,@DPTR JB ACC.6,PASS ; Mission mode, so don't check. fuse_test: mov r7,#8 ; byte counter fuse_test_lp1: mov DPTR,#FUSE_TABLE ; load pointer to fuse table mov a,r7 movc a,@a+dptr ; read fuse table mov r6,a mov a,r7 MOV DPTR,#20FDH ; TRIMSEL address MOVX @DPTR,A ; load reg to read trim MOV DPL,#0FFH ; TRIM value address MOVX A,@DPTR ; Get trim value cjne a,06H,FAIL ; test if flash read and trim == djnz r7,fuse_test_lp1 JMP PASS fuse_save: MOV DPTR,#2003H ; Check if in brownout mode MOVX A,@DPTR ; If brownout mode, don't save them and enter sleep. JNB ACC.6,FAIL ; mov 84H,#LOW(FUSE_TABLE) mov r7,#8 ; byte counter fuse_save_lp1: mov a,r7 MOV DPTR,#20FDH ; TRIMSEL address MOVX @DPTR,A ; load reg to read trim MOV DPL,#0FFH ; TRIM value address MOVX A,@DPTR ; Get trim value mov r6,a mov a,r7 mov dptr,#FUSE_TABLE add a,DPL mov DPL,a mov a,r6 orl 0B2H,#01H ; set to flash write movx @dptr,a ; write fuse table byte djnz r7,fuse_save_lp1 mov a,#55H ; control byte to mark mov dptr,#FUSE_TABLE orl 0B2H,#01H ; set to flash write movx @dptr,a ; write fuse table byte jmp PASS FAIL: MOV DPTR,#20A9H ; WAKE address MOV A,#0C1H ; SLEEP forces wake and fuse re-read MOVX @DPTR,A PASS: CLR A ; Select 0 MOV DPTR,#20FDH ; TRIMSEL address MOVX @DPTR,A ; clear trim select register. POP ACC MOV R6,A ; Restore R7 in this register set POP ACC MOV R7,A ; Restore R7 in this register set POP ACC POP DPH POP DPL POP PSW ; Restore registers, so it's reentrant. POP IEN0 RET END
?
RTC例程
復(fù)位后,軟件不應(yīng)該馬上調(diào)節(jié)時(shí)鐘修正休眠模式下時(shí)差,而是首先計(jì)算需要消除的時(shí)間偏差,然會(huì)設(shè)置時(shí)鐘進(jìn)行補(bǔ)償。
評(píng)論
查看更多