Author Topic: Assembly, Atmega 328 PWM, phase correct mode - out of range problem  (Read 367 times)

0 Members and 1 Guest are viewing this topic.

Offline MikeliniusTopic starter

  • Newbie
  • Posts: 2
  • Country: pl
Hi,
I'm new into assembly and I need to generate in PWM, phase correct mode signal with frequency 255 Hz and FF 55 % but I have problem with Atmega328 - you can see in the screen what is wrong. But what's more - my code works for Atmega32. It looks like I use some kind of bad instruction, but I've no idea what exactly is wrong. Do you have any idea what's wrong?

.include"m328def.inc"

SBI DDRB, 3
LDI R16, 140 // Fill factor 140/255 = 55%
OUT OCR2A, R16

LDI R17, 0b00000001 //PWM PC
out TCCR2A, R17
LDI R18, 0b00000101 //prescaler 128
out TCCR2B, R17

loop:
rjmp loop
« Last Edit: January 20, 2022, 05:58:33 pm by Mikelinius »
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 14263
  • Country: de
Re: Assembly, Atmega 328 PWM, phase correct mode - out of range problem
« Reply #1 on: January 20, 2022, 05:43:13 pm »
The newer Megaxx8 have many of the registers at different locations and some of the timers get out of the range of the OUT instruction. So one would need to use STS instead.
For some reason they had (not sure if fixed) some of the examples in the datasheet also wrong in this aspect.

Some of register names also changes, though to a large part same funtionality as the mega32.
 
The following users thanked this post: Mikelinius

Offline Skashkash

  • Regular Contributor
  • *
  • Posts: 118
  • Country: us
Re: Assembly, Atmega 328 PWM, phase correct mode - out of range problem
« Reply #2 on: January 20, 2022, 05:49:05 pm »
The newer Megaxx8 have many of the registers at different locations and some of the timers get out of the range of the OUT instruction. So one would need to use STS instead.
For some reason they had (not sure if fixed) some of the examples in the datasheet also wrong in this aspect.

Some of register names also changes, though to a large part same funtionality as the mega32.

Was about to say exactly the same.
I've taken to using the following macros so I don't have to keep track of the register address.
(These may not be appropriate for the more modern chips or newer versions of studio.) 


Code: [Select]
;--- in for any port address           
.macro   in_reg                       
.if    @1<0x40                     
    in @0,@1                   
.else                                 
    lds @0,@1                   
.endif                                 
.endm                                 
   
;--- out for any port address         
.macro   out_reg                       
.if @0<0x40                     
out @0,@1                   
.else                                 
    sts @0,@1                   
.endif                                 
.endm               



 
The following users thanked this post: Mikelinius


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf