⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basicoper.c

📁 单相复费率MSP430硬件时钟参考代码,MSP413设计方案
💻 C
字号:
#include "msp430x41x.h"
#include "general.h"
#include "string.h"
#include "stdlib.h"

void Delay(unsigned short len)
{
    for(;len>0;len--);
}

#ifdef _HARDWARE_RTC
unsigned char SystemTimeCS(void)
{
    unsigned char i,sum=0;

    for(i=1;i<7;i++)
    {
        sum+=g_time[i];
    }
    return (~sum);
}
#endif

unsigned char CheckSystemTime(unsigned char *time)
{
    unsigned short temp1,temp2;
    
#ifdef _HARDWARE_RTC
    temp1=(unsigned char)Bcd2Hex((unsigned long)g_time[SPACE_OF_HOUR])*60+(unsigned char)Bcd2Hex((unsigned long)g_time[SPACE_OF_MINUTE]);
#else
    temp1=g_time[SPACE_OF_HOUR]*60+g_time[SPACE_OF_MINUTE];
#endif
    temp2=(unsigned char)Bcd2Hex((unsigned long)time[2]) *60+(unsigned char)Bcd2Hex((unsigned long)time[1]);
    if(abs(temp1-temp2)<=5)
    {
        return 0;
    }
    return 1;
}

#ifdef _HARDWARE_RTC
void RenewSystemTime(void)
{
    RealtimeOper(0x05,g_time,7);
    g_time[7]=SystemTimeCS();
}
#endif

#ifdef _HARDWARE_RTC
void GetSystemTime(void)
{
    unsigned char ttime[7];

    if(SystemTimeCS()==g_time[7])
    {
        if(g_time[SPACE_OF_SECOND]>=0x59)
        {
            RealtimeOper(0x05,ttime,7);
            if((CheckSystemTime(ttime)==0)||((ttime[SPACE_OF_MINUTE]==0)&&(ttime[SPACE_OF_HOUR]==0)))
            {
RenewSysT:
                RenewSystemTime();
            }
            else
            {
                g_time[SPACE_OF_SECOND]=0;
            }
            if((g_time[SPACE_OF_HOUR]==0)&&(g_time[SPACE_OF_MINUTE]==0))
            {
                ResetLcdDisplay();
            }
            SystemTime_MonthHex=(unsigned char)Bcd2Hex((unsigned long)g_time[SPACE_OF_MONTH])-1; // BCD->HEX
            SetSystemEvent(EVENT_01_MINUTE);
        }
        else
        {
            g_time[SPACE_OF_SECOND]=(unsigned char)__bcd_add_short((unsigned short)g_time[SPACE_OF_SECOND],1);
        }
    }
    else
    {
        goto RenewSysT;
    }
}
#endif

void SetSystemTime(void)
{
#ifdef _HARDWARE_RTC
    RealtimeOper(0x00,g_time,7);
    RenewSystemTime();
    g_time[SPACE_OF_SECOND]=0x59;
#else
    unsigned char time_hex[7];
    GetSystemTimeHex(time_hex);
    memcpy(g_time,time_hex,7);
#endif
}

#ifndef _HARDWARE_RTC
/*days for 12 months*/
const unsigned char days_every_month[] = {
    31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31,
};
#endif

#ifndef _HARDWARE_RTC
void SecondUp(void)
{
    unsigned char month_days;

    if(g_time[SPACE_OF_SECOND]>=60)
    {
        _DINT();
        g_time[SPACE_OF_SECOND]-=60;
        _EINT();
        g_time[SPACE_OF_MINUTE]++;
    }
    if(g_time[SPACE_OF_MINUTE]>=60)
    {
        g_time[SPACE_OF_MINUTE]-=60;
        g_time[SPACE_OF_HOUR]++;
    }
    if(g_time[SPACE_OF_HOUR]>=24)
    {
        g_time[SPACE_OF_HOUR]-=24;
        g_time[SPACE_OF_DAY]++;
        month_days=days_every_month[g_time[SPACE_OF_MONTH]];
        if(((g_time[SPACE_OF_YEAR]&0x03)==0)&&(g_time[SPACE_OF_MONTH]==1))
        {
            month_days=29;
        }
        if(g_time[SPACE_OF_DAY]>=month_days)
        {
            g_time[SPACE_OF_DAY]-=month_days;
            g_time[SPACE_OF_MONTH]++;
        }
        if(g_time[SPACE_OF_MONTH]>=12)
        {
            g_time[SPACE_OF_MONTH]-=12;
            g_time[SPACE_OF_YEAR]++;
        }
        if(g_time[SPACE_OF_YEAR]>=100)
        {
            g_time[SPACE_OF_YEAR]=0;
        }
    }
}
#endif

