查看: 1874|回复: 1

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

[复制链接]
  • TA的每日心情
    开心
    2020-2-14 12:16
  • 签到天数: 827 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2016-12-31 14:24:59 | 显示全部楼层 |阅读模式
    分享到:
    接上一篇帖子
    四周年庆-【赚周年币】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初始化函数,如下
    36.jpg
    2、也可以使用fireduino的D4输出控制用户LED,注意是共阳极,低电平亮

    评分

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

    查看全部评分

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2020-2-14 12:16
  • 签到天数: 827 天

    连续签到: 1 天

    [LV.10]以坛为家III

     楼主| 发表于 2017-1-2 12:06:18 | 显示全部楼层
    arduino IIC 使用A4、A5
    接线如下
    20170101_112130.jpg
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 01:33 , Processed in 0.132423 second(s), 19 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.