cancel
Showing results for 
Search instead for 
Did you mean: 

simple time scheduling-systick-stm32f407

mohamad_armoon
Associate III
Posted on September 04, 2014 at 11:34

hi every body.

i have a 6 or 7 sensors which all of them are connected my stm32f407 through ADC,Usart and i2c(kind of sensor fusion)

i want to show the data of these sensors in pc through usart.

i did this , but i just get the data of sensors from adc and ... (in while loop) and after that send it from usart.

ok  , some of my sensors have more priority of others , i mean for example accelerometer , i need the data of it in 50 hz but for the barometer in decreases to 10 hz.

i can use systick to have a accurate delay so let's consider i config systick to tick every 1 ms(

SysTick_Config

(

SystemCoreClock

/

1000

)

)

now question is : for doing it , do i have to know the accurate time for

1- getting data from sensor 

2- calculation that is implemented on sensor data

3- usart that want to send data 

and then set the suitable delays between them to for example get accelerometer  data in 10hz

or my procedure is wrong ??
3 REPLIES 3
chen
Associate II
Posted on September 04, 2014 at 13:30

Hi

You are thinking about it in the wrong way.

Make SysTick increment a counter, do not worry about the roll over for now.

You need a function which can read the current SysTick count.

For your time delays - get the current SysTick count and add the appropriate difference value (ie 1000 for 1second if SysTick is 1ms)

In the main loop/processing loop :

for each thing to do - check if the SysTick count is greater than the delay value

if false - move onto next thing

if true - execute the sensor read/usart send/what ever

(This is where you have to worry about the roll over - or maybe not if you are not worried about an occasional glitch)

mohamad_armoon
Associate III
Posted on September 05, 2014 at 00:42

thank you for your response.

as your post seemed more logical than mine, i wrote my code according to your idea. so my code is here :

int main()
{
uint8_t tenHZ = 0;
uint8_t ClockCounter = 0;
while(1)
{
for(int i = 0 ; i <
50
;i++)
{
// for generataing 50hz data
if(
UpCounter
= 
20
*i)
{
/****************************sensor 1 ********************************************/
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor1_x);
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor1_y);
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor1_z);
/**************************sensor 2 ******************************************/
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor2_x);
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor2_y);
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor2_z);
/**********************************sensor 3**********************************/
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor3);
/**********************************sensor 4**********************************/ 
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor4);
tenHZ++;
ClockCounter++;
}
// for generating 10hz data
if(tenHZ%10 == 0)
{
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor5);
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor6);
while( !(USART2->SR & 0x00000040) ); 
USART_SendData(USART2,sensor7);
tenHZ = 0;
ClockCounter++;
}
}
}
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{ 
TimingDelay--;
}
 // 1 sec(1000m) = one cycle
if(UpCounter < 1000)
{
UpCounter++;
}else
{
UpCounter = 0;
USART_SendData(USART2,ClockCounter);
ClockCounter = 0;
}
}

in while(1) in first ''if'' statement if i put ''>'' instead of ''='' (as you mentioned) the else here works but for ''='' it never executed :

if(UpCounter < 1000)
{
UpCounter++;
}else
{
UpCounter = 0;
USART_SendData(USART2,ClockCounter);
ClockCounter = 0;
}

and when i change the amount of ''if ''statement in my main loop to change 50hz to 60 or 40 the the result that is received from usart not accurate and changes in refresh time is not obvious. any help would be grate, thanks
mohamad_armoon
Associate III
Posted on September 05, 2014 at 09:02

and i also didn't understand this:

''

For your time delays - get the current SysTick count and add the appropriate difference value (ie 1000 for 1second if SysTick is 1ms)

''

i also didn't understand that when we use systick it counts the number of ticks, so these ticks can be use for, for example getting data from sensor and when it arrived to 0 an interrupt triggered and my function :  

TimingDelay_Decrement()

will execute ???

but i think it didn't , because counting from 0 to 1000(my upCounter in this function) takes over 8 sec.

from your perspective  where in this code i mistake ??