Timer test

This commit is contained in:
Ea-r-th
2026-03-11 00:31:40 -07:00
parent 43bdee4406
commit 5b0819a300
7 changed files with 134 additions and 36 deletions

View File

@@ -16,6 +16,13 @@ void systick_init(){
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
}
extern "C" void SysTick_Handler() {
ticks++;
}
static uint32_t millis();
void SHAL_delay_us(uint32_t us){
uint32_t ticks = us * (SystemCoreClock / 1000000U);

View File

@@ -40,13 +40,16 @@ void Timer::stop() {
SHAL_clear_bitmask(rcc_reg.reg,rcc_reg.enable_mask);
}
void Timer::setPrescaler(uint16_t presc) {
getTimerRegister(m_key)->PSC = presc;
void Timer::setPrescaler(const uint16_t presc) const {
auto prescalerReg = getTimerPrescalerRegister(m_key);
SHAL_set_bits(prescalerReg.reg,16,presc,0);
}
void Timer::setARR(uint16_t arr) {
getTimerRegister(m_key)->ARR = arr;
}
void Timer::setARR(const uint16_t arr) const {
auto autoReloadReg = getTimerAutoReloadRegister(m_key);
SHAL_set_bits(autoReloadReg.reg,16,arr,0);}
void Timer::enableInterrupt() {
getTimerRegister(m_key)->DIER |= TIM_DIER_UIE;
@@ -62,6 +65,28 @@ void Timer::init(uint16_t prescaler, uint16_t autoReload) {
setARR(autoReload);
}
void Timer::configurePWM(SHAL_Timer_Channel channel, uint16_t prescaler, uint16_t autoReload, uint16_t captureCompareThreshold) {
setPrescaler(prescaler);
setARR(autoReload);
setOutputCompareMode(channel, SHAL_TIM_Output_Compare_Mode::PWMMode1);
enableChannel(channel,SHAL_Timer_Channel_Main_Output_Mode::Polarity_Normal,SHAL_Timer_Channel_Complimentary_Output_Mode::Disabled);
setCaptureCompareValue(channel, captureCompareThreshold);
}
void Timer::configureOneshot(SHAL_Timer_Channel channel, uint16_t prescaler, uint16_t autoReload, uint16_t captureCompareThreshold) {
setPrescaler(prescaler);
setARR(autoReload);
setOutputCompareMode(channel, SHAL_TIM_Output_Compare_Mode::Toggle);
enableChannel(channel,SHAL_Timer_Channel_Main_Output_Mode::Polarity_Normal,SHAL_Timer_Channel_Complimentary_Output_Mode::Disabled);
setCaptureCompareValue(channel, captureCompareThreshold);
}
void Timer::setOutputCompareMode(SHAL_Timer_Channel channel, SHAL_TIM_Output_Compare_Mode outputCompareMode) {
auto channelNum = static_cast<uint8_t>(channel);

View File

@@ -1,6 +1,26 @@
#include <cstdio>
#include "SHAL.h"
#define SER PIN(B10)
#define RCLK PIN(B9)
#define SRCLK PIN(B8)
void scanOutputs(uint8_t num_outputs) {
for (size_t i = 0; i < num_outputs + 1; i++) {
if (i == 0) {
SER.setHigh();
}
else {
SER.setLow();
}
RCLK.setHigh();
SRCLK.setHigh();
RCLK.setLow();
SRCLK.setLow();
}
}
int main() {
SHAL_init();
@@ -9,11 +29,41 @@ int main() {
PIN(A8).setAlternateFunction(GPIO_Alternate_Function::AF2);
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
SER.setPinMode(PinMode::OUTPUT_MODE);
RCLK.setPinMode(PinMode::OUTPUT_MODE);
SRCLK.setPinMode(PinMode::OUTPUT_MODE);
SHAL_TIM1.init(48,100);
SHAL_TIM1.setOutputCompareMode(SHAL_Timer_Channel::CH1,SHAL_TIM_Output_Compare_Mode::PWMMode1);
SHAL_TIM1.enableChannel(SHAL_Timer_Channel::CH1,SHAL_Timer_Channel_Main_Output_Mode::Polarity_Reversed,SHAL_Timer_Channel_Complimentary_Output_Mode::Disabled);
SHAL_TIM1.setCaptureCompareValue(SHAL_Timer_Channel::CH1, 5);
SHAL_TIM1.configurePWM(SHAL_Timer_Channel::CH1, 4800, 100, 20);
SHAL_TIM1.start();
SER.setLow();
RCLK.setLow();
SRCLK.setLow();
uint8_t numDisplays = 6;
uint8_t count = 0;
while (true) {
if (count == 0) {
SER.setLow();
}
else {
SER.setHigh();
}
count++;
RCLK.setHigh();
SRCLK.setHigh();
RCLK.setLow();
SRCLK.setLow();
if (count == numDisplays) {
count = 0;
}
SHAL_delay_us(50);
}
}