3
votes

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, but the LED just full brighs. I checked wiring too. Here's my pwm.asm code:

.nolist
.include "m328pdef.inc"
.list

.cseg
.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
        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

I tried to change duty cycle value but nothing changes.

1
With avr-gcc I have more or less the same .lss (assembly) and it is working fine. Maybe LED is connected incorrectly or so?KIIV

1 Answers

2
votes

I found myself the solution. The code is correct and the wiring too. Just setting the duty cycle to 50% doesn't make a difference for the human eye. So I tried to put 16 in ocr0b (duty cycle = 6.25%) and then i noticed a real difference. This is because it follows a logarithmic logic, so the difference between a duty cycle of 50% and 100% is actually very small