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

📄 pub_func.c

📁 NEC527多功能电表完整源代码,包括LCD驱动,显示,计量,存储,整个527驱动程序!
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "includes.h"

void ClrWdt(void)
{
    WDTE = 0xAC; 
    WDI_OUT|=WDI;
    Delay(5);
    WDI_OUT&=~(WDI);
}

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

/* check sum
*/
unsigned long  SysDoCheckSum(unsigned char *ptr, unsigned short len)
{
    unsigned short ii;
    unsigned char sum = 0;
    
    //length
    for(ii=0;ii<len;ii++)
    {
        sum += *(ptr ++);
    }
    
    return sum;
}

unsigned char TimeCompare( unsigned char *buff1, unsigned char *buff2, unsigned char len )
{
    unsigned char i;

    if( IsEqual( &buff1[0], &buff2[0], len ) == OK ) return TIME_EQUAL;
	
    for( i = 0; i < len; i++ )
    {
        if( buff1[i] < buff2[i] )
        {
            return TIME_LITTER;	
        }
        else if( buff1[i] > buff2[i] )
        {			
            return TIME_LARGER;
        }
    }
	
    return ERROR;
}

unsigned char IsBcdCode(unsigned char *bcd,unsigned char len)
{
    unsigned char tmp,rc;
    
    do{
        rc=OK;
        len--;
        tmp=(*(bcd+len))&0x0F;
        if(tmp>10)
        {
            rc=ERROR;
        }
        tmp=((*(bcd+len))&0xF0)>>4;
        if(tmp>10)
        {
            rc=ERROR;
        }
        if(rc)
        {
            *(bcd+len)=0;
        }
    }while(len);
    
    return rc;
}

unsigned char Hex2BcdChar(unsigned char hex)
{
    return((hex%10)|(((unsigned char)(hex/10))<<4));
}

unsigned char Bcd2HexChar(unsigned char bcd)
{
    return((bcd>>4)*10+(bcd&0x0F));
}

