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.
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.
Thread.Sleep(1000);
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());
Thread.Sleep(1000);
}
});
MyThread.Start();
Inside the button pressed call I advance the position of the cursor and try to reset the value of the counter on the rotary.
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.
@ 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 - 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.
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.