attiny261 PWM

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

Hi Everyone, 

I am trying to create a 38KHz freq on PB3.
I have used similar code before on other avr's using timer0 and it is working.

I want to use PB3 on an attiny261.

The output is there (15.6 KHz) but it does not change regardless of the OCR1B setting.
Can someone tell me what I am doing wrong here?

 

.nolist
.include "tn261def.inc"
.list

.org 0
	rjmp	Start

Start:

	ldi	temp,LOW(RAMEND)
	out	SPL,temp		; 
	
	sbi	DDRB,3			;PB3 = output

	ldi	temp, (1 << COM1B0)	; PWM ON OC1B (PB3)
	out	TCCR1A, temp	

	ldi   temp,(1 << CS10)
	out   TCCR1B,temp       	; no prescaling

	ldi	temp, 0x2F	
	out	OCR1B, temp

stop:
	rjmp	stop

 

This topic has a solution.
Last Edited: Mon. Dec 9, 2019 - 04:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I haven't looked at your code, but from your description, it sounds like your using the wrong mode for T1, it has many more modes then does T0, but you should be able to find one that matches your needs. 

Tell us what mode you used for T0 that worked, and a freak can guide you to a working mode for T1.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

Thanks Jim,

 

I am trying to use Mode 1 (toggle OC1B on Compare match)

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

newbie123 wrote:

	ldi	temp, 0x2F

How do you calculate the 2F value here? 

 

Naively, if you are after 38KHz and getting 15.6kHz then the period is wrong in the ratio of 38/15.6 which is 2.44. So try dividing 0x2F by 2.44 and you get 0x13 (roughly in integer maths). So try that. (except that because of integer inaccuracy it's likely to really be 38.58kHz in fact).

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

The timer1 in a 261 looks to be quite different to the timer1 in most other AVRs. When you say

newbie123 wrote:
have used similar code before on other avr's using timer0 and it is working
exactly which model of AVR are you porting from?

 

EDIT: Oh I see, these are the AVRs with the PLLs. Do you know if the PLL is involved in timer clocking by default in these "odd" AVRs ?

 

Last Edited: Mon. Dec 9, 2019 - 02:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I used it before with a 3.6864 MHz crystal, tested it with a scope and it is correct.

 

It does not matter what the value is here , it never changes.( I checked with a scope)

 

so there has to be something I am missing.

(maybe it's the cough medicine, I am home today with a cold)

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

this is the code I use for an atmega 328PB @ 3.6864 Mhz clock:

;WGM01 = CTC mode 2
;Clear Timer on Compare Match Mode
;the counter is cleared to ZERO when the
;counter value (TCNT0) matches the OCR0A
;
	ldi	temp, (1 << COM0A0) | (1 << WGM01)
	out	TCCR0A, temp	; PWM ON

	ldi   temp,(1 << CS00)
	out   TCCR0B,temp       ; no prescaling

	ldi	temp, 0x2F
	out	OCR0A, temp	;output on PD6
    	ret

stop:
	rjmp stop

The only thing I am trying to do differently is to use OC1B (PB3) on an attiny261.

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

But the timer1 in 328PB is nothing like the timer1 in attiny261. What makes you think you can just use the same code ??

 

Have you compared the datasheets? (the only way to port code between dissimilar micros)

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

clawson wrote:
 Do you know if the PLL is involved in timer clocking by default in these "odd" AVRs ?

 

No it is NOT, I checked the fuses and they are ok. Just as a test I toggled PB3 and measure it, got exactly 38.4KHz so the timing/clocking is correct.

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

clawson wrote:

But the timer1 in 328PB is nothing like the timer1 in attiny261. What makes you think you can just use the same code ??

 

Have you compared the datasheets? (the only way to port code between dissimilar micros)

 

the code is different in the two posts.

 

Yes I checked the datasheets. Quite frankly the attiny261 datasheet gives me a headache.

 

I figure it is a simple mistake somewhere someone might just spot it.

Last Edited: Mon. Dec 9, 2019 - 03:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well the code wizard in Imagecraft C gives the following setup for 38kHz output on ocr1b

//TIMER1 initialize - prescale:1
// WGM: 12) &CTC, TOP=ICRn
// desired value: 38KHz
// actual value: 38.462KHz (1.2%)
void timer1_init(void)
{
TCCR1A=0x00;
TCCR1B=0x01;
TCCR1C=0x00;
TCCR1D=0x0C;
TCCR1E=0x00;
TCNT1=0x00;
TC1H=0x00;
OCR1A=0x00;
OCR1B=0x00;
OCR1C=0x80;
OCR1D=0x00;
TIMSK=0x00;
TIFR=0x00;
DT1=0x00;
}

 

Compare that to what you have and see if it helps.

The above assumed your where using the default 1MHz clock, if not let me know and I'll try again.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Mon. Dec 9, 2019 - 03:50 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks, I already figured it out.

 

here is the working code:

	ldi	temp, (1 << COM1B0)	; PWM ON OC1B (PB3)
	out	TCCR1A, temp	

	ldi   temp,(1 << CS10)
	out   TCCR1B,temp       ; no prescaling

	ldi	temp, 0x67		;38.462 KHz @ 8MHz

;this was the problem!
	out	OCR1C, temp		;carrier frequency
  	ret

 

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


Oh, you mean like it says in the datasheet?

 

But I also note the comment about "Output compare in normal mode is not recommended"

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

Well I still don't understand it fully. How do you determine which pin the output is on?

IS it

 

	ldi	temp, (1 << COM1B0)	; PWM ON OC1B (PB3)
	OR
	ldi	temp, (1 << COM1A0)	; PWM ON OC1A (PB1)

And what is the significance of OCR1A, OCR1B, OCR1C, OCR1D?

Different modes require different registers I presume.

 

Anyways, I'll keep reading the long datasheet until it puts me to sleep.

 

Thanks guys for your help, I really appreciate it!

 

Last Edited: Mon. Dec 9, 2019 - 04:29 PM