if(xTaskGetTickCount()-saveDRequest>(TIMEDELAYREQUEST/portTICK_RATE_MS)){
		saveDRequest=xTaskGetTickCount();
		delayRequest();
	}
	#endif

}
void printfClock(Clock clock){
			if(clock.sign==true){
				pc.printf("%lu s,%u\r\n",clock.second,(unsigned int)clock.halfmillis);
			}
			else{
				pc.printf("-%lu s,%u\r\n",clock.second,(unsigned int)clock.halfmillis);
			}
}
void updateClock(void){
		Clock diff;
		if(timeProt.correction.previousSignOffset==timeProt.offset.sign){
			timeProt.correction.previousSignOffset=timeProt.offset.sign;
			timeProt.correction.previoustimeOffset.second=timeProt.correction.currentTimeOffsetSync.second;
			timeProt.correction.previoustimeOffset.halfmillis=timeProt.correction.currentTimeOffsetSync.halfmillis;
			readClock(&timeProt.correction.currentTimeOffsetSync);
			if(timeProt.correction.previoustimeOffset.second!=0){
				if(sup(timeProt.correction.currentTimeOffsetSync,timeProt.correction.previoustimeOffset)){
												
					diff=subClock(timeProt.correction.currentTimeOffsetSync,timeProt.correction.previoustimeOffset);
					//printf("diff: ");
					//printfClock(diff);
					timeProt.correction.sumTime=sumClock(timeProt.correction.sumTime,diff);
					sumOffset=sumClock(sumOffset,timeProt.offset);
					/*printf("sumOffset: ");
					printfClock(sumOffset);
					printf("sumClock: ");
					printfClock(timeProt.correction.sumTime);
					
					printf("nb corrr  : %d",timeProt.correction.nbCorrection);
					*/
					timeProt.correction.nbCorrection++;
				}
				else{
					resetSofftwareCorrection();
				}
			}
		}
		else{
			resetSofftwareCorrection();
		}
		timeProt.correction.previousSignOffset=timeProt.offset.sign;
		Clock timeCopy;
byte MCP7941x::getAlarm( byte almNr, byte *ckA ) {
	byte err = readClock( mcAlarm+almNr*7, 6 );
	if( err != 6 ) {
		errCode = RTC_AL_RD_ERR;
		return errCode;
	} else {
		for( byte ix = 0; ix < 6; ix++ ) *(ckA+ix) = Wire.read( );
		return RTC_OK;
	}
} // getAlarm
byte MCP7941x::getStatus( byte regadr, byte *val ) {
	byte err = readClock( regadr, 1 );		//request one status byte
	if( err != 1 ) {
		errCode = RTC_ST_RD_ERR;
		return errCode;
	} else {
		*val = Wire.read( );
	}
	return RTC_OK;
} // getStatus - single byte
byte MCP7941x::getStatus( byte regadr, byte cnt, byte *ckS ) {
	byte err = readClock( regadr, cnt );	// request cnt status bytes
	if( err != cnt ) {
		errCode = RTC_ST_RD_ERR;
		return errCode;
	} else {
		for( byte ix = 0; ix < cnt; ix++ ) *(ckS+ix) = Wire.read( );
	}
	return RTC_OK;
} // getStatus - block
	sendP[3]=(uint8_t)(timeProt.saveTime[id].halfmillis&0x00FF);
	sendP[4]=(uint8_t)((timeProt.saveTime[id].halfmillis>>8)&0x00FF);
	timeProt.saveTime[id].second=0;
	Clock timeSave={timeManage.second,timeManage.halfmillis,true};
	sendP[5]=(uint8_t)(timeSave.second&0x00FF);
	sendP[6]=(uint8_t)((timeSave.second>>8)&0x00FF);
	sendP[7]=(uint8_t)(timeSave.halfmillis&0x00FF);
	sendP[8]=(uint8_t)((timeSave.halfmillis>>8)&0x00FF);
	
	send(type,9,sendP);
byte DS3231::getStatus( byte regadr, byte *ckS ) {
	byte err = readClock( regadr, 1 );		//request single byte
	if( err != 1 ) {
		errCode = RTC_ST_RD_ERR;
		return errCode;
	} else {
		*ckS = Wire.read( );
	}
	return RTC_OK;
} // getStatus - single byte
byte MCP7941x::getTime( Time &mct ) {
	byte err = readClock(  mcTime, 3 );	// request time
	if( err != 3 ) {
		errCode = RTC_TM_RD_ERR;
		return errCode;
	} else {
		mct.sec = Wire.read( )&0x7f;
		mct.min = Wire.read( );
		mct.hr = Wire.read( );
		return RTC_OK;
	} // if err
} // getTime
					sendHMI("	Erreur type unknow");
					
									
			}		
		}
		else{
			hmi.printf("wrong type");
		}
			
	}
	else{
		hmi.printf("error CRC!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n");	
byte DS3231::getAlarm( byte almNr, byte *ckA ) {
	byte nrbytes = 3;
	if( almNr == 0 ) nrbytes = 4;
	byte err = readClock( dsAlarm+almNr*4, nrbytes );
	if( err != nrbytes ) {
		errCode = RTC_AL_RD_ERR;
		return errCode;
	} else {
		for( byte ix = 0; ix < nrbytes; ix++ ) *(ckA+ix) = Wire.read( );
		return RTC_OK;
	}
} // getAlarm
Exemple #10
0
byte DS3231::getTime( Time &dst ) {
	byte err = readClock(  dsTime, 3 );	// request time
	if( err != 3 ) {
		errCode = RTC_TM_RD_ERR;
		return errCode;
	} else {
		dst.sec = Wire.read( );
		dst.min = Wire.read( );
		dst.hr = Wire.read( );
		return RTC_OK;
	} // if err
} // getTime
byte MCP7941x::getDate( Date &mcd ) {
	byte err = readClock(  mcDate, 4 );	// request date
	if( err != 4 ) {
		errCode = RTC_DT_RD_ERR;
		return errCode;
	} else {
		mcd.dow = Wire.read( )&0x07;
		mcd.dom = Wire.read( );
		mcd.mo = Wire.read( );
		mcd.yr = Wire.read( );
		return RTC_OK;
	} // if err
} // getDate
Exemple #12
0
byte DS3231::getDate( Date &dsd ) {
	byte err = readClock(  dsDate, 4 );	// request date
	if( err != 4 ) {
		errCode = RTC_DT_RD_ERR;
		return errCode;
	} else {
		dsd.dow = Wire.read( );
		dsd.dom = Wire.read( );
		dsd.mo = Wire.read( );
		dsd.yr = Wire.read( );
		return RTC_OK;
	} // if err
} // getDate
// Propagates and updates the state estimate from tlefile to the current time
// and places the new position and velocity in the corresponding variables.
void currentOrbitState(const char* rvfile, const char* init_tlefile, const char* clockfile, double* rvtime, double posn[3], double vel[3]) {

  const gravconsttype GRAV_CONSTS = wgs72;
  // Check whether these are the right ones to hard-code.

  elsetrec satrecord;
  // This is an object which contains the satellite info.


  // Initialise satrecord directly from TLE file...
  // ... trust me, it's cleaner than using sgp4init()
  FILE *tle_fptr;
  tle_fptr = fopen( init_tlefile, "r" );
  if (tle_fptr == NULL) {
    printf("Error opening file: %s\n", init_tlefile);
    return;
  }

  char tle_line1[130], tle_line2[130];
  fgets(tle_line1, 130, tle_fptr);
  fgets(tle_line2, 130, tle_fptr);

  fclose(tle_fptr);

  double startmfe, stopmfe, deltamin;

  // The twoline2rv() function initialises the satrecord object

  twoline2rv(tle_line1, tle_line2, 'v', 'm', 'i', GRAV_CONSTS, startmfe, stopmfe, deltamin, satrecord);
  // Again, check whether these are the inputs we want.


  // Propagate to new state
  *rvtime = readClock(clockfile);
  sgp4(GRAV_CONSTS, satrecord, 1440.0*(*rvtime - satrecord.jdsatepoch), posn, vel);

  // rvtime, posn, vel will be stored in the pointers as outputs, but we'll write to file as well.
  writeRV(rvfile, *rvtime, posn, vel);

}
void ppsISR(void)
{	
	Disable_global_interrupt();
	Clock timeCopy={0,0,true};
	//Disable_global_interrupt();
	Clock timeMaster={0,0,true};
	readClock(&timeCopy);
	//Enable_global_interrupt();
	timeMaster.second=timeCopy.second;
	timeMaster.halfmillis=timeCopy.halfmillis;
	if(timeMaster.halfmillis>=RTC_FREQ/2){//for the accurate 500ms
		timeMaster.second++;
	}
	timeMaster.halfmillis=0;
	/*if((timeMaster.halfmillis>RTC_FREQ)||(timeMaster.halfmillis<0)){
			printf("timeMasterpps");
			printfClock(timeMaster);
	}
	if((timeCopy.halfmillis>RTC_FREQ)||(timeCopy.halfmillis<0)){
		printf("timeCopypps:'");
		printfClock(timeCopy);
	}*/
	timeProt.offset=subClock(timeMaster,timeCopy);
	//timeProt.offset.second=0;
	#ifdef MASTERMODE
		updateClock();
	#else
		if(timeProt.offset.sign==true){
			sprintf(messageHMIError,"		error pps: %lus %u	t:%lus",timeProt.offset.second,(unsigned int)(timeProt.offset.halfmillis/2),timeMaster.second);
		}
		else{
			sprintf(messageHMIError,"		error pps: -%lus %u	t:%lus",timeProt.offset.second,(unsigned int)(timeProt.offset.halfmillis/2),timeMaster.second);
		}
		sendHMI(messageHMIError);
	#endif
	Enable_global_interrupt();
}
Exemple #15
0
void main(void) 
{
    // initialise the RTC communication line
    I2C_Init() ;
    // initialise the LCD display
    LCDInit(LS_NONE);
    // initialise the actual RTC
    DS1307_Init() ;

    //clear the display
    LCDClear();
    
    // read back our running time
    DS1307_readRam(&runningMinutes,0,2) ;
    DS1307_readRam(&runningHours,2,2) ;
    
    LCDWriteString("Starting counter");
    LCDGotoXY(0,1) ;
    LCDWriteString("at ");
    LCDWriteInt(runningHours,1);
    LCDWriteString(":");
    LCDWriteInt(runningMinutes,1);
    __delay_ms(750) ;
    __delay_ms(750) ;
    __delay_ms(750) ;
    __delay_ms(750) ;

    // compensate for our handling on the first run of the readClock routine
    runningMinutes-- ;

    LCDClear();
    // infinite loop, read the clock, display on the LCD, check for the button, and if needed, process the menus
   while(1)
  {       
      readClock();
      
      showClock() ;           
      
      readInputs() ;
      
      if (bButton)
        doMenu();
      else if (state & 0x30)
      {
          // rotate the encoder to select a menu function, well, that was the aim, but encoder reading
          // seems a tad slow or inaccurate.
          //
          // there is a timeout if the button isn't pressed after selecting the menu option
          
          if (state & DIR_CW)
          {
              menuFunction++ ;
              if (menuFunction == MENU_LAST)
                  menuFunction = MENU_NONE ;
          }
          else if (state & DIR_CCW)
          {
              menuFunction-- ;
              if (menuFunction < MENU_NONE)
                  menuFunction = MENU_LAST-1 ;
          }
          
          if (menuFunction != MENU_NONE)
          {
              next_menu_clear = minute * 60 + seconds + 20 ;
              LCDGotoXY(9,0);
              if (menuFunction == MENU_TIME)
                LCDWriteString("Time ?");
              if (menuFunction == MENU_DATE)
                LCDWriteString("Date ?");
          }
          else
              clearPrompt();
      }
      else if (next_menu_clear && ((minute * 60 + seconds) > next_menu_clear))
      {
          clearPrompt();
      }
  }
}
	if(xTaskGetTickCount()-saveDRequest>(TIMEDELAYREQUEST/portTICK_RATE_MS)){
		saveDRequest=xTaskGetTickCount();
		delayRequest();
	}
	#endif

}
void printfClock(Clock clock){
			if(clock.sign==true){
				hmi.printf("%lu s,%u\r\n",clock.second,(unsigned int)clock.halfmillis);
			}
			else{
				hmi.printf("-%lu s,%u\r\n",clock.second,(unsigned int)clock.halfmillis);
			}
}
void updateClock(void){
		Clock diff;
		if(timeProt.correction.previousSignOffset==timeProt.offset.sign){
			timeProt.correction.previousSignOffset=timeProt.offset.sign;
			timeProt.correction.previoustimeOffset.second=timeProt.correction.currentTimeOffsetSync.second;
			timeProt.correction.previoustimeOffset.halfmillis=timeProt.correction.currentTimeOffsetSync.halfmillis;
			readClock(&timeProt.correction.currentTimeOffsetSync);
			if(timeProt.correction.previoustimeOffset.second!=0){
				if(sup(timeProt.correction.currentTimeOffsetSync,timeProt.correction.previoustimeOffset)){
												
					diff=subClock(timeProt.correction.currentTimeOffsetSync,timeProt.correction.previoustimeOffset);
					//printf("diff: ");
					//printfClock(diff);
					timeProt.correction.sumTime=sumClock(timeProt.correction.sumTime,diff);
					sumOffset=sumClock(sumOffset,timeProt.offset);
					/*printf("sumOffset: ");
					printfClock(sumOffset);
					printf("sumClock: ");
					printfClock(timeProt.correction.sumTime);
					
					printf("nb corrr  : %d",timeProt.correction.nbCorrection);
					*/
					timeProt.correction.nbCorrection++;
				}
				else{
					resetSofftwareCorrection();
				}
			}
		}
		else{
			resetSofftwareCorrection();
		}
		timeProt.correction.previousSignOffset=timeProt.offset.sign;
		Clock timeCopy;
		readClock(&timeCopy);


		
		timeCopy=sumClock(timeCopy,timeProt.offset);//add offset
		writeClock(timeCopy);
		uint8_t state;
		state=stateLed;
		report(timeProt.offset);
		if(timeManage.halfmillis<(RTC_FREQ/2)){//all 500ms
			stateLed=LOW;
		}
		else{
			stateLed=HIGH;