iic总线的Wire.onRequest(requestEvent)函数问题
看过网上一些主从机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)函数,求大神指教 這是很正常, onRequest 是設定有 request 時要執行的函數, 你再設定一次, 就把原先的改了.
比如以下例子:
int a;
a = 1;
a = 2;
你會因為最後 a 的值變成 2 而覺得有問題嗎? Super169 发表于 2015-3-24 17:23 static/image/common/back.gif
這是很正常, onRequest 是設定有 request 時要執行的函數, 你再設定一次, 就把原先的改了.
比如以下例子 ...
哦,也就是说要接受多个数据,是在事件子函数requestEvent里面改,一个程序里面只能有一个Wire.onReceive(receiveEvent);Wire.onRequest(requestEvent); 还有这个函数是不是不能被其他函数调用 你看看 Wire.onReceive 及 Wire.onRequest 的源碼, 就會明白了, 只是一句簡單的 設定.
// sets function called on slave write
void TwoWire::onReceive( void (*function)(int) )
{
user_onReceive = function;
}
// sets function called on slave read
void TwoWire::onRequest( void (*function)(void) )
{
user_onRequest = function;
}
情況就等同於執行a = 1; 一樣. 之後, 當有需要時, 就是這樣執行你設定的 function:
// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{
// don't bother if user hasn't registered a callback
if(!user_onReceive){
return;
}
// don't bother if rx buffer is in use by a master requestFrom() op
// i know this drops data, but it allows for slight stupidity
// meaning, they may not have read all the master requestFrom() data yet
if(rxBufferIndex < rxBufferLength){
return;
}
// copy twi rx buffer into local read buffer
// this enables new reads to happen in parallel
for(uint8_t i = 0; i < numBytes; ++i){
rxBuffer = inBytes;
}
// set rx iterator vars
rxBufferIndex = 0;
rxBufferLength = numBytes;
// alert user program
user_onReceive(numBytes);
}
// behind the scenes function that is called when data is requested
void TwoWire::onRequestService(void)
{
// don't bother if user hasn't registered a callback
if(!user_onRequest){
return;
}
// reset tx buffer iterator vars
// !!! this will kill any pending pre-master sendTo() activity
txBufferIndex = 0;
txBufferLength = 0;
// alert user program
user_onRequest();
}
哦哦,就直接改里面的值了,但他是一个函数应该可以被调用啊,我却调用不了 Super169 发表于 2015-3-24 17:43 static/image/common/back.gif
之後, 當有需要時, 就是這樣執行你設定的 function:
// behind the scenes function that is called whe ...
谢谢了啊,查看了库基本明白了
页:
[1]