|
已調(diào)試
智能溫控方案簡(jiǎn)介
主要功能:
本系統(tǒng)采用STM32f103c8t6作為主控芯片,能對(duì)DS18B20采集到的信息進(jìn)行解算,并將結(jié)果顯示在OLED顯示屏上面,并可以通過兩個(gè)按鍵實(shí)現(xiàn)設(shè)定溫度加減到設(shè)定溫度。
具體方案:
首先:
OLED接線:CS:PA6
RST:PC15
RS:PA3
SCL:PB0
SDA:PB1
DS18B20接線:PC13
按鍵:PA2.PA4
單片機(jī)源程序如下:
- #include "stm32f10x.h"
- #include "delay.h"
- #include "LED.h"
- #include "oled.h"
- #include "ds18b20.h"
- #include "usart.h"
- #include "key.h"
- #include "stm32f10x_tim.h"
- unsigned int speed_count=0;//占空比計(jì)數(shù)器 50次一周期
- static void NVIC_TIM2Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Set the Vector Table base address at 0x08000000 */
- //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);
- /* Enable the TIM5 gloabal Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void TIM2_Init(void)
- { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- /* TIM2 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- /* Time base configuration */
- //這個(gè)就是自動(dòng)裝載的計(jì)數(shù)值,由于計(jì)數(shù)是從0開始的,周期為100us
- TIM_TimeBaseStructure.TIM_Period = (100 - 1);//10kHz
- // 這個(gè)就是預(yù)分頻系數(shù),當(dāng)由于為0時(shí)表示不分頻所以要減1
- TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);//1MHz
- // 高級(jí)應(yīng)用本次不涉及。定義在定時(shí)器時(shí)鐘(CK_INT)頻率與數(shù)字濾波器(ETR,TIx)
- // 使用的采樣頻率之間的分頻比例
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- //向上計(jì)數(shù)
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- //初始化定時(shí)器5
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- /* Clear TIM5 update pending flag[清除TIM5溢出中斷標(biāo)志] */
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- /* TIM IT enable */ //打開溢出中斷
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- /* TIM5 enable counter */
- TIM_Cmd(TIM2, ENABLE); //計(jì)數(shù)器使能,開始工作
- /* 中斷參數(shù)配置 */
- NVIC_TIM2Configuration();
- }
- int main(void)
- {
- u8 a=0;u8 b=0; u8 c=0; int temp=0;
- u8 set=20;
- u8 shiwei=0;
- u8 gewei=0;
- float temperature;
- Init_LEDpin();
- delay_init();
- OLED_Init(); //初始化OLED
- DS18B20_Init(); //PC13
- uart_init(9600);
- KEY_Init();//按鍵初始化PA2 PA4
- TIM2_Init();//電機(jī)運(yùn)動(dòng)PWM定時(shí)器
- // OLED_ShowString(0,0, "");
- // OLED_ShowString(0,16,"imodule");
- OLED_ShowString(0,32,"NowTemp:");
- OLED_ShowString(0,48,"SetTemp:");
- // OLED_ShowString(63,48,"CODE:"); OLED_ShowNum(63,32,3,1,16);
- // OLED_ShowChar(63,50,'r',1,1); OLED_ShowNum(63,32,34,2,16);
- OLED_Refresh_Gram();
-
- while(1)
- {
- if( !S1)
- {
- delay_ms(10);
- if(!S1)
-
- {
- while( !S1);
- set+=1;
- shiwei=set/10;
- gewei=set%10;
- OLED_ShowNum(63,48,shiwei,1,16);
- OLED_ShowNum(71,48,gewei,1,16);
- OLED_ShowChar(79,48,'.',16,1);
- OLED_ShowNum(87,48,0,1,16);
- OLED_ShowChar(95,48,'^',16,1);
- OLED_ShowChar(103,48,'C',16,1);
- OLED_Refresh_Gram();
- printf("temperature:");
- }
- }
-
- if(!S2)
- {
- delay_ms(10);
- if(!S2)
-
- {
- while(!S2);
- set-=1;
- shiwei=set/10;
- gewei=set%10;
- OLED_ShowNum(63,48,shiwei,1,16);
- OLED_ShowNum(71,48,gewei,1,16);
- OLED_ShowChar(79,48,'.',16,1);
- OLED_ShowNum(87,48,0,1,16);
- OLED_ShowChar(95,48,'^',16,1);
- OLED_ShowChar(103,48,'C',16,1);
- OLED_Refresh_Gram();
- printf("temp:");
-
- }
- }
-
- if( speed_count >= 500)
- {
- speed_count = 0;
-
-
- temperature=DS18B20_Get_Temp();
- temp=(int)temperature;
-
- a=temp/100;
- b=(temp/10)%10;
- c=temp%10;
- OLED_ShowNum(63,32,a,1,16);
- OLED_ShowNum(71,32,b,1,16);
- OLED_ShowChar(79,32,'.',16,1);
- OLED_ShowNum(87,32,c,1,16);
- OLED_ShowChar(95,32,'^',16,1);
- OLED_ShowChar(103,32,'C',16,1);
- OLED_Refresh_Gram();
- printf("temperature=%.2f \n",temperature/10);
-
- }
- }
-
-
-
-
-
- }
復(fù)制代碼
所有資料51hei提供下載:
基于STM32實(shí)現(xiàn)的溫度控制系統(tǒng)OLED顯示.7z
(203.27 KB, 下載次數(shù): 335)
2019-6-22 02:58 上傳
點(diǎn)擊文件名下載附件
|
|