简体   繁体   English

使用I2c和IoT将字节从Raspberry Pi发送到Arduino以实现PWM

[英]Sending Byte from Raspberry Pi to Arduino With I2c & IoT for PWM

I am very new to the using both I2C and C#/Windows IoT so apologies up front if any of this is a dumb question. 我对同时使用I2C和C#/ Windows IoT感到很陌生,因此如果对此有任何疑问,请提前道歉。 I have a Raspberry Pi 3 master and Arduino slave. 我有一个Raspberry Pi 3主站和Arduino从站。 I am trying to send a value from a slider on my UI form over I2C to the Arduino which I will use to adjust my PWM duty cycle. 我试图通过I2C从UI窗体上的滑块向Arduino发送一个值,该值将用于调整PWM占空比。 There a couple of issues I am having and can't work out if its the Pi, Arduino or both. 我遇到了两个问题,如果是Pi,Arduino或两者都无法解决。

Here is my Arduino Slave code: 这是我的Arduino Slave代码:

#include <Wire.h>
#define MyAddress 0x03

byte ReceivedData;
int pass;

void setup() {
    Wire.begin(MyAddress);
    Wire.onReceive(I2CReceived);
    Serial.begin(9600);
    //Wire.onRequest(I2CRequest);
}

void loop() {

    delay(100);
}

void I2CReceived(int NumberOfBytes)
{
    /* WinIoT have sent data byte; read it */
  byte ReceivedData = Wire.read();
  Serial.println(ReceivedData);
  if (ReceivedData <= 127){
      Serial.println("Equal or under");
      return;
  }else{
      Serial.println("over");
      return;
  }

}

And my Pi Master: 还有我的Pi大师:

using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Core;




using Windows.Devices.Enumeration;

using Windows.Devices.I2c;

using System.Diagnostics;

using System.Threading;

namespace I2COutput
{

    public sealed partial class MainPage : Page

    {


        private I2cDevice TransPump;

        private Timer periodicTimer;

        private const byte pump = 0x03;

        double pos;

        public MainPage()

        {

            InitializeComponent();

            initcomunica();

        }



        private async void initcomunica()

        {


            var pumpset = new I2cConnectionSettings(pump);


            pumpset.BusSpeed = I2cBusSpeed.StandardMode;

            string aqs = I2cDevice.GetDeviceSelector("I2C1");

            var dis = await DeviceInformation.FindAllAsync(aqs);


            TransPump = await I2cDevice.FromIdAsync(dis[0].Id, pumpset);


        }

        private async void SendChange()

        {
            byte[] sendpos;
            try
            {
               sendpos = BitConverter.GetBytes(pos);
                TransPump.Write(sendpos);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }

        }


        private void tempLbl_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
        {

            pos = slider.Value;
            temp2Lbl.Text = pos.ToString();
            Convert.ToInt16(pos);
            SendChange();

            return;

        }
    }
}

The first issue I am having is that my ReceivedData on the Arduino is always 0 not matter what the value of sendpos is on the Pi (yes, it does change when I move the slider). 我遇到的第一个问题是,无论Pi上sendpos的值是sendpos ,我在Arduino上的ReceivedData始终为0(是的,当我移动滑块时,它确实会改变)。

The second issue I am having is the first time the slider is moved I get the output on the Arduino serial but then nothing after. 我遇到的第二个问题是第一次移动滑块时,我在Arduino串行上获得了输出,但之后什么也没有。 If I either reset or reload the Arduino I then get the output of the initial slider change again and nothing after. 如果我重置或重新加载了Arduino,那么我会再次获得初始滑块的输出,此后什么也没有。

Apologies if any of this is too vague or explained poorly, any help or nudge in the right direction would be greatly appreciated. 抱歉,如果其中任何一个内容过于含糊或解释不充分,请向正确的方向提供帮助或微调。

Thanks in advance. 提前致谢。

you have to change the "Wire.onReceive(I2CReceived);" 您必须更改“ Wire.onReceive(I2CReceived);” to the loop because when it's in the setup the arduino exute it only one, (sorry for my english) 进入循环,因为在设置时arduino仅将其退出,(对不起我的英语)

I wrote an I2C slave for Arduino UNO based on Nick Gammon Web Site . 我基于Nick Gammon网站为Arduino UNO编写了一个I2C从设备。 It worked but I could not get more than 10 K byte /second. 它可以工作,但是我不能超过10 K字节/秒。 You have some missing part in your own code. 您自己的代码中缺少一些部分。

Here is stripped down version of the Arduino Code 这是Arduino代码的简化版本

#include <Wire.h>

#define I2C_400K 1 // http://www.gammon.com.au/forum/?id=10896

bool receiveEventcommandReceived = false;
bool requestEventCommandReceived = false;

int _currentRequestInputParameterLen = 0;

void receiveEvent(int howMany) {

    receiveEventcommandReceived = true;
    while (Wire.available() > 0)
    {
        _cmd = Wire.read(); 

        if (_cmd == ArduinoCommand_EpromWrite) {
            // Some code
        }
        else if (_cmd == ArduinoCommand_EpromRead) {

            _addr0 = Wire.read();
            _addr1 = Wire.read();
            _addr  = (_addr0 * 256) + _addr1;
            _len   = Wire.read();
            _EEPROMBuffer = NusbioEx.EEPROMRead(_addr, _len);
            _r     = 128+1;
        }
        else {
            // Some code
        }
        _count++;
    }
}

void requestEvent()
{
    requestEventCommandReceived = true;

    if (_cmd == ArduinoCommand_EpromRead) {

        Wire.write(_EEPROMBuffer, strlen(_EEPROMBuffer));
    }
    else { // ArduinoCommand_EpromWrite or any other api

        int v1 = _r >> 8;
        int v2 = _r & 0xFF;

        char buffer[2];
        buffer[0] = v1;
        buffer[1] = v2;
        Wire.write(buffer, 2); // MUST BE SENT IN ONE BUFFER -> TO CREATE ONE I2C TRANSACTION
    }
}

void SetupI2C() {

    Wire.begin(I2C_SLAVE_ADDR);                // join i2c bus with address #4

    #if I2C_400K
        TWBR = 12; // http://www.gammon.com.au/forum/?id=10896
    #endif

    Wire.onReceive(receiveEvent); // register event
    Wire.onRequest(requestEvent); // register event
}

void setup() {
    SetupI2C();
}

void loop() {

    if (requestEventCommandReceived) 
    {
        requestEventCommandReceived = false;
    }
    #endif
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM