久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標題:
基于STM32F103RC的MLX90614源代碼
[打印本頁]
作者:
lin1053742555
時間:
2019-12-20 19:57
標題:
基于STM32F103RC的MLX90614源代碼
實測可用
單片機源程序如下:
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "stdint.h"
#include "usart.h"
#include "mlx90614.h"
#include "GUI.h"
#include "Lcd_Driver.h"
#include "TFT_demo.h"
//STM32F103核心板例程
//庫函數版本例程
int main(void)
{
u8 lcd_id[12]; //存放LCD ID字符串
delay_init(); //延時函數初始化
NVIC_Configuration(); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
uart_init(9600); //串口初始化為9600
LED_Init(); //LED端口初始化
BEEP_Init();
Lcd_Init(); //1.44寸液晶屏--初始化配置
Lcd_Clear(GRAY0);//清屏
Gui_DrawFont_GBK16(0,16,RED,GRAY0," STM32 TFT 1.44 ");
Gui_DrawFont_GBK16(0,48,BLUE,GRAY0," 嵌入式開發網 ");
Gui_DrawFont_GBK16(0,64,BLUE,GRAY0,"mcudevcom ");
SMBus_Init();
float temp;
Lcd_Clear(GRAY0);//清屏
int a;
while(1)
{
temp = SMBus_ReadTemp();
printf("溫度 = %5.2f℃\r\n",temp);
sprintf((char*)lcd_id,"%5.2f",temp);
Gui_DrawFont_GBK16(0,10,BLUE,GRAY0,"Temperature:");
DisplayButtonUp(30,28,85,52); //x1,y1,x2,y2
Gui_DrawFont_GBK16(40,32,RED,GRAY0,lcd_id);//體溫顯示到TFT
delay_ms(1000);
a=(int)temp;
if(a<=30|a>=42)//啟動蜂鳴器報警
{
GPIO_SetBits(GPIOC,GPIO_Pin_14);
}
else GPIO_ResetBits(GPIOC,GPIO_Pin_14);
delay_ms(1000);
}
}
復制代碼
#include "mlx90614.h"
#define ACK 0
#define NACK 1
#define SA 0x00 //從機地址,單個MLX90614時地址為0x00,多個時地址默認為0x5a
#define RAM_ACCESS 0x00 //RAM access command
#define EEPROM_ACCESS 0x20 //EEPROM access command
#define RAM_TOBJ1 0x07 //To1 address in the eeprom
#define SMBUS_PORT GPIOB
#define SMBUS_SCK GPIO_Pin_6
#define SMBUS_SDA GPIO_Pin_7
#define RCC_APB2Periph_SMBUS_PORT RCC_APB2Periph_GPIOB
#define SMBUS_SCK_H() SMBUS_PORT->BSRR = SMBUS_SCK
#define SMBUS_SCK_L() SMBUS_PORT->BRR = SMBUS_SCK
#define SMBUS_SDA_H() SMBUS_PORT->BSRR = SMBUS_SDA
#define SMBUS_SDA_L() SMBUS_PORT->BRR = SMBUS_SDA
#define SMBUS_SDA_PIN() SMBUS_PORT->IDR & SMBUS_SDA //讀取引腳電平
/*******************************************************************************
* 函數名: SMBus_StartBit
* 功能 : 產生起始位
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StartBit(void)
{
SMBUS_SDA_H(); // Set SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Generate bus free time between Stop
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
}
/*******************************************************************************
* 函數名: SMBus_StopBit
* 功能: Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StopBit(void)
{
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SDA_L(); // Clear SDA line
SMBus_Delay(5); // Wait a few microseconds
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min)
SMBUS_SDA_H(); // Set SDA line
}
/*******************************************************************************
* 函數名: SMBus_SendByte
* 功能: Send a byte on SMBus
* Input : Tx_buffer
* Output : None
* Return : None
*******************************************************************************/
u8 SMBus_SendByte(u8 Tx_buffer)
{
u8 Bit_counter;
u8 Ack_bit;
u8 bit_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 SDA
Tx_buffer<<=1; // Get next bit for checking
}
Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bit
return Ack_bit;
}
/*******************************************************************************
* 函數名: SMBus_SendBit
* 功能: Send a bit on SMBus 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
if(bit_out==0)
{
SMBUS_SDA_L();
}
else
{
SMBUS_SDA_H();
}
SMBus_Delay(2); // Tsu:dat = 250ns minimum
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(6); // High Level of Clock Pulse
SMBUS_SCK_L(); // Clear SCL line
SMBus_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
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{
u8 Ack_bit;
SMBUS_SDA_H(); //引腳靠外部電阻上拉,當作輸入
SMBus_Delay(2); // High Level of Clock Pulse
SMBUS_SCK_H(); // Set SCL line
SMBus_Delay(5); // High Level of Clock Pulse
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L(); // Clear SCL line
SMBus_Delay(3); // Low Level of Clock Pulse
return Ack_bit;
}
/*******************************************************************************
* 函數名: SMBus_ReceiveByte
* 功能: Receive a byte on SMBus
* Input : ack_nack
* Output : None
* Return : RX_buffer
*******************************************************************************/
u8 SMBus_ReceiveByte(u8 ack_nack)
{
u8 RX_buffer;
u8 Bit_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_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack); // Sends acknowledgment bit
return RX_buffer;
}
/*******************************************************************************
* 函數名: SMBus_Delay
* 功能: 延時 一次循環約1us
* Input : time
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
u16 i, j;
for (i=0; i<4; i++)
{
for (j=0; j<time; j++);
}
}
/*******************************************************************************
* 函數名: SMBus_Init
* 功能: 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();
}
/*******************************************************************************
* 函數名: SMBus_ReadMemory
* 功能: READ DATA FROM RAM/EEPROM
* Input : slaveAddress, command
* Return : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
u16 data; // Data storage (DataH:DataL)
u8 Pec; // PEC byte storage
u8 DataL=0; // Low data byte storage
u8 DataH=0; // High data byte storage
u8 arr[6]; // Buffer for the sent bytes
u8 PecReg; // Calculated PEC byte storage
u8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614
ErrorCounter=0x00; // Initialising of ErrorCounter
slaveAddress <<= 1; //2-7位表示從機地址
do
{
repeat:
SMBus_StopBit(); //If slave send NACK stop comunication
--ErrorCounter; //Pre-decrement ErrorCounter
if(!ErrorCounter) //ErrorCounter=0?
{
break; //Yes,go out from do-while{}
}
SMBus_StartBit(); //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_StartBit(); //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_StopBit(); //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;
}
/*******************************************************************************
* 函數名: PEC_calculation
* 功能 : 數據校驗
* Input : pec[]
* Output : None
* Return : pec[0]-this byte contains calculated crc value
*******************************************************************************/
u8 PEC_Calculation(u8 pec[])
{
u8 crc[6];
u8 BitPosition=47;
u8 shift;
u8 i;
u8 j;
u8 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_ReadTemp
* 功能: 計算并返回溫度值
* Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{
return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
}
/*********************************END OF FILE*********************************/
復制代碼
所有資料51hei提供下載:
MLX90614.7z
(258.93 KB, 下載次數: 242)
2019-12-21 01:46 上傳
點擊文件名下載附件
整個工程文件
下載積分: 黑幣 -5
作者:
east_hong
時間:
2020-2-29 11:37
謝謝分享!!!
作者:
小白不想努力
時間:
2020-4-30 15:54
這個不外接電阻可以嗎
作者:
小白不想努力
時間:
2020-4-30 15:55
#define SMBUS_SCK_H() SMBUS_PORT->BSRR = SMBUS_SCK這個是怎么實現的
作者:
onono
時間:
2020-6-28 14:42
感謝分享,今天恰好在調試MLX90614。
作者:
acoollamata
時間:
2022-3-9 19:58
你好 為什么我的測出來一直是絕對零度呢
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
在线中文字幕视频
|
国产粉嫩尤物极品99综合精品
|
www.亚洲一区
|
国产精品一区二区欧美
|
日韩欧美国产精品
|
久草成人
|
日韩精品 电影一区 亚洲
|
国产精品极品美女在线观看免费
|
国产精品2区
|
午夜视频在线免费观看
|
日韩中文字幕免费在线观看
|
欧美黑人体内she精在线观看
|
99re6在线视频精品免费
|
久久99精品久久久
|
欧美一级淫片007
|
国产精品视频网
|
亚洲欧美在线观看
|
二区中文字幕
|
一区二区三区免费
|
特级特黄特色的免费大片
|
国产精品久久久久久吹潮
|
91精品国产91久久久久久最新
|
www.久久久.com
|
尹人av
|
人妖videosex高潮另类
|
国产成人综合在线
|
久久亚洲精品国产精品紫薇
|
xx性欧美肥妇精品久久久久久
|
精品国产一区探花在线观看
|
奇米久久
|
日本成人综合
|
av在线免费网站
|
免费成人在线网站
|
亚洲欧美综合精品久久成人
|
成人av鲁丝片一区二区小说
|
精品久久久久一区二区国产
|
www.青娱乐
|
在线一区视频
|
国产一区二区精品在线观看
|
亚洲精品短视频
|
一区二区三区视频在线观看
|