esp32计数参考手册 456页

#define BTN_test    0
hw_timer_t *timer=NULL;
volatile SemaphoreHandle_t timerSemaphore;//信号量,用于定时器中完成任务,通知到loop中
portMUX_TYPE timerMux=portMUX_INITIALIZER_UNLOCKED;
volatile uint32_t isrCounter = 0;//计数
volatile uint32_t lastIsrAt = 0;//时间记录

void onTimer00(void){
{
  portENTER_CRITICAL_ISR(&timerMux);//进入临界段,期间内容不许被中断打扰
  isrCounter++;
  lastIsrAt=millis();
  portEXIT_CRITICAL_ISR(&timerMux);//退出临界段
  xSemaphoreGiveFromISR(timerSemaphore,NULL);
}

void zz_test_time(void) 
{
  pinMode(BTN_test,INPUT);
  timerSemaphore=xSemaphoreCreateBinary();//创建二值信号量 
  //每个定时器都以 APB 时钟(缩写 APB_CLK,频率通常为 80 MHz)作为基础时钟,p456
  timer=timerBegin(0,80,true);//参数1是定时器号,一共有4个定时器。参数2是psc:2~65536。参数3是true向上计数模式
  timerAttachInterrupt(timer,&onTimer00,true);
  timerAlarmWrite(timer,1000000,true);

}