AVR Embedded Tutorial - Analog Write: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: AVR Embedded Tutorial - Analog Write
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # PWM Analog Write | |
| 2 | + | ||
| 3 | + | An Atmega328 is used in this example. | |
| 4 | + | ||
| 5 | + | ## Delay | |
| 6 | + | ||
| 7 | + | A simple delay function used to slowly increase and decrease the brightness of the LEDs. | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | procedure sleep; | |
| 11 | + | var | |
| 12 | + | i : Int16; | |
| 13 | + | begin | |
| 14 | + | for i := 0 to 1999 do | |
| 15 | + | asm | |
| 16 | + | Nop // No operation instruction to waste clock cycles | |
| 17 | + | end; | |
| 18 | + | end; | |
| 19 | + | ``` | |
| 20 | + | ||
| 21 | + | ## Example | |
| 22 | + | ||
| 23 | + | In this example, the PWM frequency is set to be very slow, so that you can see the brightening and darkening of the LEDs. You can see that the analog output is actually modulated. | |
| 24 | + | ||
| 25 | + | The following must be set: | |
| 26 | + | ||
| 27 | + | - With **DDR**x, the PWM output must be set as an output. | |
| 28 | + | - **COM**xx specifies the pins to run in PWM mode. The ATmega328 has 6 pins that can run in this mode. Each timer can operate two outputs. | |
| 29 | + | - **WGM** activates the PWM output of the timer. | |
| 30 | + | - **TCCR0B** is set to a clock of 1024, if you choose a smaller value, you will no longer see the LEDs flashing. | |
| 31 | + | - **OCR0** specifies the **HIGL** length of the modulation. 0 is off, 128 is the mean and 255 is the maximum. | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | var | |
| 35 | + | i : integer; // Loop counter | |
| 36 | + | begin | |
| 37 | + | // Configure PD5 and PD6 as outputs | |
| 38 | + | DDRD := %01100000; | |
| 39 | + | ||
| 40 | + | // Configure PD5 and PD6 in PWM mode | |
| 41 | + | TCCR0A := (%10 shl COM0A) or (%10 shl COM0B) or (1 shl WGM0); | |
| 42 | + | ||
| 43 | + | // Enable timer 0, clock / 1024 | |
| 44 | + | TCCR0B := TCCR0B or %101; | |
| 45 | + | ||
| 46 | + | // Main loop that continuously makes the LEDs brighter and darker | |
| 47 | + | repeat | |
| 48 | + | for i := 0 to 255 do | |
| 49 | + | begin | |
| 50 | + | OCR0A := i; | |
| 51 | + | OCR0B := 255 - i; | |
| 52 | + | sleep; | |
| 53 | + | end; | |
| 54 | + | ||
| 55 | + | for i := 0 to 255 do | |
| 56 | + | begin | |
| 57 | + | OCR0A := 255 - i; | |
| 58 | + | OCR0B := i; | |
| 59 | + | sleep; | |
| 60 | + | end; | |
| 61 | + | until False; | |
| 62 | + | end. | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | ## Pitfalls | |
| 66 | + | ||
| 67 | + | > **Note:** Since PWM is controlled by a timer, there may be overlaps with the timer functions. | |
| 68 | + | ||
| 69 | + | For information on the timer functions, see the [[AVR Embedded Tutorial - Timer, Counter|Timer, Counter]] tutorial. | |
| 70 | + | ||
| 71 | + | # See also | |
| 72 | + | ||
| 73 | + | - [[AVR Embedded Tutorial|AVR Embedded Tutorials]] - Overview | |
| 74 | + | ||
| 75 | + | --- | |
| 76 | + | *From the Free Pascal wiki, page "AVR Embedded Tutorial - Analog Write", as it stood on 2020-10-19 after a change by Mathias, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 77 | + | Category: AVR | |
| 78 | + | Category: Arduino | |
| 79 | + | Category: Embedded | |
| 80 | + | Category: Tutorials |
