Raspberry Pi3 mit PiFace Digital 2

  • C#

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Otti.

    Raspberry Pi3 mit PiFace Digital 2

    Hallo liebe Leute

    ​Bevor gefragt wird warum...ja ich habe Win IOT 10 auf meinem Raspberry, da Linux nix für mich ist.
    ​Ich habe im Netz ein Code - Schnipzel gefunden mit welchen ich meine PiFace Digital Karte ansprechen kann.
    ​Da ich nicht der Experte darin bin, wollte ich mal fragen wer mir evt. den Code aufschlüssel kann.
    ​Einzelne Ausgänge kann ich mit BYte ansteuern, wobei diese dann auch dauerhaft geschaltet bleichen. Ein "Ein / Aus" Befehl bekomme ich nicht hin,
    ​genau so wie die Eingänge, wo ich nicht weiß wie ich diese Abfragen soll.

    ​Wenn jemand eine Idee hat, bzw. Erfahrung mit PiFace Di2 in Verbindung mit Pi3, würde ich mich freuen wenn ihr mich dran teilnehmen lasst.

    ​Hier mal der Code Schnipzel, wo bei dieser bestimmt zuverkleineren ist.

    C#-Quellcode

    1. ​namespace PifaceDigital2
    2. {
    3. public class PiFaceEventArgs : EventArgs
    4. {
    5. public byte PinMask { get; set; }
    6. public byte PinState { get; set; }
    7. }
    8. public delegate void PiFaceInputChange(object sender, PiFaceEventArgs e);
    9. public class PiFaceSpiDriver : IDisposable
    10. {
    11. // notes reset on the MCP23S17 is tied to 3.3V
    12. // (is configured as inputs)
    13. // (is configured as outputs)
    14. // leave the IOCON.BANK = 0
    15. private GpioController IoController = null;
    16. private GpioPin InterruptPin = null;
    17. private const string SPI_CONTROLLER_NAME = "SPI0"; /* For Raspberry Pi 2, use SPI0 */
    18. private const Int32 SPI_CHIP_SELECT_LINE = 0; /* Line 0 maps to physical pin number 24 on the Rpi2 */
    19. private const Int32 INTERRUPT_PIN = 25;
    20. private const byte SPI_SLAVE_READ = 1;
    21. private const byte SPI_SLAVE_WRITE = 0;
    22. private const byte SPI_SLAVE_ID = 0x40;
    23. private const byte SPI_SLAVE_ADDR = 0; // 0, 1, 2
    24. private const byte IOCONA = 0xA;
    25. private const byte IODIRA = 0x00;
    26. private const byte IODIRB = 0x01;
    27. private const byte GPPUA = 0x0C;
    28. private const byte GPPUB = 0x0D;
    29. private const byte GPIOA = 0x12;
    30. private const byte GPIOB = 0x13;
    31. private const byte OLATA = 0x14;
    32. private const byte OLATB = 0x15;
    33. private const byte DEFVALB = 0x7;
    34. private const byte INTCONB = 0xB;
    35. private const byte GPINTENB = 0x5;
    36. private const byte INTFB = 0xF;
    37. private const byte INTCAPB = 0x11;
    38. private SpiDevice SpiDev = null;
    39. public event EventHandler<PiFaceEventArgs> PiFaceInterrupt;
    40. public async Task InitHardware()
    41. {
    42. IoController = GpioController.GetDefault();
    43. InterruptPin = IoController.OpenPin(INTERRUPT_PIN);
    44. InterruptPin.DebounceTimeout = new TimeSpan(0, 0, 0, 0, 50);
    45. InterruptPin.Write(GpioPinValue.High);
    46. InterruptPin.SetDriveMode(GpioPinDriveMode.Input);
    47. InterruptPin.ValueChanged += Interrupt;
    48. var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
    49. settings.ClockFrequency = 10000000; // max frequency for MCSP23S17 is 10Mhz
    50. settings.Mode = SpiMode.Mode3; // data read on the rising edge - idle high
    51. string spiAqs = SpiDevice.GetDeviceSelector(SPI_CONTROLLER_NAME);
    52. var devicesInfo = await DeviceInformation.FindAllAsync(spiAqs);
    53. SpiDev = await SpiDevice.FromIdAsync(devicesInfo[0].Id, settings);
    54. await Task.Delay(20);
    55. Write(IOCONA, 0x28); // BANK=0, SEQOP=1, HAEN=1 (Enable Addressing) interrupt not configured
    56. Write(IODIRA, 0x00); // GPIOA As Output
    57. Write(IODIRB, 0xFF); // GPIOB As Input
    58. Write(GPPUB, 0xFF); // configure pull ups
    59. Write(DEFVALB, 0x00); // normally high, only applicable if INTCONB == 0x0xFF
    60. Write(INTCONB, 0x00); // interrupt occurs upon pin change
    61. //Write(INTCONB, 0xFF); // interrupt occurs when pin values become DEFVALB values
    62. Write(GPINTENB, 0xFF); // enable all interrupts
    63. Write(GPIOA, 0x00); // Beim Start werden Alle Ausgänge auf Null/Zurück gesetzt
    64. }
    65. private void Write(byte address, byte data)
    66. {
    67. if (SpiDev != null)
    68. {
    69. byte[] buffer = new byte[3];
    70. buffer[0] = SPI_SLAVE_ID | ((SPI_SLAVE_ADDR << 1) & 0x0E) | SPI_SLAVE_WRITE;
    71. buffer[1] = address;
    72. buffer[2] = data;
    73. SpiDev.Write(buffer);
    74. }
    75. }
    76. private byte Read(byte address)
    77. {
    78. if (SpiDev != null)
    79. {
    80. byte[] buffer = new byte[2];
    81. buffer[0] = SPI_SLAVE_ID | ((SPI_SLAVE_ADDR << 1) & 0x0E) | SPI_SLAVE_READ;
    82. buffer[1] = address;
    83. byte[] read_buffer = new byte[1];
    84. SpiDev.TransferSequential(buffer, read_buffer);
    85. return read_buffer[0];
    86. }
    87. return 0;
    88. }
    89. public void WriteGPA(byte value)
    90. {
    91. Write(GPIOA, value);
    92. }
    93. public byte ReadGPA()
    94. {
    95. return Read(OLATA);
    96. }
    97. public byte ReadGPB()
    98. {
    99. return Read(GPIOB);
    100. }
    101. private void Interrupt(GpioPin sender, GpioPinValueChangedEventArgs args)
    102. {
    103. if (PiFaceInterrupt != null)
    104. {
    105. PiFaceEventArgs pfea = new PiFaceEventArgs();
    106. //pfea.PinMask = Read(INTFB); // find out what caused the interrupt
    107. pfea.PinState = Read(INTCAPB); // read the state of the pins
    108. PiFaceInterrupt(this, pfea);
    109. }
    110. }
    111. #region IDisposable Support
    112. private bool disposedValue = false; // To detect redundant calls
    113. protected virtual void Dispose(bool disposing)
    114. {
    115. if (!disposedValue)
    116. {
    117. if(SpiDev != null)
    118. SpiDev.Dispose();
    119. if(InterruptPin != null)
    120. InterruptPin.Dispose();
    121. disposedValue = true;
    122. }
    123. }
    124. ~PiFaceSpiDriver()
    125. {
    126. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    127. Dispose(false);
    128. }
    129. // This code added to correctly implement the disposable pattern.
    130. public void Dispose()
    131. {
    132. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    133. Dispose(true);
    134. GC.SuppressFinalize(this);
    135. }
    136. #endregion
    137. }
    138. }


    C#-Quellcode

    1. ​{
    2. public sealed partial class MainPage : Page
    3. {
    4. PiFaceSpiDriver spi_driver = new PiFaceSpiDriver();
    5. public MainPage()
    6. {
    7. this.InitializeComponent();
    8. Init();
    9. }
    10. private async void Init()
    11. {
    12. await spi_driver.InitHardware();
    13. }
    14. private void button_Click(object sender, RoutedEventArgs e)
    15. {
    16. spi_driver.WriteGPA(0);
    17. }
    Bilder
    • PiFaceDI2.png

      344,94 kB, 500×416, 219 mal angesehen
    Hallo zusammen....

    ​Habe es nun auf gegeben in C# und bin nun wieder bei Net.
    ​Dort habe ich es nun soweit das ich einen Pin als Ausgang habe....klappt auch.
    ​Wer kann mir denn verraten wie ich einen Eingang definiere? Hat das schon wer ausprobiert?

    Quellcode

    1. ​Imports Windows.Devices.Gpio
    2. Public NotInheritable Class MainPage
    3. Inherits Page
    4. Private gpio
    5. Private pin As GpioPin
    6. Private Const Relais_PIN = 18
    7. Private Const Eingang = 21
    8. Private Sub Ini()
    9. If gpio Is Nothing Then
    10. gpio = GpioController.GetDefault
    11. End If
    12. End Sub
    13. Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
    14. Call Relais()
    15. End Sub
    16. Private Sub Relais()
    17. Call Ini()
    18. 'Check if LED's pin was already initialied
    19. If pin Is Nothing Then
    20. pin = gpio.OpenPin(Relais_PIN)
    21. pin.Write(GpioPinValue.Low)
    22. pin.SetDriveMode(GpioPinDriveMode.Output)
    23. End If
    24. 'Read pin status and invert state
    25. If pin.Read = GpioPinValue.High Then
    26. pin.Write(GpioPinValue.Low)
    27. Else
    28. pin.Write(GpioPinValue.High)
    29. End If
    30. End Sub
    31. End Class
    Hallo zusammen.

    ​So habe es nun herraus gefunden wie ich meine Pin in Eingänge deklarieren kann und abfragen kann.

    Einen Timer setzen......

    Quellcode

    1. Dim timer As DispatcherTimer = New DispatcherTimer()
    2. AddHandler timer.Tick, AddressOf MainTimer_Tick
    3. timer.Interval = TimeSpan.FromMilliseconds(50)
    4. timer.Start()​


    Und hier abfragen

    Quellcode

    1. ​If pin17.Read = GpioPinValue.High Then
    2. textBox.Text = "1 AN"
    3. pin18.Write(GpioPinValue.Low)
    4. Else
    5. textBox.Text = "1 AUS"
    6. pin18.Write(GpioPinValue.High)
    7. End If