Go Down

Topic: How to use system timer in stm32? (Read 163 times) previous topic - next topic

arpitthumar

I am using stm32. I want my led to on for 10sec and off after that. I am comparing my data with previous data if present data is greater than previous data it should make led on for 10sec and off after that. This  is a continuing process. For that i need system timer which checks this condition every time and if the condition is true then it should make led on for 10sec.

fnb111

Post the code you have written using code tags and the members will help you.

arpitthumar

Hi,


Here is my code.In this code i am comparing byte value coming from my censor which starts from "02,01,04,03" so i am comparing this input data with these bytes if this data is getting a match then finding byte number 28  which a total number of object.  Then this byte i has been taken in the variable 'temp' which denotes the previous byte. After that this previous byte which is temp compare with incoming data if the previous and current data value is same then led remains off otherwise it should get on.

Now what i want to do is if the previous byte is not equal to incoming current data then it should on for 10sec it doesn't matter in between 10sec this data is equal, greater or less. For this, i need timer which checks this data process of comparing in the background and if agin this data is not equal then again it should make led on for 10sec.







Code: [Select]




#define LED_PIN 10
int i = 1, j = 0, flag = 0, falg1, flag2;
int decimal = 0, place = 1;
char rBuff[800], number, temp;
unsigned char flag1, count_enable = 1;
int count = 0, res;
unsigned char test, transmit_flag = 0;


void setup()
{

  Serial.begin(921600);

}
void loop()
{

  if (transmit_flag == 0)
  {
    if (Serial.available())
    {
      rBuff[i] = Serial.read();
      //Serial.print(rBuff[i]);
      i++;
      if (i >= 100)
        transmit_flag = 1;
    }

  }

  if (transmit_flag == 1)
  {
    i = 0;
    for (j = 0; j < 100; j++)
    {
      //Serial.print(rBuff[j]);

      if (count_enable == 1)
      {
        count++;
      }
      if ((rBuff[j] == 0x02) && (flag == 0)) // 2  && (flag == 0) && (zero_detect == 1)
      {
        flag = 1;
        count_enable = 1;
       // Serial.print(rBuff[j]);
      }
   

     if ((rBuff[j] == 0x01) && (flag == 1) && (count == 3) ) // 1 3  && (zero_detect == 1) && (count == 3) && (flag == 1)
      {
        flag = 2;
        // Serial.print(rBuff[j]);
      }
     
      if ((rBuff[j] == 0x04) && (flag == 2) && (count == 4) ) // 4  6  && (zero_detect == 1) && (count == 3)
      {
        flag = 3;
       // Serial.print(rBuff[j]);
      }
 

      if (flag == 3)
      {
        count++;

        if (count == 29)
        {
          //Serial.print(count);
          Serial.print(rBuff[count]);
          if (temp==rBuff[count])
          {
            digitalWrite(LED_PIN, LOW);
          }
          else
          {
            temp = rBuff[count];
            digitalWrite(LED_PIN, HIGH);
          }


        }

      }
    }

  }
  transmit_flag = 0;
  flag = 0;
  count = 0;
}

septillion

Doesn't the STM32 Arduino core not simply implement millis()?
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

arpitthumar


What if i want to implement same code with samd21?

Timer code is this


https://gist.github.com/jdneo/43be30d85080b175cb5aed3500d3f989

I want to make this 10sec so that led should be blink when the condition is satisfied and the background process of comparing object value should be continued.

sterretje

What if i want to implement same code with samd21?
How about answering reply #3?

If you use Arduino's abstraction layer and use millis(), you will be independent of the hardware that you're using (as long as there are cores for that hardware).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

arpitthumar

#6
Aug 17, 2019, 08:03 am Last Edit: Aug 17, 2019, 08:05 am by arpitthumar
When i posted a question it was on sam32 but now it has been changed for some reason from sam32 to samd21 so i could not say about the use of millis() in my program.

septillion

Why not? Isn't it you using the Arduino IDE to program the devices? Would be very surprised if they did not implement millis() for those boards in the Arduino HAL.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up