stm32f4+gps+ds1302

960阅读 0评论2014-02-24 枫海8深蓝
分类:C/C++

/**
  ******************************************************************************
* author ??????
* E-mail ??971376711@qq.com
* phone ??15666383949
* time   ??2013 10 09
* project ???ì???????à??rtu
* brief  ??±????????????????ì???????à???????ú????????±à??????????????????????????????
*?ú×??????à???????ó???????????à?????ú?·????????±????????????????????????ù????STM32F407
*?¨??·???????????lwip+udp???????ú????????????????±????ò???ì??????????
  ******************************************************************************  
  */
#include "GX_DS1302.h"
#include "time.h"
#include "stdint.h"
#include "main.h"
#include
#include
#include "GX_COM.h"
#include "GX_USER_COMUSE.h"


unsigned char time_buf[8];        //?????±????????×é
struct time time2,time1,time3;
time_t time0;//?¨????????time_t???????à????????




u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //??????????±í  


//???ê????·?????±í


const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};






/*********************?ê?¨?????ò*****************************/
/*DS1302???·*/
#define DS1302_sec_add 0x80  
#define DS1302_min_add 0x82  
#define DS1302_hr_add 0x84  
#define DS1302_date_add 0x86  
#define DS1302_month_add 0x88  
#define DS1302_day_add 0x8a  
#define DS1302_year_add 0x8c  
#define DS1302_control_add 0x8e  
#define DS1302_charger_add 0x90  
#define DS1302_clkburst_add 0xbe




#define DS1302_SCLK(x) (x ? GPIO_SetBits(GPIOF, GPIO_Pin_12): GPIO_ResetBits(GPIOF, GPIO_Pin_12))
#define DS1302_OUTPUT(x) (x ? GPIO_SetBits(GPIOB, GPIO_Pin_2): GPIO_ResetBits(GPIOB, GPIO_Pin_2))//DS1302_IO ?????ê?¨??
#define DS1302_CE(x) (x ? GPIO_SetBits(GPIOF, GPIO_Pin_11): GPIO_ResetBits(GPIOF, GPIO_Pin_11))


#define DS1302_INPUT() GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)  //DS1302_IO ?????ê?¨??


#define Charge() Writeoneword_1302(0x90,0xa4)
#define ENWrite() Writeoneword_1302(0x8e,0x00)
#define DISWrite() Writeoneword_1302(0x8e,0x80)


/*************?????ù?÷??*********************/




//u8 ChangeTohex(u8 dat);
//u8 ChangeToDec(u8 dat);


//**************************?????ù?÷???ò***********************************/


/**??????×????????ù????**/
u8 ChangeTohex(u8 dat)
{
u8 temp;
temp=(dat/10)*16+dat%10;
return temp;
}


/**???ù????×???????????**/
u8 ChangeToDec(u8 dat)
{
u8 temp;
temp=(dat/16)*10+dat%16;
return temp;
}




//DS1302??????
void DS1302_init(void) 
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //????GPIOF???±??  PF12 --DS18B20_SCLK??PF11 --DS1302_CE
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //????GPIOB???±??  PB2 --DS1302_IO


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;  //PF12 --DS18B20_SCLK??PF11 --DS1302_CE
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;     //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    //???ì
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;  //PB2 --DS1302_IO
GPIO_Init(GPIOB, &GPIO_InitStructure);

/*???????±?????á????*/
  //Settime_1302(2013,8,8, u8 1, 12,00,00) ;//??gps???±??
}


/**??????×???????**/
void Writeonechar_1302(u8 m)
{
u8 i;
for(i=0;i<8;i++)
{
DS1302_SCLK(0);
Delay_DS1302(30);
if(m&0x01==1)
{
DS1302_OUTPUT(1);
}
else
{
DS1302_OUTPUT(0);
}
Delay_DS1302(30);
DS1302_SCLK(1);
Delay_DS1302(30);
m=m>>1;
delay_ms(1);
}
}


/**????×???????**/
u8 Readonechar_1302()  
{
u8 i,temp=0x00;


for(i=0;i<8;i++)
{
temp=temp>>1;
DS1302_SCLK(0);
Delay_DS1302(30);
if(DS1302_INPUT()==1)
temp=temp|0x80;
DS1302_SCLK(1);
Delay_DS1302(30);
}
return temp;
}  


/**??DS1302????????**/
u8 Readoneword_1302(u8 address)
{
u8 temp;
GPIO_InitTypeDef GPIO_InitStructure; 
DS1302_SCLK(0);
Delay_DS1302(30);
DS1302_CE(1);
Delay_DS1302(300);

GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;  //PB2 DS1302 ????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;     //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    //???ì
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);


Writeonechar_1302(address);


GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2;  //PB2 DS1302 ????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;     //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;    //????
GPIO_Init(GPIOB, &GPIO_InitStructure);


temp=Readonechar_1302();
Delay_DS1302(30);
DS1302_SCLK(0);
Delay_DS1302(30);
DS1302_CE(0);
return temp;
}


/**?òDS1302????????**/
void Writeoneword_1302(u8 address,u8 shuju)
{

GPIO_InitTypeDef GPIO_InitStructure;
DS1302_SCLK(0);
Delay_DS1302(30);
DS1302_CE(1);

GPIO_InitStructure.GPIO_Pin =GPIO_Pin_12;  //PF12 --DS18B20_SCLK
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;     //????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    //???ì
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);

Writeonechar_1302(address);
Writeonechar_1302(shuju);
DS1302_SCLK(0);
Delay_DS1302(30);
DS1302_CE(0);

}


//**?è???±??,????week????0?±????????
void Settime_1302(u16 year,u8 month,u8 day, u8 week, u8 hour,u8 minute,u8 second) 
{


year=ChangeTohex(year-2000);  
month=ChangeTohex(month);
day=ChangeTohex(day);
hour=ChangeTohex(hour);
minute=ChangeTohex(minute);
week=ChangeTohex(week);

ENWrite();
Writeoneword_1302(0x8c,year);
Writeoneword_1302(0x88,month);
Writeoneword_1302(0x86,day);

if (week != 0)
Writeoneword_1302(0x8a,week);

 Writeoneword_1302(0x84,hour);
 Writeoneword_1302(0x82,minute);
 Writeoneword_1302(0x80,second);
 DISWrite();
//  printf("DS1302??????GPS???ü????????");
}


/**?????±??**/
u32 Gettime_1302(void) //????1302???????±??
{


u32 time_temp=0;
time1.w_year=Readoneword_1302(0x8d);
time1.w_month=Readoneword_1302(0x89)&0x1f;  
time1.w_date=Readoneword_1302(0x87)&0x3f;
time1.week=Readoneword_1302(0x8b)&0x3f;
time1.hour=Readoneword_1302(0x85)&0x1f;
time1.min =Readoneword_1302(0x83)&0x7f;
time1.sec=Readoneword_1302(0x81)&0x7f;    


// timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec
/******???????????ù??????????×???????????????*******/
// time1.w_year =ChangeToDec(buffer[6])+2000;
// time1.w_month =ChangeToDec(buffer[5]);
// time1.w_date =ChangeToDec(buffer[4]);
// time1.week =ChangeToDec(buffer[3]);
// time1.hour =ChangeToDec(buffer[2]);
// time1.min  =ChangeToDec(buffer[1]);
// time2.sec =ChangeToDec(buffer[0]);
time_temp = RTC_Get(time1.w_year,time1.w_month,time1.w_date,time1.hour,time1.min,time1.sec);//???±??×????????ú1900?ê?????ù

return time_temp;

}




u32 RTC_Get(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)


