Timer example

arkayabt
Posts: 17
Joined: Fri Dec 06, 2019 6:14 am

Timer example

Postby arkayabt » Mon Dec 16, 2019 9:09 am

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.

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Timer example

Postby ESP_Dazz » Mon Dec 16, 2019 9:26 am

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.

arkayabt
Posts: 17
Joined: Fri Dec 06, 2019 6:14 am

Re: Timer example

Postby arkayabt » Mon Dec 16, 2019 10:10 am

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;
      }


ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: Timer example

Postby ESP_Sprite » Mon Dec 16, 2019 10:42 am

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?

arkayabt
Posts: 17
Joined: Fri Dec 06, 2019 6:14 am

Re: Timer example

Postby arkayabt » Mon Dec 16, 2019 12:34 pm

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.

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: Timer example

Postby ESP_Sprite » Mon Dec 16, 2019 12:51 pm

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().

arkayabt
Posts: 17
Joined: Fri Dec 06, 2019 6:14 am

Re: Timer example

Postby arkayabt » Tue Dec 17, 2019 5:41 am

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.

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: Timer example

Postby ESP_Sprite » Tue Dec 17, 2019 7:53 am

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.

arkayabt
Posts: 17
Joined: Fri Dec 06, 2019 6:14 am

Re: Timer example

Postby arkayabt » Thu Dec 19, 2019 5:48 am

I have used micros() the same way as you suggest but I am not getting the same value each time for certain key.

ESP_Sprite
Posts: 9043
Joined: Thu Nov 26, 2015 4:08 am

Re: Timer example

Postby ESP_Sprite » Thu Dec 19, 2019 10:41 am

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.

Who is online

Users browsing this forum: No registered users and 71 guests