细化时钟滴答与微秒的转换,RTL8710AF里面不能整除

This commit is contained in:
大石头X2 2016-12-24 10:23:19 +08:00
parent 614c1e30a4
commit 3e6c3942cf
3 changed files with 26 additions and 19 deletions

View File

@ -15,7 +15,7 @@ public:
uint Seconds; // 全局秒数,系统启动后总秒数。累加 uint Seconds; // 全局秒数,系统启动后总秒数。累加
UInt64 Milliseconds; // 全局毫秒数系统启动后总毫秒1000ms整部分。累加 UInt64 Milliseconds; // 全局毫秒数系统启动后总毫秒1000ms整部分。累加
uint BaseSeconds; // 基准秒数。系统启动时相对于1970年的秒数时间调节加上Seconds得到当前时间Now() uint BaseSeconds; // 基准秒数。系统启动时相对于1970年的秒数时间调节加上Seconds得到当前时间Now()
byte Ticks; // 每微秒的时钟滴答数 //byte Ticks; // 每微秒的时钟滴答数
byte Index; // 定时器 byte Index; // 定时器
#if ! (defined(STM32F0) || defined(GD32F150)) #if ! (defined(STM32F0) || defined(GD32F150))
byte Div; // 分频系数。最大分频64k无法让大于64M主频的芯片分配得到1k时钟 byte Div; // 分频系数。最大分频64k无法让大于64M主频的芯片分配得到1k时钟
@ -37,8 +37,10 @@ public:
void SetTime(UInt64 seconds); // 设置时间 void SetTime(UInt64 seconds); // 设置时间
void Sleep(uint ms, bool* running = nullptr) const; void Sleep(uint ms, bool* running = nullptr) const;
// 微秒级延迟 void Delay(uint us) const; // 微秒级延迟
void Delay(uint us) const;
uint TicksToUs(uint ticks) const;
uint UsToTicks(uint us) const;
}; };
extern const TTime Time; extern const TTime Time;
@ -63,7 +65,7 @@ class TimeCost
{ {
public: public:
UInt64 Start; // 开始时间,毫秒 UInt64 Start; // 开始时间,毫秒
ushort StartTicks; // 开始滴答 uint StartTicks; // 开始滴答
TimeCost(); TimeCost();

View File

@ -22,7 +22,7 @@
TTime::TTime() TTime::TTime()
{ {
Seconds = 0; Seconds = 0;
Ticks = 0; //Ticks = 0;
#if defined(STM32F0) || defined(GD32F150) #if defined(STM32F0) || defined(GD32F150)
Index = 13; Index = 13;
#else #else
@ -111,11 +111,12 @@ void TTime::Delay(uint us) const
// 无需关闭中断,也能实现延迟 // 无需关闭中断,也能实现延迟
UInt64 ms = Current(); UInt64 ms = Current();
uint ticks = CurrentTicks() + us * Ticks; uint ticks = CurrentTicks() + UsToTicks(us);
if(ticks >= (1000 - 1) * Ticks) uint max = UsToTicks(1000 - 1);
if(ticks >= max)
{ {
ms++; ms++;
ticks -= (1000 - 1) * Ticks; ticks -= max;
} }
while(true) while(true)
@ -188,17 +189,17 @@ TimeCost::TimeCost()
// 逝去的时间,微秒 // 逝去的时间,微秒
int TimeCost::Elapsed() int TimeCost::Elapsed()
{ {
short ts = Time.CurrentTicks() - StartTicks; int ts = (int)(Time.CurrentTicks() - StartTicks);
int ms = (int)(Time.Current() - Start); int ms = (int)(Time.Current() - Start);
short us = ts / Time.Ticks; // 有可能滴答部分不是完整的一圈
if(ts < 0 && ms > 0) if(ts > 0) return ms * 1000 + Time.TicksToUs(ts);
{
ms--;
us += 1000;
}
return ms * 1000 + us; // 如果毫秒部分也没有,那么可能是微小错误偏差
if(ms <= 0) return 0;
// 如果滴答是负数,则干脆减去
return ms * 1000 - Time.TicksToUs(-ts);
} }
void TimeCost::Show(cstring format) void TimeCost::Show(cstring format)

View File

@ -13,6 +13,7 @@
#define SYSTICK_ENABLE SysTick_CTRL_ENABLE_Msk // 0 /* Config-Bit to start or stop the SysTick Timer */ #define SYSTICK_ENABLE SysTick_CTRL_ENABLE_Msk // 0 /* Config-Bit to start or stop the SysTick Timer */
static TIM_TypeDef* const g_Timers[] = TIMS; static TIM_TypeDef* const g_Timers[] = TIMS;
static uint gTicks;
void TTime::Init() void TTime::Init()
{ {
@ -23,13 +24,13 @@ void TTime::Init()
// 96M时每秒96M/8=12M个滴答1us=12滴答 // 96M时每秒96M/8=12M个滴答1us=12滴答
// 120M时每秒120M/8=15M个滴答1us=15滴答 // 120M时每秒120M/8=15M个滴答1us=15滴答
// 168M时每秒168M/8=21M个滴答1us=21滴答 // 168M时每秒168M/8=21M个滴答1us=21滴答
Ticks = clk / 1000000; // 每微秒的时钟滴答数 gTicks = clk / 1000000; // 每微秒的时钟滴答数
//uint ticks = SYSTICK_MAXCOUNT; //uint ticks = SYSTICK_MAXCOUNT;
// ticks为每次中断的嘀嗒数也就是重载值 // ticks为每次中断的嘀嗒数也就是重载值
//SysTick_Config(ticks); //SysTick_Config(ticks);
// 上面的函数会打开中断 // 上面的函数会打开中断
uint ticks = Ticks * 1000; // 1000微秒便于跟毫秒叠加 uint ticks = gTicks * 1000; // 1000微秒便于跟毫秒叠加
SysTick->LOAD = ticks - 1; SysTick->LOAD = ticks - 1;
SysTick->VAL = 0; SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk; SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
@ -133,4 +134,7 @@ UInt64 TTime::Current() const
return Milliseconds + cnt; return Milliseconds + cnt;
} }
uint TTime::TicksToUs(uint ticks) const { return !ticks ? 0 : (ticks / gTicks); }
uint TTime::UsToTicks(uint us) const { return !us ? 0 : (us * gTicks); }
#pragma arm section code #pragma arm section code