lehrkraefte:blc:math:formi:led-pwm-breadboard

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
lehrkraefte:blc:math:formi:led-pwm-breadboard [2017/02/08 19:52] – created Ivo Blöchligerlehrkraefte: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, erscheint diese heller oder dunkler, je nachdem, welchen Bruchteil der Zeit die LED angeschaltet ist. Siehe auch https://www.arduino.cc/en/Tutorial/PWM
 +
 +<code c++>
 +  analogWrite(3,25);  // 10% Duty-Cycle (0-255)
 +</code>
 +
 +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);        /* set pins as outputs */
 +    TCCR1A = _BV(COM1A1) | _BV(COM1B1)  /* non-inverting PWM */
 +        | _BV(WGM11);                   /* mode 14: fast PWM, TOP=ICR1 */
 +    TCCR1B = _BV(WGM13) | _BV(WGM12)
 +        | _BV(CS10);                    /* no prescaling */
 +    ICR1 = 0xffff;                      /* TOP counter value */
 +}
 +
 +/* 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;
 +    }
 +}
 +</code>