Page 1 of 1

Timer example

Posted: Mon Dec 16, 2019 9:09 am
by arkayabt
Is there any simple example code for a single timer of simple start and stop in Arduino other than which are given in its documentation? I just want to start and count when the process gets a start then when the process gets end i want to stop the timer.Kindly post the code if you have.

Re: Timer example

Posted: Mon Dec 16, 2019 9:26 am
by ESP_Dazz
Mod Note: Moved to Arduino sub-forum

@arkayabt
Could you be more specific as to what timer you're talking about, and what you mean by a "process getting a start"? For example, are you expecting to use a software timer or a hardware timer? What order of precision do you need for this timer (e.g. milliseconds or microseconds).

If you simply want to time the duration of a piece of code, you can call xthal_get_ccount() to get the current CPU clock count at the start and end your code, then divide the difference in clock count by your CPU frequency to get the elapsed time.

Re: Timer example

Posted: Mon Dec 16, 2019 10:10 am
by arkayabt
Hi ESP_Dazz,
I want to convert below code to esp32 arduino code using timer to find remote control key value without using any library and rmt. When i press the key of remote timer starts counting when it detects low and high pulses means timer count pulse duration of high and low pulses and store in to an array.

Snippet of code:
Here no_pulse, total_pulse, and time_over value is for enter the loop when key of remote press.

Code: Select all

 while((no_pulse < total_pulse)&& (time_over == 0))
      {
        while((IR_IN == 1) && (time_over == 0) );
        TON_bit = 0;
        TON_bit = 1;
        TMR1 = 0;
        while((IR_IN == 0)&& (time_over ==0));
         irdata[no_pulse++] = (TMR1>>8)&0xff;
         irdata[no_pulse++] = TMR1&0xff;
        TON_bit = 0;
        TON_bit = 1;
        TMR1 = 0;
        while((IR_IN == 1)&& (time_over ==0));
         irdata[no_pulse++] = (TMR1>>8)&0xff;
         irdata[no_pulse++] = TMR1&0xff;
      }


Re: Timer example

Posted: Mon Dec 16, 2019 10:42 am
by ESP_Sprite
I don't understand, where is the problem exactly, are you having issues with the xthal_get_ccount() call my colleague told you about? Also, what is the reason you don't want to use RMT?

Re: Timer example

Posted: Mon Dec 16, 2019 12:34 pm
by arkayabt
Hi ESP_Sprite,
Thanks for the reply. I don't have any knowledge about xthal_get_ccount() and i am not able to find any arduino code using xthal_get_ccount() which i can understand and built code. And as i am new to esp32 so i want to read remote control signal using this way because i want to read any type of remote key value and then store it into an array. As per i read rmt library not all types of remote protocol. For knowledge purpose i want to know that using rmt will i be able to store all types of remote key values and generate the same signal frequency in the blaster circuit? If yes,is there any code in arduino which help me with this? And if possible guide me in reading remote key value using timer only because in future if any new remote protocol may come there is no need to do any modification in this.I want to do in this way because as per my limited knowledge i find this way is the simplest way to read remote control value.

Re: Timer example

Posted: Mon Dec 16, 2019 12:51 pm
by ESP_Sprite
Ah, gotcha. That call indeed is not Arduino-specific, even if you can still use it. Arduino has call that is more specific to it, namely micros().

Re: Timer example

Posted: Tue Dec 17, 2019 5:41 am
by arkayabt
If this is the case then how do i reset micros() between each pulse interval? I was not able to reset micros() when i used it.

Re: Timer example

Posted: Tue Dec 17, 2019 7:53 am
by ESP_Sprite
No need. You store micros() at the start of whatever you're doing, wait for the pulse, then take the difference between the current value of micros() and what you stored at the start, and you get your value.

Re: Timer example

Posted: Thu Dec 19, 2019 5:48 am
by arkayabt
I have used micros() the same way as you suggest but I am not getting the same value each time for certain key.

Re: Timer example

Posted: Thu Dec 19, 2019 10:41 am
by ESP_Sprite
Yeah, there's likely some jitter in the remote control and receiver. Last time I checked the periods could easily be off by 10% or so. It shouldn't matter; most remote control protocols use a huge time difference of 300% or so between transmitted one an zero symbols.