123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #include "mlx90614.h"
- #include "smbus.h"
- #include "iot_gpio.h"
- #include "iot_gpio_ex.h"
- #define WIFI_IOT_IO_NAME_GPIO_7 7
- #define WIFI_IOT_IO_NAME_GPIO_14 14
- #define WIFI_IOT_IO_NAME_GPIO_10 10
- #define WIFI_IOT_IO_NAME_GPIO_12 12
- #define WIFI_IOT_I2C_IDX_1 1
- uint8_t RxBuffer[3];
- #define BEEP_GPIO 8
- void MLX90614_Init(void)
- {
- IoTGpioInit(BEEP_GPIO);
- IoTGpioSetFunc(BEEP_GPIO, IOT_GPIO_FUNC_GPIO_8_GPIO);
- IoTGpioSetDir(BEEP_GPIO, IOT_GPIO_DIR_OUT);
-
- IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_10);
- IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_10, IOT_GPIO_FUNC_GPIO_10_GPIO);
- IoTGpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, IOT_GPIO_DIR_OUT);
- IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_12);
- IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_12, IOT_GPIO_FUNC_GPIO_12_GPIO);
- IoTGpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, IOT_GPIO_DIR_OUT);
- IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_14);
- IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_14, IOT_GPIO_FUNC_GPIO_14_GPIO);
- IoTGpioSetDir(WIFI_IOT_IO_NAME_GPIO_14, IOT_GPIO_DIR_OUT);
- IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_7);
- IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_7, IOT_GPIO_FUNC_GPIO_7_GPIO);
- IoTGpioSetDir(WIFI_IOT_IO_NAME_GPIO_7, IOT_GPIO_DIR_OUT);
- }
- uint8_t PEC_Calculation(uint8_t pec[])
- {
- uint8_t crc[6];
- uint8_t BitPosition=47;
- uint8_t shift;
- uint8_t i;
- uint8_t j;
- uint8_t temp;
- do
- {
-
- crc[5]=0;
- crc[4]=0;
- crc[3]=0;
- crc[2]=0;
- crc[1]=0x01;
- crc[0]=0x07;
-
- BitPosition=47;
-
- shift=0;
-
- i=5;
- j=0;
- while((pec[i]&(0x80>>j))==0 && i>0)
- {
- BitPosition--;
- if(j<7)
- {
- j++;
- }
- else
- {
- j=0x00;
- i--;
- }
- }
-
- shift=BitPosition-8;
-
- while(shift)
- {
- for(i=5; i<0xFF; i--)
- {
- if((crc[i-1]&0x80) && (i>0))
- {
- temp=1;
- }
- else
- {
- temp=0;
- }
- crc[i]<<=1;
- crc[i]+=temp;
- }
- shift--;
- }
-
- for(i=0; i<=5; i++)
- {
- pec[i] ^=crc[i];
- }
- }
- while(BitPosition>8);
- return pec[0];
- }
- uint16_t SMBus_ReadMemory(uint8_t slaveAddress, uint8_t command)
- {
- uint16_t data;
- uint8_t Pec;
- uint8_t DataL=0;
- uint8_t DataH=0;
- uint8_t arr[6];
- uint8_t PecReg;
- uint8_t ErrorCounter;
- ErrorCounter=0x00;
- slaveAddress <<= 1;
-
- do
- {
- repeat:
- SMBus_Stop();
- --ErrorCounter;
- if(!ErrorCounter)
- {
- break;
- }
- SMBus_Start();
- if(SMBus_SendByte(slaveAddress))
- {
- goto repeat;
- }
- if(SMBus_SendByte(command))
- {
- goto repeat;
- }
- SMBus_Start();
- if(SMBus_SendByte(slaveAddress+1))
- {
- goto repeat;
- }
- DataL = SMBus_ReceiveByte(ACK);
- DataH = SMBus_ReceiveByte(ACK);
- Pec = SMBus_ReceiveByte(NACK);
- SMBus_Stop();
- arr[5] = slaveAddress;
- arr[4] = command;
- arr[3] = slaveAddress+1;
- arr[2] = DataL;
- arr[1] = DataH;
- arr[0] = 0;
- PecReg=PEC_Calculation(arr);
- }
- while(PecReg != Pec);
- data = (DataH<<8) | DataL;
- return data;
- }
- float MLX90614_Read_TempData(uint8_t ambient_or_object)
- {
- return SMBus_ReadMemory(MLX90614_ADDR_WRITE, ambient_or_object)*0.02-273.15;
- }
- void LedSafeStatusSet(SwitchStatus status)
- {
- if (status == ON) {
- IoTGpioSetOutputVal(7, 1);
- }
- if (status == OFF) {
- IoTGpioSetOutputVal(7, 0);
- }
- }
- void LedWarnStatusSet(SwitchStatus status)
- {
- if (status == ON) {
- IoTGpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_14, 1);
- }
- if (status == OFF) {
- IoTGpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_14, 0);
- }
- }
- void BeepStatusSet(SwitchStatus status)
- {
- if (status == ON) {
- IoTGpioSetOutputVal(BEEP_GPIO, 1);
- }
- if (status == OFF) {
- IoTGpioSetOutputVal(BEEP_GPIO, 0);
- }
- }
|