已调试
智能温控方案简介
主要功能:
本系统采用STM32f103c8t6作为主控芯片,能对DS18B20采集到的信息进行解算,并将结果显示在OLED显示屏上面,并可以通过两个按键实现设定温度加减到设定温度。
具体方案:
首先:
OLED接线:CS:PA6
RST:PC15
RS:PA3
SCL:PB0
SDA:PB1
DS18B20接线:PC13
按键:PA2.PA4
单片机源程序如下:
- #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;//占空比计数器 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 */
- //这个就是自动装载的计数值,由于计数是从0开始的,周期为100us
- TIM_TimeBaseStructure.TIM_Period = (100 - 1);//10kHz
- // 这个就是预分频系数,当由于为0时表示不分频所以要减1
- TIM_TimeBaseStructure.TIM_Prescaler = (72 - 1);//1MHz
- // 高级应用本次不涉及。定义在定时器时钟(CK_INT)频率与数字滤波器(ETR,TIx)
- // 使用的采样频率之间的分频比例
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- //向上计数
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- //初始化定时器5
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- /* Clear TIM5 update pending flag[清除TIM5溢出中断标志] */
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- /* TIM IT enable */ //打开溢出中断
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
- /* TIM5 enable counter */
- TIM_Cmd(TIM2, ENABLE); //计数器使能,开始工作
- /* 中断参数配置 */
- 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();//电机运动PWM定时器
- // 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);
-
- }
- }
-
-
-
-
-
- }
复制代码
所有资料51hei提供下载:
基于STM32实现的温度控制系统OLED显示.7z
(203.27 KB, 下载次数: 335)
|