2021-02-17 01:03 PM
I am trying to generate a delay in STM8s103 using Timer peripheral TIM2.
but on equating the values to the Prescaler register(PSCR) and ARR. that is not happening
like I debugged my code step by step and on seeing the values of TIM2 registers, they are not loaded with assigned values. below is oic for it.
I am running my MCU at 2MHZ HSI and then using the PSC value for TIM2 be 1 so that my TImer frequency is 1MHZ and then generating delay of us, ms, and sec by using these functions
#define PB_ODR *(unsigned char*)0x5005
#define PB_DDR *(unsigned char*)0x5007
#define PB_CR1 *(unsigned char*)0x5008
// Unsigned int is 16 bit in STM8.
// So, the maximum possible value is 65536.
unsigned long int dl; // Delay
#include <stm8s.h>
void timer_init(void)
{
TIM2->CR1 |= 1<<7; //ARR buffered
//Enable the update generation so that PSC and ARR can be loaded
TIM2->EGR |= 1<<0;
//Timer prescaler value, PSC[3:0] =1 so Ftim2
TIM2->PSCR = 0x01;
//Timer Auto reload register value
TIM2->ARRH = 0xff;
TIM2->ARRL = 0xff;
TIM2->SR1 &= ~(1<<0);
TIM2->CR1 |= 1<<2; //generate update at Counter overflow/underflow
TIM2->CNTR = 0x00;
CLK->CCOR |= CLK_CCOR_CCOEN | CLK_CCOR_CCOSEL;
}
void delay_us(uint16_t us)
{
uint16_t i = us;
TIM2->CNTR = 0;
TIM2->CR1 |= (1<<0);
while(TIM2->CNTR < i);
TIM2->CR1 &= ~(1<<0);
//TIM2->SR1 &= ~(1<<0); //clear flag
}
void delay_ms(uint16_t ms)
{
uint16_t i;
uint16_t milisec = ms;
for(i=0; i<milisec; i++)
delay_us(1000);
}
void delay_sec(uint16_t secs)
{
uint16_t i;
uint16_t sec =secs;
for(i=0; i<sec; i++)
delay_ms(1000);
}
int main()
{
PB_ODR = 0x00; // Turn all pins of port B to low
PB_DDR |= (1 << 5)|(1<<4); // 0x00100000 PB5 is now output
PB_CR1 |= (1 << 5)|(1<<4); // 0x00100000 PB5 is now pushpull
timer_init();
while(1)
{
PB_ODR ^= (1 << 5)|(1<<4); // Toggle PB5 output
//PB_ODR &= 1<< 4;
delay_sec(1);
//for (dl = 0; dl < 18000; dl++) {}
}
}