我正在开发一个Arduino项目,我通过I2C通信接收消息。我有几个例程,程序花了很多时间而不返回。目前,我在发生中断时设置了一个中断标志,我基本上在几个地方检查这些功能,如果发生中断,我会返回。我想知道中断函数是否可以调用我的入口点函数。
所以这是我当前的中断函数
void ReceivedI2CMessage(int numBytes)
{
Serial.print(F("Received message = "));
while (Wire.available())
{
messageFromBigArduino = Wire.read();
}
Serial.println(messageFromBigArduino);
I2CInterrupt = true;
}
并且在程序花费大部分时间的功能中,我必须在几个地方执行此操作
if(I2CInterrupt) return;
现在我想知道是否可以从我的ReceiveI2CMessage中调用我的入口点函数。我主要担心的是,这可能会导致内存泄漏,因为当中断发生时,我会将我正在执行的函数留下,并且我将回到程序的开头。
答案 0 :(得分:4)
It is okay but not preferred. It is always safer to do less -- perhaps simply set a flag -- and exit interrupts as fast as possible. Then take care of the flag/semaphore back in your main loop. For example:
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
int SelectedRowIndex=jTable1.getSelectedRow();
jComboBox2.addItem((String)jTable1.getValueAt(SelectedRowIndex, 0));
jComboBox2.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 0));
jComboBox3.addItem((String)jTable1.getValueAt(SelectedRowIndex, 1));
jComboBox3.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 1));
jTextField2.setText((String)jTable1.getValueAt(SelectedRowIndex, 2));
jComboBox5.addItem((String)jTable1.getValueAt(SelectedRowIndex, 3));
jComboBox5.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 3));
jComboBox6.addItem((String)jTable1.getValueAt(SelectedRowIndex, 4));
jComboBox6.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 4));
jTextField6.setText((String)jTable1.getValueAt(SelectedRowIndex, 5));
jComboBox14.addItem((String)jTable1.getValueAt(SelectedRowIndex, 6));
jComboBox14.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 6));
jTextField5.setText((String)jTable1.getValueAt(SelectedRowIndex, 7));
jComboBox13.addItem((String)jTable1.getValueAt(SelectedRowIndex, 8));
jComboBox13.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 8));
jComboBox4.addItem((String)jTable1.getValueAt(SelectedRowIndex, 9));
jComboBox4.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 9));
jTextField3.setText((String)jTable1.getValueAt(SelectedRowIndex, 10));
java.sql.Date date1 = new java.sql.Date(jDateChooser2.getDate().getTime());
date1.setDate((int)jTable1.getValueAt(SelectedRowIndex, 11));
}
Then in your main loop:
volatile uint8_t i2cmessage = 0; // must be volatile since altered in an interrupt
void ReceivedI2CMessage(int numBytes) // not sure what numBytes are used for...
{
i2cmessage = 1; // set a flag and get out quickly
}
This achieves the goal of the interrupt, which is ideally to set a semaphore to be acted on quickly in the main loop.