我最近尝试使用免费下载版本的MikroC for dsPIC IDE进行编程,该版本是MAX44009传感器,可与PIC24FJ64GB002微控制器接口。传感器的照度结果应在LCD上显示。该代码编译没有失败,但是没有用。当我使用示波器查看SDA和SCL线时,它们始终保持高电平。但是如果我转换:
浮动亮度->整数亮度
和
FloatToStr(亮度,勒克斯)-> IntToString(亮度,勒克斯)
然后将代码加载到嵌入式设备上,示波器显示正在进行I2C通信,但是LCD显示的值未提供任何有价值的数据。勒克斯显示为-17204,它从不改变。
我试图在LCD上显示“指数”,它显示为:-17204 我试图在液晶显示屏上显示“尾数”,并显示:255
当我尝试调试代码时,将亮度声明为浮点数时,出现错误:“模拟错误:PC超出范围[0x0011F0]” 当我从float更改为int时;我可以调试代码,但是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]范围内