P8X32A Counters (Setting Registers)

Started by Chris Savage, Aug 05, 2024, 09:39 AM

Previous topic - Next topic

Chris Savage

I covered this on my old website, but of course the data is gone and there are still many P1 users who are confused by the way the registers work, specifically, setting the 32-bit registers based on the existing examples which, while clever, confuse novice programmers. Here's what AppNote - AN001 says about the counter registers:

QuoteCounter Registers
Each counter has three distinct registers that control its operation. In each cog these registers are named CTRA, FRQA and PHSA for the first counter; and CTRB, FRQB and PHSB for the second counter.

You cannot view this attachment.

As you can see in Table 1, certain bits in certain fields need to be set and one example code that does this is as follows:

CTRA := (%00100 << 26) + (%000 << 23) + (0 << 9) + 1
While this is clever programming, it isn't always easy for a novice to understand exactly what is happening. I have been asked many times, "Why can't you just write the 32-bit register out as a single 32-bit value. The answer is, "YOU CAN!" There's nothing wrong with specifying the register by saying:

CTRA := %00010000000000000000000000000001

Some point out the possibility of misalignment of the value during editing, causing bugs / errors, but this is easily overcome by doing things in the following manner:

''        MODE DIV        BPIN     APIN
''       33222222222211111111110000000000
''       10987654321098765432109876543210
         --------------------------------
CTRA := %00010000000000000000000000000001

You can also break the fields up to make it easier to see the individual fields without having to rely on the comment bit positions.

''         MODE  DIV          BPIN       APIN
''       - ----- --- -------- ------ --- ------
CTRA := %0_00100_000_00000000_000000_000_000001

With the comment "fields" to act as "placeholder" for each field of bits, it's more difficult to forget a bit or misalign the entire long.

I hope this helps some to understand how you can easily setup the bits properly without risking losing alignment. If you have questions, please reply.

        I'm only responsible for what I say, not what you understand.