极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13723|回复: 6

iic总线的Wire.onRequest(requestEvent)函数问题

[复制链接]
发表于 2015-3-24 16:50:49 | 显示全部楼层 |阅读模式
看过网上一些主从机iic总线的程序,后自改写发现Wire.onRequest(requestEvent);这个函数如果初始化两个程序的话,如

Serial.begin(9600);      
  Wire.begin(2);   
  Wire.onReceive(pair);
  Wire.onRequest(pair);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  最终执行的是  Wire.onRequest(requestEvent); 这个函数,上一个函数并未执行,另外
子函数
void pair(int a)
{
  char c = Wire.read();
    Serial.print(c);  
    Wire.onRequest(pair);
}
是无法调用Wire.onRequest(pair)函数,求大神指教
回复

使用道具 举报

发表于 2015-3-24 17:23:22 | 显示全部楼层
這是很正常, onRequest 是設定有 request 時要執行的函數, 你再設定一次, 就把原先的改了.

比如以下例子:
  1. int a;

  2. a = 1;
  3. a = 2;
复制代码


你會因為最後 a 的值變成 2 而覺得有問題嗎?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-24 17:38:29 | 显示全部楼层
Super169 发表于 2015-3-24 17:23
這是很正常, onRequest 是設定有 request 時要執行的函數, 你再設定一次, 就把原先的改了.

比如以下例子 ...

哦,也就是说要接受多个数据,是在事件子函数requestEvent里面改,一个程序里面只能有一个Wire.onReceive(receiveEvent);  Wire.onRequest(requestEvent); 还有这个函数是不是不能被其他函数调用
回复 支持 反对

使用道具 举报

发表于 2015-3-24 17:41:51 | 显示全部楼层
你看看 Wire.onReceive 及 Wire.onRequest 的源碼, 就會明白了, 只是一句簡單的 設定.

  1. // sets function called on slave write
  2. void TwoWire::onReceive( void (*function)(int) )
  3. {
  4.   user_onReceive = function;
  5. }

  6. // sets function called on slave read
  7. void TwoWire::onRequest( void (*function)(void) )
  8. {
  9.   user_onRequest = function;
  10. }
复制代码


情況就等同於執行  a = 1; 一樣.
回复 支持 反对

使用道具 举报

发表于 2015-3-24 17:43:43 | 显示全部楼层
之後, 當有需要時, 就是這樣執行你設定的 function:

  1. // behind the scenes function that is called when data is received
  2. void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
  3. {
  4.   // don't bother if user hasn't registered a callback
  5.   if(!user_onReceive){
  6.     return;
  7.   }
  8.   // don't bother if rx buffer is in use by a master requestFrom() op
  9.   // i know this drops data, but it allows for slight stupidity
  10.   // meaning, they may not have read all the master requestFrom() data yet
  11.   if(rxBufferIndex < rxBufferLength){
  12.     return;
  13.   }
  14.   // copy twi rx buffer into local read buffer
  15.   // this enables new reads to happen in parallel
  16.   for(uint8_t i = 0; i < numBytes; ++i){
  17.     rxBuffer[i] = inBytes[i];   
  18.   }
  19.   // set rx iterator vars
  20.   rxBufferIndex = 0;
  21.   rxBufferLength = numBytes;
  22.   // alert user program
  23.   user_onReceive(numBytes);
  24. }

  25. // behind the scenes function that is called when data is requested
  26. void TwoWire::onRequestService(void)
  27. {
  28.   // don't bother if user hasn't registered a callback
  29.   if(!user_onRequest){
  30.     return;
  31.   }
  32.   // reset tx buffer iterator vars
  33.   // !!! this will kill any pending pre-master sendTo() activity
  34.   txBufferIndex = 0;
  35.   txBufferLength = 0;
  36.   // alert user program
  37.   user_onRequest();
  38. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-24 17:45:25 | 显示全部楼层
哦哦,就直接改里面的值了,但他是一个函数应该可以被调用啊,我却调用不了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-25 08:58:11 | 显示全部楼层
Super169 发表于 2015-3-24 17:43
之後, 當有需要時, 就是這樣執行你設定的 function:

// behind the scenes function that is called whe ...

谢谢了啊,查看了库基本明白了
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-18 05:39 , Processed in 0.035592 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表