Timer: the comp registers as an array

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

I want to make a function like:

void SetServo(uint8_t S, uint16_t P); 

Where I have 4 servos connected to port c, bits 0 through 3.

So, could I, as a C Luddite, do something like:

(((*uint16_t)TCC4.CCA) + 2*S) = P

In order to access CCA, CCB, CCC and CCD by numbers 0 through 3?

Of course, I really don't know how to get a pointer to TCCR.CCA. I could use a switch statement, but that would seem silly.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Quote:

I could use a switch statement, but that would seem silly.
I don't see why, it's the most portable solution. Pointer arithmetic will work but say Atmel brought out a new model where the registers weren't equally spaced this kind of "trick" could bite you and may be a devil to find if you later ported the code. On the whole the obvious solution is often the best solution.

Last Edited: Sat. Aug 3, 2013 - 02:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You probably want CCABUF instead of CCA to avoid glitches. Maybe something like

volatile uint16_t *servo = (uint16_t *) &TCC4.CCABUF;

But I don't know if you get the 16-bit access in correct order.

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

I was just realizing that I want CCABUF.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

You could initialize an array with pointers to the CCxBUF registers ahead of time:

uint16_t *ccbuf_array[4] = {
    &TCC4.CCABUF,
    &TCC4.CCBBUF,
    &TCC4.CCCBUF,
    &TCC4.CCDBUF
};

Then access them like this:

*ccbuf_array[0] = 123;
*ccbuf_array[1] = 234;
*ccbuf_array[2] = 345;
*ccbuf_array[3] = 456;

This would eliminate the porting issue Cliff mentioned.

Gamu The Killer Narwhal
Portland, OR, US
_________________
Atmel Studio 6.2
Windows 8.1 Pro
Xplained boards mostly

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

I like that! I just had switch statements. Now I have an array of pointers.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

It worked, but gave me warnings until I put "volatile"

volatile uint16_t *ccbuf_array[4] = 
{
	&TCC4.CCABUF,
	&TCC4.CCBBUF,
	&TCC4.CCCBUF,
	&TCC4.CCDBUF
};

Now it gives me no warnings and continues to work.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

You know, could use the same idea to select one of multiple timers, should I decide I want more than 4 channels out.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut.