STM32F103C6T6电机控制系统:L9110H、PWM、矩阵键盘、DHT11、红外对射式测速
Altium Designer画的PCB图如下:(51hei附件中可下载工程文件)
单片机源程序如下:
- #include "stm32f10x.h" // Device header
- #include "Delay.h"
- #include "OLED.h"
- #include "MatrixKey.h"
- #include "Infrared.h"
- #include "Timer.h"
- #include "Motor.h"
- #include "PWM.h"
- #include "DHT11.h"
- unsigned char KeyNum=0,x=50,tempH,humiH;
- uint16_t count=0,Speed=0,countdown=0;
- unsigned int i,time,flag,flag2=0;
- float pid_p=3,pid_i=2,pid_d=1;
- unsigned int lastError=0;
- unsigned char pid_val_mid;//pid_val_mid脉冲宽度
- long int sumError=0;//sum偏差和
- unsigned int PID(void);
- void Speedcount(void);
- void Buzzer(void);
- int main(void)
- {
- // NVIC_Configuration();
- Buzzer_Init();
- OLED_Init();
- CountSensor_Init();
- Timer_Init();
- DHT11_Init();
- Motor_Init();
- while(1)
- {
-
- if(flag==1)
- {
- Speedcount();
- }
-
-
- DHT11_Read_Data(&tempH,&humiH);
- OLED_ShowString(1, 1, "Tem:");
- OLED_ShowNum(2,1,tempH,2);
- OLED_ShowString(1, 7, "PWM:");
- OLED_ShowNum(2,7,x,2);
- OLED_ShowString(3, 1, "Speed:");
- OLED_ShowNum(4,1,countdown,4);
- OLED_ShowString(3, 8, "KeyNum:");
- if(tempH>=30&&tempH<=35)
- {
- x=70;
- }
- else if(tempH>=35)
- {
- x=100;
- Buzzer();
- }
- else if(tempH<30)
- {
- x=50;
- }
-
- Motor_SetSpeed(x);
- // PID();
- }
- }
- void TIM3_IRQHandler(void)
- {
-
- if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET) //1s
- {
- TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- time++;
- flag=1;
- flag2=1;
- }
- }
- void Speedcount(void)
- {
- count=CountSensor_Get();
- countdown=count-Speed;
- Delay_ms(20);
- Speed=CountSensor_Get();
- // GPIO_ResetBits(GPIOC,GPIO_Pin_13);
- flag=0;
- }
- void Buzzer(void)
- {
- GPIO_ResetBits(GPIOC,GPIO_Pin_15);
- flag2=0;
- }
复制代码
Keil代码下载:
电机控制代码.7z
(1.53 MB, 下载次数: 53)
|