我正在使用Arduino mega 2560和光传感器BH1750FVI。显示结果我正在使用minicom,在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)的正确方法吗?求救!
答案 0 :(得分:0)
要使用Two-Wirte接口(I²C; TWI)中断驱动,您必须在TWI ISR内部实现某种状态机。
Atmel Application Note AVR135稍微了解一下如何做到这一点。您必须将缓冲区准备好并将抽象事务放入某种结构中。例如,您可以拥有
struct i2c_transfer {
uint8_t i2c_sla; /* slave address */
const uint8_t *txbuf;
size_t txbytes;
uint8_t *rxbuf;
size_t rxbytes;
}
你设置了,然后有一个功能开始发送I2C并通过检查txbytes == 0 && rxbytes == 0
或发生错误来检查你是否完成了。然后,您的ISR需要检查其状态并根据它应该写入和读取的字节数进行适当的操作。