2025-02-02 12:42 PM
I'm a bit rusty on 8Bit MCU's, more used to STM32 but have to get that STM8 project done.
I have a problem get TIM4 running, it might be I don't see the wood for all that trees.
It gets compiled using SDCC and the header file is slightly adopted for that.
here my code
#include "stm8l15x.h"
#define F_CPU 16000000UL
#define LED0_PIN 4
#define LED1_PIN 1
static inline void delay_ms(uint16_t ms) {
uint32_t i;
for (i = 0; i < ((F_CPU / 16000UL) * ms); i++)
__asm__("nop");
}
void timer_isr(void) __interrupt(25) {
GPIOC->ODR ^= (1 << LED0_PIN);
TIM4->SR1 &= ~(1 << TIM4_SR1_UIF);
}
int main(void) {
__asm__("rim");
CLK->CKDIVR = 0x0;
CLK->PCKENR1 = CLK_PCKENR1_TIM4;
GPIOC->DDR |= (1 << (LED0_PIN)); // configure PB1 as output
GPIOC->CR1 |= (1 << (LED0_PIN)); // push-pull mode
GPIOB->DDR |= (1 << (LED1_PIN)); // configure PB1 as output
GPIOB->CR1 |= (1 << (LED1_PIN)); // push-pull mode
TIM4->PSCR = 0x02;
TIM4->ARR = 0x02;
TIM4->IER |= (1 << TIM4_IER_UIE); // Enable Update Interrupt
TIM4->CR1 |= (1 << TIM4_CR1_CEN); // Enable TIM4
TIM4->SR1 &= ~(1 << TIM4_SR1_UIF);
while (1) {
/* toggle pin every 250ms */
GPIOB->ODR ^= (1 << LED1_PIN);
//GPIOB->ODR ^= (1 << LED1_PIN);
delay_ms(250);
}
}