clock

Dependencies:   mbed

myScope/mybc95.cpp

Committer:
donghuoyinzi
Date:
2018-05-22
Revision:
3:b8766d623f01
Parent:
2:e54d9d87c6cb

File content as of revision 3:b8766d623f01:

#include "mybc95.h"

char buf_bc95Tx[7]="";
char bc95Act::buf[50]= {0x00};
char bc95Act::buf_bc95Tx[buf_bc95TxSize]= {0x00};
char bc95Act::buf_bc95Rx[buf_bc95RxSize]= {0x00};
int bc95Act::stpSe=0;
int bc95Act::cnt=0;
int bc95Act::cntche;
int bc95Act::cntlink=0;
int bc95Act::year=0;
int bc95Act::mon=0;
int bc95Act::day=0;
int bc95Act::hour=0;
int bc95Act::min=0;
int bc95Act::sec=0;
const int mon_table[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
/*****************************
Fun.: base initialization of bc95。
Desc.: 
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::init(void)
{
    know::uart3.baud(9600);
    know::uart3.attach(&getISR,SerialBase::RxIrq);
}

/*****************************
Fun.: initialization of bc95 start。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::start(void)
{
    know::pow.write(1);     //BC95POW_ON();
    wait(50);
}

/*****************************
Fun.: initialization of bc95 data。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::data(void)
{
    stpSe=1;
    cnt=0; 
}

/*****************************
Fun.: initialization of bc95 ready。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::ready(void)
{

}

/*****************************
Fun.: bc95 revoke
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::revok(void)
{
    know::pow.write(0);    //BC95POW_OFF();
}

/*****************************
Fun.: get time。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
char *bc95Act::get_tim(void)
{
    static char buf_tim[7]="";
    tm *tim;
    time_t seconds=time(NULL);
    tim=localtime(&seconds);
    
    buf_tim[0]=char(tim->tm_year-100);
    buf_tim[1]=char(tim->tm_mon);
    buf_tim[2]=char(tim->tm_mday);
    buf_tim[3]=char(tim->tm_hour);
    buf_tim[4]=char(tim->tm_min);
    buf_tim[5]=char(tim->tm_sec);
    
    return buf_tim;
}

/*****************************
Fun.: get know。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::get_knw(void)
{
    if(bc95Act::stpSe==1){ 
        know::uart3.printf("AT+CCLK?\r\n");
        jump_Judg(2,250);    

    }
    if(bc95Act::stpSe==2) CheckOK(200,1,10);             //10是最大的查询次数
    if(bc95Act::stpSe==250){                            //规范250是失败
        know::polSta[15]='2';
        bc95Act::stpSe=0;
    } 
    if(bc95Act::stpSe==200){                           //规范200是成功,
        bc95Act::stpSe=0;
        //know::magtsk();   
        know::polSta[15]='1';
    }   
    return;
}

/*****************************************
Fun.: rtc set。
Desc.:
Auth. Vesion:2017.7.2
*****************************************/ 
void bc95Act::setrtc(void)
{
    uint32_t secc;

    changdate();
    secc=changsec();
    set_time(secc);
}

void bc95Act::changdate(void)
{
    year= (buf_bc95Rx[5]-0x30)*10+(buf_bc95Rx[6]-0x30)+2000;                //年
    mon = (buf_bc95Rx[8]-0x30)*10+(buf_bc95Rx[9]-0x30);                     //月
    day = (buf_bc95Rx[11]-0x30)*10+(buf_bc95Rx[12]-0x30);                   //日
    hour= (buf_bc95Rx[14]-0x30)*10+(buf_bc95Rx[15]-0x30);                   //时
    min = (buf_bc95Rx[17]-0x30)*10+(buf_bc95Rx[18]-0x30);                   //分
    sec = (buf_bc95Rx[20]-0x30)*10+(buf_bc95Rx[21]-0x30);                   //秒
}

uint32_t bc95Act::changsec(void)
{
    uint16_t t;
    uint32_t seccount=0;

    for(t=1970; t< year; t++) {
        if(Is_Leap_Year(t)) seccount+=31622400;
        else seccount+=31536000;
    }
    mon-=1;
    for(t=0; t< mon; t++) {
        seccount+=(uint32_t) mon_table[t]*86400;
        if(Is_Leap_Year(year)&&t==1) seccount+=86400;
    }
    seccount+=(uint32_t)( day-1)*86400;
    seccount+=(uint32_t) hour*3600;
    seccount+=(uint32_t) min*60;
    seccount+= sec;
    return seccount;
}

/*****************************************
Fun.: 闰年判断。
Desc.:
Auth. Vesion:2017.7.2
*****************************************/
uint8_t bc95Act::Is_Leap_Year(uint16_t year)
{  
    if(year%4==0) {
        if(year%100==0) {
            if(year%400==0)return 1;//
            else return 0;
        } else return 1;
    } else return 0;
}

/*****************************
Fun.: 判断AT指令返回值。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::jump_Judg(int Chekstep,int Nexnexstep){
    bc95Act::stpSe =Chekstep;
    bc95Act::cntche=0;
    bc95Act::cntlink++;
    if(bc95Act::cntlink>2){
        bc95Act::stpSe=Nexnexstep; 
        bc95Act::cntlink=0;
    }    
}

void bc95Act::CheckOK(int Nexstp,int Lastp,int maxstp)
{
    if(know::polSta[17]=='1'){  
        know::polSta[17]='0';
        bc95Act::stpSe=Nexstp;
        bc95Act::cntlink=0;
    }
    else{
        bc95Act::stpSe= bc95Act::stpSe; 
        bc95Act::cntche++;
        if(bc95Act::cntche>maxstp) bc95Act::stpSe=Lastp;
    }   
}

void bc95Act::put(void)
{
    know::uart3.printf("AT+NSOCR=DGRAM,17,9090,1\r\n");
    wait_ms(50);
    know::uart3.printf("%s",know::polDat[2]);
    wait_ms(50);
    know::uart3.printf("AT+NSOCL=0\r\n");
    wait_ms(50);

    know::polSta[16]=1;
}

/*****************************
Fun.: bc95接收数据中断函数。
Desc.:
Auth. Vesion:2017.7.2
*****************************/
void bc95Act::getISR(void)
{
    char buf;
    
    buf=know::uart3.getc();
    bc95Act::buf[cnt]=buf;
    cnt++;     
    if(cnt>5&&buf==0x0D) cnt=0;
    if(cnt>40) cnt=0;
}