Mbed OS
PWM
特点:
PWM 频率
PWM信号的周期是不变的,而高电平的宽度是变化的。
PWM 占空比 (Duty circle)
高电平的时间占整个周期的比值。占空比越高,LED就越暗
PWM 的占空比=100%*(高电平时间)/(脉冲周期)。PWM 的占空比决定了输出信号的电压平均值,占空比高,输出的平均值就高。使用一个RC滤波器电路就可以方便地将PWM 信号转换成平均值的电信号。所以PWM是另一种形式的模拟量输出方式。
Mbed OS PWMOut 类
Mbed 提供了PWMOut 类,它内部是使用STM32 中的TIM 产生PWM 信号的。所以并不是所有GPIO脚都可以输出PWM信号的。具体的选择方式要查找modular-2 IO引脚定义表。
构造函数 | |
---|---|
pwmout() | |
成员函数 | |
write (float value) | 写入当前的占空比(Duty circle)。浮点百分比 |
Read() | 读出当前的占空比。浮点百分比 |
period (float seconds) | 设置周期(秒) |
period_ms (int ms) | 设置周期(豪秒) |
period_us (int us) | 设置周期(微秒) |
pulsewidth (float seconds) | 脉冲宽度(秒) |
pulsewidth_ms (int ms) | 脉冲宽度(毫秒) |
作图
上图中 脉宽时间占总周期时间的比例,就是占空比
比方说周期的时间是10ms,脉宽时间是8ms 那么低电平时间就是2ms 总的占空比 8/(8+2)= 80%
这就是占空比为80%的脉冲信号
PWM就是在合适的信号频率下,通过一个周期里改变占空比的方式来改变输出的有效电压
Application
PWM has many useful applications in embedded systems.
The main two categories are:
When a microcontroller does not have a DAC circuit, PWM can be used to modulate different analog values.
Some devices are built to be used with PWM. The most famous example is servo motors.
Controlling the brightness of LED by adjusting the duty cycle
With an RGB (red green blue) LED, you can control how much of each of the three colors you want in the mix of color by dimming them with various amounts.
SPI & I2C
I2C xy (D1, D2);
SPI xys (D1, D2, D3) ;