Will via plug on 19 Feb 2024 19:08:31 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Arduino Syntax Questions


Casey, 

This is basic C and C++ here. 

1.) `->` is a operator to allow you to set a value from within a struct. Look up the arrow operator here: https://overiq.com/c-programming-101/pointer-to-a-structure-in-c/
2.) The `|` or pipe operator is an OR operator. What you're doing is setting the value of likely a 16 bit register.

Think of the or operator like this: Regardless if the bit is on or off already, I want to turn bit (position) in this register. 

So if I want to turn on bits in position to 9, 5, 4, 1 and one. I can either write 1124 in base ten 0x464... which is just some value. Or to show the bits I can show `2 << 9 | 2 << 5 | 2 << 4 | 2 << 1` which is the same value. 

To make the example more legible, I use use defines so when I or them together, it is more readable. I don't know the exact values above but the header file probably has something like this:

#define GCLK_CLKCTRL_CLKEN 2<<9
#define GCLK_CLKCTRL_GEN_GCLK0 2<<5
#define GCLK_CLKCTRL_ID 2<<1

The idea is that the definitions will be easier to read as it should correspond to which bit in the register is defined. 

-Will C


On Mon, Feb 19, 2024 at 9:39 PM Casey Bralla via plug <plug@lists.phillylinux.org> wrote:

I've been working with some auduino boards lately, and have come across some published programs that contain syntax that I do not understand.  I've checked multiple on-line references, and haven't found a good reference to explain what is going on.  Maybe someone here can help?

Here's are some examples of program statements that I don't understand.  These all have to do with setting internal timer registers in a SAMD21 processor on an Arduino Nano 33 IoT board.

From an interrupt routine:

  • TC5->COUNT16.INTFLAG.bit.MC0 = 1;       //Writing a 1 to INTFLAG.bit.MC0 clears the interrupt so that it will run again
  • GCLK->CLKCTRL.reg = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TC4_TC5)) ;


So my questions:

  1. what does the "->" signify?
  2. What is the "|" character telling me about the assignment statement?

TIA!

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug