注:1.這篇文章斷斷續(xù)續(xù)寫了很久,畫圖技術也不精,難免錯漏,大家湊合看.有問題可以留言. 2.論壇排版把我的代碼縮進全弄沒了,大家將代碼粘貼到arduino編譯器,然后按ctrl+T重新格式化代碼格式即可看的舒服. 一、什么是PWM PWM 即Pulse Wavelength Modulation 脈寬調制波,通過調整輸出信號占空比,從而達到改 變輸出平均電壓的目的。相信Arduino 的PWM 大家都不陌生,在Arduino Duemilanove 2009 中,有6 個8 位精度PWM 引腳,分別是3, 5, 6, 9, 10, 11 腳。我們可以使用analogWrite()控 制PWM 腳輸出頻率大概在500Hz 的左右的PWM 調制波。分辨率8 位即2 的8 次方等于 256 級精度。但是有時候我們會覺得6 個PWM 引腳不夠用。比如我們做一個10 路燈調光, 就需要有10 個PWM 腳。Arduino Duemilanove 2009 有13 個數(shù)字輸出腳,如果它們都可以 PWM 的話,就能滿足條件了。于是本文介紹用軟件模擬PWM。 二、Arduino 軟件模擬PWM Arduino PWM 調壓原理:PWM 有好幾種方法。而Arduino 因為電源和實現(xiàn)難度限制,一般 使用周期恒定,占空比變化的單極性PWM。 通過調整一個周期里面輸出腳高/低電平的時間比(即是占空比)去獲得給一個用電器不同 的平均功率。 如圖所示,假設PWM 波形周期1ms(即1kHz),分辨率1000 級。那么需要一個信號時間 精度1ms/1000=1us 的信號源,即1MHz。所以說,PWM 的實現(xiàn)難點在于需要使用很高頻的 信號源,才能獲得快速與高精度。下面先由一個簡單的PWM 程序開始: const int PWMPin = 13; int bright = 0; void setup() { pinMode(PWMPin, OUTPUT); } void loop() { if((bright++) == 255) bright = 0; for(int i = 0; i < 255; i++) { if(i < bright) { digitalWrite(PWMPin, HIGH); delayMicroseconds(30); } else { digitalWrite(PWMPin, LOW); delayMicroseconds(30); } } } 這是一個軟件PWM 控制Arduino D13 引腳的例子。只需要一塊Arduino 即可測試此代碼。 程序解析:由for 循環(huán)可以看出,完成一個PWM 周期,共循環(huán)255 次。 假設bright=100 時候,在第0~100 次循環(huán)中,i 等于1 到99 均小于bright,于是輸出PWMPin 高電平; 然后第100 到255 次循環(huán)里面,i 等于100~255 大于bright,于是輸出PWMPin 低電平。無 論輸出高低電平都保持30us。 那么說,如果bright=100 的話,就有100 次循環(huán)是高電平,155 次循環(huán)是低電平。 如果忽略指令執(zhí)行時間的話,這次的PWM 波形占空比為100/255,如果調整bright 的值, 就能改變接在D13 的LED 的亮度。 這里設置了每次for 循環(huán)之后,將bright 加一,并且當bright 加到255 時歸0。所以,我們 看到的最終效果就是LED 慢慢變亮,到頂之后然后突然暗回去重新變亮。 這是最基本的PWM 方法,也應該是大家想的比較多的想法。 然后介紹一個簡單一點的。思維風格完全不同。不過對于驅動一個LED 來說,效果與上面 的程序一樣。 const int PWMPin = 13; int bright = 0; void setup() { pinMode(PWMPin, OUTPUT); } void loop() { digitalWrite(PWMPin, HIGH); delayMicroseconds(bright*30); digitalWrite(PWMPin, LOW); delayMicroseconds((255 - bright)*30); if((bright++) == 255) bright = 0; } 可以看出,這段代碼少了一個For 循環(huán)。它先輸出一個高電平,然后維持(bright*30)us。然 后輸出一個低電平,維持時間((255-bright)*30)us。這樣兩次高低就能完成一個PWM 周期。 分辨率也是255。 三、多引腳PWM Arduino 本身已有PWM 引腳并且運行起來不占CPU 時間,所以軟件模擬一個引腳的PWM 完全沒有實用意義。我們軟件模擬的價值在于:他能將任意的數(shù)字IO 口變成PWM 引腳。 當一片Arduino 要同時控制多個PWM,并且沒有其他重任務的時候,就要用軟件PWM 了。 多引腳PWM 有一種下面的方式: int brights[14] = {0}; //定義14個引腳的初始亮度,可以隨意設置 int StartPWMPin = 0, EndPWMPin = 13; //設置D0~D13為PWM 引腳 int PWMResolution = 255; //設置PWM 占空比分辨率 void setup() { //定義所有IO 端輸出 for(int i = StartPWMPin; i <= EndPWMPin; i++) { pinMode(i, OUTPUT); //隨便定義個初始亮度,便于觀察 brights[ i ] = random(0, 255); } } void loop() { //這for 循環(huán)是為14盞燈做漸亮的。每次Arduino loop()循環(huán), //brights 自增一次。直到brights=255時候,將brights 置零重新計數(shù)。 for(int i = StartPWMPin; i <= EndPWMPin; i++) { if((brights[i]++) == PWMResolution) brights[i] = 0; } for(int i = 0; i <= PWMResolution; i++) //i 是計數(shù)一個PWM 周期 { for(int j = StartPWMPin; j <= EndPWMPin; j++) //每個PWM 周期均遍歷所有引腳 { if(i < brights[j])\ 所以我們要更改PWM 周期的話,我們將精度(代碼里面的變量:PWMResolution)降低就行,比如一般調整LED 亮度的話,我們用64 級精度就行。這樣速度就是2x32x64=4ms。就不會閃了。
上傳時間: 2013-10-23
上傳用戶:mqien
This book introduces embedded systems to C and C++ programmers. Topics include testing memory devices, writing and erasing Flash memory, verifying nonvolatile memory contents, controlling on-chip peripherals, device driver design and implementation, optimizing embedded code for size and speed, and making the most of C++ without a performance penalty. Pages : 336 Slots : 1
標簽: programmers introduces embedded include
上傳時間: 2013-12-10
上傳用戶:shizhanincc
int trace (int i, int j, int low, IMAGE im,IMAGE mag, IMAGE ori) float gauss(float x, float sigma) float dGauss (float x, float sigma) float meanGauss (float x, float sigma) void hysteresis (int high, int low, IMAGE im, IMAGE mag, IMAGE oriim) void canny (float s, IMAGE im, IMAGE mag, IMAGE ori)
上傳時間: 2015-01-30
上傳用戶:杜瑩12345
最新的支持向量機工具箱,有了它會很方便 1. Find time to write a proper list of things to do! 2. Documentation. 3. Support Vector Regression. 4. Automated model selection. REFERENCES ========== [1] V.N. Vapnik, "The Nature of Statistical Learning Theory", Springer-Verlag, New York, ISBN 0-387-94559-8, 1995. [2] J. C. Platt, "Fast training of support vector machines using sequential minimal optimization", in Advances in Kernel Methods - Support Vector Learning, (Eds) B. Scholkopf, C. Burges, and A. J. Smola, MIT Press, Cambridge, Massachusetts, chapter 12, pp 185-208, 1999. [3] T. Joachims, "Estimating the Generalization Performance of a SVM Efficiently", LS-8 Report 25, Universitat Dortmund, Fachbereich Informatik, 1999.
上傳時間: 2013-12-16
上傳用戶:亞亞娟娟123
This directory contains 3 file system modules: - file system ISO9660 iso9660.c iso9660.h - file system FAT12/16 fat.c fat.h - file system FAT32 fat32.c fat32.h file.c and file.h contains all high levels functions/macro/definition for your application. fs_variable.c contains all definitions of variables that can be shared with the all file systems. config.h must contain the definition of the file system used by your application.
標簽: 9660 file system directory
上傳時間: 2015-03-15
上傳用戶:腳趾頭
一個簡單的應用程序,用來說明循環(huán)結構與分支結構的嵌套使用 * 程序功能:猜數(shù)游戲。定義被猜的數(shù)。通過鍵盤輸入進行猜數(shù), * 如猜錯則顯示“**Wrong**”, “Too high”或“Too low ”; * 猜對則顯示“**Right**”后退出
標簽: 應用程序
上傳時間: 2014-01-16
上傳用戶:tuilp1a
Altera recommends the following system configuration: * Pentium II 400 with 512-MB system memory (faster systems give better software performance) * SVGA monitor * CD-ROM drive * One or more of the following I/O ports: - USB port (if using Windows XP or Windows 2000) for USB-Blaster(TM) or MasterBlaster(TM) communications cables, or APU programming unit - Parallel port for ByteBlasterMV(TM) or ByteBlaster(TM) II download cables - Serial port for MasterBlaster communications cable * TCP/IP networking protocol installed * Windows 2000, Windows NT 4.0 with Service Pack 3 or later, or Windows XP * Internet Explorer 5.0 or later Memory & Disk Space Requirements USB開發(fā)
標簽: system configuration recommends following
上傳時間: 2015-03-27
上傳用戶:13188549192
JILRuntime A general purpose, register based virtual machine (VM) that supports object-oriented features, reference counting (auto destruction of data as soon as it is no longer used, no garbage collection), exceptions (handled in C/C++ or virtual machine code) and other debugging features. Objects and functions can be written in virtual machine code, as well as in C or C++, or any other language that can interface to C object code. The VM is written for maximum performance and thus is probably not suitable for embedded systems where a small memory footprint is required. Possible uses of the VM are in game development, scientific research, or to provide a stand-alone, general purpose programming environment.
標簽: object-oriented JILRuntime register supports
上傳時間: 2013-12-23
上傳用戶:cc1015285075
jboss 開發(fā)人員 手冊 JBoss: A Developer s Notebook also introduces the management console, the web services messaging features, enhanced monitoring capabilities, and shows you how to improve performance. At the end of each lab, you ll find a section called "What about..." that anticipates and answers likely follow-up questions, along with a section that points you to articles and other resources if you need more information.
標簽: introduces management Developer the
上傳時間: 2015-04-17
上傳用戶:dreamboy36
Quality, object.oriented architecture is the product of careful study, decision making, and experimentation. At a minimum, the object.oriented architecture process includes farming of requirements, architecture mining, and hands.on experience. Ideally, object.oriented architecture comprises a set of high.quality design decisions that provide benefits throughout the life cycle of the system.
標簽: architecture decision oriented Quality
上傳時間: 2014-10-28
上傳用戶:love_stanford