久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4027|回復(fù): 4
打印 上一主題 下一主題
收起左側(cè)

arduino的修改過的可改pwm頻率的庫函數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

  1. /*
  2. By default Arduino's analogWrite (and consequently pwmWrite() since it mimics analogWrite()) uses 8 bit
  3. pwm across all timers. 8 bit PWM allows for a total of 256 possible values. This library has some methods
  4. for fine tuning resolution if a higher resolution is needed:

  5. void pwmWriteHR(uint8_t pin, uint16_t duty)
  6.   Same a pwmWrite, except the range is 0 - 65535 (16 bit) instead
  7.   of 0 - 255 (8 bit)

  8. float TimerX_GetResolution() (replace X with a timer number)
  9.   Gets the timer's resolution in base 2. The value returned in other words
  10.   represents the number of bits required to represent the timer's range. ie
  11.   the value 7 means it takes 7 bits to represent all possible pin duties at
  12.   that frequency, or 7-bit resolution. Note that a float is returned, not
  13.   an int.

  14. float GetPinResolution(uint8_t pin)
  15.   Returns the same value as TimerX_GetResolution(), but takes a pin number
  16.   as a parameter so that the caller is timer agnostic.

  17. There are several things to keep in mind when trying to optimize resolution:
  18. -pwmWriteHR() is only useful for 16 bit timers, since 8 bit timers are inherently limited to 8 bit pwm
  19. -The higher the frequency, the lower the resolution. pwmWriteHR() will automatically map input to the
  20.   actual timer resolution
  21. -Resolution can be optimized in this way for 2 pins on the Uno (9 and 10), and 11 pins on the Mega (2,
  22. 3, 5, 6, 7, 8, 11, 12, 44, 45,  and 46)
  23.   
  24. Use the serial moniter to see output from this program
  25. This example runs on mega and uno.
  26. */

  27. #include <PWM.h>

  28. //use pin 11 on the mega for this example to work
  29. int led = 9; // the pin that the LED is attached to

  30. void setup()
  31. {
  32.   InitTimersSafe(); //initialize all timers except for 0, to save time keeping functions
  33.   Serial.begin(115200);
  34.   Serial.println();
  35.   
  36.   demonstrateFrequencysEffectOnResolution();
  37.   settingHighResolutionDuty();
  38. }

  39. void demonstrateFrequencysEffectOnResolution()
  40. {
  41.   Serial.println("As frequency increases, resolution will decrease...");
  42.   for(int i = 1; i < 10000; i+=10)
  43.     {
  44.       SetPinFrequency(led, i);  //setting the frequency
  45.       
  46.       uint16_t frequency = Timer1_GetFrequency();
  47.       uint16_t decimalResolution = Timer1_GetTop() + 1;
  48.       uint16_t binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int
  49.       
  50.       char strOut[75];
  51.       sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );
  52.       
  53.       Serial.println(strOut);
  54.     }
  55.    
  56.     Serial.println("...Finished");
  57. }

  58. void settingHighResolutionDuty()
  59. {
  60. SetPinFrequency(led, 10); //setting the frequency to 10 Hz
  61. Serial.println("\r\npwmWrite() and pwmWriteHR() are identical except for the valid range of inputs.\r\nThe following loop calls both functions to produce the same result on the \r\nLED pin. The pin should to run 10Hz at 50% duty regardless of the function called.\r\n");

  62. //the led should flicker (10Hz 50% duty) for 1 second before calling
  63. //the other function. This demonstrates the use of pwmWriteHR() and how its
  64. //use is nearly identical to pwmWrite()
  65. while(true)
  66. {
  67.    //setting the duty to 50% with 8 bit pwm. 128 is 1/2 of 256
  68.    pwmWrite(led, 128);
  69.    Serial.println("8-Bit PWM");
  70.    delay(1000);
  71.    
  72.    //setting the duty to 50% with the highest possible resolution that
  73.    //can be applied to the timer (up to 16 bit). 1/2 of 65536 is 32768.
  74.    pwmWriteHR(led, 32768);
  75.    Serial.println("High Resolution PWM");
  76.    delay(1000);
  77. }

  78. }

  79. void loop()
  80. {
  81. }
復(fù)制代碼
PWM.zip (14.53 KB, 下載次數(shù): 36)
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

5#
ID:138707 發(fā)表于 2023-7-29 18:28 | 只看該作者
樓主給力 我就改不成功,我在試一試
回復(fù)

使用道具 舉報(bào)

地板
ID:830831 發(fā)表于 2022-5-15 09:03 | 只看該作者
樓主給力 我就改不成功
回復(fù)

使用道具 舉報(bào)

板凳
ID:1026624 發(fā)表于 2022-5-15 00:38 | 只看該作者

thank you so much.
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:189412 發(fā)表于 2022-5-9 07:58 | 只看該作者
thank you so much.
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 日韩亚洲在线 | 九九热这里| 国产成人亚洲精品自产在线 | 欧美精品999| 日韩亚洲在线 | www色| 四虎在线免费视频 | 看逼网站 | 中文字幕免费av | 伊人网视频 | av片在线免费观看 | 欧美黄网站 | 日韩天堂在线 | a级片免费在线观看 | 黄色一级录像 | 一级黄色片在线观看 | 麻豆av在线 | 一级黄色片免费 | 日本不卡视频在线观看 | 久久精品一二三 | 一区二区三区在线观看免费 | 精品一区在线播放 | 欧美国产日韩一区 | 国产美女免费 | 日本免费观看视频 | 亚洲午夜在线观看 | 亚洲影院av | 色婷婷综合在线 | 国产九九精品 | 一级片在线观看视频 | 国产黄在线观看 | 国产精品美女毛片真酒店 | 99久久国产视频 | 国产一二 | 日韩欧美精品一区二区 | 成人永久免费视频 | 亚洲视频在线播放 | 91色视频 | 欧美在线视频播放 | 国产丝袜av | 日韩欧美黄色片 |