motorcontroldemo_028/Vsrc/V_Global_time.c
2019-07-29 08:18:57 +03:00

218 lines
6.4 KiB
C

/*!
Copyright 2017 ÀÎ "ÍÈÈÝÒ" è ÎÎÎ "ÍÏÔ ÂÅÊÒÎÐ"
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
\file Global_time.c
\brief Ìîäóëü ðàáîòû ñî âðåìåíåì
\author ÎÎÎ "ÍÏÔ Âåêòîð". http://motorcontrol.ru
\version v 1.2 2013_10_16
*/
/** \addtogroup Global_time */
/*@{*/
#ifdef __cplusplus
extern "C" {
#endif
#include <main.h>
//!Èíèöèàëèçàöèß ìîäóëß
//!Åñëè åñòü ÷àñû ðåàëüíîãî âðåìåíè, ñ÷èòûâàåò èç íèõ âðåìß.
//!Âîññòàíàâëèâàåò âðåìß ðàáîòû è âðåìåíè âêëþ÷åííîãî ñîñòîßíèß èç SPI.
//! \memberof TGlobalTime
void GlobalTime_init(TGlobalTime *p)
{
p->read_PowerOn_time(p);
p->read_oper_time(p);
#ifdef USE_ABSOLUTE_TIME_CLOCK
//èíèöèàëèçàöèß àáñîëþòíîãî âðåìåíè
if (p->time_type == 1)
{
//èíèöèàëèçàöèß èç ÷àñîâ ðåàëüíîãî âðåìåíè
p->absolute_time.day=spiclock.day;
p->absolute_time.hour=spiclock.hour;
p->absolute_time.minute=spiclock.minute;
p->absolute_time.second=spiclock.second;
p->absolute_time.year=spiclock.year;
}
#endif
//èíèöèàëèçàöèß îòíîñèòåëüíîãî âðåìåíè
p->relative_time1.ON = 1;//ò.ê. 1-å âðåìß ßâëßåòñß îáßçàòåëüíûì
p->relative_time1.relative_date.hour = (p->PowerOn_time >> 6);
p->relative_time1.relative_date.minute = (p->PowerOn_time & 0x3f);
p->relative_time2.relative_date.hour = (p->operational_time >> 6);
p->relative_time2.relative_date.minute = (p->operational_time & 0x3f);
}
//!Ðàñ÷åò îòíîñèòåëüíîãî âðåìåíè.
//!Âûçûâàåò ôóíêöèè ðàñ÷åòà îòíîñèòåëüíî âðåìåíè è, åñëè åñòü, ñ÷èòûâàåò âðåìß
//!ñ ìîäóëß ÷àñîâ ðåàëüíîãî âåðìåíè â àáñîëþòíîå âðåìß.
//! \memberof TGlobalTime
void GlobalTime_calc(TGlobalTime *p)
{
//ñ÷åò îòíîñèòåëüíûõ âðåìåí
GlobalTime_REL_TIME_calc(&p->relative_time1);
GlobalTime_REL_TIME_calc(&p->relative_time2);
}
//!Ðàñ÷åò îòíîñèòåëüíîãî âðåìåíè.
//!Âûçûâàåò ôóíêöèè ðàñ÷åòà îòíîñèòåëüíî âðåìåíè è, åñëè åñòü, ñ÷èòûâàåò âðåìß
//!ñ ìîäóëß ÷àñîâ ðåàëüíîãî âåðìåíè â àáñîëþòíîå âðåìß.
//! \memberof TGlobalTime
void GlobalTime_ms_calc(TGlobalTime *p) {
//óïðàâëåíèå ñ÷åòîì operational_time
if (sm_ctrl.state != 0) //Åñëè â êàêîì-òî ðåæèìå ðàáîòû
global_time.relative_time2.ON = TRUE;
else
global_time.relative_time2.ON = FALSE;
//Âðåìß çàïèòàííîãî ñîñòîßíèß â ìèíóòàõ
p->PowerOn_time = (global_time.relative_time1.relative_date.hour << 6) + (global_time.relative_time1.relative_date.minute & 0x3f);
//Âðåìß â ðàáîòå, â ìèíóòàõ
p->operational_time = (global_time.relative_time2.relative_date.hour << 6) + (global_time.relative_time2.relative_date.minute & 0x3f);
p->PowerOn_time_min = p->relative_time1.relative_date.hour*60 +p->relative_time1.relative_date.minute;
p->operational_time_min = p->relative_time2.relative_date.hour*60 + p->relative_time2.relative_date.minute;
#ifdef USE_ABSOLUTE_TIME_CLOCK
if (global_time.time_type == 1) {
//âðåìß - èç ÷àñîâ ðåàëüíîãî âðåìåíè
global_time.absolute_time.day = spiclock.day;
global_time.absolute_time.hour = spiclock.hour;
global_time.absolute_time.minute = spiclock.minute;
global_time.absolute_time.second = spiclock.second;
global_time.absolute_time.year = spiclock.year;
}
#endif
}
//!Ðàñ÷åò îòíîñèòåëüíîãî âðåìåíè
//!Ó÷èòûâàß, ÷òî ôóíêöèß âûçûâàåòñß ñ êàêîé-òî îïðåäåëåííîé äèñêðåòèçàöèåé,
//!èíêðåìåíòèðóåò îòíîñèòåëüíîå âðåìß, ïåðåäåííîå åé â ñòðóêòóðå òèïà RELATIVE_TIME
//! \memberof TGlobalTime
void GlobalTime_REL_TIME_calc(RELATIVE_TIME *p) {
if (p->ON == 0)
return;
if (p->tic_isr != 0)
p->tic_isr--;
p->delta_millisecond = 0;
p->delta_second = 0;
if (p->tic_isr == 0) {
//ìèëëèñåêóíäà
p->tic_isr = GLOBAL_TIME_CALC_FREQ;
p->millisecond_counter++;
p->delta_millisecond = 1;
if (p->relative_date.millisecond == 999) {
//ñåêóíäà
p->relative_date.millisecond = 0;
p->second_counter++;
p->delta_second = 1;
if (p->relative_date.second == 59) {
//ìèíóòà
p->relative_date.second = 0;
if (p->relative_date.minute == 59) {
//÷àñ
p->relative_date.minute = 0;
p->relative_date.hour++;
} else
p->relative_date.minute++;
} else
p->relative_date.second++;
} else
p->relative_date.millisecond++;
}
}
//!×èòàåò âðåìß íàðàáîòêè.
//! \memberof TCurPar
void GlobalTime_read_PowerOn_time(TGlobalTime *p) { //÷òåíèå work_time
UserMem.MemStartAddr = GLOBAL_TIME_POWER_ON_TIME_ADDR;
UserMem.MCUStartAddr = (Uint16*) (&(p->PowerOn_time));
UserMem.data_length = 4;
UserMem.read(&UserMem);
}
//!çàïèñûâàåò âðåìß íàðàáîòêè.
//! \memberof TCurPar
void GlobalTime_write_PowerOn_time(TGlobalTime *p) { //çàïèñü work_time
UserMem.MemStartAddr = GLOBAL_TIME_POWER_ON_TIME_ADDR;
UserMem.MCUStartAddr = (Uint16*) (&(p->PowerOn_time));
UserMem.data_length = 4;
UserMem.write(&UserMem);
}
//!×èòàåò âðåìß âêëþ÷åííîãî ñîñîòßíèß.
//! \memberof TCurPar
void GlobalTime_read_oper_time(TGlobalTime *p) { //÷òåíèå operational_time
UserMem.MemStartAddr = GLOBAL_TIME_OPERATIONAL_TIME_ADDR;
UserMem.MCUStartAddr = (Uint16*) (&(p->operational_time));
UserMem.data_length = 4;
UserMem.read(&UserMem);
}
//!çàïèñûâàåò âðåìß âêëþ÷åííîãî ñîñîòßíèß.
//! \memberof TCurPar
void GlobalTime_write_oper_time(TGlobalTime *p) { //çàïèñü operational_time
UserMem.MemStartAddr = GLOBAL_TIME_OPERATIONAL_TIME_ADDR;
UserMem.MCUStartAddr = (Uint16*) (&(p->operational_time));
UserMem.data_length = 4;
UserMem.write(&UserMem);
}
void GlobalTime_slow_calc(TGlobalTime*p) {
//åñëè ñòîèò ôëàã íà ñîõðàíåíèå âðåìåí â SPI èëè ñ ìîìåíò ïðîøëîé çàïèñè ïðîøåë ÷àñ
if ((p->WtiteSPI_flag) || (p->PrevWriteSPIHour != global_time.relative_time1.relative_date.hour)) {
p->write_PowerOn_time(p); //çàïèñü âðåìåíè âêë. ñîñòîßíèß
p->write_oper_time(p); //çàïèñü ðàáî÷åãî âðåìåíè
p->PrevWriteSPIHour = global_time.relative_time1.relative_date.hour;
p->WtiteSPI_flag = 0;
}
}
#ifdef __cplusplus
} // Extern "C"
#endif
/*@}*/