My Timer 1 doesn't seem to be Timer 1ing.

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

It used to. I saw it! Really, I'm not going nuts. Oh, sorry, I already am nuts.

Let's see the code. I'll copy/paste it here and check it as I go. If I don't see any boneheaded mistake before I'm done, I'll post :shock:

Starting the timer: (scratch is R16)

ldi scratch,0 ; Stop the clock
out tccr1a,scratch
out TCCR1B,scratch
out TCNT1H,scratch ; Clear the count
out TCNT1L,scratch
ldi scratch,0b01000001 ; Rising edge capture, Clock/1
out TCCR1B,scratch ; Timer is running?

Set the comparator:

ldi scratch,(ACI<<1) ; Clear any interrupt flag
out ACSR,scratch
sbi ACSR,ACIE ; Enable comparator interrupt
sbi ACSR,ACIC ; and captures the Timer 1 count

On the comparator interrupt:

ldi scratch,0 ; Stop the timer 1
out TCCR1B,scratch

out ACSR,scratch ; Stop the comparator
cbi PORTB,1 ; Discharge the capacitor

lds xh,tcnt1H ; Would use ICR1H and ICR1L if this worked
lds xl,tcnt1L ; Get the desired speed
OUT PORTA,xh ; Output so we can see if it's working
OUT PORTC,xl

The interrupt is triggering 'cause I can see my little cpapcitor charging and discharging, even varying the charge time as I vary the input voltage, but I always read 0s on PortA and PortC.

Since I still don't see where I've goofed up, I'll hit Submit.

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

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

Hi,

Which AVR is the above code for?

Regards,
Steve

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

There are many things we >>can't<< see from the code fragment. Are global interrupts enabled? Could they have been cleared in some fashion, like another ISR not properly saving SREG? Are you sure the ACOMP ISR is firing? etc.

Lee

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

I think it's because I can't lds from tcnt1h and tcnt1l and have to IN.

It's a 162 processor.

Yup. My interrupts are interrupting 'cause I set a pin high on one interrupt and low on another.

Having trouble knowing when to use IN and OUT or lds and sts.

I'm trying not to just post the whole program and ask, "Hows come this doesn't work?" :shock:

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

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

Hi,

Sorry if I am pointing out something already obvious to you. You can look at the Register Summary in the datasheet. On the left hand edge is an address. If the address of the register of interest is 0x3F or lower then you can use IN and OUT. Otherwise you have to use LDS and STS. It is also easy (at least in my mega169 datasheet) because addresses above 0x3F only have the one address listed while 0x3F and below have the 2 addresses listed.

[edit] In my opinion it would be really nice if Atmel would include 2 register summaries. One sorted by address (as is the current summary) and one sorted alphabetically by Register name. Guess I should send Atmel an email requesting that shouldn't I?

HTH,
Steve

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

You can access TCNT1H with LDS but you can't use the TCNT1H equate. If you look at the register summary pages in the datasheet, you'll notice some have two addresses. For example TCNT1H has 0x2D (0x4D). The 0x2D works for in/out and 0x4D is for ld/lds/st/sts. All registers, including R0 through R31, are accessable with ld/lds/st/sts. Figure 9, the Data Memory Map, shows this.

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

Oh I see... Tricksey, they are! (there's not a smiley for "Gollem.")

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

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

Nobody has yet noticed that I'm reading the count high byte first where I need to read it low byte first. :shock:

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead. 

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

Torby wrote:
Nobody has yet noticed that I'm reading the count high byte first where I need to read it low byte first. :shock:

we left that one for you to find :P

Something is always missed. Someone would have caught it eventually. Even better was the fact that you did.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

And now my A/D made with a timer and a capacitor works very nicely. I seem to have an arithmetic problem where I scale it to my output, but that's a problem for tomorrow! :)

The largest known prime number: 282589933-1

It's easy to stop breaking the 10th commandment! Break the 8th instead.