PWM主要用途有兩個

1.調節電壓(也就是用三用電表量測會看到的)(改變工作週期就能調節電壓)

2.輸出指定的方波來做應用(要用示波器才能到)

 

以下是ESP32的PWM設定方式

程式碼:

// the number of the LED pin
const int ledPin = 16;  // PIN16 

// setting PWM properties
const int freq = 40000;//頻率40khz,目前測試到50HZ都正常
const int ledChannel = 0;//通道0,共有0~16
const int resolution = 8;//分辨率8bit

void setup(){
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);//設置 通道,頻率,分辨率
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);//使用pwm的腳位及通道
}
 
void loop(){
  // increase the LED brightness
  ledcWrite(ledChannel, 127);//通道及工作週期50%
}

arrow
arrow
    創作者介紹
    創作者 凶王 的頭像
    凶王

    凶王的部落

    凶王 發表在 痞客邦 留言(0) 人氣()