糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > stm32使用cubemx生成HAL库工程驱动mlx90614

stm32使用cubemx生成HAL库工程驱动mlx90614

时间:2022-03-02 23:37:00

相关推荐

stm32使用cubemx生成HAL库工程驱动mlx90614

MLX90614驱动代码

简单说明:

使用的IC是stm32f103c8t6,stm32cubemx生成HAL库工程的过程太简单不重复了

直接贴上驱动的.c和.h自行添加即可

mlx90614.c

/******************************************************************************** 文件名: mlx90614.c* 作 者: * 版 本: * 日 期: -08-07* 描 述: mlx90614函数PA6:SCLPA7:SDA在主函数中先初始化SMBus_Init();需要读取温度就调用temp=SMBus_ReadTemp(); //读取温度,temp是浮点数,转整数:i=ceil(temp); *******************************************************************************//* Includes ------------------------------------------------------------------*/#include "mlx90614.h"/* Private typedef -----------------------------------------------------------*//* Private define ------------------------------------------------------------*/#define ACK 0 //应答#defineNACK 1 //无应答#define SA0x00 //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a#define RAM_ACCESS0x00 //RAM access command RAM存取命令#define EEPROM_ACCESS0x20 //EEPROM access command EEPROM存取命令#define RAM_TOBJ10x07 //To1 address in the eeprom 目标1温度,检测到的红外温度 -70.01 ~ 382.19度//#define SMBUS_PORTGPIOA//PB端口(端口和下面的两个针脚可自定义)//#define SMBUS_SCKGPIO_Pin_6 //PB6:SCL//#define SMBUS_SDAGPIO_Pin_7 //PB7:SDA//#define RCC_APB2Periph_SMBUS_PORTRCC_APB2Periph_GPIOB#define SMBUS_SCK_H() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET)//SMBUS_PORT->BSRR = SMBUS_SCK //置高电平#define SMBUS_SCK_L() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET)//SMBUS_PORT->BRR = SMBUS_SCK //置低电平#define SMBUS_SDA_H() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_SET)//SMBUS_PORT->BSRR = SMBUS_SDA#define SMBUS_SDA_L() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_7,GPIO_PIN_RESET)#define SMBUS_SDA_PIN() HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)//SMBUS_PORT->IDR & SMBUS_SDA //读取引脚电平/* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*//******************************************************************************** Function Name : SMBus_StartBit* Description : Generate START condition on SMBus* Input: None* Output : None* Return : None*******************************************************************************/void SMBus_StartBit(void){SMBUS_SDA_H();// Set SDA lineSMBus_Delay(5); // Wait a few microsecondsSMBUS_SCK_H();// Set SCL lineSMBus_Delay(5); // Generate bus free time between StopSMBUS_SDA_L();// Clear SDA lineSMBus_Delay(5); // Hold time after (Repeated) Start// Condition. After this period, the first clock is generated.//(Thd:sta=4.0us min)在SCK=1时,检测到SDA由1到0表示通信开始(下降沿)SMBUS_SCK_L(); // Clear SCL lineSMBus_Delay(5); // Wait a few microseconds}/******************************************************************************** Function Name : SMBus_StopBit* Description : Generate STOP condition on SMBus* Input: None* Output : None* Return : None*******************************************************************************/void SMBus_StopBit(void){SMBUS_SCK_L();// Clear SCL lineSMBus_Delay(5); // Wait a few microsecondsSMBUS_SDA_L();// Clear SDA lineSMBus_Delay(5); // Wait a few microsecondsSMBUS_SCK_H();// Set SCL lineSMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min)SMBUS_SDA_H();// Set SDA line在SCK=1时,检测到SDA由0到1表示通信结束(上升沿)}/******************************************************************************** Function Name : SMBus_SendByte* Description : Send a byte on SMBus* Input: Tx_buffer* Output : None* Return : None*******************************************************************************/uint8_t SMBus_SendByte(uint8_t Tx_buffer){uint8_tBit_counter;uint8_tAck_bit;uint8_tbit_out;for(Bit_counter=8; Bit_counter; Bit_counter--){if (Tx_buffer&0x80){bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out}else{bit_out=0; // else clear bit_out}SMBus_SendBit(bit_out);// Send the current bit on SDATx_buffer<<=1;// Get next bit for checking}Ack_bit=SMBus_ReceiveBit();// Get acknowledgment bitreturnAck_bit;}/******************************************************************************** Function Name : SMBus_SendBit* Description : Send a bit on SMBus 82.5kHz* Input: bit_out* Output : None* Return : None*******************************************************************************/void SMBus_SendBit(uint8_t bit_out){if(bit_out==0){SMBUS_SDA_L();}else{SMBUS_SDA_H();}SMBus_Delay(2);// Tsu:dat = 250ns minimumSMBUS_SCK_H();// Set SCL lineSMBus_Delay(6);// High Level of Clock PulseSMBUS_SCK_L();// Clear SCL lineSMBus_Delay(3);// Low Level of Clock Pulse//SMBUS_SDA_H(); // Master release SDA line ,return;}/******************************************************************************** Function Name : SMBus_ReceiveBit* Description : Receive a bit on SMBus* Input: None* Output : None* Return : Ack_bit*******************************************************************************/uint8_t SMBus_ReceiveBit(void){uint8_t Ack_bit;SMBUS_SDA_H();//引脚靠外部电阻上拉,当作输入SMBus_Delay(2);// High Level of Clock PulseSMBUS_SCK_H();// Set SCL lineSMBus_Delay(5);// High Level of Clock Pulseif(SMBUS_SDA_PIN()){Ack_bit=1;}else{Ack_bit=0;}SMBUS_SCK_L();// Clear SCL lineSMBus_Delay(3);// Low Level of Clock PulsereturnAck_bit;}/******************************************************************************** Function Name : SMBus_ReceiveByte* Description : Receive a byte on SMBus* Input: ack_nack* Output : None* Return : RX_buffer*******************************************************************************/uint8_t SMBus_ReceiveByte(uint8_t ack_nack){uint8_t RX_buffer;uint8_tBit_Counter;for(Bit_Counter=8; Bit_Counter; Bit_Counter--){if(SMBus_ReceiveBit())// Get a bit from the SDA line{RX_buffer <<= 1;// If the bit is HIGH save 1 in RX_bufferRX_buffer |=0x01;}else{RX_buffer <<= 1;// If the bit is LOW save 0 in RX_bufferRX_buffer &=0xfe;}}SMBus_SendBit(ack_nack);// Sends acknowledgment bitreturn RX_buffer;}/******************************************************************************** Function Name : SMBus_Delay* Description : 延时 一次循环约1us* Input: time* Output : None* Return : None*******************************************************************************/void SMBus_Delay(uint16_t time){uint16_t i, j;for (i=0; i<4; i++){for (j=0; j<time; j++);}}/******************************************************************************** Function Name : SMBus_Init* Description : SMBus初始化* Input: None* Output : None* Return : None*******************************************************************************/void SMBus_Init(){// GPIO_InitTypeDef GPIO_InitStructure;///* Enable SMBUS_PORT clocks *///RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE);// /*配置SMBUS_SCK、SMBUS_SDA为集电极开漏输出*/// GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);// SMBUS_SCK_H();// SMBUS_SDA_H();}/******************************************************************************** Function Name : SMBus_ReadMemory* Description : READ DATA FROM RAM/EEPROM* Input: slaveAddress, command* Output : None* Return : Data*******************************************************************************/uint16_t SMBus_ReadMemory(uint8_t slaveAddress, uint8_t command){uint16_t data;// Data storage (DataH:DataL)uint8_t Pec;// PEC byte storageuint8_t DataL=0;// Low data byte storageuint8_t DataH=0;// High data byte storageuint8_t arr[6];// Buffer for the sent bytesuint8_t PecReg;// Calculated PEC byte storageuint8_t ErrorCounter;// Defines the number of the attempts for communication with MLX90614ErrorCounter=0x00;// Initialising of ErrorCounterslaveAddress <<= 1;//2-7位表示从机地址do{repeat:SMBus_StopBit(); //If slave send NACK stop comunication--ErrorCounter; //Pre-decrement ErrorCounterif(!ErrorCounter)//ErrorCounter=0?{break; //Yes,go out from do-while{}}SMBus_StartBit();//Start conditionif(SMBus_SendByte(slaveAddress))//Send SlaveAddress 最低位Wr=0表示接下来写命令{gotorepeat; //Repeat comunication again}if(SMBus_SendByte(command)) //Send command{gotorepeat; //Repeat comunication again}SMBus_StartBit();//Repeated Start conditionif(SMBus_SendByte(slaveAddress+1))//Send SlaveAddress 最低位Rd=1表示接下来读数据{gotorepeat; //Repeat comunication again}DataL = SMBus_ReceiveByte(ACK);//Read low data,master must send ACKDataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACKPec = SMBus_ReceiveByte(NACK);//Read PEC byte, master must send NACKSMBus_StopBit();//Stop conditionarr[5] = slaveAddress;//arr[4] = command;//arr[3] = slaveAddress+1;//Load array arrarr[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:DataLreturn data;}/******************************************************************************** Function Name : PEC_calculation* Description : Calculates the PEC of received bytes* 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_tBitPosition=47;uint8_tshift;uint8_ti;uint8_tj;uint8_ttemp;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];}/******************************************************************************** Function Name : SMBus_ReadTemp* Description : Calculate and return the temperature* Input: None* Output : None* Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15*******************************************************************************/float SMBus_ReadTemp(void){ float temp;temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;return temp;}/*********************************END OF FILE*********************************/

mlx90614.h

/******************************************************************************** 文件名: mlx90614.h* 作 者: * 版 本: * 日 期: -08-07* 描 述: mlx90614函数*******************************************************************************//* Define to prevent recursive inclusion -------------------------------------*/#ifndef __MLX90614_H#define __MLX90614_H/* Includes ------------------------------------------------------------------*/#include "stm32f1xx_hal.h"/* Exported types ------------------------------------------------------------*//* Exported variables --------------------------------------------------------*//* Exported constants --------------------------------------------------------*//* Exported macro ------------------------------------------------------------*//* Exported functions ------------------------------------------------------- */void SMBus_StartBit(void);void SMBus_StopBit(void);void SMBus_SendBit(uint8_t);uint8_t SMBus_SendByte(uint8_t);uint8_t SMBus_ReceiveBit(void);uint8_t SMBus_ReceiveByte(uint8_t);void SMBus_Delay(uint16_t);void SMBus_Init(void);uint16_t SMBus_ReadMemory(uint8_t, uint8_t);uint8_t PEC_Calculation(uint8_t*);float SMBus_ReadTemp(void); //获取温度值#endif/*********************************END OF FILE*********************************/

main.c(简略版)

#include "mlx90614.h"double temp;//温度变量浮点数int main(void){while (1){temp=SMBus_ReadTemp(); //读取温度HAL_Delay(500);printf("温度:%f\r\n",temp);}}

如果觉得《stm32使用cubemx生成HAL库工程驱动mlx90614》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。