📄 sys_init.c
字号:
/*************************************
;Ade7169 demo program
;*************************************
;AUTHOR: Su RuTong
;DATE: 03 09 2006
;*************************************/
#include "hal.h"
#include "utilities.h"
#include "sys_init.h"
#include "public.h"
#include "Display.h"
#include "RTC.h"
#include "Measure.h"
#include "UART.h"
#include "storage.h"
// extern _sys_tariff_data data curdata;
#ifdef __IAR_SYSTEMS_ICC__
void __ret_int(void)
{
asm("reti");
}
void __no_operation(void)
{
asm("nop");
}
#endif
__LowLevelInit(void)
{
IE=0;
IP=0;
IEIP2=0;
/*==================================*/
/* Initialize hardware. */
/*==================================*/
/***********************************************************
-2/3/2006: Petre M.
-the core frequency is set to 4.096MHz
-the XRAM access is enabled
***********************************************************/
//set 4.096MHz core frequency
KYREG = 0xA7;
green=0x00;
POWCON=green;//POWCON & 0xF8; //CD2 CD1 CD0=000 4.096MHz
//enable XRAM (Bit0=1)
CFG |= BIT0;// CFG | 0x01;
/*==================================*/
/* Choose if segment initialization */
/* should be done or not. */
/* Return: 0 to omit seg_init */
/* 1 to run seg_init */
/*==================================*/
return (1);
}
/*********************************************************************
Input:None
Output:None
Sets up PSM interrupt for PSR and battery switchover for low VDD only.
ONLY CALL THIS FUNCTION IF VSW = VBAT
**********************************************************************/
void InitPSR(void)
{
green=0;
BATPR=green;
green = BIT3; // set if LCD On in PSM2 mode.
PERIPH = green;
// Display BATPR
// Display PERIPH
IPSMF=0; // clear flag register
IPSME=EPSR; // enable battery switchover interrupt and low VDCIN interrupt
IEIP2|=EPSM; // enable the power supply interrupt
//__enable_interrupt(); // make sure interrupts are enabled
}
/*********************************************************************
Input:None
Output:None
Sets up PSM interrupt for VDCIN low and to switch over
on low VDD only.
ONLY CALL THIS FUNCTION IF VSW = VDD
**********************************************************************/
#ifdef __IAR_SYSTEMS_ICC__
#pragma optimize=none
#endif
void InitBSO(void)
{
//green=0; // Battery Switchover enabled on low VDD
green = BIT3; // set if LCD On in PSM2 mode.
PERIPH = green;
green=1; // Battery Swichover Enabled on Low VDD and Low VDCIN
BATPR=green;
IPSMF=0; // clear flag register
IPSME=EPSR|EVDCIN|EBSO; // enable battery switchover interrupt and low VDCIN interrupt
IEIP2|=EPSM; // enable the power supply interrupt
// make sure interrupts are enabled
}
void InitIO(void)
{
EPCFG=0x08;
green=INT0PRG; // allow wakeups from INT0
INTPR = green;
TCON &= 0xf0;
//TCON_bit.IT0=1; // make INT0 edge triggered
//TCON_bit.IE0=0; // make sure interrupt flag is clear before enabling INT0
TCON|=BIT0;
PX0=0;
//IE_bit.EX0=1; // enable INT0
IE|=BIT0;
green=0xff;
PINMAP0=green;
P0 = P1 = P2 = 0xff;
return;
}
/*********************************************************************
Input:None
Output:None
Sets up the ADI 3.3V LCD to use the charge pump
**********************************************************************/
void InitLCD(void)
{
LCDCON=LCDEN|MUX4_BIAS3; // LCDEN+MUX4_BIAS3; enable LCD with 1/3 bias and 4x multiplecing. LCDEN|MUX4_BIAS3|BLINKEN Blink mod by srt
LCDCLK=FD1|BLKMOD2; // FD1; FD1|BLKMOD1 Blink mod by srt
LCDCONX=BIASLVL4|BIASLVL3; // use charge pump. set the bias level for 3V;Va = Vref X (BLVL[4:0]/31)
//LCDCONX=BIASLVL5; // use charge pump. set the bias level for 3V;Va = Vref X (1+(BLVL[4:0])/31)
LCDSEGE2=SEG19EN|SEG18EN|SEG17EN; // enable FP17-19
LCDSEGE=SEG23EN|SEG22EN|SEG21EN|SEG20EN; // enable FP20-23
LCDCONY=0x40;//0;
}
void InitTIMER0(void)
{
TF0=0;
PT0=1;
TMOD|=0x01;
TH0=0xc4;
TL0=0x3c;//b4
TR0=1;
ET0=1;
}
void InitTIMER1(void)
{
#if 1
TF1=0;
PT1=0;
TMOD|=0x10;
TH1=0xEC;
TL1=0x78;
TR1=0;
ET1=0;
#endif
}
void InitTIMER2(void)
{
}
void InitWDT(void)
{
//__disable_interrupt();
WDWR = 1; // WDCON |=BIT0;
#ifdef _WDTEN_
WDCON = 0x73; // Disable WDT; 0x73 enable WDT 2s
#else
WDWR = 1;
WDCON = 0x73;//0x01; // Disable WDT; 0x73 enable WDT 2s
WDE = 1;
#endif
}
void _clr_wdt(void)
{
WDWR = 1; // WDCON |=BIT0;
#ifdef _WDTEN_
WDCON = 0x73; // Disable WDT; 0x73 enable WDT 2s
#else
//WDCON = 0x01; // Disable WDT; 0x73 enable WDT 2s
//WDWR = 1;
EA = 0;//disable all interrupts
WDWR = 1;
WDE = 1;
EA = 1;//enable all interrupts
#endif
}
#if 0
void __ret_int(void)
{
//asm("RETI");
}
#endif
void mcu_init(void)
{
#ifndef __IAR_SYSTEMS_ICC__
__LowLevelInit();
#endif
InitIO();
#if 0
// PERIPH.6 = 1 means supplied by VDD
PERIPH_TMP=PERIPH;
if((PERIPH_TMP&BIT6)==0) // supplied by VDD?
{
//POWER_DOWN_LOOP:
InitPSR(); // if not, enable power supply restore interrupt
//set 32768Hz core frequency
KYREG = 0xA7;
LcdInterval=0x03; // LcdInterval as temp
POWCON=LcdInterval; //POWCON & 0xF8; //CD2 CD1 CD0=000 4.096MHz
_Lcd_Write(0x0a,0x04);// battery ICON
do
{
PERIPH_TMP=PERIPH;
CLR_WDT();
//__enable_interrupt();
}while((PERIPH_TMP&BIT6)==0);
__LowLevelInit();
//if((PERIPH_TMP&BIT5)==0) goto POWER_DOWN_LOOP;
}
__disable_interrupt();
#endif
// #ifndef __IAR_SYSTEMS_ICC__ // For debug 1006-11-20
InitUART();
// #endif
InitBSO(); // supplied by VDD so enable battery switchover
// Send start msg
// InitI2C();
InitLCD();
InitTIMER0();
InitTIMER1();
InitTIMER2();
// Disp_All(0xff);
// recovered data todo
}
void sys_init(void)
{
Sys_buf_Cnt=0;
Sys_buf_ptr=&Sys_buf[0];
// Uart var initialize
bIn_Send=0; // Send in process
bIn_rec=0; // Recieve in process
bBuf_InUse=0; // Uart buffer in use
LowVDCINFlag=0; // If this flag is set, it indicates that
// Vdcin went low and preparations should
// be made for battery switchover
Uart_buf_SndCnt=0x00; // Send count
Uart_Snd_Pointer=&Sys_buf[0]; // Send buffer pointer
Uart_Rec_Tout_Cnt=0x00;
Uart_buf_RecCnt=0x00; // Recieve count
// Uart_Rec_Pointer=0x00; // Recieve buffer pointer
Uart_Rec_Tout_Cnt=0x00;
Uart_Snd_Delay_cnt=0x00;
curdata.tariff=0;
// for Lcd
DisplayPointer = 0;
LcdInterval=6;//0x05;
}
void dev_init(void)
{
InitWDT();
//CLR_WDT();
// RTC
SetupRTC();
// ADE
InitADE();
////InitRMSThresh(); // for now. Eventually this will be in the EEPROM
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -