首页 > 解决方案 > 您是否曾经将 MAX44009 传感器与 uC 接口?

问题描述

我最近尝试使用免费下载版本的 MikroC for dsPIC IDE 进行编程,MAX44009 传感器与 PIC24FJ64GB002 微控制器接口。传感器的勒克斯结果应显示在 LCD 上。代码编译没有失败,但它不工作。当我使用示波器查看 SDA 和 SCL 线时,它们总是保持在高位。但如果我转换:

浮点亮度-> int 亮度

FloatToStr(亮度,勒克斯)--> IntToString(亮度,勒克斯)

然后将代码加载到嵌入式设备上,示波器显示 I2C 通信正在进行,但 LCD 显示的值没有给出任何有价值的数据。勒克斯显示为 -17204,它永远不会改变。

我试图在 LCD 上显示“指数”并显示:-17204 我试图在 LCD 上显示“尾数”并显示:255

当我尝试调试代码时,当亮度被声明为浮点数时,出现错误:“模拟错误:PC 超出范围 [0x0011F0]”当我从浮点数更改为整数时;我可以调试代码,但是LCD上输出的值没用。

//LCD module connections
sbit LCD_RS at LATB2_bit;
sbit LCD_EN at LATB3_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB7_bit;
sbit LCD_D6 at LATB13_bit;
sbit LCD_D7 at LATB14_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB13_bit;
sbit LCD_D7_Direction at TRISB14_bit;
//End LCD Module connections

//Global Constants
char txt1[] = "LUX:";
char txt2[] = "check";
//End Global Constants


// Global var.
unsigned int buff[2];
//unsigned char b1Index;
int mantissa;
int exponent;
float luminance;
char lux[16];
//end of Global variable declaration

//Function declaration
void MAX44009_getvalue();
//end of function declaration

void main(){
AD1PCFG = 0xffff;                        //Configure AN pins as digital 
I/O
Delay_ms(500);
I2C1_Init(100000);                       //Initialize I2C communication 
on SDA1 and SCL1
I2C1_Start();                  //Wake-up I2C components
I2C1_Write(0x96);              //Address MAX44009 lux sensor 
specifically, and prepare for write command
I2C1_Write(0x02);              //Address specific register 02 (interupt 
register) for MAX44009
I2C1_Write(0x40);              //Continuous mode, 100ms integration time
I2C1_Stop();                   //issue I2C stop signal
Delay_ms(300);                 //Because Arduino said so....

Lcd_Init();                              //Initialize LCD
Lcd_Cmd(_LCD_CLEAR);                     //Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF);                //Cursor Off

/*Delay_ms(1000);
Lcd_Out(2,1,txt2);
Delay_ms(1000);*/

while (1){
    MAX44009_getvalue();
    exponent = (buff[0] & 0xF0) >> 4;
    mantissa = ((buff[0] & 0x0F ) << 4) | (buff[1] & 0x0F);
    luminance = pow(2, exponent)*mantissa*0.045;

    FloatToStr(luminance, lux);

    Lcd_Out(1,1,txt1);
    Lcd_Out(2,1,lux);
    Delay_ms(500);
}
}

void MAX44009_getvalue(){

I2C1_Start();
I2C1_Write(0x96);
I2C1_Write(0x03);             //MAX44009 LUX High-Byte Register 0x03
I2C1_Restart();
I2C1_Write(0x97);

buff[0] = I2C1_Read(0);

I2C1_Restart();

I2C1_Write(0x96);
I2C1_Write(0x04);              //MAX44009 LUX Low-Byte Register 0x04
I2C1_Restart();
I2C1_Write(0x97);

buff[1] = I2C1_Read(0);

I2C1_Stop();
}     

预期结果:工作 MAX44009 照度传感器代码,在 LCD 上显示照度值

模拟错误:PC 超出范围 [0x0011F0]

标签: csensorsi2cmikrocdspic

解决方案


推荐阅读