标题:
stm32 18b20温度控制代码
[打印本页]
作者:
heroybc
时间:
2017-7-18 17:32
标题:
stm32 18b20温度控制代码
stm32温度控制
单片机源程序如下:
/*******************************************************************************
// 斌超
//2017/7/3
*******************************************************************************/
#include "config.h"
#include "gui.h"
int ch1=400,ch2=300,ch3=200,ch4=100;
int Encoder_left;
int Encoder_right;
int stop_b;
uint8_t showValue[7] = {0,0,0,0,0,0,0};
uint8_t showTemp[2]={0,0};
RTC_TimeTypeDef time;
int temp_x=0,temp_g=0,temp_s=0,temp_shound=20;
double temp;
int temp_t,temp_pid_h,temp_pid_c,temp_pid; //实际温度的十倍
void GUI_DisplayTemp(uint8_t num);
void TEMP_Set(uint8_t num, uint8_t state);
float PID_h(float temp_pid_h);
float PID_c(float temp_pid_c);
float kp_h=10 , kp_c=10 ;
float ki_h=0.01 , ki_c=0.01 ;
float kd_h=0.1 , kd_c=0.1 ;
/********************************************************************************************************/
/********************************************************************************************************/
int main()
{
uint8_t keyValue;
uint8_t setState, m;
//显示无高亮位置
config();
GUI_Show12Char(0, 105, "右键:进入或者退出设置模式", BLUE, BLACK );
GUI_Show12Char(0, 126, "左键:设置位置左移", BLUE, BLACK);
GUI_Show12Char(0, 147, "上键:设置位置数字加一", BLUE, BLACK);
GUI_Show12Char(0, 168, "下键:设置位置数字减一", BLUE, BLACK);
setState = 0; //初始设置为普通模式,非设置模式
m = 0; //显示无高亮位置
while(1) //轮子速度
{
temp=readtemp(); //读取温度
temp_t=temp*10;
// printf("当前温度为:%0.4lf ℃\r\n",temp);
keyValue = KEY_Scan();
/* 如果按键是右键,进入或者退出设置模式 */
if(keyValue == KEY_RIGHT)
{
if(setState == 0)
{
setState = 1; //进入设置模式
}
else //若先前已经为设置模式1则退出设置模式0
{
setState = 0; //退出设置模式
}
if(setState)
{
m = 1; //进入设置模式则标志m为1位开始
}
else
{
RTC_SetClock(&time); //退出设置模式则更新时间
m = 0;
}
}
/* 进入设置模式 */
if(setState == 1)
{
switch(keyValue)
{
case(KEY_UP): //上键高亮数字加1
TEMP_Set(m, 1);
break;
case(KEY_DOWN): //下键高亮数字减1
TEMP_Set(m, 0);
break;
case(KEY_LEFT): //左键高亮位置左移1位
if(m == 3)
{
m = 1;
}
else
{
m++;
}
break;
default:
break;
}
}
/* 普通模式显示时钟 */
else
{
//temp_shound=temp_t;//将当前温度赋值给变量
temp_shound=temp_x+(temp_g*10)+(temp_s*100); //将设置温度的十倍赋值给变量
}
GUI_DisplayTemp(m); //显示函数
// GPIO_ResetBits(GPIOC, GPIO_Pin_LED);
// temp_pid=temp_shound-temp_t;
// if(temp_pid>0)
// {
// temp_pid_h=temp_pid;
// ch1=PID_h(temp_pid_h);
// TIM_SetCompare1(TIM1,ch1);
// }
// else if(temp_pid<0)
// {
// temp_pid_c=(-temp_pid);
// TIM_SetCompare1(TIM1,ch4);
// }
//GPIO_SetBits(GPIOC, GPIO_Pin_LED);
//delay_ms(1000);
// Senor_Using();//超声波
// end();
// time = RTC_Time; //读取时钟
//GUI_DisplayTime(m); //显示时钟
//Motor_Speed_L();
// if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0)
// {
// stop();
// delay_ms(2000);
// }
// //start();
//stop();
// MoveQti();
// if(stop_b)
// {
// end();
// while(1);
// }
// else ;
//length_j();
//printf("right:%d\n ",(int)Senor_Using());
}
}
void GUI_DisplayTemp(uint8_t num)
{
/* 显示实时温度 */
showValue[0] = (temp_t /100) + '0';
//showValue[1] = '';
showValue[1] = (temp_t % 100 / 10) + '0';
showValue[2] = '.';
showValue[3] = (temp_t % 10) + '0';
showValue[4] = ' ';
showValue[5] = 'C';
GUI_Show12Char(100, 50, showValue, YELLOW, BLACK);
/* 显示温度十位 */
showTemp[0] = temp_s + '0';
if(num == 3)
{
GUI_Show12Char(100, 84, showTemp, GREEN, BLACK);
}
else
{
GUI_Show12Char(100, 84, showTemp, RED, BLACK);
}
/* 显示小数点个位 */
showTemp[0] = temp_g+ '0';
if(num == 2)
{
GUI_Show12Char(112, 84, showTemp, GREEN, BLACK);
}
else
{
GUI_Show12Char(112, 84, showTemp, RED, BLACK);
}
showTemp[0] = '.' ;
GUI_Show12Char(124, 84, showTemp, RED, BLACK);
/* 显示小数点位 */
showTemp[0] =temp_x+ '0';
if(num == 1)
{
GUI_Show12Char(136, 84, showTemp, GREEN, BLACK);
}
else
{
GUI_Show12Char(136, 84, showTemp, RED, BLACK);
}
showTemp[0] = 'C' ;
GUI_Show12Char(148, 84, showTemp, RED, BLACK);
showTemp[0] = ' ' ;
GUI_Show12Char(160, 84, showTemp, RED, BLACK);
}
void TEMP_Set(uint8_t num, uint8_t state)
{
switch(num)
{
/* 高亮部分为小数点后一位*/
case(1):
if(state)
{
if(temp_x == 9)
{
temp_x = 0;
}
else
{
temp_x++;
}
}
else
{
if(temp_x == 0)
{
temp_x = 9;
}
else
{
temp_x--;
}
}
break;
/* 高亮部分为温度个位数*/
case(2):
if(state)
{
if(temp_g == 9)
{
temp_g = 0;
}
else
{
temp_g++;
}
}
else
{
if(temp_g == 0)
{
temp_g = 9;
}
else
{
temp_g--;
}
}
break;
/* 高亮部分为稳定十位*/
case(3):
if(state)
{
if(temp_s == 9)
{
temp_s = 0;
}
else
{
temp_s++;
}
}
else
{
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
STM32代码.7z
(245.19 KB, 下载次数: 19)
2022-10-17 20:56 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1