打印

基于F4探索版的简易温度控制器

[复制链接]
894|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
为了学校比赛做的一个练习,使用了正点原子探索版的工程模板。
目前具备以下功能:
1)可以对水泥电阻进行加热,加热的温升速率 25 度/分钟,加热温度达到75 度,同时保持温度恒定,恒定时间为 2 分钟,恒定温度偏差为±2 度。
2)mlx90614温度测试,在lcd屏上动态显示,可以实时显示加热的温度和温度曲线,温度显示精度为 0.5 度。

3)可以实时显示加热输出的电流、电压。  

4)可以实时显示加热输出功率的大小。  

程序框架大概如下:
在main.c中初始化所用到的外设,死循环查询TIM3中断标志,中断1s触发1次,中断服务函数中通过SMBus读取温度并显示,将温度值换算为屏幕上的纵坐标,横坐标每秒自增1,同时描出折线。每次中断运算一次pid,若当前温度小于标称温度减10,直接使用预设功率加热;若大于,将参数传递到TIM8的PWM输出口起到控制加热的目的。(不好意思用语不太专业,新手初来乍到多包涵!)pid运算的各参数预先调试后写死在函数里,正在尝试添加触摸屏按键实现动态参数pid运算,这样温升速率、标定温度都可调。
基本就是这样,贴出部分源码和调试结果,驱动控制电路从其他地方查资料修改后焊的洞洞板,SMBus驱动也是找的人家的自己修改了一下,有兴趣的小伙伴大家多探讨,目前还比较简陋,欢迎前辈、各路大神来提出建议!

/*******************************************************************************
* 文件名        : main.c
* 作  者        :
* 版  本        :
* 日  期        : 2019-03-11
* 描  述        :
*******************************************************************************/


#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "lcd.h"
#include "mlx90614.h"
#include "pid.h"
#include "timer.h"
#include "pwm.h"
#include "lcdtask.h"

int main(void)
{  
        extern PID pid;
        
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
        delay_init(168);//初始化延时函数
        uart_init(115200);//初始化串口波特率为115200        
        
         LCD_Init();        
        LCD_ShowBackground();//屏幕背景等
        
        SMBus_Init();//传感器通讯设置
        
        PID_Init();
        
        TIM3_Int_Init(10000-1,8400-1);//定时器时钟84M,分频系数8400,所以84M/8400=10Khz的计数频率,计数10000次为1s
        TIM5_PWM_Init(1000-1,84-1);//84M/84=1Mhz的计数频率,重装载值1000,所以PWM频率为 1M/1000=1Khz.     
//        TIM_SetCompare1(TIM5,0);//修改比较值,修改占空比
         
        while(1)
        {                        
            //查询中断标志位
        }
}


