找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1974|回复: 0
打印 上一主题 下一主题
收起左侧

stm32 18b20温度控制代码

[复制链接]
跳转到指定楼层
楼主
ID:220603 发表于 2017-7-18 17:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
stm32温度控制

单片机源程序如下:
  1. /*******************************************************************************
  2. // 斌超
  3. //2017/7/3
  4. *******************************************************************************/

  5. #include "config.h"
  6. #include "gui.h"

  7. int ch1=400,ch2=300,ch3=200,ch4=100;
  8. int Encoder_left;
  9. int Encoder_right;
  10. int stop_b;
  11. uint8_t showValue[7] = {0,0,0,0,0,0,0};
  12. uint8_t showTemp[2]={0,0};
  13. RTC_TimeTypeDef time;

  14. int temp_x=0,temp_g=0,temp_s=0,temp_shound=20;
  15. double temp;
  16. int temp_t,temp_pid_h,temp_pid_c,temp_pid;     //实际温度的十倍

  17. void GUI_DisplayTemp(uint8_t num);
  18. void TEMP_Set(uint8_t num, uint8_t state);
  19. float PID_h(float temp_pid_h);
  20. float PID_c(float temp_pid_c);

  21. float kp_h=10    ,  kp_c=10     ;
  22. float ki_h=0.01  ,  ki_c=0.01   ;
  23. float kd_h=0.1   ,  kd_c=0.1    ;


  24. /********************************************************************************************************/
  25. /********************************************************************************************************/


  26. int main()
  27. {

  28.         uint8_t keyValue;
  29.         uint8_t setState, m;
  30.         
  31.         
  32. //显示无高亮位置
  33.                
  34.         config();

  35.         

  36.     GUI_Show12Char(0, 105, "右键:进入或者退出设置模式", BLUE, BLACK  );
  37.                                  
  38.     GUI_Show12Char(0, 126, "左键:设置位置左移", BLUE, BLACK);

  39.     GUI_Show12Char(0, 147, "上键:设置位置数字加一", BLUE, BLACK);

  40.     GUI_Show12Char(0, 168, "下键:设置位置数字减一", BLUE, BLACK);


  41.                
  42.                            
  43.     setState = 0; //初始设置为普通模式,非设置模式
  44.     m = 0;        //显示无高亮位置
  45.         


  46.   while(1)               //轮子速度
  47.         {
  48.     temp=readtemp();  //读取温度
  49.           temp_t=temp*10;
  50. //    printf("当前温度为:%0.4lf ℃\r\n",temp);        
  51.                
  52.           keyValue = KEY_Scan();

  53.         /* 如果按键是右键,进入或者退出设置模式 */
  54.         if(keyValue == KEY_RIGHT)
  55.         {
  56.             if(setState == 0)
  57.             {
  58.                 setState = 1;     //进入设置模式
  59.             }
  60.             else                  //若先前已经为设置模式1则退出设置模式0
  61.             {
  62.                 setState = 0;     //退出设置模式
  63.             }
  64.             if(setState)  
  65.             {
  66.                 m = 1;     //进入设置模式则标志m为1位开始
  67.             }
  68.             else
  69.             {
  70.                 RTC_SetClock(&time);  //退出设置模式则更新时间
  71.                 m = 0;
  72.             }
  73.         }

  74.         /* 进入设置模式 */
  75.         if(setState == 1)
  76.         {
  77.             switch(keyValue)
  78.             {
  79.                 case(KEY_UP):      //上键高亮数字加1
  80.                     TEMP_Set(m, 1);
  81.                     break;
  82.                 case(KEY_DOWN):    //下键高亮数字减1
  83.                     TEMP_Set(m, 0);
  84.                     break;
  85.                 case(KEY_LEFT):    //左键高亮位置左移1位
  86.                     if(m == 3)
  87.                     {
  88.                         m = 1;
  89.                     }
  90.                     else
  91.                     {
  92.                         m++;
  93.                     }
  94.                     break;
  95.                 default:
  96.                     break;        
  97.             }
  98.         }
  99.         /* 普通模式显示时钟 */
  100.         else
  101.         {
  102.                                         //temp_shound=temp_t;//将当前温度赋值给变量
  103.                                         temp_shound=temp_x+(temp_g*10)+(temp_s*100);  //将设置温度的十倍赋值给变量
  104.         }
  105.                                 
  106.                                 
  107.     GUI_DisplayTemp(m);        //显示函数
  108. //                                                                GPIO_ResetBits(GPIOC, GPIO_Pin_LED);
  109.      
  110. //                                temp_pid=temp_shound-temp_t;
  111. //                                if(temp_pid>0)
  112. //                                {
  113. //                                        temp_pid_h=temp_pid;
  114. //                                        ch1=PID_h(temp_pid_h);
  115. //                                  TIM_SetCompare1(TIM1,ch1);
  116. //                                }
  117. //                                else if(temp_pid<0)
  118. //                                {
  119. //                                        temp_pid_c=(-temp_pid);
  120. //                                  TIM_SetCompare1(TIM1,ch4);
  121. //                                }
  122.                        
  123. //GPIO_SetBits(GPIOC, GPIO_Pin_LED);
  124. //delay_ms(1000);                                

  125.                                 
  126. //                Senor_Using();//超声波
  127. //                  end();
  128. // time = RTC_Time;       //读取时钟
  129. //GUI_DisplayTime(m);    //显示时钟               
  130. //Motor_Speed_L();
  131. //                if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0)
  132. //                {
  133. //                        stop();
  134. //                        delay_ms(2000);
  135. //                }
  136. //                //start();
  137.                 //stop();
  138. //   MoveQti();
  139. //    if(stop_b)        
  140. //                {
  141. //     end();
  142. //      while(1);
  143. //                }
  144. //    else ;
  145. //length_j();
  146. //printf("right:%d\n ",(int)Senor_Using());
  147.         }
  148. }        


  149. void GUI_DisplayTemp(uint8_t num)
  150. {
  151.    
  152.     /* 显示实时温度 */
  153.     showValue[0] = (temp_t /100) + '0';
  154.           //showValue[1] =  '';
  155.     showValue[1] = (temp_t % 100 / 10) + '0';
  156.           showValue[2] =  '.';
  157.     showValue[3] = (temp_t % 10) + '0';
  158.                 showValue[4] =  ' ';
  159.                 showValue[5] =  'C';
  160.     GUI_Show12Char(100, 50, showValue, YELLOW, BLACK);
  161.    
  162.     /* 显示温度十位 */
  163.     showTemp[0] = temp_s + '0';
  164.     if(num == 3)
  165.     {
  166.         GUI_Show12Char(100, 84, showTemp, GREEN, BLACK);
  167.     }
  168.     else
  169.     {
  170.         GUI_Show12Char(100, 84, showTemp, RED, BLACK);
  171.     }
  172.    
  173.     /* 显示小数点个位 */
  174.     showTemp[0] = temp_g+ '0';
  175.     if(num == 2)
  176.     {
  177.         GUI_Show12Char(112, 84, showTemp, GREEN, BLACK);
  178.     }
  179.     else
  180.     {
  181.         GUI_Show12Char(112, 84, showTemp, RED, BLACK);
  182.     }
  183.                 showTemp[0] = '.' ;
  184.     GUI_Show12Char(124, 84, showTemp, RED, BLACK);
  185.                
  186.     /* 显示小数点位 */
  187.     showTemp[0] =temp_x+ '0';
  188.     if(num == 1)
  189.     {
  190.         GUI_Show12Char(136, 84, showTemp, GREEN, BLACK);
  191.     }
  192.     else
  193.     {
  194.         GUI_Show12Char(136, 84, showTemp, RED, BLACK);        
  195.     }
  196.                 showTemp[0] = 'C' ;
  197.     GUI_Show12Char(148, 84, showTemp, RED, BLACK);
  198.                         showTemp[0] = ' ' ;
  199.     GUI_Show12Char(160, 84, showTemp, RED, BLACK);
  200.                
  201. }

  202. void TEMP_Set(uint8_t num, uint8_t state)
  203. {
  204.     switch(num)
  205.     {   
  206.         /* 高亮部分为小数点后一位*/
  207.         case(1):         
  208.             if(state)               
  209.             {
  210.                 if(temp_x == 9)
  211.                 {
  212.                     temp_x = 0;
  213.                 }
  214.                 else
  215.                 {
  216.                     temp_x++;
  217.                 }   
  218.             }
  219.             else
  220.             {
  221.                 if(temp_x == 0)
  222.                 {
  223.                     temp_x = 9;
  224.                 }
  225.                 else
  226.                 {
  227.                     temp_x--;
  228.                 }   
  229.             }
  230.             break;
  231.         /* 高亮部分为温度个位数*/
  232.         case(2):        
  233.             if(state)
  234.             {
  235.                 if(temp_g == 9)
  236.                 {
  237.                     temp_g = 0;
  238.                 }
  239.                 else
  240.                 {
  241.                     temp_g++;
  242.                 }   
  243.             }
  244.             else
  245.             {
  246.                 if(temp_g == 0)
  247.                 {
  248.                     temp_g = 9;
  249.                 }
  250.                 else
  251.                 {
  252.                     temp_g--;
  253.                 }   
  254.             }
  255.             break;
  256.         /* 高亮部分为稳定十位*/
  257.         case(3):
  258.             if(state)
  259.             {
  260.                 if(temp_s == 9)
  261.                 {
  262.                     temp_s = 0;
  263.                 }
  264.                 else
  265.                 {
  266.                     temp_s++;
  267.                 }   
  268.             }
  269.             else
  270.             {
  271. ……………………

  272. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
STM32代码.7z (245.19 KB, 下载次数: 19)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表