找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1673|回復(fù): 0
收起左側(cè)

ESP32產(chǎn)生PWM的Arduino程序

[復(fù)制鏈接]
ID:1059445 發(fā)表于 2022-12-28 12:41 | 顯示全部樓層 |閱讀模式
const int ledPin = 2;  // PWM生成后實(shí)際輸出引腳
//設(shè)置PWM參數(shù)
const int freq = 4000;//PWM頻率
const int ledChannel = 0;//信號(hào)生成GPIO
const int resolution = 12;//12位分辨率,by Jason. 可設(shè)置1~14bits
void setup(){
  //PWM參數(shù)設(shè)置
  ledcSetup(ledChannel, freq, resolution);
  //將生成信號(hào)通道綁定到輸出通道上
  ledcAttachPin(ledPin, ledChannel);
}
void loop(){
  //逐漸變亮,12位分辨率因而到4096
  for(int dutyCycle = 0; dutyCycle <= 123;dutyCycle++){   
    // changing the LED brightness withPWM
    ledcWrite(ledChannel, dutyCycle);
    delay(20);
  }
  //逐漸變暗
  for(int dutyCycle = 123; dutyCycle >= 0;dutyCycle--){
    // changing the LED brightness with PWM
    ledcWrite(ledChannel,dutyCycle);   
    delay(20);
  }
}
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表