clock

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mybc95.cpp Source File

mybc95.cpp

00001 #include "mybc95.h"
00002 
00003 char buf_bc95Tx[7]="";
00004 char bc95Act::buf[50]= {0x00};
00005 char bc95Act::buf_bc95Tx[buf_bc95TxSize]= {0x00};
00006 char bc95Act::buf_bc95Rx[buf_bc95RxSize]= {0x00};
00007 int bc95Act::stpSe=0;
00008 int bc95Act::cnt=0;
00009 int bc95Act::cntche;
00010 int bc95Act::cntlink=0;
00011 int bc95Act::year=0;
00012 int bc95Act::mon=0;
00013 int bc95Act::day=0;
00014 int bc95Act::hour=0;
00015 int bc95Act::min=0;
00016 int bc95Act::sec=0;
00017 const int mon_table[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
00018 /*****************************
00019 Fun.: base initialization of bc95。
00020 Desc.: 
00021 Auth. Vesion:2017.7.2
00022 *****************************/
00023 void bc95Act::init(void)
00024 {
00025     know::uart3.baud(9600);
00026     know::uart3.attach(&getISR,SerialBase::RxIrq);
00027 }
00028 
00029 /*****************************
00030 Fun.: initialization of bc95 start。
00031 Desc.:
00032 Auth. Vesion:2017.7.2
00033 *****************************/
00034 void bc95Act::start(void)
00035 {
00036     know::pow.write(1);     //BC95POW_ON();
00037     wait(50);
00038 }
00039 
00040 /*****************************
00041 Fun.: initialization of bc95 data。
00042 Desc.:
00043 Auth. Vesion:2017.7.2
00044 *****************************/
00045 void bc95Act::data(void)
00046 {
00047     stpSe=1;
00048     cnt=0; 
00049 }
00050 
00051 /*****************************
00052 Fun.: initialization of bc95 ready。
00053 Desc.:
00054 Auth. Vesion:2017.7.2
00055 *****************************/
00056 void bc95Act::ready(void)
00057 {
00058 
00059 }
00060 
00061 /*****************************
00062 Fun.: bc95 revoke
00063 Desc.:
00064 Auth. Vesion:2017.7.2
00065 *****************************/
00066 void bc95Act::revok(void)
00067 {
00068     know::pow.write(0);    //BC95POW_OFF();
00069 }
00070 
00071 /*****************************
00072 Fun.: get time。
00073 Desc.:
00074 Auth. Vesion:2017.7.2
00075 *****************************/
00076 char *bc95Act::get_tim(void)
00077 {
00078     static char buf_tim[7]="";
00079     tm *tim;
00080     time_t seconds=time(NULL);
00081     tim=localtime(&seconds);
00082     
00083     buf_tim[0]=char(tim->tm_year-100);
00084     buf_tim[1]=char(tim->tm_mon);
00085     buf_tim[2]=char(tim->tm_mday);
00086     buf_tim[3]=char(tim->tm_hour);
00087     buf_tim[4]=char(tim->tm_min);
00088     buf_tim[5]=char(tim->tm_sec);
00089     
00090     return buf_tim;
00091 }
00092 
00093 /*****************************
00094 Fun.: get know。
00095 Desc.:
00096 Auth. Vesion:2017.7.2
00097 *****************************/
00098 void bc95Act::get_knw(void)
00099 {
00100     if(bc95Act::stpSe==1){ 
00101         know::uart3.printf("AT+CCLK?\r\n");
00102         jump_Judg(2,250);    
00103 
00104     }
00105     if(bc95Act::stpSe==2) CheckOK(200,1,10);             //10是最大的查询次数
00106     if(bc95Act::stpSe==250){                            //规范250是失败
00107         know::polSta[15]='2';
00108         bc95Act::stpSe=0;
00109     } 
00110     if(bc95Act::stpSe==200){                           //规范200是成功,
00111         bc95Act::stpSe=0;
00112         //know::magtsk();   
00113         know::polSta[15]='1';
00114     }   
00115     return;
00116 }
00117 
00118 /*****************************************
00119 Fun.: rtc set。
00120 Desc.:
00121 Auth. Vesion:2017.7.2
00122 *****************************************/ 
00123 void bc95Act::setrtc(void)
00124 {
00125     uint32_t secc;
00126 
00127     changdate();
00128     secc=changsec();
00129     set_time(secc);
00130 }
00131 
00132 void bc95Act::changdate(void)
00133 {
00134     year= (buf_bc95Rx[5]-0x30)*10+(buf_bc95Rx[6]-0x30)+2000;                //年
00135     mon = (buf_bc95Rx[8]-0x30)*10+(buf_bc95Rx[9]-0x30);                     //月
00136     day = (buf_bc95Rx[11]-0x30)*10+(buf_bc95Rx[12]-0x30);                   //日
00137     hour= (buf_bc95Rx[14]-0x30)*10+(buf_bc95Rx[15]-0x30);                   //时
00138     min = (buf_bc95Rx[17]-0x30)*10+(buf_bc95Rx[18]-0x30);                   //分
00139     sec = (buf_bc95Rx[20]-0x30)*10+(buf_bc95Rx[21]-0x30);                   //秒
00140 }
00141 
00142 uint32_t bc95Act::changsec(void)
00143 {
00144     uint16_t t;
00145     uint32_t seccount=0;
00146 
00147     for(t=1970; t< year; t++) {
00148         if(Is_Leap_Year(t)) seccount+=31622400;
00149         else seccount+=31536000;
00150     }
00151     mon-=1;
00152     for(t=0; t< mon; t++) {
00153         seccount+=(uint32_t) mon_table[t]*86400;
00154         if(Is_Leap_Year(year)&&t==1) seccount+=86400;
00155     }
00156     seccount+=(uint32_t)( day-1)*86400;
00157     seccount+=(uint32_t) hour*3600;
00158     seccount+=(uint32_t) min*60;
00159     seccount+= sec;
00160     return seccount;
00161 }
00162 
00163 /*****************************************
00164 Fun.: 闰年判断。
00165 Desc.:
00166 Auth. Vesion:2017.7.2
00167 *****************************************/
00168 uint8_t bc95Act::Is_Leap_Year(uint16_t year)
00169 {  
00170     if(year%4==0) {
00171         if(year%100==0) {
00172             if(year%400==0)return 1;//
00173             else return 0;
00174         } else return 1;
00175     } else return 0;
00176 }
00177 
00178 /*****************************
00179 Fun.: 判断AT指令返回值。
00180 Desc.:
00181 Auth. Vesion:2017.7.2
00182 *****************************/
00183 void bc95Act::jump_Judg(int Chekstep,int Nexnexstep){
00184     bc95Act::stpSe =Chekstep;
00185     bc95Act::cntche=0;
00186     bc95Act::cntlink++;
00187     if(bc95Act::cntlink>2){
00188         bc95Act::stpSe=Nexnexstep; 
00189         bc95Act::cntlink=0;
00190     }    
00191 }
00192 
00193 void bc95Act::CheckOK(int Nexstp,int Lastp,int maxstp)
00194 {
00195     if(know::polSta[17]=='1'){  
00196         know::polSta[17]='0';
00197         bc95Act::stpSe=Nexstp;
00198         bc95Act::cntlink=0;
00199     }
00200     else{
00201         bc95Act::stpSe= bc95Act::stpSe; 
00202         bc95Act::cntche++;
00203         if(bc95Act::cntche>maxstp) bc95Act::stpSe=Lastp;
00204     }   
00205 }
00206 
00207 void bc95Act::put(void)
00208 {
00209     know::uart3.printf("AT+NSOCR=DGRAM,17,9090,1\r\n");
00210     wait_ms(50);
00211     know::uart3.printf("%s",know::polDat[2]);
00212     wait_ms(50);
00213     know::uart3.printf("AT+NSOCL=0\r\n");
00214     wait_ms(50);
00215 
00216     know::polSta[16]=1;
00217 }
00218 
00219 /*****************************
00220 Fun.: bc95接收数据中断函数。
00221 Desc.:
00222 Auth. Vesion:2017.7.2
00223 *****************************/
00224 void bc95Act::getISR(void)
00225 {
00226     char buf;
00227     
00228     buf=know::uart3.getc();
00229     bc95Act::buf[cnt]=buf;
00230     cnt++;     
00231     if(cnt>5&&buf==0x0D) cnt=0;
00232     if(cnt>40) cnt=0;
00233 }