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

📄 sys_time.c

📁 MSP430方案,用于脉冲采集器,其功能众多,上传代码仅供参考,不得用于商业
💻 C
字号:
#include "sys_time.h"
#include "pub_func.h"
#include "sys_event.h"
#include "uart.h"

/* time of system*/
__no_init _sys_time_s  _sys_time;
/* tick counter*/
unsigned char _tick_counter=0;

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

/* excute every second
*/
void second_up(void)
{
    unsigned char month_days;

    if(_sys_time.seconds>=60)
    {
        _disable_interrupt();
        // second repeat
        _sys_time.seconds-=60;
        _enable_interrupt();
        // minute up
        _sys_time.minutes++;
    }
    if(_sys_time.minutes>=60)
    {
        // minute repeat
        _sys_time.minutes-=60;
        // hour up
        _sys_time.hours++;
    }
    if(_sys_time.hours>=24)
    {
        // hour repeat
        _sys_time.hours-=24;
        // day up
        _sys_time.days++;
        // week up
        _sys_time.week=(_sys_time.week+1)%7;
        // get day-count of current month
        month_days=days_every_month[_sys_time.months];
        // if this year is leap year, there are 29 days in February
        if(((_sys_time.years&0x03)==0)&&(_sys_time.months==1))
        {
            month_days=29;
        }
        if(_sys_time.days>=month_days)
        {
            // day repeat
            _sys_time.days-=month_days;
            // month up
            _sys_time.months++;
        }
        if(_sys_time.months>=12)
        {
            // month repeat
            _sys_time.months-=12;
            // year up
            _sys_time.years++;
        }
        if(_sys_time.years>=100)
        {
            // year repeat
            _sys_time.years=0;
        }
    }
}

/* get system time and convert it to BCD code
*/
void _get_sys_time_bcd(unsigned char *time_bcd)
{
    unsigned char ii,temp,temp1;
    unsigned char *now_time=(unsigned char *)&_sys_time.seconds;

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

/* set system time by Hex code
*/
void _set_sys_time_hex(unsigned char *time_bcd)
{
    unsigned char ii,temp;
    unsigned char *now_time=(unsigned char *)&_sys_time.seconds;

    _disable_interrupt();
    for(ii=0;ii<7;ii++)
    {
        temp=time_bcd[ii];
        temp=(temp>>4)*10+(temp&0x0F);
        now_time[ii]=temp;
        //now_time[ii]=(time_bcd[ii]>>4)*10+time_bcd[ii]&0x0F;
    }
    _enable_interrupt();
}

/* wakeup system every second
*/
#pragma vector=WDT_VECTOR
__interrupt void _wdt_timer(void)
{
    WDTCTL=WDT_ADLY_250;
    _tick_counter++;
    if(_wait_time)
    {
        _wait_time--;
        if(_wait_time==0)
        {
            _frm_ptr=0;
        }
    }
    if(_tick_counter>=TICK_CNT_PER_SECOND)
    {
        // descend tick counter by TICK_CNT_PER_SECOND
        _tick_counter-=TICK_CNT_PER_SECOND;
        // second up
        _sys_time.seconds ++;    
        // second event
        SYS_EVT_ADD(SYS_EVT_SECONDS);
        // Clear LPM3 bits from saved SR on Stack
        _BIC_SR_IRQ(LPM3_bits);
    }
}

⌨️ 快捷键说明

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