/*******************************************************************************
* 文件名        : mlx90614.c
* 作  者        :
* 版  本        :
* 日  期        : 2019-02-17
* 描  述        : mlx90614函数
PB6:SCL
PB7:SDA
在主函数中先初始化SMBus_Init();
需要读取温度就调用temp=SMBus_ReadTemp();  //读取温度,temp是浮点数,转整数:i=ceil(temp);
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/

#include "mlx90614.h"
#include "delay.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ACK         0 //应答
#define        NACK 1 //无应答
#define SA                                0x00 //Slave address 单个MLX90614时地址为0x00,多个时地址默认为0x5a
#define RAM_ACCESS                0x00 //RAM access command RAM存取命令
#define EEPROM_ACCESS        0x20 //EEPROM access command EEPROM存取命令
#define RAM_TOBJ1                0x07 //To1 address in the eeprom 目标1温度,检测到的红外温度 -70.01 ~ 382.19度

#define SMBUS_PORT        GPIOG      //PB端口(端口和下面的两个针脚可自定义)
#define SMBUS_SCK                GPIO_Pin_6 //PB6:SCL
#define SMBUS_SDA                GPIO_Pin_7 //PB7:SDA

#define RCC_AHB1Periph_SMBUS_PORT                RCC_AHB1Periph_GPIOG

#define SMBUS_SCK_H()            SMBUS_PORT->BSRRL = SMBUS_SCK //置高电平
#define SMBUS_SCK_L()            SMBUS_PORT->BSRRH = SMBUS_SCK  //置低电平
#define SMBUS_SDA_H()            SMBUS_PORT->BSRRL = SMBUS_SDA
#define SMBUS_SDA_L()            SMBUS_PORT->BSRRH = SMBUS_SDA

#define SMBUS_SDA_PIN()            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_OUTMode();
        SMBus_Delay(5);        
        
    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)在SCK=1时,检测到SDA由1到0表示通信开始(下降沿)
    SMBUS_SCK_L();            // Clear SCL line
    SMBus_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_SDA_OUTMode();
        SMBus_Delay(5);        
        
    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在SCK=1时,检测到SDA由0到1表示通信结束(上升沿)
}

/*******************************************************************************
* Function Name  : SMBus_SendByte
* Description    : 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;
}

/*******************************************************************************
* Function Name  : SMBus_SendBit
* Description    : Send a bit on SMBus 82.5kHz
* Input          : bit_out
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{
        SMBus_SDA_OUTMode();
        SMBus_Delay(5);        
        
    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_INMode();
        SMBus_Delay(5);        

    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;
}

/*******************************************************************************
* Function Name  : SMBus_ReceiveByte
* Description    : 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;
}

/*******************************************************************************
* Function Name  : SMBus_Delay
* Description    : 延时  一次循环约1us
* Input          : time
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{
    delay_us(time);  
}

/*******************************************************************************
* Function Name  : SMBus_Init
* Description    : SMBus初始化
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Init()
{
    GPIO_InitTypeDef    GPIO_InitStructure;

        /* Enable SMBUS_PORT clocks */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_SMBUS_PORT, ENABLE);

    /*配置SMBUS_SCK、SMBUS_SDA为集电极开漏输出(后改为推挽输出)*/

        
        GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    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
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{
    u16 data;                        // Data storage (DataHataL)
    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=DataHataL
    return 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
*******************************************************************************/
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&(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<<=1;
                crc+=temp;
            }/*End of for*/
            shift--;
        }/*End of while*/

        /*Exclusive OR between pec and crc*/
        for(i=0; i<=5; i++)
        {
            pec ^=crc;
        }/*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
*******************************************************************************/
u16 SMBus_ReadTemp(void)
{   
        float temp;
        temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1);//*0.02-273.15
        return temp;
}

/*********************************END OF FILE*********************************/

void SMBus_SDA_INMode()
{
                GPIO_InitTypeDef    GPIO_InitStructure;
        
                GPIO_InitStructure.GPIO_Pin = SMBUS_SDA;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
                GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
                GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);//初始化
               
}
void SMBus_SDA_OUTMode()
{
                GPIO_InitTypeDef  GPIO_InitStructure;
               
                GPIO_InitStructure.GPIO_Pin =SMBUS_SDA;
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
                GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
                GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);//初始化
}


/*******************************************************************************
* 文件名        : pid.c
* 作  者        :
* 版  本        :
* 日  期        : 2019-03-11
* 描  述        :
*******************************************************************************/


#include "pid.h"
#include "mlx90614.h"

PID pid;

void PID_Init()
{
//        pid.Sv=75;
//        pid.Kp=20;
//        pid.T=1000;
//        pid.Ti=5000;
//        pid.Td=1200;
//        pid.OUT0=600;
        
        pid.Sv=75;
        pid.Hv=636;
        pid.Kp=56;
        pid.T=100;
        pid.Ti=100000;
        pid.Td=6;
        pid.OUT0=10;
        
}

