#include "stm32f4xx.h"
#define SR_CC1IF (1U<<1)
#define SR_CC2IF (1U<<2)
#define SR_CC3IF (1U<<3)
#define SR_CC4IF (1U<<4)
int reg_a;
int reg_b;
int reg_c;
int reg_d;
int x;
int main(void)
{
tim1_all_channels_input_capture();
while(1)
{
while( (TIM1->SR & SR_CC1IF)==1 && (TIM1->SR & SR_CC2IF)==1 &&
(TIM1->SR & SR_CC3IF)==1 && (TIM1->SR & SR_CC4IF)==1 )
{
x=x+1;
reg_a =TIM1->CCR1;
reg_b =TIM1->CCR2;
reg_c =TIM1->CCR3;
reg_d =TIM1->CCR4;
}
}
}
Above is my code. I am doing input capture on TIM1 ch1,2,3,4 (all channels). Second while loop checks if an input capture occurred or not by checking the Status register(SR) bits 1,2,3,4 respectively for ch1,ch2,ch3, and ch4. If 1 that means capture occurred.
When all of these channels have been captured then I enter the while loop and start storing the CCRx value in variables. Whenever I read a value from CCR1 register the SR registers bit 1 has to reset to 0. For CCRx, SR register bit x should reset according to the reference manual by ST. I am using a STM32F401RE.
The problem is when I am reading CCR1(ch1), CCIF1(SR bit 1) is set to '0' but also rest of the SR bits are set to '0' (bit 2,3,4). Even though I have not read from their respective registers.
Is there any way to avoid this?
edit::
#include "stm32f4xx.h"
#include <stdint.h>
#include <stdio.h>
#include "math.h"
#include<stdlib.h>
#define SR_CC1IF (1U<<1)
#define SR_CC2IF (1U<<2)
#define SR_CC3IF (1U<<3)
#define SR_CC4IF (1U<<4)
#define SR_CC_ALL (SR_CC1IF | SR_CC2IF | SR_CC3IF | SR_CC4IF)
int reg_a=0;
int reg_b=0;
int reg_c=0;
int reg_d=0;
void tim2_all_channels_input_capture(void);
int x= 0;
void main(void)
{
tim2_all_channels_input_capture();
while(1)
{
volatile uint32_t sr = TIM1->SR;
if((sr & SR_CC_ALL) == SR_CC_ALL)
{
x=x+1;
reg_a =TIM1->CCR1;
reg_b =TIM1->CCR2;
reg_c =TIM1->CCR3;
reg_d =TIM1->CCR4;
}
}
}


staticto move them off the stack, while keeping them in local scope. – Lundin Oct 31 '23 at 15:48while(1). In a more complex program you would perhaps use interrupt instead, especially if an immediate reaction is required. – Lundin Oct 31 '23 at 15:51floatit should normally be avoided entirely in embedded systems. I think your particular flavour of MCU has FPU on-chip though, so it can be used without getting horrible slow code. As for when to use it, see this: https://electronics.stackexchange.com/a/494924/6102 – Lundin Nov 01 '23 at 07:26