Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| lehrkraefte:blc:math:formi:led-pwm-breadboard [2017/02/11 13:35] – [PWM: Pulse width modulation] Ivo Blöchliger | lehrkraefte:blc:math:formi:led-pwm-breadboard [2017/02/17 20:14] (current) – [PWM: Pulse width modulation] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{backlinks> | ||
| + | ===== PWM: Pulse width modulation ===== | ||
| + | Analog dimmen ist mühsam, ein- und auschalten ist einfach. Wird die LED ganz schnell ein- und ausgeschaltet, | ||
| + | |||
| + | <code c++> | ||
| + | analogWrite(3, | ||
| + | </ | ||
| + | |||
| + | Die Arduino-PWM haben eine Frequenz von ca. 0.5 oder 1 kHz, je nach Pin. Die Auflösung ist 8 Bit mit Werten zwischen 0 und 255. Gerade für das Dimmen von LED ist diese Auflösung speziell im tiefen Bereich ungenügend. | ||
| + | |||
| + | ==== 16-Bit PWM auf Pins 9 und 10 ==== | ||
| + | <code c++> | ||
| + | /* Configure digital pins 9 and 10 as 16-bit PWM outputs. */ | ||
| + | void setupPWM16() { | ||
| + | DDRB |= _BV(PB1) | _BV(PB2); | ||
| + | TCCR1A = _BV(COM1A1) | _BV(COM1B1) | ||
| + | | _BV(WGM11); | ||
| + | TCCR1B = _BV(WGM13) | _BV(WGM12) | ||
| + | | _BV(CS10); | ||
| + | ICR1 = 0xffff; | ||
| + | } | ||
| + | |||
| + | /* 16-bit version of analogWrite(). Works only on pins 9 and 10. */ | ||
| + | void analogWrite16(uint8_t pin, uint16_t val) | ||
| + | { | ||
| + | switch (pin) { | ||
| + | case 9: OCR1A = val; break; | ||
| + | case 10: OCR1B = val; break; | ||
| + | } | ||
| + | } | ||
| + | </ | ||