اطلاعیه

Collapse
No announcement yet.

راه اندازی سنسور MLX90614 در روی میکروی AVR و Mikrobasic

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    راه اندازی سنسور MLX90614 در روی میکروی AVR و Mikrobasic

    با سلام:
    در رابطه با کار با سنسور دمای مادون قرمز MLX90614 ( در مد ارتباطی I2C ) یک سری کدهایی برای کار با آن در نرم افزار Arduino وجود دارند که البته در تست انجام شده دمای سنسور گاهی درست نمایش داده می شد و گاهی نه!!!

    بر اساس دیتاشیت این سنسور کار با آن مشابه کار با ای سی DS1307 (تایمر) می باشد، کدهای مربوطه در آردوینو به صورت زیر می باشد:

    کد:
    [COLOR=#858C93][FONT=inherit]#include[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]<i2cmaster.h>[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]void[/FONT][/COLOR][COLOR=#303336][FONT=inherit] setup[/FONT][/COLOR][COLOR=#303336][FONT=inherit](){[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]Serial[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]begin[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]9600[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]Serial[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]println[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Setup..."[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    i2c_init[/FONT][/COLOR][COLOR=#303336][FONT=inherit]();[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]//Initialise the i2c bus[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    PORTC[/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]1[/FONT][/COLOR][COLOR=#303336][FONT=inherit]<<[/FONT][/COLOR][COLOR=#303336][FONT=inherit] PORTC4[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]|[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]1[/FONT][/COLOR][COLOR=#303336][FONT=inherit]<<[/FONT][/COLOR][COLOR=#303336][FONT=inherit] PORTC5[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]//enable pullups[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#303336][FONT=inherit]}[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]void[/FONT][/COLOR][COLOR=#303336][FONT=inherit] loop[/FONT][/COLOR][COLOR=#303336][FONT=inherit](){[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]int[/FONT][/COLOR][COLOR=#303336][FONT=inherit] dev [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0x5A[/FONT][/COLOR][COLOR=#303336][FONT=inherit]<<[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]1[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]int[/FONT][/COLOR][COLOR=#303336][FONT=inherit] data_low [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]int[/FONT][/COLOR][COLOR=#303336][FONT=inherit] data_high [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]int[/FONT][/COLOR][COLOR=#303336][FONT=inherit] pec [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    i2c_start_wait[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]dev[/FONT][/COLOR][COLOR=#303336][FONT=inherit]+[/FONT][/COLOR][COLOR=#303336][FONT=inherit]I2C_WRITE[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    i2c_write[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0x07[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#858C93][FONT=inherit]// read[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    i2c_rep_start[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]dev[/FONT][/COLOR][COLOR=#303336][FONT=inherit]+[/FONT][/COLOR][COLOR=#303336][FONT=inherit]I2C_READ[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    data_low[/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] i2c_readAck[/FONT][/COLOR][COLOR=#303336][FONT=inherit]();[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]//Read 1 byte and then send ack[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    data_high[/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] i2c_readAck[/FONT][/COLOR][COLOR=#303336][FONT=inherit]();[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]//Read 1 byte and then send ack[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    pec[/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] i2c_readNak[/FONT][/COLOR][COLOR=#303336][FONT=inherit]();[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    i2c_stop[/FONT][/COLOR][COLOR=#303336][FONT=inherit]();[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#858C93][FONT=inherit]//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]double[/FONT][/COLOR][COLOR=#303336][FONT=inherit] tempFactor [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0.02[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]// 0.02 degrees per LSB (measurement resolution of the MLX90614)[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]double[/FONT][/COLOR][COLOR=#303336][FONT=inherit] tempData [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0x0000[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]// zero out the data[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]int[/FONT][/COLOR][COLOR=#303336][FONT=inherit] frac[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]// data past the decimal point[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#858C93][FONT=inherit]// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    tempData[/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#101094][FONT=inherit]double[/FONT][/COLOR][COLOR=#303336][FONT=inherit])((([/FONT][/COLOR][COLOR=#303336][FONT=inherit]data_high [/FONT][/COLOR][COLOR=#303336][FONT=inherit]&[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0x007F[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]<<[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]8[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]+[/FONT][/COLOR][COLOR=#303336][FONT=inherit] data_low[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    tempData[/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]tempData [/FONT][/COLOR][COLOR=#303336][FONT=inherit]*[/FONT][/COLOR][COLOR=#303336][FONT=inherit] tempFactor[/FONT][/COLOR][COLOR=#303336][FONT=inherit])-[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0.01[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]float[/FONT][/COLOR][COLOR=#303336][FONT=inherit] celcius [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] tempData [/FONT][/COLOR][COLOR=#303336][FONT=inherit]-[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]273.15[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]float[/FONT][/COLOR][COLOR=#303336][FONT=inherit] fahrenheit [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]celcius[/FONT][/COLOR][COLOR=#303336][FONT=inherit]*[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]1.8[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]+[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]32[/FONT][/COLOR][COLOR=#303336][FONT=inherit];[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]Serial[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]print[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Celcius: "[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]Serial[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]println[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]celcius[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]Serial[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]print[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]"Fahrenheit: "[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#2B91AF][FONT=inherit]Serial[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]println[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]fahrenheit[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    delay[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]1000[/FONT][/COLOR][COLOR=#303336][FONT=inherit]);[/FONT][/COLOR][COLOR=#858C93][FONT=inherit]// wait a second before printing again[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    
    
    
    [/FONT][/COLOR][COLOR=#303336][FONT=inherit]}[/FONT][/COLOR]
    خلاصه روش ارتباطی با سنسور برگرفته از دیتاشیت به صورت زیر می باشد:

    حال برای کار روشی به صورت عکس زیر مورد استفاده قرار داده ام:

    بر این اساس کدهای زیر را در محیط Mikrobasic برای ارتباط I2C به صورت زیر نوشته شده است:
    کد:
    '' Write
        Soft_I2C_Start()                         ' issue start signal
        Soft_I2C_Write(0xB4)                 ' address dev
        Soft_I2C_Write(0x07)                   ' write 01 to year word (REG6)
    '    Soft_I2C_Stop()                          ' issue stop signal
    
    '
    '' Read
        Soft_I2C_Start()                        ' Issue repeated start signal
        Soft_I2C_Write(0xB5)                 ' address dev
    '    
        data_low1=Soft_I2C_Read(1)
        data_high1=Soft_I2C_Read(1)
        pec1=Soft_I2C_Read(0)
        Soft_I2C_Stop()   ' Issue stop signal
    گل کدهای ا« به صورت زیر و شبیه سازی در محیط پروتئوس ( بدون کتابخانه MLX90614) در لینک زیر موجود می باشد:

    حال مشکلی که وجود دارد این می باشد که در اتصال این سنسور با میکروی AVR عددی که نشان داده می شود متغییر است گاهی 273- گاهی 59 درجه و ...
    لطفا در صورت امکان در رابطه با تبدیل کدهای بیس آردوینو و نوع ارتباط I2C از لحاظ رجیستر ها و ... راهنمایی بفرمایید.

    یکی از سئوال هایی که داشتم این است که در کدهای آردوینو آدرس سنسور به صورت:
    0x5A<<1می باشد که به معنای 0xB4 می باشد، آیا نباید آدرس خواندن را به صورت 0x5A ای که بیت اول آن را به یک تغییر داده یعنی 0X5B داده شود؟

    یکی از احتمالات ممکن اشتباه واردکردن آدرس سنسور در ارتباط I2C می باشد.

    با تشکر.
    جدیدترین ویرایش توسط soheil_sabz; ۱۳:۳۵ ۱۳۹۶/۱۲/۰۳.
لطفا صبر کنید...
X