#ifndef _HARDWARE_RTC
void GetSystemTimeBcd(unsigned char *time_bcd)
{
    unsigned char ii,temp,temp1;

    for(ii=0;ii<7;ii++)
    {
        temp=g_time[ii];
        temp1=temp%10;
        temp1|=((unsigned char)(temp/10))<<4;
        time_bcd[ii]=temp1;
        //time_bcd[ii]=(temp%10)||(((unsigned char)(temp/10))<<4);
    }
}
#endif

#ifndef _HARDWARE_RTC
void GetSystemTimeHex(unsigned char *time_bcd)
{
    unsigned char ii,temp;

    for(ii=0;ii<7;ii++)
    {
        temp=g_time[ii];
        time_bcd[ii]=(temp>>4)*10+(temp&0x0F);
    }
}
#endif

#ifdef _SUPPORT_RELAY
unsigned char RelayStateCheck(unsigned char on_off)
{
    unsigned char ii,status;

    status=(on_off<<2)&RELAY_TEST;
    for(ii=0;ii<10;ii++)
    {
        if(status==(RELAY_TEST_IN&RELAY_TEST)) break;
        Delay(100);
    }
    if(ii==10) return(1);
    else return(0);
}

void RelayCtrl(unsigned char on_off)
{
    unsigned char ii;

    ii=0;
    do
    {
        // 先检测继电器当前状态,然后再操作继电器,这样可避免无用操作
        if(RelayStateCheck(on_off)) break;
        // 操作继电器
        P6OUT&=~(on_off);
        Delay(3000);
        P6OUT|=(RELAY_OPEN|RELAY_CLOSE);
        Delay(1500);
        ii++;
    }while(ii<3);
    E2promWrite(ADDRESS_OF_RELAY_STATE,&on_off,1);
}
#endif

#if 0
void RelayOnHook()
{
    RelayCtrl(RELAY_OPEN);
}

void RelayOffHook()
{
    RelayCtrl(RELAY_CLOSE);
}
#endif

void BellOn(void)
{
    BELL_OUT&=~(BELL);
    Delay(0xFFFF);
    BELL_OUT|=BELL;
}

void SetSystemEvent(unsigned char event)
{
    _DINT();
    SystemIndication|=event;
    _EINT();
}

void ClearSystemEvent(unsigned char event)
{
    _DINT();
    SystemIndication&=~(event);
    _EINT();
}

unsigned char IsEqualSpecialData(unsigned char *src,unsigned char data,unsigned char len)
{
    do{
        if(*src!=data)
        {
            return(0);
        }
        src++;
    }while(--len);
    return(1);
}

unsigned char IsEqual(unsigned char *src1,unsigned char * src2,unsigned char len)
{
    do{
        if(*src1!=*src2)
        {
            return(0);
        }
        src1++;
        src2++;
    }while(--len);
    return(1);
}

void LcdLightOn(unsigned char sec)
{
    LCD_LIGHT_OUT&=~LCD_LIGHT;
    TimerCounter[0]=sec;
    TimerProc[0]=LcdLightOff;
}

void LcdLightOff()
{
    LCD_LIGHT_OUT|=LCD_LIGHT;
}

void ClrWDT(void)
{
    P6OUT|=WDT;
    Delay(5);
    P6OUT&=~WDT;

    WDTCTL=WDTPW+WDTSSEL+WDTCNTCL;
}

unsigned long Hex2Bcd(unsigned long hex)
{
    unsigned long temp;
    unsigned long ret;
    unsigned char ii;

    ret=0;
    for(ii=0;ii<8;ii++)
    {
        ret>>=4;
        temp=hex%10;
        temp<<=28;
        ret|=temp;
        hex=hex/10;
    }
    return(ret);
}

unsigned long Bcd2Hex(unsigned long bcd)
{
    unsigned long temp;
    unsigned long ret;
    unsigned char ii;

    ret=0;
    for(ii=0;ii<8;ii++)
    {
        temp=bcd&0xF0000000;
        temp>>=28;
        ret=ret*10+temp;
        bcd<<=4;
    }
    return(ret);
}

void ReadPPC(void)
{
    E2promRead(ADDRESS_OF_DEVICE_CONSTANT+1,&PowerPulseConstant_, 1);
    PowerPulseConstant_=(unsigned char)Bcd2Hex((unsigned long)PowerPulseConstant_);
}

void ReadPeriodOfTimeCount(unsigned char *data)
{
    E2promRead(ADDRESS_OF_PERIOD_OF_TIME_COUNT,data,1);
    *data=(unsigned char)Bcd2Hex((unsigned long)*data);
}

void MemSetTo0(unsigned char * data,unsigned char len)
{
    memset(data,0,len);
}

void ClearPowerPulseDataInRam(void)
{
    _DINT();
    PowerPulseCounter=0;
    _EINT();
    MemSetTo0(PowerScalar,10);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -