int main(void)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
// u8 tbuf[40];
u8 t=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2,òòÎaóÃμ½á˶¨ê±Æ÷1msÖD¶Ï
uart_init(115200); //3õê¼»ˉ′®¿ú1 8-N-1
delay_init(168); //3õê¼»ˉSystick1¤×÷ê±Öó
My_RTC_Init(); //3õê¼»ˉRTC
RTC_Set_WakeUp(RTC_WakeUpClock_CK_SPRE_16bits,0); //ÅäÖÃWAKE UPÖD¶Ï,1ÃëÖóÖD¶Ïò»′Î
SPI_Configuration(); //SPI3õê¼»ˉ
reset_w5500(); /*ó2¸′λW5500*/
PHY_check(); /*¼ì2éíøÏßêÇ·ñ½óèë*/
set_w5500_mac(); /*ÅäÖÃMACμØÖ·*/
// set_w5500_ip(); /*ÅäÖÃIPμØÖ·*/
socket_buf_init(txsize, rxsize); /*3õê¼»ˉ8¸öSocketμÄ·¢Ëí½óêÕ»o′æ′óD¡¶¼Îa2K*/
// printf(" W5500ÎaTCP ·tÎñÆ÷£¬½¨á¢Õììy£¬μè′yPC×÷ÎaTCP Client½¨á¢ᬽó
");
// printf(" W5500¼àìy¶Ë¿úÎa£o %d
",local_port);
// printf(" ᬽó3é1|oó£¬TCP Client·¢Ëíêy¾Y¸øW5500£¬W5500½«·μ»Ø¶Ôó|êy¾Y
");
printf(" NTP·tÎñÆ÷IPÎa:%d.%d.%d.%d
",ntp_server_ip[0],ntp_server_ip[1],ntp_server_ip[2],ntp_server_ip[3]);
printf(" NTP·tÎñÆ÷¶Ë¿úÎa:%d
",ntp_port);
ntp_client_init(); /*NTP3õê¼»ˉ*/
while(1)
{
do_dhcp(); /*′óDHCP·tÎñÆ÷»ñè¡2¢ÅäÖÃIPμØÖ·*/
if(dhcp_ok==1) /*3é1|»ñè¡IPμØÖ·*/
{
do_ntp_client();
}
}
进入了UDP模式
但是没获取时间
uint8 ntp_try_times=0; /*ÇëÇó′Îêy*/
void do_ntp_client(void)
{
if(total_seconds>0)return; /*òÑ»ñè¡ê±¼äÔò2»ÔùÖ′DDNPT3ìDò*/
else
{
uint16 len;
uint8 * data_buf = bufpub;
uint32 destip = 0;
uint16 destport;
uint16 startindex = 40; /*»Ø¸′°üÖDê±¼äêy¾Yê×μØÖ·*/
uint16 k=0;
k= getSn_SR(SOCK_NTP) ;
printf("k=%x ",k); /*??socket???*/
switch(k)
//switch(getSn_SR(SOCK_NTP))
{
case SOCK_UDP: /*UDPÄ£꽿aÆô*/
if(total_seconds>0) return; /*òÑ»ñè¡ê±¼äÔò2»ÔùÖ′DDNPT3ìDò*/
if(ntp_try_times<100)
{
if(ntptimer>0) /*1ÃëÇëÇóò»′Î*/
{
/*·¢ËíÇëÇóμ½NPT·tÎñÆ÷*/
sendto(SOCK_NTP,ntp_message,sizeof(ntp_message),ntp_server_ip, ntp_port);
ntp_try_times++;
if(npt_debuge)
//printf("ntp_try_times:%d
",ntp_try_times); /*′òó¡ÇëÇó′Îêy*/
printf(" . .");
ntptimer=0; /*Ôú¶¨ê±Æ÷2ÖD¶ÏÖD¼ó1*/
}
}
else
{
ntp_try_times=0;
if(npt_debuge)
printf("ntp retry failed!
"); /*NTPÇëÇó꧰ü*/
}
if ((len = getSn_RX_RSR(SOCK_NTP)) > 0)
{
if (len > TX_RX_MAX_BUF_SIZE) len = TX_RX_MAX_BUF_SIZE;
recvfrom(SOCK_NTP, data_buf, len, (uint8*)&destip, &destport);/*½óêÕNTP·tÎñÆ÷»Ø¸′êy¾Y*/
get_seconds_from_ntp_server(data_buf,startindex); /*′óNTP·tÎñÆ÷»ñè¡ê±¼ä*/
printf("
±±¾©ê±¼ä£o%d-%02d-%02d %02d:%02d:%02d
",
(nowdate.year[0]<<8)+nowdate.year[1],
nowdate.month,
nowdate.day,
nowdate.hour,
nowdate.minute,
nowdate.second);
ntp_try_times=0;
}
break;
case SOCK_CLOSED:
socket(SOCK_NTP,Sn_MR_UDP,ntp_port,0);
break;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>