void PID_Cal(void)
{
        float  DelEk;
        float  ti,ki;
        float Pout,Iout,Dout;
        float kd,td;

        pid.Ek=pid.Sv-pid.Pv;//当前偏差值
        Pout=pid.Kp*pid.Ek;//比例输出

        pid.SEk+=pid.Ek;//运算时间区间上偏差积分
        DelEk=pid.Ek-pid.Ek_1;//偏差微分
        
        ti=pid.T/pid.Ti;//积分时间
        ki=ti*pid.Kp;//积分系数
        Iout=ki*pid.SEk*pid.Kp;//积分项
        
        td=pid.Td/pid.T;//微分时间
        kd=pid.Kp*td;//微分系数
        
        Dout=kd*DelEk;//微分项
        pid.Ek_1=pid.Ek;//误差迭代
        
        pid.OUT=Pout+Iout+Dout+pid.OUT0;//全项
        
         
}

/*******************************************************************************
* 文件名        : lcdtask.c
* 作  者        :
* 版  本        :
* 日  期        : 2019-03-11
* 描  述        :
*******************************************************************************/


#include "lcdtask.h"

void LCD_ShowBackground(void)
{
    //以下设置屏幕
    POINT_COLOR=RED;//设置字体为红色
        LCD_ShowString(30,50,400,16,16,"Welcome!This is a simple heater by L.JP!");        
        
        POINT_COLOR=BLACK ;//设置字体为黑色
        // void LCD_ShowString(u16 x,u16 y,u16 width,u16 height,u8 size,u8 *p)
         LCD_ShowString(30,70,200,16,16,"Temp:      .   C");        
        LCD_ShowString(30,90,200,16,16,"Time:   min    s");        
//         LCD_ShowString(260,70,200,16,16,"Vol:       V");
//        LCD_ShowString(260,90,200,16,16,"Cur:       A");
//        LCD_ShowString(260,110,200,16,16,"Heating Power:    W");
                                
        LCD_ShowString(260,70,200,16,16,"Vol:   .  V");
        LCD_ShowString(260,90,200,16,16,"Cur:  .    A");
        LCD_ShowString(260,110,200,16,16,"Heating Power:   .  W");
        //画线
    //x1,y1:起点坐标
    //x2,y2:终点坐标  
    //void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
        LCD_DrawLine(30, 150, 30, 700);//时间轴30到450(420),温度轴150到700(550),时间跨度5min(300s),温度跨度75C
        LCD_DrawLine(30, 700, 450, 700);//1个像素1s,3个像素0.5度
        LCD_ShowString(420,720,200,12,12,"Time(min)");
        LCD_ShowString(30,130,200,12,12,"Temp(C)");
        
        LCD_DrawLine(90, 150, 90, 700);LCD_DrawLine(150, 150,150, 700);LCD_DrawLine(210, 150, 210, 700);LCD_DrawLine(270, 150, 270, 700);
        LCD_DrawLine(330, 150, 330, 700);LCD_DrawLine(390, 150, 390, 700);LCD_DrawLine(450, 150, 450, 700);//横分格
        LCD_ShowString(30,710,200,12,12,"0");
        LCD_ShowString(90,710,200,12,12,"1");
        LCD_ShowString(150,710,200,12,12,"2");
        LCD_ShowString(210,710,200,12,12,"3");
        LCD_ShowString(270,710,200,12,12,"4");
        LCD_ShowString(330,710,200,12,12,"5");
        LCD_ShowString(390,710,200,12,12,"6");
        LCD_ShowString(450,710,200,12,12,"7");
        
        LCD_DrawLine(30, 550, 450, 550);LCD_DrawLine(30, 400, 450, 400);LCD_DrawLine(30, 250, 450, 250);LCD_DrawLine(30, 150, 450, 150);//竖分格
        LCD_ShowString(10,550,200,12,12,"25");
        LCD_ShowString(10,400,200,12,12,"50");
        LCD_ShowString(10,250,200,12,12,"75");
        LCD_ShowString(5,150,200,12,12,"91.7");
    POINT_COLOR=BLUE;//设置字体为蓝色

}

































QQ图片20190311202448.jpg

(5.02 MB, 下载次数: 24)









下载附件







2019-3-11 20:26 上传

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

370

主题

370

帖子

0

粉丝