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); // 设置GPIO_8为输出模式
- 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); // 设置GPIO_8为输出模式
- 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); // 设置GPIO_14为输出模式
- 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); // 设置GPIO_7为输出模式
- }
- /*******************************************************************************
- * 函数名: PEC_calculation
- * 功能 : 数据校验
- * Input : pec[]
- * Output : None
- * Return : pec[0]-this byte contains calculated crc value
- *******************************************************************************/
- 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
- {
- /*Load pattern value 0x000000000107*/
- crc[5]=0;
- crc[4]=0;
- crc[3]=0;
- crc[2]=0;
- crc[1]=0x01;
- crc[0]=0x07;
- /*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/
- BitPosition=47;
- /*Set shift position at 0*/
- shift=0;
- /*Find first "1" in the transmited message beginning from the MSByte byte5*/
- i=5;
- j=0;
- while((pec[i]&(0x80>>j))==0 && i>0)
- {
- BitPosition--;
- if(j<7)
- {
- j++;
- }
- else
- {
- j=0x00;
- i--;
- }
- }/*End of while */
- /*Get shift value for pattern value*/
- shift=BitPosition-8;
- /*Shift pattern value */
- 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;
- }/*End of for*/
- shift--;
- }/*End of while*/
- /*Exclusive OR between pec and crc*/
- for(i=0; i<=5; i++)
- {
- pec[i] ^=crc[i];
- }/*End of for*/
- }
- while(BitPosition>8); /*End of do-while*/
- return pec[0];
- }
- /*******************************************************************************
- * 函数名: SMBus_ReadMemory
- * 功能: READ DATA FROM RAM/EEPROM
- * Input : slaveAddress, command
- * Return : Data
- *******************************************************************************/
- uint16_t SMBus_ReadMemory(uint8_t slaveAddress, uint8_t command)
- {
- uint16_t data; // Data storage (DataH:DataL)
- uint8_t Pec; // PEC byte storage
- uint8_t DataL=0; // Low data byte storage
- uint8_t DataH=0; // High data byte storage
- uint8_t arr[6]; // Buffer for the sent bytes
- uint8_t PecReg; // Calculated PEC byte storage
- uint8_t ErrorCounter; // Defines the number of the attempts for communication with MLX90614
- ErrorCounter=0x00; // Initialising of ErrorCounter
- slaveAddress <<= 1; //2-7位表示从机地址
-
- do
- {
- repeat:
- SMBus_Stop(); //If slave send NACK stop comunication
- --ErrorCounter; //Pre-decrement ErrorCounter
- if(!ErrorCounter) //ErrorCounter=0?
- {
- break; //Yes,go out from do-while{}
- }
- SMBus_Start(); //Start condition
- if(SMBus_SendByte(slaveAddress))//Send SlaveAddress 最低位Wr=0表示接下来写命令
- {
- goto repeat; //Repeat comunication again
- }
- if(SMBus_SendByte(command)) //Send command
- {
- goto repeat; //Repeat comunication again
- }
- SMBus_Start(); //Repeated Start condition
- if(SMBus_SendByte(slaveAddress+1)) //Send SlaveAddress 最低位Rd=1表示接下来读数据
- {
- goto repeat; //Repeat comunication again
- }
- DataL = SMBus_ReceiveByte(ACK); //Read low data,master must send ACK
- DataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACK
- Pec = SMBus_ReceiveByte(NACK); //Read PEC byte, master must send NACK
- SMBus_Stop(); //Stop condition
- arr[5] = slaveAddress; //
- arr[4] = command; //
- arr[3] = slaveAddress+1; //Load array arr
- arr[2] = DataL; //
- arr[1] = DataH; //
- arr[0] = 0; //
- PecReg=PEC_Calculation(arr);//Calculate CRC
- }
- while(PecReg != Pec); //If received and calculated CRC are equal go out from do-while{}
- data = (DataH<<8) | DataL; //data=DataH:DataL
- return data;
- }
- /***************************************************************************
- * @brief Read object temperature(Tobj) from RAM registers of MLX90614
- * @param ambient_or_object : Which temperature you want to get
- * @retval Object temperature
- ****************************************************************************/
- float MLX90614_Read_TempData(uint8_t ambient_or_object)
- {
- return SMBus_ReadMemory(MLX90614_ADDR_WRITE, ambient_or_object)*0.02-273.15;
- }
- /***************************************************************
- * 函数名称: LedSafeStatusSet
- * 说 明: LED_Safe状态设置
- * 参 数: status,ENUM枚举的数据
- * OFF,关
- * ON,开
- * 返 回 值: 无
- ***************************************************************/
- void LedSafeStatusSet(SwitchStatus status)
- {
- if (status == ON) {
- IoTGpioSetOutputVal(7, 1); // 设置GPIO_7输出高电平点亮灯
- }
- if (status == OFF) {
- IoTGpioSetOutputVal(7, 0); // 设置GPIO_7输出低电平关闭灯
- }
- }
- /***************************************************************
- * 函数名称: LedWarnStatusSet
- * 说 明: LED_Warn状态设置
- * 参 数: status,ENUM枚举的数据
- * OFF,关
- * ON,开
- * 返 回 值: 无
- ***************************************************************/
- void LedWarnStatusSet(SwitchStatus status)
- {
- if (status == ON) {
- IoTGpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_14, 1); // 设置GPIO_14输出高电平点亮灯
- }
- if (status == OFF) {
- IoTGpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_14, 0); // 设置GPIO_14输出低电平关闭灯
- }
- }
- /***************************************************************
- * 函数名称: BeepStatusSet
- * 说 明: Beep状态设置
- * 参 数: status,ENUM枚举的数据
- * OFF,关
- * ON,开
- * 返 回 值: 无
- ***************************************************************/
- void BeepStatusSet(SwitchStatus status)
- {
- if (status == ON) {
- IoTGpioSetOutputVal(BEEP_GPIO, 1); // 设置GPIO_7输出高电平点亮灯
- }
- if (status == OFF) {
- IoTGpioSetOutputVal(BEEP_GPIO, 0); // 设置GPIO_7输出低电平关闭灯
- }
- }
|