/* tmr0pwm.c LED om PD6 or PD6 connected to inbuilt LED at PB5 */ // this code sets up counter0 for an 8kHz Fast PWM wave @ 16Mhz Clock // Arduino UNO R3 // AVR_ATmega328P #define F_CPU 16000000UL #include int main(void) { DDRD |= (1 << DDD6); // PD6 (PWM pin OC0A) is now an output DDRB &= ~( 1 << PB5 ); // LED pin as input could be connected to output PD6 OCR0A = 128; // set PWM for 50% duty cycle at pin OC0A // Bits in TCCR0A register: "COM0A1 COM0A0 COM0B1 COM0B0 - - WGM01 WGM00" TCCR0A |= (1 << COM0A1); // set none-inverting mode "10------" TCCR0A |= (1 << WGM01) | (1 << WGM00); // set fast PWM Mode "------11" // Bits in TCCR0B register: "FOC0A FOC0B - - WGM02 CS02 CS01 CS00" TCCR0B |= (1 << CS01); // set prescaler to 8 and starts PWM "-----010" while (1); { // we have a working Fast PWM } } /* *********************************** */ /* FUNCTIONS */ /* *********************************** */ /* *********************************** */ /* HARDWARE */ /* *********************************** */ /* Chip ATMega328 Arduino Uno R3 stackable header _______ Digital: _____/ \__ Analog: ______________ ______________ -|D00 >RXD A5|- | \/ | -|D01 -|D13 LED --- | -(D08)-|14 PB0 PWM/PB1 15|-(D09)- -|GND | |______________________________| -|AREF | -|SCL | -|SDA | |________________| */