ATTiny24 PWM on 16 bit timer

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi I am new to this forum so if is in the wrong place I am sorry.

I am trying to control two DC motorsfor a small combat robot and the motor drivers I am using requires the PWM on four pins depending on direction so I need 4 PWM outputs.

I have no problem with the 8 bit counter it all works as expected but I can't seem to get any output from 16 bit counter which I am trying to run in 8 bit mode.

Unfortunately being a retired hardware man I am programming in assembler. angry

I have put the relevant parts of the code below in the hope that someone can tell me where I am going wrong.

Test:

;temp is r16

;temp2 is r17

;set_port_A:

ldi temp,0B11111111 ;port_A_set

out DDRA,temp ;set port A as out

;set_port_B:

ldi temp,0B11111111 ;port_B_set ;0B11111111

out DDRB,temp ;set port D as PD7-0 as out

 

;set_timer_0: 8bit pwm counter

ldi temp,0B11110011

out TCCR0A,temp

ldi temp,0B00000001

out TCCR0B,temp

ldi temp,0

;set_timer_1:16bit pwm counter 8 bit mode

ldi temp,0B11110001

sts TCCR1A,temp

ldi temp,0B00000001

sts TCCR1B,temp

 

;output some values to generate waveforms

 

ldi temp,0x40

out OCR0A,temp

ldi temp,0x60

out OCR0B,temp

ldi temp2,0x0

ldi temp,0x08

sts OCR1AH,temp2

sts OCR1AL,temp

ldi temp2,0x0

ldi temp,0x0A

sts OCR1BH,temp2

sts OCR1BL,temp

ret

This topic has a solution.
Last Edited: Wed. Aug 30, 2017 - 08:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see a RET there with no CALL and apparently no infinite loop? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First, find the Code tags on the toolbar; they look like <>  That preserves code formatting, and makes it easier to read.

 

Ideally, post the smallest complete test program that demonstrates your symptoms.

http://www.avrfreaks.net/comment...

 

Post a complete test program that demonstrates the symptoms.  Tell AVR model, and clock speed.  Tell language, toolchain, version, and optimization settings.

 

Tell what you expect to happen, and tell what >>is<< happening.

 

[I should make that my signature.  I've typed it enough times.]

In this case, the test results:   Tell what you expect to happen, and tell what >>is<< happening.

 

What pins are you looking at?  How are you looking at them?

 

PeterW wrote:
;set_timer_1:16bit pwm counter 8 bit mode ldi temp,0B11110001 sts TCCR1A,temp ldi temp,0B00000001 sts TCCR1B,temp

The "magic numbers" force us to go to the datasheet and decode.  I guess that would force you kicking and screaming to use C-style bit expressions.

 

PeterW wrote:
Unfortunately being a retired hardware man I am programming in assembler.

I hear you.  Since I am an old farm boy, I can only drive tractors, and don't know what an Interstate highway is.  I've heard there are things like traffic signals, but no need to learn about stuff like that.

 

From the pinout, and desiring two channels of PWM off of Timer1, I'd guess you want OC1B/PA5 and OC1A/PA6.  Those pins are indeed made outputs. 

 

You have made 8 bits of port A and 8 bits of port B as outputs.  A cute trick on a model that has 11 or 12 I/O in total.  Indeed for your app you want ALL output pins?!?

 

For some reason, you have decided to use STS on the timer1 registers, using the address value (e.g. OCR1AH) that is the address to be used with IN/OUT.  [Yes, you can use STS and the memory-mapped I/O address.  But then you have to add 0x20.  And/or use the LOAD/STORE macros from app note 001.  Or, as all I/O is in reach on this model, just use OUT.]

 

What timer mode do you really want?  If mode 5, 8-bit Fast PWM, then you need to set WGM2.  Or are you really aiming at mode 1?

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First of all thanks for the quick replies.

Clawson

The code I put up was just a subroutine containing all the various set up code so the call and infinite loop weren't shown.

Theusch

You solved the problem first time it was the <sts> in place of the <out> that was the problem.

I copied the setup code from one of my previous programs for a different AVR processor where the 16 bit timer registers were located in extended memory  so you couldn't access them via I/O instructions and I just assumed this one was the same.

I have set it to mode PWM, Phase Correct, 8 bit and now it gives the same output as the 8 bit timer for the same input value which is what I wanted to achieve.

My original controller had drivers that only required 1 PWM signal and two directional outputs so two channels could be run from the 8 bit counter.

When I first changed to the new drivers I just switched the PWM outputs with a couple of nand gates but that seemed a bit gash so I am redoing it a little more elegantly.

Here is a picture of the existing controller the small piggyback board is a 2.4 GHz receiver.

Thanks for the help Peter.

Last Edited: Tue. Aug 29, 2017 - 09:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PeterW wrote:
You solved the problem first time it was the in place of the that was the problem. I copied the setup code from one of my previous programs for a different AVR processor where the 16 bit timer registers were located in extended memory so you couldn't access them via I/O instructions and I just assumed this one was the same.
Locate AVR001 and take the LOAD / STORE macros from it. They auto adjust to use IN/OUT or LDS/STS based on the target address of the SFR so you won't make the same mistake again.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PeterW wrote:
Clawson The code I put up was just a subroutine containing all the various set up code so the call and infinite loop weren't shown.

IMO/IME it is still easier for us to help if:

 

theusch wrote:
the Code tags on the toolbar; they look like <> That preserves code formatting, and makes it easier to read.
theusch wrote:
Post a complete test program that demonstrates the symptoms. Tell AVR model, and clock speed. Tell language, toolchain, version, and optimization settings. Tell what you expect to happen, and tell what >>is<< happening.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
IMO/IME it is still easier for us to help if:

 

theusch wrote:
the Code tags on the toolbar; they look like <>

 

Full instructions here: http://www.avrfreaks.net/comment... (also how to post images)