Tiny2313 Timer1 mode settings

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

What am I doing wrong?

TCCR1A 10000010
bits 7,6: Clear OC1A/OC1B on Compare Match, set OC1A/OC1B at TOP
bits 1,0: WGM11,WGM10
TCCR1B 00011001
bits 4,3: WGM13,WGM12 ;WGM mode 14 selected
bits 2,1,0: system clock, no prescaler
TCCR1C 00000000
no timer interrupts used.
ICR1 0x0500 (TOP)

The timer is expected to count from 0x0000 to 0x0500 then clear to 0x0000 and count up again and so on.
OCR1A is set to 0x005D. The output pin is supposed to go LOW when the counter reaches the OCR1A value and HIGH when it reaches the TOP.

Prototype not working. :(

Simulation with AVRStudio4 4.14 build 589 is showing
Compare output mode 1A 0x02 (correct)
Compare output mode 1B 0x00 (correct; not using B)
Pulse width modulator select 0x02 (correct?; WGM11,WGM10)
Waveform generation mode 0x03 (correct?; WGM13,WGM12)

However, it is counting UP till it reaches 0x01FF, then it starts counting DOWN. This seems to correspond to WGM mode 02 instead of the selected WGM mode 14. :?: :(

[EDIT] Made description clearer ('down' replaced with 'clear'). :oops:

If you think education is expensive, try ignorance.

Last Edited: Wed. Sep 3, 2008 - 03:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Simulation with AVRStudio4 4.14 build 589

Don't trust the simulator for any complex timer activity. It may be worth building the code (or this part) for one of the AVRs that is supported in Simulator V2 as that is far more accurate.

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

Thanks, but I mentioned that my prototype isn't working either. In fact, I seldom use the simulator; I only do it to try to figure out what is wrong with my code after something doesn't work as expected in the target board.

If you think education is expensive, try ignorance.

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

I see that the mode selection bits WGM13,12,11,10 are split over two 8 bit registers. When are these bits read by the counter logic, ie. when do these bits take effect? Does the order in which these two registers are written affect the performance/functioning?

If you think education is expensive, try ignorance.

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

"Not working" isn't a lot to go on.. Can you elaborate?

Quote:

When are these bits read by the counter logic, ie. when do these bits take effect?

Immediately, AFAIK. If you are fearing that you might enter an unwanted WGM mode for the short period of time between writing to the two registers then stop the timer while modifying them (ie. set the timer prescaler to the stopped / not clocked alternative).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

I have a 2313 on the table (uh .. the turntable), and put the registervalues you posted in.
Mode14 is Fast PWM-mode, so the counter does not go up tp ICR1 value, and then counts down to zero, but it counts to ICR1-value, and clears.

From what you describe in your OP: I see it doing what I expevt it to do. But that doesn't help huh ?

Quote:
The timer is expected to count from 0x0000 to 0x0500 then back to 0x0000 and so on.
That "back to zero" .... is that counting down to zero, or a clear ?

Nard

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

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

Additional info:

My Xtal=13.5 MHz
Freq on OC1A = 10.545 kHz, which corresponds with 0x0500 as top
Pos pulse is 7 us, (scope reading), and that corresponds with 0x5D

Attachment(s): 

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

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

Quote:

That "back to zero" .... is that counting down to zero, or a clear ?

That is a clear. :oops:

And yes, that waveform is what I expect. Instead I'm getting a very short pulse - 250 nS (that is 5 clock cycles at 20MHz) at about twice the expected frequency.

If you think education is expensive, try ignorance.

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

BTW. what does your AVRStudio show if you try to simulate it?

If you think education is expensive, try ignorance.

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

Hmm. Is my scope-picture (and the numbers I gave) what you are looking for ?
My program in Bascom:

' GA209 Philips Motor Controller  -- for emuler ---

' Motor_ctrl_01.bas



$regfile = Attiny2313.dat
$baud = 9600
$crystal = 13500000                                         ' 13.5 MHz Xtal

'$PROG &HFF,&HFF,&H99,&H00' generated. Take care that the chip supports all fuse bytes.


Tccr1a = &B10000010
Tccr1b = &B00011001
Tccr1c = &B00000000
Capture1 = &H0500                                           'Icr1 0x0500(top)
Pwm1a = &H005D                                              'OCR1A is set to 0x005D

Set Ddrb.3


Do
Loop

Edit: I don't use the simulator a lot, although it's very well supported by Bascom. Instead I use a scope ;)

Edit2: pls post your program. Assembly ?

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

Last Edited: Wed. Sep 3, 2008 - 03:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If you are fearing that you might enter an unwanted WGM mode for the short period of time between writing to the two registers then stop the timer while modifying them (ie. set the timer prescaler to the stopped / not clocked alternative).

Sadly, that involves using the very register I want to avoid messing with - TCCR1B. LOL (There's a hole in my bucket, dear Liza. )

If you think education is expensive, try ignorance.

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

Do yu access the 16 bit registers as they should be read and written ?

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

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

Plons wrote:
Do yu access the 16 bit registers as they should be read and written ?

Yes, sure, no problem there. But the data sheet says the TCCR1x registers can be addressed independently, so that is what I am doing with those.

Let me play around a bit with my code.

ldi r17,0b10000010 ;mode fast pwm
out TCCR1A,r17 
ldi r17,0b00011000 ;timer1 stopped
out TCCR1B,r17
ldi r17,0b00000000
out TCCR1C,r17
out TIMSK,r17 ;all timer interrupts disabled
out TCNT1H,r17 ;clear timer1
out TCNT1L,r17
ldi r17,0b00000000 ;93 compare value A
out OCR1AH,r17
ldi r17,0b01011101
out OCR1AL,r17
ldi r17,0b00000101 ;1280 TOP
out ICR1H,r17
ldi r17,0b00000000 ;
out ICR1L,r17
ldi r17,0b11101111
out TIFR,r17 ;clear all timer flags
ldi r17,0b00011001
out TCCR1B,r17 ;start timer1

If you think education is expensive, try ignorance.

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

OK. Finally got it working, but a whole week of my life is gone. :( My project was way behind schedule even before this fiasco; now it is close to being scrapped altogether as the deadline is long gone. :(

This can best be described as a clusterf##k.

This code works:

ldi r17,0b00000000
out TIMSK,r17 ;all timer interrupts disabled
out TCNT1H,r17 ;clear timer1
out TCNT1L,r17
ldi r17,0b00000000 ;93 compare value A
out OCR1AH,r17
ldi r17,0b01011101
out OCR1AL,r17
ldi r17,0b00000101 ;1280 TOP
out ICR1H,r17
ldi r17,0b00000000 ;
out ICR1L,r17
ldi r17,0b11101111
out TIFR,r17 ;clear all timer flags
ldi r17,0b10000010 ;mode fast pwm, non inverting 
out TCCR1A,r17 
ldi r17,0b00011001
out TCCR1B,r17 ;start timer1
ldi r17,0b00000000
out TCCR1C,r17

What I deduce from this is that the timer1 mode settings are loaded into the tn2313 after TCCR1C is written. Can someone with a bit of time (a bit more than I have :P ) confirm this please? Otherwise I don't know what to think.

To add to the mess, the simulator in AVRStudio4 sucks, at least as far as timer1 is concerned. This was pointed out by clawson, and I confirmed it by making a dummy project with just the RESET vector, the stack initialization, and the timer1 code.

Is there some caching in AVRStudio4 + AVRISP mkII that I don't know about? I was doing shotgun debugging - i.e. making changes to my code and programming the AVR and seeing if it changed anything. When several, increasingly drastic changes to the code produced no change in functioning of the target board, I decided to monitor a port pin that I had set up earlier as a trace (the code in the ISR toggles the state of this pin). Sure enough, the pin was toggling. I changed the code to disable the ISR completely and built and loaded that into the AVR; still the pin kept on toggling. Meanwhile AVRStudio kept telling me that the programming and verification were successful. WTF???

Finally I loaded and burned another (unrelated) hex file to the AVR, then went back and burned the correct one, and then it started showing my changes again, just like it used to. (AVRStudio 4.14 build 589 running on XP SP3, all updates).

:(

If you think education is expensive, try ignorance.

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

It's time to think of other reasons for your troubles with this simple piece of ASM.
1. I tried your latest code using Bascom inline assembler: works. But not the very first time: the power to the 2313-board was too low .... (yes, I am embarressed :oops: ), I could program it fine, but it didn't run. Brownout was @ 4.3V.
1a. Then put it in AVRStudio like this:

; For emuler

;******************************************************************************
; start of code
;******************************************************************************

	.nolist
	.include "tn2313def.inc"
	.list

	.cseg

	.org 0
		rjmp	RESET






;******************************************************************************
; code
;******************************************************************************


RESET:

ldi xl,0b11111000
out ddrb,xl

ldi r17,0b00000000
out TIMSK,r17 ;all timer interrupts disabled
out TCNT1H,r17 ;clear timer1
out TCNT1L,r17
ldi r17,0b00000000 ;93 compare value A
out OCR1AH,r17
ldi r17,0b01011101
out OCR1AL,r17
ldi r17,0b00000101 ;1280 TOP
out ICR1H,r17
ldi r17,0b00000000 ;
out ICR1L,r17
ldi r17,0b11101111
out TIFR,r17 ;clear all timer flags
ldi r17,0b10000010 ;mode fast pwm, non inverting
out TCCR1A,r17
ldi r17,0b00011001
out TCCR1B,r17 ;start timer1
ldi r17,0b00000000
out TCCR1C,r17 

and_stay:
rjmp and_stay

2. Do you have you ddrb.3 set ?
3. Did you imclude the correct tn2313def.inc ?
4. Have you some kind of rjmp-loop at the end of the program ?

Nard

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

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

I said 'this code works' before the code that you ran. What I don't understand is why the code I posted earlier doesn't work. Can you try running that please? The code from the post just before my previous post.

The major difference is that TCCR1C was not written after TCCR1A and TCCR1B. (out of sequence).

Thanks for taking the trouble to check this out. :)

And yes, yes and yes to questions 2, 3, and 4.

If you think education is expensive, try ignorance.

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

It runs fine over here. Studio4.14 build 584.
Programmed hex with Bascom as tool. Reason for me to use that: I know it inside out, and it does a good job.

; For emuler

;******************************************************************************
; start of code
;******************************************************************************

	.nolist
	.include "tn2313def.inc"
	.list

	.cseg

	.org 0
		rjmp	RESET

;******************************************************************************
; code
;******************************************************************************

RESET:

ldi xl,0b11111000
out ddrb,xl

ldi r17,0b10000010 ;mode fast pwm
out TCCR1A,r17
ldi r17,0b00011000 ;timer1 stopped
out TCCR1B,r17
ldi r17,0b00000000
out TCCR1C,r17
out TIMSK,r17 ;all timer interrupts disabled
out TCNT1H,r17 ;clear timer1
out TCNT1L,r17
ldi r17,0b00000000 ;93 compare value A
out OCR1AH,r17
ldi r17,0b01011101
out OCR1AL,r17
ldi r17,0b00000101 ;1280 TOP
out ICR1H,r17
ldi r17,0b00000000 ;
out ICR1L,r17
ldi r17,0b11101111
out TIFR,r17 ;clear all timer flags
ldi r17,0b00011001
out TCCR1B,r17 ;start timer1 

and_stay:
rjmp and_stay

Cut and paste this block in your AVR-Studio and compile ... and program. All I program is the flash memory ... no fancy tricks. Are you sure that the board gets proper power ?

Edit: programmed now via Dragon ISP: same result: pulses on OC1A, same picture as I posted earlier.

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

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

Quote:
It's time to think of other reasons for your troubles with this simple piece of ASM.

I agree with this. TCCR1C has nothing whatsoever to do with the timer modes. It should not affect your timer at all until the timer is running, and then only on the next clock.

Regards,
Steve A.

The Board helps those that help themselves.

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

And because of the time-difference (it must be late in India): here a snapshot of the screen. It may be helpfull.

Attachment(s): 

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips

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

So what am I supposed to think now? :( That AVRStudio suffers from temporary insanity? The stale programming mentioned above took me several hours to figure out. Now I program a marker in the code to toggle a port pin in a certain way so I know that the latest code is running in the AVR.

What are the other factors that could produce the behaviour I mentioned? A short 250ns pulse in lieu of the expected transitions. Take a look at Plons' Foto 8.jpg above. That is what I was expecting (and am now getting). Instead of that I was getting a 250ns positive pulse on the rising edge of the expected waveform and a 250ns positive pulse on the falling edge. The rest of the time the output was low.

What is worrying me that if I can't figure out what caused it, I might face the same situation again and be just as stumped as I was this time. :P

You're right - it is late here in India. Maybe my brain will work a little better in the morning.

If you think education is expensive, try ignorance.

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

I don't think the problem is caused by Studio. Since you're an experienced HW-guy (amongst other things :) ), I had to think of : "At the plumber's home, the tap drips" It's a Dutch proverb ... literally translated. It took me 10 minutes to find out that supplying the recordplayerboard with 5V instead of 10V was the reason I got nothing out of OC1A. But I could program and verify the board OK .....
Attached the zipped project-folder: now you can check your hex against mine etc etc. Also a screencapture of the fuses is included.
You'll find it. There is no magic involved. Just a blind spot ....

Nard

Attachment(s): 

A GIF is worth a thousend words   They are called Rosa, Sylvia, Tessa and Tina, You can find them https://www.linuxmint.com/

Dragon broken ? http://aplomb.nl/TechStuff/Dragon/Dragon.html for how-to-fix tips