简体   繁体   English

如何改变PWM AVR的占空比(装配 - 无C)

[英]How to change duty cycle on PWM AVR (assembly - no C)

I want to change my PWM duty cycle from 20% to say 70%. 我想将PWM占空比从20%改为70%。 Although while I change my OCR1AL value. 虽然我改变了我的OCR1AL值。 The duty cycle remains the same. 占空比保持不变。 Why is that? 这是为什么? Should I change something else? 我应该改变别的吗? As far as I understand while OCR1A shows the top value, it is the only parameter (register) that I can use in order to change the duty cycle. 据我所知,当OCR1A显示最高值时,它是我可以用来改变占空比的唯一参数(寄存器)。

.include "m16def.inc"
.cseg

PWM:
    ;input
    ldi r16, 0x00
    out DDRA, r16

    ;output
    ldi r16, 0xff
    out DDRD, r16
    out PORTD, r16

    ldi r20, 0xc1
    out TCCR1A, r20
    ldi r20, 0x01


    out TCCR1B, r20
    ldi r20, 0x00
    out OCR1AH, r20
    ldi r20, 0x33
    out OCR1AL , r20

    in r22, TIFR
    andi r22, 0b00000100
    out PORTD, r22

SW0:

in r23, PINA
cpi r23, 0b11111110
brne SW0

ldi r16, 0x82
in r17, OCR1AL
ADD r16,r17
out OCR1AL, r16
jmp SW0

ldi r16, 0x00
out PORTD, r16

I am using AVR ATmega16 at 4MHz 我在4MHz使用AVR ATmega16

Your problem is related to the timer setup. 您的问题与计时器设置有关。 You should check it first. 你应该先检查一下。 If you want to use a 16 bit timer you should set your timer settings with these config: Type: Fast PWM top 0xFFFF 如果要使用16位定时器,则应使用以下配置设置定时器设置:输入:Fast PWM top 0xFFFF

You can find out how to configure your timer in your microcontroller datasheet. 您可以在微控制器数据表中找到如何配置定时器的方法。

After this configuration if you change the top 8 bits the duty cycle will change well. 在此配置之后,如果更改前8位,占空比将会很好地改变。

I think your setup is for a 8 bit timer so you have to change OCR1AL from 0 to 255 to change duty cycle from 0 to 100 percent. 我认为您的设置是针对8位定时器,因此您必须将OCR1AL从0更改为255以将占空比从0更改为100%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM