最近在调试MLX90614红外模块,写了一个测试程序,发现应答不成功(SDA一直为高),看了下时序没有看出啥问题(能力有限)。移植了官方程序,也是应答不成功。具体代码如下。没来得及注释。希望各路大神看看。
static void rx_mlx90614_start(void)
{
MLX90614_SCKOUT_0;
delay_ms(10);
MLX90614_SCKOUT_1;
MLX90614_SDA_OUT;
MLX90614_SDA_OUT_1;
// delay_ms(1);
MLX90614_SCKOUT_1;
delay_us(46);
MLX90614_SDA_OUT_0;
delay_ms(29);
}
static void rx_mlx90614_write_onebyte(uchar com)
{
uchar i = 0;
MLX90614_SDA_OUT;
MLX90614_SCKOUT_0;
delay_ms(29);
for (i=0; i<8; i++)
{
if ((com & 0x80) == 0x80)
MLX90614_SDA_OUT_1;
else
MLX90614_SDA_OUT_0;
MLX90614_SCKOUT_1;
delay_us(46);
MLX90614_SCKOUT_0;
delay_ms(29);
com <<= 1;
}
}
static void rx_mlx90614_write_ack(uchar ack_noack)
{
MLX90614_SDA_OUT;
MLX90614_SCKOUT_0;
// delay_ms(29);
if (ack_noack == 0)
MLX90614_SDA_OUT_0;
else
MLX90614_SDA_OUT_1;
// delay_ms(1);
MLX90614_SCKOUT_1;
// delay_us(46);
MLX90614_SCKOUT_0;
// delay_ms(29);
// MLX90614_SDA_OUT_1;
}
static ack_result rx_mlx90614_read_ack(void)
{
uint i = 0;
ack_result ack_temp = ACK_OK;
MLX90614_SDA_IN;
MLX90614_SCKOUT_1;
// delay_us(1);
while ((MLX90614_SDA_READ == BIT1))
{
i++;
if (i == 100)
break;
printf("\r\n NO ACK !");
}
MLX90614_SCKOUT_0;
// delay_ms(3);
if (i == 100)
return ACK_FAILURE;
return ack_temp;
}
static uchar rx_mlx90614_read_onebyte(void)
{
uchar i = 0, data = 0;
MLX90614_SDA_IN;
MLX90614_SCKOUT_1;
delay_us(46);
for (i=0; i<8; i++)
{
data <<= 1;
data += (uchar)((MLX90614_SDA_READ == BIT1) ? 1 : 0);
MLX90614_SCKOUT_0;
delay_ms(29);
MLX90614_SCKOUT_1;
delay_us(46);
}
return data;
}
static void rx_mlx90614_stop(void)
{
MLX90614_SDA_OUT;
MLX90614_SCKOUT_1;
// delay_ms(30);
MLX90614_SDA_OUT_0;
delay_us(46);
MLX90614_SDA_OUT_1;
MLX90614_SCKOUT_0;
delay_ms(29);
}
uint rx_mlx90614_eeprom_or_ram_read(uchar adr, uchar com)
{
uchar data_high8_bit = 0, data_low8_bit = 0, data_pec = 0;
rx_mlx90614_rst:
rx_mlx90614_start();
rx_mlx90614_write_onebyte((adr << 1) | MLX90614_W_BIT);
if (rx_mlx90614_read_ack() == ACK_FAILURE)
goto rx_mlx90614_rst;
rx_mlx90614_write_onebyte(MLX90614_RAM_COM(com));
if (rx_mlx90614_read_ack() == ACK_FAILURE)
goto rx_mlx90614_rst;
rx_mlx90614_start();
rx_mlx90614_write_onebyte((adr << 1) | MLX90614_R_BIT);
if (rx_mlx90614_read_ack() == ACK_FAILURE)
goto rx_mlx90614_rst;
data_low8_bit = rx_mlx90614_read_onebyte();
rx_mlx90614_write_ack(ACK);
data_high8_bit = rx_mlx90614_read_onebyte();
rx_mlx90614_write_ack(ACK);
data_pec = rx_mlx90614_read_onebyte();
rx_mlx90614_write_ack(NACK);
rx_mlx90614_stop();
return (uint)((data_high8_bit << 8) | data_low8_bit);
}
|