clock

Dependencies:   mbed

myScope/mybc95.cpp

Committer:
donghuoyinzi
Date:
2018-05-08
Revision:
0:17e9016529cf
Child:
1:013b9fdc4e78

File content as of revision 0:17e9016529cf:

#include "mybc95.h"

char bc95Act::buf[50]={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
*****************************/
void bc95Act::get_tim(void)
{
   /* if(bc95Act::stpSe==1){ 
        uart.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[5] =2;
        bc95Act::stpSe =0;
    } 
    if(bc95Act::stpSe ==200){                           //规范200是成功,
        bc95Act::stpSe =0;
         bc95Act::setrtc();
        know:: polSta[5] =1;
    }   
    return;*/
}

/*****************************
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[6]=2;
        bc95Act::stpSe=0;
    } 
    if(bc95Act::stpSe==200){                           //规范200是成功,
        bc95Act::stpSe=0;
        //know::magtsk();   
        know::polSta[6]=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)
{
    unsigned char temp1=0;

    int i;

    for(temp1=0;temp1<27;temp1++){
        year= (buf[temp1+5]-0x30)*10+(buf [temp1+6]-0x30)+2000;        //年
        mon = (buf[temp1+8]-0x30)*10+(buf [temp1+9]-0x30);             //月
        day = (buf[temp1+11]-0x30)*10+(buf [temp1+12]-0x30);            //日
                        
        if((buf[temp1+23]== '0') && (buf [temp1+24] == '0'))               //UTC
                hour =(buf[temp1+14]-0x30)*10+(buf [temp1+15]-0x30)+8;  //时
        else if((buf[temp1+23]== '0') && (buf [temp1+24] == '8'))            //EAST 8
                hour=(buf[temp1+14]-0x30)*10+(buf[temp1+15]-0x30); 
        else  hour=(buf[temp1+14]-0x30)*10+(buf[temp1+15]-0x30)+8;
            min=(buf[temp1+17]-0x30)*10+(buf[temp1+18]-0x30);             //分
        sec= (buf[temp1+20]-0x30)*10+(buf[temp1+21]-0x30);              //秒
    }
    for(i=0;i<27;i++)  buf[i]=0;
}

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].c_str()); 
    wait_ms(50);
    know::uart3.printf("AT+NSOCL=0\r\n"); 
    wait_ms(50);

    know::polSta[7] =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;
}