steam32仿真,温度控制电机正反转,串口和LCD打印信息
时间:2022-11-29 16:00:00
目录
运行结果
main.c
源代码连接
运行结果
main.c
#include "main.h" #include "gpio.h" #include "delay.h" #include "key.h" #include "usart1.h" #include "beep.h" #include "dht11.h" #include "lcd.h" #include "pic.h" #include "relay.h" #include "adc.h" #include "timer.h" #include #include void delay(u32 i) { while(i--); } //主函数 int main(void) { u8 T,H; int CCR; char buff[30]={0}; LED_Init(); //LED初始化 KEY_Init(); //按键初始化 BEEP_Init(); //蜂鸣器初始化 USART_Config(9600); //串口初始化 DHT11_Pin_Init(); //dht11初始化 LCD_Init(); //LCD初始化屏幕 Relay_Init(); ///继电器初始化 ADC_Init_Config(); //ADC初始化 PWM_Init(); //// TIM_SetCompare2(TIM3,800);//设置CCR数值 LCD_DrawBMP(0,0,128,8,(u8*)picture); LCD_Init(); delay(300); while(1) { DHT11_Data(&T, &H); //dht11初始化 printf("温度:%d\r\n",T); ///输出温度 printf("湿度:%d\r\n",H); //输出湿度 if(T > 30){ printf("温度大于30,电机正转!\r\n"); GPIO_SetBits(GPIOA,GPIO_Pin_7); ///电机正转 CCR=(T-30)*50; ///温差越大,CCR电机越大,正转越快 TIM_SetCompare2(TIM3,CCR); sprintf(buff,"CCR:M/1000",CCR); LCD_Dis_String(0,16,(const unsigned char*)buff); printf("温差:%d,CCR:%d,电机正转加速!\r\n",T-40,CCR); } if(T < 30){ printf("温度小于30,电机反转!\r\n"); GPIO_ResetBits(GPIOA,GPIO_Pin_7); ///电机反转 CCR=(30-T)*30 100;; ///温差越大,CCR电机反转越大越快,CCR最大为1000 TIM_SetCompare2(TIM3,CCR); sprintf(buff,"CCR:M/1000",CCR); LCD_Dis_String(0,16,(const unsigned char*)buff); printf("温差:%d,CCR:%d,电机反转加速!\r\n",30-T,CCR); } if(H > 80){ printf("打开蜂鸣器,湿度大于80!\r\n"); BEEP_ON; ///打开蜂鸣器 LCD_Init(); LCD_Dis_String(0,0,"上岸"); LCD_DrawBMP(0,0,112,8,(u8*)Image); delay(500); LCD_Init(); } if(H<80){ BEEP_OFF;//关闭蜂鸣器 } if(H < 40){ printf("湿度小于40,点亮四个LED!\r\n"); LED3_ON; //打开LED LED4_ON; LED5_ON; LED6_ON; } Delay_ms(20); } }
源代码连接
链接:https://pan.baidu.com/s/1DO7-RQyVQhpFoWtPBl2LFg
提取码:dhcz