{


       u16 t;


       u32 seccount=0;
       syear=syear+2000;
       if(syear<2000||syear>2099)return 1;//syear·??§1970-2099???????è??·??§??2000-2099       


       for(t=1970;t

       {


              if(Is_Leap_Year(t))seccount+=31622400;//?ò?ê????????


              else seccount+=31536000;                    //???ê????????


       }


       smon-=1;


       for(t=0;t

       {


              seccount+=(u32)mon_table[t]*86400;//??·????????à??


              if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//?ò?ê2??·????????ì????????        


       }


       seccount+=(u32)(sday-1)*86400;//°??°???????????????à??


       seccount+=(u32)hour*3600;//???±??????


       seccount+=(u32)min*60;      //·?????????


       seccount+=sec;//×??ó????????????


                                                                                              


       //?è???±??


  /*  RCC->APB1ENR|=1<<28;//?????????±??


    RCC->APB1ENR|=1<<27;//????±?·??±??


       PWR->CR|=1<<8;    //????±?·?????±??¤


       //??????????±?????!*/




       return seccount;     


}




u8 Is_Leap_Year(u16 year)


{                    


       if(year%4==0) //±?????±?4????


       {


              if(year%100==0)


              {


                     if(year%400==0)return 1;//??????00?á??,??????±?400????         


                     else return 0;  


              }else return 1;  


       }else return 0;


}                          


//?è???±??


//°????????±??×?????????


//??1970?ê1??1?????ù×?


//1970~2099?ê????·¨?ê·?


//·?????:0,????;????:?í?ó?ú??.


//??·?????±í                                                                       


u8 RTC_Get_Week(u16 year,u8 month,u8 day)


{  
   u16 temp2;
   u8 yearH,yearL;
   yearH=year/100;     yearL=year%100;
   // ??????21????,?ê·?????100 
   if (yearH>19)yearL+=100;
   // ?ù???ò?ê??????1900?ê???ó?? 
   temp2=yearL+yearL/4;
   temp2=temp2%7;
   temp2=temp2+day+table_week[month-1];
   if (yearL%4==0&&month<3)temp2--;
   return(temp2%7);
}


u8 RTC_Set(u8 time_temp_0,u8 time_temp_1,u8 time_temp_2,u8 time_temp_3)//?¨??????????????????????×???


{


       static u16 daycnt=0;


       u32 timecount=0;


       u32 temp=0;


       u16 temp1=0;


 


       timecount=time_temp_0*16777216+time_temp_1*65536+time_temp_2*256+time_temp_3;


         


       /*timecount=RTC->CNTH;//?????????÷??????(??????)


       timecount<<=16;


       timecount+=RTC->CNTL;    */          


 


       temp=timecount/86400;   //?????ì??(????????????)


       if(daycnt!=temp)//???????ì??


       {      


              daycnt=temp;


              temp1=1970;  //??1970?ê????


              while(temp>=365)


              {                         


                     if(Is_Leap_Year(temp1))//???ò?ê


                     {


                            if(temp>=366)temp-=366;//?ò?ê????????


                            else {temp1++;break;} 


                     }


                     else temp-=365;       //???ê


                     temp1++; 


              }  


              time3.w_year=temp1;//?????ê·??????????????ê·?


              temp1=0;


              while(temp>=28)//????????????


              {


                     if(Is_Leap_Year(time3.w_year)&&temp1==1)//?±?ê???????ò?ê/2??·?


                     {


                            if(temp>=29)temp-=29;//?ò?ê????????


                            else break;


                     }


                     else


                     {


                            if(temp>=mon_table[temp1])temp-=mon_table[temp1];//???ê


                            else break;


                     }


                     temp1++; 


              }


              time3.w_month=temp1+1;//??????·?


              time3.w_date=temp+1;  //????????


       }


       temp=timecount%86400;     //??????????     


       time3.hour=temp/3600;     //???±


       time3.min=(temp%3600)/60; //·???     


       time3.sec=(temp%3600)%60; //????


       time3.week=RTC_Get_Week(time3.w_year,time3.w_month,time3.w_date);//????????  


 
Settime_1302(time3.w_year,time3.w_month,time3.w_date,0,time3.hour,time3.min,time3.sec);//?????????????÷?ú?????ü???ú?????±??????
       return 0;


}

前面是ds1302后面是gps
/**
  ******************************************************************************
* author ??????
* E-mail ??971376711@qq.com
* phone ??15666383949
* time   ??2013 10 09
* project ???ì???????à??rtu
* brief  ??±????????????????ì???????à???????ú????????±à??????????????????????????????
*?ú×??????à???????ó???????????à?????ú?·????????±????????????????????????ù????STM32F407
*?¨??·???????????lwip+udp???????ú????????????????±????ò???ì??????????
  ******************************************************************************  
  */
#include  
#include "GX_DS1302.h"
#include "time.h"
#include "stdint.h"
#include "main.h"
#include
#include
#include "GX_COM.h"
#include "GX_USER_COMUSE.h"
#include "GX_GPS.h"


/******************?????ù?÷??*********************/
#define NULL 0
extern struct time time2;




/******************?????ù?÷??*********************/
GPSINFO GpsInfo;




/*****************?????ù?÷??*********************/


//??????????GPS?á??????????????
void GpsDataInit(void)
{
GpsInfo.UtcTime[9]='\0';
GpsInfo.Latitude[10]='\0';
GpsInfo.Longitude[11]='\0';
GpsInfo.Speed[5]='\0';
GpsInfo.Azimuth[6]='\0';
GpsInfo.UtcDate[6]='\0';
GpsInfo.Altitude[7]='\0';

}


//???????????é??×?·?????·???str??×???ptr??????????
u16 FindStr(u8 *str,u8 *ptr)
{
u16 index=0;
u8 *STemp=NULL;
u8 *PTemp=NULL;
u8 *MTemp=NULL;

if(0==str||0==ptr)
return 0;
for(STemp=str;*STemp!='\0';STemp++) //?????é??×?·???
{
index++;  //?±?°????????1
MTemp=STemp; //???ò?±?°×?·???
//±???
for(PTemp=ptr;*PTemp!='\0';PTemp++)
{
if(*PTemp!=*MTemp)
break;
MTemp++;
}
if(*PTemp=='\0')  //???????ù???é????×?·???????
break;
}
return index;
}


//??????????GPS????????????
//×?????DataLen???????¤?????????±??????
void GPS_DataProcess(u8 *RCVData, vu8 DataLen)
{
u8 CommaNum=0; //??????
u8 BufIndex=0; //??×???
  u8 Sbuf;
u8 *Pstr;
u16 index;

RCVData[255]=NULL;
memset(&GpsInfo,0x00,sizeof(GpsInfo));//?????á????
index= FindStr(RCVData,"$GPRMC,");//?é??"$GPRMC,"?ù??????u16 FindStr(u8 *str,u8 *ptr)
 
if( (index>0)&&(index {
CommaNum=0;
Pstr=RCVData + index + 6; //????GPRMC???ó???????·
do
{
Sbuf=*(Pstr++);
// USART_SendData(USART1,Sbuf);
switch(Sbuf)
{
case ',':CommaNum++;  //?¨??????????????????×???·??à
BufIndex=0;
break;
default:
switch(CommaNum)
{
case 0:GpsInfo.UtcTime[BufIndex]=Sbuf;break; 
case 1:GpsInfo.Statue=Sbuf;             break;
case 2:GpsInfo.Latitude[BufIndex]=Sbuf;break;
case 3:GpsInfo.LatitudeNS=Sbuf;break;
case 4:GpsInfo.Longitude[BufIndex]=Sbuf;break;
case 5:GpsInfo.LongitudeEW=Sbuf;break;
case 6:GpsInfo.Speed[BufIndex]=Sbuf;break;
case 7:GpsInfo.Azimuth[BufIndex]=Sbuf;break;
case 8:GpsInfo.UtcDate[BufIndex]=Sbuf;break;
default:break;
}
BufIndex++; //
break;
}
}while(Sbuf!='*');//?±????????*??????

}
//??????×÷
index= FindStr(RCVData,"$GPGGA,");
if( (index>0)&&(index {
CommaNum=0;
Pstr=RCVData+index+6;
do
{
Sbuf=*Pstr++ ;
switch(Sbuf)
{
case ',':CommaNum++;
BufIndex=0;
break;
default:
switch(CommaNum)
{
case 0:break;
case 1:break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
case 7:break;
case 8:GpsInfo.Altitude[BufIndex]=Sbuf;break;
default:break;
}
BufIndex++;
break;
}
}while(Sbuf!='*');
} 

}


void GPS_PPS_GPIO_Init(void) //GPS_PPS??GPIO????
{
GPIO_InitTypeDef      GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP ; //????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOD, &GPIO_InitStructure);
GpsDataInit();
}




//????????????GPS???????±????DS1302???±
void GPSTime_To_DS1302(GPSINFO tempGpsInfo)
{

time2.w_year = (tempGpsInfo.UtcDate[4]-'0')*10 + (tempGpsInfo.UtcDate[5]-'0') + 2000;
time2.w_month = (tempGpsInfo.UtcDate[2]-'0')*10 + (tempGpsInfo.UtcDate[3]-'0');
time2.w_date  = (tempGpsInfo.UtcDate[0]-'0')*10 + (tempGpsInfo.UtcDate[1]-'0');
// temptime.week =tempGpsInfo;
time2.hour  = (tempGpsInfo.UtcTime[0]-'0')*10 + (tempGpsInfo.UtcTime[1]-'0') + 8;
time2.min  = (tempGpsInfo.UtcTime[2]-'0')*10 + (tempGpsInfo.UtcTime[3]-'0');
time2.sec = (tempGpsInfo.UtcTime[4]-'0')*10 + (tempGpsInfo.UtcTime[5]-'0');

//?òDS1302?????±??????
Settime_1302(time2.w_year,time2.w_month,time2.w_date,0,time2.hour,time2.min,time2.sec);
}








上一篇:stm32f4关于pwm的一点见解
下一篇:82c54调试代码