我使用Arduino mega 2560和光傳感器BH1750FVI。顯示結果我使用的小型機,工作在Ubuntu 16.04 LTS如何使用中斷從光傳感器讀取數據結果
我寫的代碼和它的工作,我的數據結果來自光傳感器讀取,我可以讀信息只有一次。我不明白我是如何通過中斷(TWI_vect)工作的,並且每次都使用我的uart顯示結果。
#include "my_header.h"
#define I2C_STATUS_MASK 0xF8
#define START_COND_TRANSMITTED 0x08
#define REPEATED_START_COND_TRANSMITTED 0x10
#define SLA_W_TRANSMITTED_ACK_RECEIVED 0x18
#define DATA_TRANSMITTED_ACK_RECEIVED 0x28
#define SLA_R_TRANSMITTED_ACK_RECEIVED 0x40
#define DATA_RECEIVED_ACK_RETURNED 0x50
#define DATA_RECEIVED_NACK_RETURNED 0x58
volatile int light_intensity = 0; // var for read data from sensor
ISR(USART0_UDRE_vect) {
if (!bufferIsEmpty(&buffer)) //if we have something to read do it
UDR0 = popFromBuff(&buffer);
else
UCSR0B &= ~_BV(UDRIE0); //disallow interrupts
}
int main(void) {
cli();
init_port(); //initialize my port
init_buffer(&buffer, BUFF_SIZE); //init ring_buffer for uart
init_uart();
TWI_init();
sei();
TWI_start(); //send start
TWI_send_SLA(WRITE); //send SLA+W to light sensor
TWI_sendData(0b00010000); //opecode for Measurement
TWI_stop(); //stop
TWI_start(); //send start
TWI_send_SLA(READ); // send SLA+R to sensor
TWI_readData(); // read data from it light_intensity = TWDR
TWI_stop(); // stop
u_printnumbers(light_intensity); /*my func that send data to ring_buffer and later
*using interrupts send it form buffer to UDR
*/
u_print("\n");
while (1) {
if (!bufferIsEmpty(&buffer)) /*if buffer is not empty allow interrupts for uart*/
UCSR0B |= _BV(UDRIE0);
}
}
所以這段代碼工作,我從傳感器接收數據,一切正常。但是我想用中斷(TWI_vect)來使用它,並在while()中像傳感器一樣接收數據。我讀了很多信息,但不明白它應該如何顯示。 你能告訴我正確的方式使用我的代碼與中斷(TWI_vect)一些小例子。 Thx尋求幫助!
我們不是一個輔導服務。 – Olaf