I'm writing Arduino UNO (=ATMega328P-PU) programs in assembly to save memory, so I use avra.exe (same as atmel studio's avrasm32) to compile and avrdude to upload, and simple programs like blinking run fine. But now i tried to half-bright a LED with pwm. I checked up my code for errors but I didn't find any, and the LED just full brighs. I checked wiring too. I'm getting struck :c. Here's my pwm.asm code:

.include "m328pdef.inc"

.org 0x00
     rjmp start
.org 0x34
 start: sbi ddrb, 5 ;pin 13
        sbi portb, 5 ;pin 13 on, just to compare with the PWMed led
        sbi ddrd, 5 ;pin 5 pwm
        ldi r16, 0b00100011 ;fast pwm mode, non inverted pwm at oc0b = pin 5 (right?)
        out tccr0a, r16 ;I'm using Timer0
        ldi r16, 0b00000001 ;no prescaler
        out tccr0b, r16
        ldi r16, 128 ; duty cycle = 50%
        out ocr0b, r16

  loop: rjmp loop
  • Try writing the code in C and see if you get any other output using avr-objdump -S <filename>.elf. From my limited knowledge of assembly, the code looks file. What happens if you connect the led the other way around (VCC--resistor--led--pwm-pin vs. pwm-pin--resistor--led--GND)? Out eyes don't perceive different light levels in a linear scale.
    – Gerben
    Jul 7, 2016 at 15:59

2 Answers 2


I tried your program, after converting it to GNU-as syntax. It works as expected, as seen on the scope. Your problem may be simply that the difference between 50% ON and 100% ON is not very obvious to the eye because of its logarithmic perception.

  • Got it! i tried to set the duty cycle to 16 (6.25% duty cycle) and I noticed a difference. I didn't know about its logarithmic perception. Thanks!
    – Frazzo
    Jul 7, 2016 at 19:33

you have to set the PRR timers on

start: lds r16,PRR andi r16,0b10010111 ; PRTIM2 | PRTIM0 | PRTIM1 sts PRR,r16

  • I'm having trouble making sense of this as an answer to the question, which has an accepted answer which mentions nothing about PRR. PRR defaults to 0x00 on reset, which is to say nothing is being shut off to reduce power by default. This is not changed by the bootloader or his code. Why would they need to change PRR?
    – timemage
    Jan 2, 2021 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.