⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time.c

📁 ATmega103、ATmega128做的开发板web server源码
💻 C
字号:
/******************************************************************
* time.c:
* 
* Copyright (c) 2001 Atmel Corporation.
* All Rights Reserved.
*
* You are autorized to use, copy and distribute this software only at 
* a single site (the term "site" meaning a single company location). 
* This copyright notice must be included in any copy, modification 
* or portion of this software merged into another program.
* 
* This software is licenced solely for use with Atmel AVR micro 
* controller family. The software may not be modified to execute on 
* any other microcontroller architectures
*
* This software is provided "as is"; Without warranties either express
* or implied, including any warranty regarding merchantability, 
* fitness for a particular purpose or noninfringement. 
*
* In no event shall Atmel or its suppliers be liable for any special, 
* indirect,incidential or concequential damages resulting from the 
* use or inability to use this software.
*
* Revision history:
*
* January 17, 2001:   Version 1.0
*			  Created by JB
*
*******************************************************************/
#define ENABLE_BIT_DEFINITIONS
#include "comp_a90.h"
#include "tcp.h"
#include "config.h"
#include "time.h"
#include <iom103.h>
#include <stdio.h>



unsigned long time( void )
{
  SOCKET *sock;
  unsigned long time;
  unsigned int timeIP0,timeIP1;
  char buffer[10];
  unsigned char * ptemp=(unsigned char *) &time;

  if (!getOption("TIME\0","timeIp0\0",buffer))
  {
    return 0;                   //error reading from server.ini
  } 
  sscanf(buffer,"%x",&timeIP0);
  
  if (!getOption("TIME\0","timeIp1\0",buffer))
  {
    return 0;                   //error reading from server.ini
  } 
  sscanf(buffer,"%x",&timeIP1);
  
  sock=TCPaopen(TIME_TIMESERVE_PORT,timeIP0,timeIP1,0);//connect
  
  if( timeWait(sock) )                    //wait for response or timeout
  {
   
    TCPget(sock,10, buffer );
    /*extract the current time from the received datagram*/
    ptemp[0]=buffer[3];
    ptemp[1]=buffer[2];
    ptemp[2]=buffer[1];
    ptemp[3]=buffer[0];
    TCPclose(sock);
    return time;
  }
  else                                          //timeout
  {
    TCPclose(sock);
    return 0;
  }
}


unsigned char daytime(char * data, unsigned char maxLength)
{
  SOCKET *sock;
  char buffer[10];
  unsigned int timeIP0,timeIP1;
  unsigned int read;

  if (!getOption("TIME\0","timeIp0\0",buffer))
  {
    return 0;                                   //error reading from server.ini
  } 
  sscanf(buffer,"%x",&timeIP0);
  
  if (!getOption("TIME\0","timeIp1\0",buffer))
  {
    return 0;                                   //error reading from server.ini
  } 
  sscanf(buffer,"%x",&timeIP1);

  sock=TCPaopen(TIME_DAYTIME_PORT,timeIP0,timeIP1,0);//connect
  
  if( timeWait(sock) )                          //wait for response or timeout
  {
    read=TCPget(sock,maxLength-1, data );       //read data, subtract 1 from maxLentgh, for termination of the string
    data[read]='\0';                            //terminate the string
    TCPclose(sock);                             //close the socket
    return read;                                //return the length of the string
  }
  else                                          //timeout
  {
    TCPclose(sock);                             //close the socket
    return 0;
  }
}

unsigned char timeWait(SOCKET * sock)
{
  unsigned int testus;

  TCCR1B|=(1<<CTC1 | 1<<CS12 | 1<<CS10);      //clear counter on compare match, clock divided by 1024
  OCR1A=TIME_TIMEOUT;
  TIFR|=(1<<OCF1A);                           //output compare flag 1A is cleared by writing a logic one
  TCNT1=0;                                    //clear the counter
  for (;;)
  {
    if (TCPsize(sock))                        //poll TCP buffer
    {                      
      return 1;
    }
    else if (TIFR&(1<<OCF1A))                 //output compare flag 1A is set, timeout
    {
      testus=TCPsize(sock);
      testus=TCPsize(sock);
      testus=TCPsize(sock);
      return 0;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -