Timer ISR

whemming
Posts: 10
Joined: Fri Feb 17, 2017 3:13 am

Timer ISR

Postby whemming » Fri Feb 17, 2017 3:22 am

Are there any examples of timer ISR?

I need to sample the adc a a specified rate (9600 times a sec). I would like to attachinterrupt to an ISR. Here is what I'm doing in the ESP8266. How do I do this in the ESP32?

void timer_init(void) {
timer1_isr_init();
timer1_attachInterrupt(sample_isr);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write(8333); // 9600 samples/s
}

void ICACHE_RAM_ATTR sample_isr() {
...do some stuff
}

Thanks!

William

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Timer ISR

Postby WiFive » Fri Feb 17, 2017 3:53 am

Max ADC sample rate has been quoted at 6khz

whemming
Posts: 10
Joined: Fri Feb 17, 2017 3:13 am

Re: Timer ISR

Postby whemming » Fri Feb 17, 2017 4:29 am

WiFive wrote:Max ADC sample rate has been quoted at 6khz
Where is this documented? Do you have a link? That is a pretty sad rate if true. My whole point of using the ESP32 was so I wouldn't have to use an external ADC, as I currently do on the esp8266.

I would still like to figure out the ISR, as SPI ADC's run 200k samples and cost less than a dollar. But will be surely disappointed if 6k is the true limit.

whemming
Posts: 10
Joined: Fri Feb 17, 2017 3:13 am

Re: Timer ISR

Postby whemming » Fri Feb 17, 2017 12:48 pm

I'm going to close this topic, as I got my answer elsewhere. I will open a new topic on the ADC sample rate discussion.

Here is an example timer sketch, for others

hw_timer_t * timer = NULL;

void onTimer(){
static unsigned int counter = 1;

Serial.print("onTimer ");
Serial.print(counter);
Serial.print(" at ");
Serial.print(millis());
Serial.println(" ms");

if (counter == 10)
endTimer();

counter++;
}

void startTimer() {
timer = timerBegin(0, 80, true); // timer_id = 0; divider=80; countUp = true;
timerAttachInterrupt(timer, &onTimer, true); // edge = true
timerAlarmWrite(timer, 1000000, true); //1000 ms
timerAlarmEnable(timer);
}

void endTimer() {
timerEnd(timer);
timer = NULL;
}

void setup() {
Serial.begin(115200);
startTimer();
}

void loop() {}

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Timer ISR

Postby kolban » Fri Feb 17, 2017 4:00 pm

The maximum sampling frequency of 6000 times a second can be found here ...

https://esp32.com/viewtopic.php?f=2&t=1075

If I may ask, what kind of project needs to sample an analog input at more than 6000 times a second?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

whemming
Posts: 10
Joined: Fri Feb 17, 2017 3:13 am

Re: Timer ISR

Postby whemming » Fri Feb 17, 2017 4:49 pm

kolban wrote:The maximum sampling frequency of 6000 times a second can be found here ...

https://esp32.com/viewtopic.php?f=2&t=1075

If I may ask, what kind of project needs to sample an analog input at more than 6000 times a second?
I'm decoding AFSK1200. I take 8 samples per AFSK bit. I have this working fine with a 8266 and MCP3008 external ADC. I was hoping to eliminate the external ADC, by using the ESP32. However if the ADC rate is 6K, I guess it won't work.

William

Who is online

Users browsing this forum: No registered users and 125 guests