unsigned short Hex2BcdShort(unsigned short hex)
{
    unsigned short temp;
    unsigned short ret;
    unsigned char ii;

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

unsigned short Bcd2HexShort(unsigned short bcd)
{
    unsigned short temp;
    unsigned short ret;
    unsigned char ii;

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

unsigned long Hex2BcdLong(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 Bcd2HexLong(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);
}

unsigned short MaxValueOfNums(unsigned short num1,unsigned short num2, unsigned short num3)
{
    unsigned short temp,max;

    temp = (num1 >= num2 ) ? num1:num2;
    max = (temp >= num3 ) ? temp:num3;    	

    return max;	
}

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

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

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

void MemSet0xFF(unsigned char * data,unsigned short len)
{
    memset(data,0xFF,len);
}

void MemSetoWriteE2prom (unsigned short addr,unsigned char len)
{
    MemSetZero(&FrameBuffer[FRM_DATA],len);
    FramWrite(addr,&FrameBuffer[FRM_DATA],len);
}

void ActiveProgram(void)
{
    if(TimerCounter[TIMER_PROC_PROG]==0)
    {
        TimerCounter[TIMER_PROC_PROG] = 14400;
        if( CoverTimerCounter[2] == 0 )
        {
            CoverTimerCounter[2] = 60;        
            SavePgmInfoToFram(ADDR_OF_COVER_PROG_OPEN,1);
        }
        else
        {
            CoverTimerCounter[2] = 60;
        }
    }
    else
    {
        TimerCounter[TIMER_PROC_PROG]=0;
    }
}

void SavePgmInfoToFram(unsigned short addr, unsigned char flag)
{
    unsigned char i=9,temp[5],len;	

    WriteAddShortToFram(addr);
    if( flag != 0 )
    {
        len = 5;
        do
        {
            FramRead( addr+2+(i-1)*5, temp, 5 );
            FramWrite( addr+2+i*5, temp, 5 );
        }while(--i);
    }		
    else
    {
        len = 4;
    }
    WriteTimeToFram( addr+2, len );
}

void SavePastTime(unsigned long addr,unsigned short t_addr,unsigned char bytes)
{
    unsigned long t1=0,t2=0;

    t1 = CalcPastTime(TMPT_BASE+t_addr);
    FramRead( (ushort)addr, (unsigned char *)&t2, bytes );
    t2 = __bcd_add_long(t1,t2);
    FramWrite( (ushort)addr, (unsigned char *)&t2, bytes );
}

void SaveTmpTime(unsigned short addr)
{
    WriteTimeToFram( addr, 6 );
}

void CancleProgram(void)
{
    
}

ushort UsignedShortSub(ushort hexA, ushort hexB)
{
    if( hexA > hexB ) 
    {
        return (hexA - hexB);
    }
    else 
    {
        return 0;
    }
}

uchar UsignedCharSub(uchar  hexA, uchar hexB)
{
    if( hexA > hexB ) 
    {
        return (hexA - hexB);
    }
    else 
    {
        return 0;
    }
}

uchar BcdAddOrSubChar(uchar  bcd1, uchar bcd2, uchar flag)
{
    unsigned char temp;
    
    bcd1 = Bcd2HexChar( bcd1 );
    bcd2 = Bcd2HexChar( bcd2 );	
    if( flag )
    {
        if( bcd1 >= bcd2 )    
            temp = Bcd2HexChar( bcd1-bcd2 );
        else 
            temp = 0;
    }
    else
    {
        temp = Bcd2HexChar( bcd1+bcd2 );
    }		
    
    return temp;	
}

ushort BcdAddOrSubShort(ushort bcd1, ushort bcd2, ushort flag)
{
    unsigned short temp;
      
    bcd1 = Bcd2HexShort( bcd1 );
    bcd2 = Bcd2HexShort( bcd2 );	
   if( flag )
    {
        if( bcd1 >= bcd2 )    
            temp = Hex2BcdShort( bcd1-bcd2 );
        else 
            temp = 0;
    }
    else
    {
        temp = Hex2BcdShort( bcd1+bcd2 );
    }		

    return temp;	
}

unsigned long BcdAddOrSubLong(unsigned long bcd1, unsigned long bcd2, unsigned char flag)
{
    unsigned long temp;

    bcd1 = Bcd2HexLong( bcd1 );
    bcd2 = Bcd2HexLong( bcd2 );	
    if( flag )
    {
        if( bcd1 >= bcd2 )    
            temp = Hex2BcdLong( bcd1-bcd2 );
        else 
            temp = 0;
    }
    else
    {
        temp = Hex2BcdLong( bcd1+bcd2 );
    }		

    return temp;	
}

unsigned long CalcPastTime(unsigned short addr)
{
    unsigned long mins[2];
    unsigned char i,tt[10],j,temp;

    FramRead( addr+1, tt, 5 );
    if(IsEqualSpecialData( (unsigned char *)&tt[0], 0, 5 ) == OK ) return (0);
    tt[5] = SystemTime[1];//minute
    tt[6] = SystemTime[2];//hour
    tt[7] = SystemTime[4];//day
    tt[8] = SystemTime[5];//month
    tt[9] = SystemTime[6];//year

    for( j = 0; j < 2; j++ )
    {
        mins[j] = 0;
        temp = j*5;
        for( i = 0; i < 5; i++ )
        {
            tt[i+temp] = Bcd2HexChar(tt[i+temp]);
        }
        tt[3+temp]--;
        
        mins[j] = (ulong)(tt[4+temp]/4)*1461+(ulong)(tt[4+temp]%4)*365+days_per_month[tt[3+temp]]+tt[2+temp]-1;
        if( ( tt[4+temp] & 0x03 ) == 0 )
        {
            if( tt[3+temp] >= 2 ) mins[j]++;
        }

        mins[j] = mins[j]*1440;
        mins[j] += (ulong)tt[1+temp]*60;
        mins[j] += tt[0+temp];
    }
	
    if( mins[1] <= mins[0] ) return(0);
    return( Hex2BcdLong( mins[1]-mins[0] ) );
}

unsigned char ReadBcdCountInfo(unsigned short addr,unsigned char * data,unsigned char dmax,unsigned char defaultval)
{
    FramRead(addr,data,1);
    *data=Bcd2HexChar(*data);
    if((*data==0)||(*data>=dmax))
    {
        *data=defaultval;
        return ERROR;
    }
    return OK;
}

unsigned short WriteAddShortToFram(unsigned short addr )
{
    unsigned short count;

    FramRead( addr, (unsigned char *)&count, 2 );
    count = __bcd_add_short(count, 1);
    FramWrite( addr, (unsigned char *)&count, 2 );

    count = Bcd2HexShort(count);
    count = ( count - 1 ) % 10;
    return ( count );
}

unsigned long WriteAddLongToBbuffer1(unsigned short addr,unsigned long data)
{
    unsigned long temp;
    
    DataflashBuffer1Read( addr, (unsigned char *)&temp, 4 );	
    temp = __bcd_add_long( temp, data );
    DataflashBuffer1Write( addr, (unsigned char *)&temp, 4 );
	
    return temp;
}

void WriteTimeToFram(unsigned short addr,unsigned char len)
{
    unsigned char time[6];
    unsigned char *t=&time[0];
    
    time[0]=SystemTime[0];//second
    time[1]=SystemTime[1];//min
    time[2]=SystemTime[2];//hour
    time[3]=SystemTime[4];//day
    time[4]=SystemTime[5];//month	
    time[5]=SystemTime[6];//year
    if( ( len==4) || ( len == 5 ) ) t++;
    FramWrite(addr,t,len);
}

unsigned char ReadTimesMod10(unsigned short addr)
{
    unsigned short count;
	
    FramRead( addr, (unsigned char *)&count, 2 );
    count = Bcd2HexShort( count );
    if( count != 0 )
    {
        count = ( count - 1 ) % 10;
    }

    return count;
}

void LcdLightOn(unsigned char sec)
{
    LCD_LIGHT_ON();
    TimerCounter[TIMER_PROC_LCD_LIGHT]=sec;
}

void LcdLightOff(void)
{
    LCD_LIGHT_OFF();
    _sys_evt_add(EVT_INIT_COMM);
}

void LcdDispPageOn(void)
{
    IR_ON();                  //红外电源打开
    LCD_LIGHT_ON();
    TimerCounter[TIMER_PROC_LCD_RESET] = 30;
    if( PowerStateFlag & PS_BREAKPOWER_FLAG )
    {
        IrPrepareForRxFromLowPower();
    }
    else
    {
        IR_OFF();
    }
    LcdDisplayOpen();
}

void LcdDispPageOff(void)
{
    IR_OFF();
    LCD_LIGHT_OFF();
    DisplayReinit();
    DisplayContentReinit();
    if( !IsPowerOn() )
    {
        if(LcdStopInterval)
        {
            LcdDisplayClose();
        }
    }
    _SysPotInfo.chksum=SysDoCheckSum((unsigned char *)&_SysPotInfo.regv_con[0],sizeof(_sys_potinfo_s)-4);
}

unsigned long GetDayAddr(unsigned char year, unsigned char mon, unsigned char day)
{
    unsigned short days,year_days = 0;
    unsigned char temp,temp1,leap,ii;

    temp1 = Bcd2HexChar(year);
    if( temp1 >= 100 )
    {
        temp1 = 0;
    }        

    for( ii = 0; ii < temp1; ii++ )
    {
        if( ( ii & 0x03 ) == 0 )
        {
            year_days += 366;
        }
        else
        {

⌨️ 快捷键说明

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