ESP32学习笔记(六) 定时器

ESP32学习笔记(六) 定时器

目录:
ESP32学习笔记(一) 芯片型号介绍
ESP32学习笔记(二) 开发环境搭建 VSCode+platformio
ESP32学习笔记(三) 硬件资源介绍
ESP32学习笔记(四) 串口通信
ESP32学习笔记(五) 外部中断
ESP32学习笔记(六) 定时器
ESP32学习笔记(七) 复位和时钟

1 定时器组(TIMG)

ESP32 芯片包含两个硬件定时器组。每组有两个通用硬件定时器。它们都是基于 16 位预分频器和 64 位自动重载功能的向上/向下计数器的 64 位通用定时器。

TIMGn_Tx 的 n 代表组别, x 代表定时器编号。

1.1 相关API

hw_timer_t * timerBegin(uint8_t timer, uint16_t divider, bool countUp);			//定时器开启
//参数:
//num : 定时器编号   0-3
//divider:分频数	   16-bit 时钟预分频器,分频系数为 2-65536
//countUp: 是否是累加模式


void timerEnd(hw_timer_t *timer);

void timerSetConfig(hw_timer_t *timer, uint32_t config);
uint32_t timerGetConfig(hw_timer_t *timer);

void timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge);	//将定时器绑到中断上
//参数:
//*timer : 目标定时器 ( 计时器结构体指针 hw_timer_t * )
//void (*fn)(void) : 中断函数入口地址
//中断边沿触发 : 是否跳变沿触发中断 定时器中断触发方式有: 电平触发中断(level type) 边缘触发中断(edge type)


void timerDetachInterrupt(hw_timer_t *timer);

void timerStart(hw_timer_t *timer);
void timerStop(hw_timer_t *timer);
void timerRestart(hw_timer_t *timer);
void timerWrite(hw_timer_t *timer, uint64_t val);
void timerSetDivider(hw_timer_t *timer, uint16_t divider);
void timerSetCountUp(hw_timer_t *timer, bool countUp);
void timerSetAutoReload(hw_timer_t *timer, bool autoreload);

bool timerStarted(hw_timer_t *timer);						//判断定时器是否开启
uint64_t timerRead(hw_timer_t *timer);
uint64_t timerReadMicros(hw_timer_t *timer);
uint64_t timerReadMilis(hw_timer_t *timer);
double timerReadSeconds(hw_timer_t *timer);
uint16_t timerGetDivider(hw_timer_t *timer);
bool timerGetCountUp(hw_timer_t *timer);
bool timerGetAutoReload(hw_timer_t *timer);

void timerAlarmEnable(hw_timer_t *timer);					//定时器报警使能
void timerAlarmDisable(hw_timer_t *timer);
void timerAlarmWrite(hw_timer_t *timer, uint64_t alarm_value, bool autoreload);
//参数:
//*timer : 目标定时器 ( 计时器结构体指针 hw_timer_t * )
//alarm_value : 计数上限值 就是自动重装载值ARR
//autoreload : 是否重装载.


bool timerAlarmEnabled(hw_timer_t *timer);					//判断定时器报警是否开启
uint64_t timerAlarmRead(hw_timer_t *timer);
uint64_t timerAlarmReadMicros(hw_timer_t *timer);
double timerAlarmReadSeconds(hw_timer_t *timer);

1.2 定时器中断流程

hw_timer_t *tim1 = NULL;							//先定义个结构体指针
tim1 = timerBegin(0, 80, true);						//定时器开启 通过时钟那一章可以知道 定时器的时钟信号为APB 所以为80MHz
timerAttachInterrupt(tim1, tim1Interrupt, true);	//将定时器绑到中断上
timerAlarmWrite(tim1, 100000, true);				//设置ARR  以及  是否重装载
timerAlarmEnable(tim1);								//定时器报警使能

esp32-hal-timer.c中有这两句注释

// NOTE: (in IDF 5.0 there wont be need to know groups/numbers 
// timer_init() will list thru all timers and return free timer handle)

2 LED PWM控制器

此外设本质上也是定时器实现的 所以放在这一章

ESP32在Arduino开发环境中,如需使用PWM功能,需要通过LEDC的功能来实现,替代了analogWrite(pin, value) 方法。

ESP32 的 LEDC 总共有16个路通道(0 ~ 15),分为高低速两组,高速通道(0 ~ 7)由80MHz时钟驱动,低速通道(8 ~ 15)由 1MHz 时钟驱动。

2.1 相关API

//channel 0-15 resolution 1-16bits freq limits depend on resolution
uint32_t    ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
//参数
//channel为通道号,取值0 ~ 15;
//freq,设置频率;
//resolution_bits计数位数,取值0 ~ 20(该值决定后面ledcWrite方法中占空比的最大值,如该值写10,则占空比最大可写2^10-1=1023 ;
//通道最终频率 = 时钟频率 / ( 分频系数 * ( 2^计数位数 ) );(分频系数最大为1024)


void        ledcWrite(uint8_t channel, uint32_t duty);
uint32_t    ledcWriteTone(uint8_t channel, uint32_t freq);
uint32_t    ledcWriteNote(uint8_t channel, note_t note, uint8_t octave);
uint32_t    ledcRead(uint8_t channel);
uint32_t    ledcReadFreq(uint8_t channel);
void        ledcAttachPin(uint8_t pin, uint8_t channel);
void        ledcDetachPin(uint8_t pin);
uint32_t    ledcChangeFrequency(uint8_t channel, uint32_t freq, uint8_t resolution_bits);

2.2 PWM产生流程

// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);//将 LEDC 通道绑定到指定 IO 口上以实现输出
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);//指定通道输出一定占空比波形

3 电机控制脉宽调制器 (PWM)

持续更新ing

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
ESP32-S2官方IDE提供了丰富的定时器中断功能,可以通过编写代码来配置和使用定时器,并执行相应的中断操作。 首先,需要在代码中包含<esp_timer.h>头文件,该头文件包含了ESP32-S2定时器的相关函数和常量。 接下来,可以使用esp_timer_create()函数来创建一个定时器。该函数需要传入一个esp_timer_create_args_t类型的参数,该参数包含了定时器的配置参数,如定时周期、中断处理函数等。其中,定时周期单位为微秒。 然后,可以使用esp_timer_start_periodic()函数启动定时器,并开始定期触发中断。该函数需要传入之前创建的定时器实例以及一个周期参数,用于指定定时器每隔多久触发一次中断。 当定时器触发中断时,会自动调用之前设置的中断处理函数。可以通过esp_timer_create_args_t结构体的arg字段来传递额外的参数给中断处理函数,以满足特定需求。 在中断处理函数中,可以执行特定的操作,如修改其他设备状态、发送数据等。需要注意的是,在中断处理函数中尽量避免执行耗时较长的操作,以避免影响定时器的精度和系统的稳定性。 最后,可以使用esp_timer_stop()函数停止定时器的工作,以及使用esp_timer_delete()函数释放定时器的资源。 总之,ESP32-S2官方IDE提供了方便易用的定时器中断功能,可以通过简洁的代码配置和使用定时器,并执行相应的中断操作,满足实际应用的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值