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 '16 at 15:59

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.

| improve this answer | |
  • 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 '16 at 19:33

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.