Новый main
This commit is contained in:
60
Source/Drivers/Tick.cpp
Normal file
60
Source/Drivers/Tick.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
#include "Tick.h"
|
||||
#include "stm32g4xx.h"
|
||||
|
||||
static unsigned long Tick = 0;
|
||||
|
||||
extern "C" void SysTick_Handler()
|
||||
{
|
||||
Tick++;
|
||||
}
|
||||
|
||||
void TICK_Init()
|
||||
{
|
||||
SysTick->LOAD = SystemCoreClock / 1000;
|
||||
SysTick->VAL = 0UL;
|
||||
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
|
||||
|
||||
NVIC_SetPriority(SysTick_IRQn, 0);
|
||||
}
|
||||
|
||||
unsigned long TICK_GetCount() // ms
|
||||
{
|
||||
return Tick;
|
||||
}
|
||||
|
||||
float TICK_GetTime() // s
|
||||
{
|
||||
return ((float)Tick)/1000.0f;
|
||||
}
|
||||
|
||||
void TICK_Delay(unsigned long us)
|
||||
{
|
||||
long tick=SysTick->VAL;
|
||||
long wait=us*(SystemCoreClock/1000000);
|
||||
|
||||
long load = SysTick->LOAD;
|
||||
|
||||
long val;
|
||||
|
||||
do
|
||||
{
|
||||
val=SysTick->VAL;
|
||||
|
||||
if(val>tick) val-=load;
|
||||
|
||||
} while(tick-val < wait);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
11
Source/Drivers/Tick.h
Normal file
11
Source/Drivers/Tick.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef TICK_H
|
||||
#define TICK_H
|
||||
|
||||
void TICK_Init();
|
||||
unsigned long TICK_GetCount();
|
||||
float TICK_GetTime();
|
||||
void TICK_Delay(unsigned long us); // 900 us maximum
|
||||
|
||||
#endif
|
||||
69
Source/Drivers/Tim.cpp
Normal file
69
Source/Drivers/Tim.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "Tim.h"
|
||||
#include "stm32g4xx.h"
|
||||
|
||||
static ProcTIM TIM6_Proc1 = 0;
|
||||
static ProcTIM TIM6_Proc2 = 0;
|
||||
static ProcTIM TIM7_Proc = 0;
|
||||
|
||||
extern "C" void TIM6_DAC_IRQHandler()
|
||||
{
|
||||
static int proc=0;
|
||||
TIM6->SR = 0;
|
||||
|
||||
|
||||
if(proc>3)
|
||||
{
|
||||
TIM6_Proc2();
|
||||
proc=0;
|
||||
}
|
||||
else proc++;
|
||||
|
||||
TIM6_Proc1();
|
||||
}
|
||||
|
||||
extern "C" void TIM7_IRQHandler()
|
||||
{
|
||||
TIM7->SR = 0;
|
||||
TIM7_Proc();
|
||||
}
|
||||
|
||||
void TIM6_Init(long Priority, unsigned long Freq, const ProcTIM& Proc1, const ProcTIM& Proc2)
|
||||
{
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN;
|
||||
|
||||
TIM6->CR1 = 0;
|
||||
TIM6->ARR = 1000 - 1;
|
||||
TIM6->PSC = (SystemCoreClock / 1000 / Freq) - 1;
|
||||
TIM6->DIER = TIM_DIER_UIE;
|
||||
|
||||
TIM6_Proc1 = Proc1;
|
||||
TIM6_Proc2 = Proc2;
|
||||
|
||||
NVIC_SetPriority(TIM6_DAC_IRQn, Priority);
|
||||
NVIC_EnableIRQ(TIM6_DAC_IRQn);
|
||||
}
|
||||
|
||||
void TIM7_Init(long Priority, unsigned long Freq, const ProcTIM& Proc)
|
||||
{
|
||||
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM7EN;
|
||||
|
||||
TIM7->CR1 = 0;
|
||||
TIM7->ARR = 1000 - 1;
|
||||
TIM7->PSC = (SystemCoreClock / 1000 / Freq) - 1;
|
||||
TIM7->DIER = TIM_DIER_UIE;
|
||||
|
||||
TIM7_Proc = Proc;
|
||||
|
||||
NVIC_SetPriority(TIM7_IRQn, Priority);
|
||||
NVIC_EnableIRQ(TIM7_IRQn);
|
||||
}
|
||||
|
||||
void TIM6_Enable()
|
||||
{
|
||||
TIM6->CR1 = TIM_CR1_CEN;
|
||||
}
|
||||
|
||||
void TIM7_Enable()
|
||||
{
|
||||
TIM7->CR1 = TIM_CR1_CEN;
|
||||
}
|
||||
14
Source/Drivers/Tim.h
Normal file
14
Source/Drivers/Tim.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef TIM_H
|
||||
#define TIM_H
|
||||
|
||||
typedef void (*ProcTIM)();
|
||||
|
||||
void TIM6_Init(long Priority, unsigned long Freq, const ProcTIM& Proc1, const ProcTIM& Proc2);
|
||||
void TIM7_Init(long Priority, unsigned long Freq, const ProcTIM& Proc);
|
||||
|
||||
void TIM6_Enable();
|
||||
void TIM7_Enable();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user