Tobonaut
Tobonaut

Reputation: 2485

Sending commands via I2C to PIC16F1503 using Windows 10 IoT Core

Scenario

I want to communicate with a PIC16F1503 controller using C# while running as an UWP app on a Raspberry Pi using Windows 10 IoT Core.

It seems that my source detects the controller using I2cDevice.FromIdAsync(...). But if I try to send the "Servo1" command 0x01 via:

pic16f1503.Write(new byte[] { COMMANDO_SERVO_1 });
pic16f1503.Write(data.ToArray()); 

Nothing happens. I (hopefully) enabled the "Servo1" with:

private void WriteConfiguration()
{
    // Create configuration mask.
    byte config = 0;

    // Enable servo 1
    config |= 1;

    // Enable servo 2
    config |= 0 << 1;

    // Enable lights
     config |= 0 << 2;

     // Light mode
     config |= 0 << 3;

    // Light on
    config |= 0 << 4;

    // Write configuration to device.
    pic16f1503.Write(new byte[] { COMMAND_CONFIG});
    pic16f1503.Write(new byte[] { config });
}

Full source

GitHub Gist

Does somebody know further tutorials or new "entry" points to get along with C# und this mc? Thanks!

Upvotes: 1

Views: 330

Answers (1)

Rita Han
Rita Han

Reputation: 9710

You send several bytes from Raspberry Pi to PIC16F1503 via I2C and I get them via debugging your code: 1, 1, 1, 1-43-7-0-0, 1.

But they seems not all match what you want to send actually because I found a problem in your WriteByte function like:

(You are trying to change a value-type variable: byte[] data. Refer to "Passing Parameters (C# Programming Guide)")

private void WriteByte(byte command, byte[] data)
{
    data.ToList().Insert(0, command);
    data.ToArray();
    pic16f1503.Write(data);
}

I edit above function to this:

private void WriteByte(byte command, byte[] data)
{
    var sendData = data.ToList();
    sendData.Insert(0, command);
    pic16f1503.Write(sendData.ToArray());
}

Then send datas: 0-1, 0-1, 1, 1-43-7-0-0, 0-1

Little issue when print the int array.

Edit this line: Debug.WriteLine($"PIC16F1503 - SetDegrees to {ms} start.\nData: {data}");

To this in order to print the right format data:

        Debug.WriteLine($"PIC16F1503 - SetDegrees to {ms} start.\n");
        Debug.WriteLine("Data: ");
        foreach (var element in data)
        {
            Debug.WriteLine(element);
        }
        Debug.WriteLine("\n");

After that you will send right data to PIC16F1503. If still not working please check two devices I2C speed match or not. And hardware pins connection correct or not.

Upvotes: 2

Related Questions