1回答

1收藏

【赚周年币】fireduino-基于labview的双机通信-从机

#线上活动 #线上活动 2649 人阅读 | 1 人回复 | 2016-12-31

接上一篇帖子
四周年庆-【赚周年币】fireduino-基于labview的双机通信-上位机-爱板网论坛 - 电子工程师学习交流园地 https://www.cirmall.com/bbs/thread-59343-1-1.html

fireduino程序如下

  1. void setup()
  2. {
  3.   Wire.begin(10);  // join i2c bus with address #4
  4.   Wire.onRequest(requestEvent);
  5.   Wire.onReceive(receiveEvent); // register event
  6.   pinMode(13,OUTPUT);     //if use fireduino,should be pinMode(4,OUTPUT);
  7.   Serial.begin(9600);           // start serial for output
  8. }

  9. void loop()
  10. {
  11.   delay(100);
  12. }

  13. // function that executes whenever data is received from master
  14. // this function is registered as an event, see setup()
  15. void receiveEvent(int howMany)
  16. {
  17.   while(0 < Wire.available()) // loop through all but the last
  18.   {
  19.     byte c = Wire.read(); // receive byte as a character
  20.     if(c==1)
  21.     {
  22.     digitalWrite(13,LOW);
  23.     }
  24.     else if(c==0)
  25.     {
  26.     digitalWrite(13,HIGH);
  27.     }
  28.     else
  29.     {
  30.     digitalWrite(13,HIGH);
  31.     }
  32.     Serial.print(c);         // print the character
  33.   }
  34.   //int x = Wire.read();    // receive byte as an integer
  35.   //Serial.println(x);         // print the integer      // print the integer
  36. }

  37. void requestEvent()
  38. {
  39.   Wire.write(i++);
  40.   if(i > 255) // loop through all but the last
  41.   {
  42.     i=0;
  43.   }
  44. }  
复制代码
程序中定义了两个函数:数据接收事件和请求事件
注意:
1、fireduino默认115200波特率,需要在其相关头文件修改;或者修改arduino初始化函数,如下

2、也可以使用fireduino的D4输出控制用户LED,注意是共阳极,低电平亮

评分

参与人数 1 +10 收起 理由
loveeeboard + 10

查看全部评分

分享到:
回复

使用道具 举报

回答|共 1 个

倒序浏览

沙发

suoma

发表于 2017-1-2 12:06:18 | 只看该作者

arduino IIC 使用A4、A5
接线如下
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条