【STM32】简易测量温度装置

  前段时间用STM32做了个简易温度测量装置,下面简单介绍一下:

所用模块:

STM32F103C8T6最小系统板

MLX90614红外测温模块

矩阵键盘

显示模块

功能实现

MLX90614.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

#include "mlx90614.h"
#define ACK         0
#define NACK        1
#define SA                        0x00 
#define RAM_ACCESS                0x00 
#define EEPROM_ACCESS             0x20 
#define RAM_TOBJ1                 0x07 
 
#define SMBUS_PORT               GPIOB
#define SMBUS_SCK                GPIO_Pin_15
#define SMBUS_SDA                GPIO_Pin_14
 
#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 
 
void SMBus_StartBit(void)
{
    SMBUS_SDA_H();                // Set SDA line
    SMBus_Delay(8);            // Wait a few microseconds
    SMBUS_SCK_H();                // Set SCL line
    SMBus_Delay(8);            // Generate bus free time between Stop
    SMBUS_SDA_L();                // Clear SDA line
    SMBus_Delay(8);            // 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(8);            // Wait a few microseconds
}
 
void SMBus_StopBit(void)
{
    SMBUS_SCK_L();                // Clear SCL line
    SMBus_Delay(8);            // Wait a few microseconds
    SMBUS_SDA_L();                // Clear SDA line
    SMBus_Delay(8);            // Wait a few microseconds
    SMBUS_SCK_H();                // Set SCL line
    SMBus_Delay(8);            // Stop condition setup time(Tsu:sto=4.0us min)
    SMBUS_SDA_H();                // Set SDA line
}
 
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;
}
 
void SMBus_SendBit(u8 bit_out)
{
    if(bit_out==0)
    {
        SMBUS_SDA_L();
    }
    else
    {
        SMBUS_SDA_H();
    }
    SMBus_Delay(6);                                        // Tsu:dat = 250ns minimum
    SMBUS_SCK_H();                                        // Set SCL line
    SMBus_Delay(8);                                        // High Level of Clock Pulse
    SMBUS_SCK_L();                                        // Clear SCL line
    SMBus_Delay(8);                                        // Low Level of Clock Pulse
    return;
}
 
u8 SMBus_ReceiveBit(void)
{
    u8 Ack_bit;
 
    SMBUS_SDA_H();          
        SMBus_Delay(6);                        // High Level of Clock Pulse
    SMBUS_SCK_H();                        // Set SCL line
    SMBus_Delay(8);                        // High Level of Clock Pulse
    if (SMBUS_SDA_PIN())
    {
        Ack_bit=1;
    }
    else
    {
        Ack_bit=0;
    }
    SMBUS_SCK_L();                        // Clear SCL line
    SMBus_Delay(8);                        // Low Level of Clock Pulse
 
    return        Ack_bit;
}
 
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;
}
 
void SMBus_Delay(u16 time)
{
    u16 i, j;
    for (i=0; i<5; i++)
    {
        for (j=0; j<time; j++);
    }
}
 
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();
}
 
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;
}
 
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];
}
 
float SMBus_ReadTemp(void)
{   
        return (SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15);
}

MLX90614.h 

#ifndef __MLX90614_H
#define __MLX90614_H
 
#include "stm32f10x.h"
void SMBus_StartBit(void);
void SMBus_StopBit(void);
void SMBus_SendBit(u8);
u8 SMBus_SendByte(u8);
u8 SMBus_ReceiveBit(void);
u8 SMBus_ReceiveByte(u8);
void SMBus_Delay(u16);
void SMBus_Init(void);
u16 SMBus_ReadMemory(u8, u8);
u8 PEC_Calculation(u8*);
float SMBus_ReadTemp(void); //?????
#endif

main.c

#include "MAIN.H"  
#include "stm32f10x.h"                  
#include "Delay.h"
#include "OLED.h"         
#include "mlx90614.h"



float Temperature;
float UP_Limit;
unsigned int Mode;
unsigned time;
int main(void)
{
   
   
                        
    OLED_Init();

    while(1)
    {   int key_val = 0;
		UP_Limit=22.00;
		Mode=0;
		KEY_GPIO_Config();
		key_val = KK_scan();
        Temperature=SMBus_ReadTemp();
		
        if(key_val == 1)
            Mode = ~Mode        ;
        if(Mode==0)//????,?2?
        {
            OLED_ShowString(1,2,"BODY");   
            Temperature += 2;  //????
        }
        else
        {
            OLED_ShowString(1,2,"OBJ");    
        }
        
        OLED_ShowString(1,6,"Temper:"); 
		OLED_ShowNum(2, 1, Temperature, 2);  
        OLED_ShowNum(2, 4, (unsigned long)(Temperature*100)%100, 2); 

        
        if(key_val == 2)
            UP_Limit = UP_Limit+0.01;
        else if(key_val == 3)
            UP_Limit = UP_Limit-0.01;
        else if(key_val == 4)
            UP_Limit = UP_Limit+1;
        else if(key_val == 5)
            UP_Limit = UP_Limit-1;

        
        if(UP_Limit<30.00)
        {
            UP_Limit=30.00;
        }
        if(UP_Limit>46.00)
        {
            UP_Limit=46.00;   
        }
         
        
        OLED_ShowString(3,1,"UP_Limit"); 
        OLED_ShowNum(3, 10,(unsigned long)UP_Limit ,2);
        
        if(Temperature<UP_Limit)
        {
             SMBus_Init();

            OLED_ShowString(1,1,"    ");     
        }
        else
        {           
            OLED_ShowString(1,1,"Waring!");
        }
        
        Delay_ms(100);
        
    }
}
        

由于本人接触嵌入式并不久,而且刚尝试写博客,如有缺陷和不足请谅解,并提上您宝贵的意见。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32简易RCL测量仪是一款基于STM32单片机的简单电路参数测量仪器。它主要用于测量电阻、电容和电感的数值并显示在液晶屏上。 该测量仪的主要原理是利用STM32单片机的ADC(模数转换器)功能来测量电路中的电压。通过将待测元件(电阻、电容或电感)连接到测量仪的测试端口上,待测元件的电压信号会经过电压分压电路降低到STM32单片机ADC输入电压范围内,然后通过ADC进行采样和转换。 在测量阻值时,电路中的电压分压电阻会根据待测电阻和已知参考电阻组成的电压分压比例将待测电阻的电压信号转化为ADC输入电压,然后通过软件计算即可得到待测电阻的阻值。 在测量电容时,通过外部电路将待测电容与参考电容并联,利用充放电过程测量电压的变化。测量仪通过对待测电容进行恒流充电并记录电压的变化曲线,根据相关公式进行计算即可得到待测电容的电容值。 在测量电感时,电路中的待测电感与所选参考电感串联,利用自谐振法或LC谐振法进行测量测量仪通过调节外部电路的频率来达到电感的谐振状态,然后通过测量电感的谐振频率即可得到待测电感的电感值。 通过以上测量原理,STM32简易RCL测量仪可以准确测量电路中的电阻、电容和电感值,并将结果通过液晶屏显示出来,提供给用户参考。同时,该测量仪还具有体积小巧、操作简便等特点,是电子爱好者和学生学习电路基础知识和调试电路的理想工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值