STM8S Timer1 input capture issue

Thread Starter

Radio Ratiometrics

Joined Mar 7, 2016
2
Hello am working with the timer1 input capture of an STM8S103F3. No input is registered by the input capture module though I fed it with square waves from 10Hz all the way up to 1MHz. The same square wave registers properly in the input capture module of a pic micro. I have seen that the PC6 (TIM1 Channnel1) pin is set to TIM1 channel1 as an alternate function but I have not found anything helpful in the STM8S standard development library. I have pasted my code below in case anyone can help thanks:


#include "stm8s.h"



void main()
{
CLK_DeInit();
CLK_HSICmd(ENABLE);
CLK_LSICmd(DISABLE);
CLK_HSECmd(DISABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);






GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_IN_PU_NO_IT);

TIM1_DeInit();
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1 , ENABLE);

TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING,TIM1_ICSELECTION_DIRECTTI,TIM1_ICPSC_DIV1, 0x00);
TIM1_SelectInputTrigger(TIM1_TS_TI1FP1);
TIM1_SelectSlaveMode(TIM1_SLAVEMODE_RESET);

TIM1_ITConfig(TIM1_IT_CC1,ENABLE);
TIM1_GenerateEvent(TIM1_EVENTSOURCE_CC1);
TIM1_ClearFlag(TIM1_FLAG_CC1);
TIM1_UpdateRequestConfig(TIM1_UPDATESOURCE_REGULAR);
TIM1_Cmd(ENABLE);


enableInterrupts();


while (1);


}


/***********Contents of stm8s_it.c*********************/

uint16_t Capture;
uint8_t Buf[10];

INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
if(TIM1_GetITStatus(TIM1_IT_CC1)==SET)
{
Capture=TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_IT_CC1);
}
}
 

Papabravo

Joined Feb 24, 2006
21,159
Clearly there is a disconnect between the hardware and the library. Do you have the source code for the library? You might have to go spelunking to find the answer. That is you might have to dive into the library.

https://en.wikipedia.org/wiki/Caving

I see that the program has no scaffolding for looking at results. What is your evidence that things are not working?
 

Thread Starter

Radio Ratiometrics

Joined Mar 7, 2016
2
Hi papabravo thanks a lot for the reply. I will share a last test I did recently I used the generate event function in my code to check if the interrupt was working and it did so properly. to me the issue seems to come from the input pin(PC6) that could not be properly setup to the input capture module. I browsed the net and even in the STM8S code library they don't set the input pin at all like I did above GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_IN_PU_NO_IT); but even without that I still don't register any input capture. I have tried to check the STM8S standard peripheral library help to find an alternate input mapping function but no result. In case you have any suggestions its welcome thanks
 

tiger762

Joined Dec 18, 2007
1
This is an old thread but this may be of use to someone else. I just went through the exact same thing, i.e. using an alternate function. In this case, using timer 1 channel 1 on pin C6 on an stm8s103. It is insufficient to just write out 0x01 to the address of OPT2 (0x004803). One must first unlock the flash memory, then enable it for writing. Assuming you are using sdcc, there is a much easier way from the Linux command line:

echo -ne '\x00\x00\xff\x01\xfe\x00\xff\x00\xff\x00\xff' > opt.bin
stm8flash -c stlinkv2 -pstm8s103?3 -s opt -w opt.bin

Now upload your IHX file as usual and C6 will now have timer1's channel 1

To do this from within your program, there are several steps involved:

#define FLASH_DUKR *(unsigned char*)0x005064
#define FLASH_IAPSR *(unsigned char*)0x00505F
#define FLASH_CR2 *(unsigned char*)0x00505B
#define FLASH_NCR2 *(unsigned char*)0x00505C
#define OPT2 *(unsigned char*)0x004803
#define NOPT2 *(unsigned char*)0x004804
#define FLASH_RASS_KEY1 ((uint8_t)0x56) /*!< First RASS key */
#define FLASH_RASS_KEY2 ((uint8_t)0xAE) /*!< Second RASS key */

FLASH_DUKR = FLASH_DUKR_KEY1;
FLASH_DUKR = FLASH_DUKR_KEY2;
while (!(FLASH_IAPSR & 0x08));

FLASH_CR2 |= 0x80;
FLASH_NCR2 &= 0x7f;

OPT2 = 0x01; // AFR0
NOPT2 = 0xfe; // AFR0

I prefer the command-line way. I spent a whole day friguring this out. Hope it helps someone else. -KEF
 

karchie

Joined Sep 7, 2020
1
Hello all, I was struggling with this couple of days, I just wanted to connect STM8S103F3 with ultrasonic sensor HC-SR04. Checked all possible problems... And I finally figured it out. It was super easy, but it could help someone. If you are using ST Visual Programmer, just set the option bit AFR0 to alternate function Port C6 = TIM1_CH1.
 
Top