Initializing the value in the counter of a Rotary

I’m trying to initialize the rotary counter. Completely stumped on where to even begin. I imported the code for the rotary to my project and have been able to manipulate it and add some functionality, but I cannot set the counter.

Help would be appreciated.


Welcome to the forum. Can you please show what you have so far, the code?

I’ll post what I have, but it’s not much. It might be best for me to explain, so I’ll do that do.

I’m trying to use a rotary and button to set an ip address and display it on a char display. Each time you press the button it will move to the next octet. I would like to preset the rotary to value of that segment of the IP address. I can clear the counter and reset it to 0 or the counter can maintain the internal counter and carry it to the next octet.

In my code I have a thread running that updates the char display when the rotary is turned.

            MyThread = new Thread(() =>
                while (true)
                    cdCounter += 1;
                    byte pos = (byte)(ipStep * 4);
                    char_Display.SetCursor(0, pos);
                    ipaddress[ipStep] = rotaryH1.GetCount();
                    string pad = new String(' ', 3 - ipaddress[ipStep].ToString().Length);
                    char_Display.PrintString(pad + ipaddress[ipStep].ToString());

Inside the button pressed call I advance the position of the cursor and try to reset the value of the counter on the rotary.

            if (ipStep < 3)
                ipStep += 1;
                ipStep = 0;

            bool resetflag = rotaryH1.Reset(100);

I was passing 100 just as a test, it never worked.

Inside RotaryH1_42.cs I placed the following function, if I comment the write command it effectively does nothing and the counter continues to count. If I run it as below the counter resets to 0 each time I click the button.

        public bool Reset(int InitialValue)
            this.Write((byte)Commands.LS7366_CLEAR | (byte)Registers.LS7366_CNTR, );

            return true;

I also tried this, to no avail.

this.Write((byte)Commands.LS7366_WRITE | (byte)Registers.LS7366_CNTR, byte(InitialValue));


@ trueweb - To load a value into the counter, you want to write into register DTR and then load DTR into CNTR. If all you want to do is reset it to 0, the PulseCount driver has a reset function that you could copy.

John, would you mind providing an example of setting DTR then loading it to CNTR?


@ trueweb - Something like the below possibly:

this.Write(Commands.LS7366_WRITE | Commands.LS7366.DTR, 0);
this.Write(Commands.LS7366_LOAD | Commands.LS7366.CNTR);

@ John - I’m happy to say I was on the right track. I did exactly that based on your previous post. Here is what happened.

If I wanted to initialize the counter with 100, I send 100 through my function as “InitialValue” then set DTR like so:

When I loaded the CNTR the result was 25,600. So I did some testing. I loaded 1, 2 & 3.

Results... 256, 512 and 768.

I tried to write an integer of 100 to DTR that created a syntax/type error.

So I'm still stuck. Any ideas?

@ trueweb - We initialize the encoder to two byte mode, so when will need to change those add a write function that sends two bytes at a time. That is possibly why you see those invalid values. is the datasheet for the chip we use on the module. It might help you as well.

Dig up your dead, dig up your dead!!!

So as part of my Hands on Robotic Motor Control with Gadgeteer I have some rotary control to set duty cycles, frequencies etc but usually means there are min and max values so being able to set the counter is handy for say if they rotate past 100% duty cycle, I really want to set the count back to 100 for example. So do we want this capability added to the module?

My added code

        /// <summary>Sets the count on the module.</summary>
        /// <param name="count">What value to set the count too (-32768 to 32767).</param>
        public void SetCount(short count)
            this.WriteCount(Commands.LS7366_WRITE | Commands.LS7366_DTR, count);
            this.Write(Commands.LS7366_LOAD | Commands.LS7366_CNTR);

        private void WriteCount(Commands register, short count)
            byte[] x = BitConverter.GetBytes(count);
            this.write3[0] = (byte)register;
            this.write3[1] = x[1];
            this.write3[2] = x[0];

            this.WriteRead(this.write3, null);

If you change the byte mode then this will blow up badly, but I don’t think anyone would do that.

1 Like