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
|