The STM32 DC fan can adjust the speed by pressing PWM and the parameters are displayed on the LCD.

Publisher:zhaodawei617Latest update time:2017-11-08 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I still remember that after I finished learning STM32, I felt like I knew nothing, and basically relied on the knowledge of microcontrollers to get through the final test. Then, I actually always wanted to learn STM32 well, but I felt that learning too much was not a good thing. I always felt that it was not a bad thing to learn a lot, but I always felt that there should be one skill that you are very outstanding in among the skills you have learned.

//
//Design: DC motor speed regulation, change the duty cycle by pressing buttons to achieve the purpose of speed regulation, and display various parameters at the same time
//Author: Touch
//Time :2017.6.5
//
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h
" #include "lcd.h"
#include "key.h"
#include "pwm.h"
 int main(void)
 { 
 
 unsigned int duty_cycle=0; 
 unsigned int k=0; 
 u8 t=0;  
       u16 led0pwmval=0;   
 u8 dir=1; 
 delay_init(); //Delay function initializationuart_init  
 (9600); //Serial port initialized to 9600
 LED_Init(); //Initialize the hardware interface connected to the LED
  LCD_Init(); 
 KEY_Init(); 
       TIM1_PWM_Init(899,0); //No frequency division. PWM frequency = 72000 / (899 + 1) = 80Khz
 
 POINT_COLOR = RED; // Set the font to red
// Fixed display area
 LCD_ShowString (70, 20, 200, 16, 16,"DC_Motor Project");
 LCD_ShowString (20, 50, 200, 16, 16,"Author: TH ZJW LZH");
 LCD_ShowString (20, 70, 200, 16, 16,"Teacher: LD"); 
 LCD_ShowString (20, 130, 200, 16, 16,"Menu:"); LCD_ShowString (20, 150, 200, 16, 16
 ,"Max_Vaule"); LCD_ShowString (
       160, 150, 200, 16, 16,"900");
 LCD_ShowString(20,170,200,16,16,"Down_Mode");
 LCD_ShowString(20,190,200,16,16,"Up_Mode");
 LCD_ShowString(20,210,200,16,16,"Stop_Mode"); //Resolution 320*240
 
 LCD_ShowString(20,230,200 ,16,16,"Duty_cycle");
 LCD_ShowString(180,230,200,16,16,"%");
 LCD_ShowString(20,250,200,16,16,"Air_Grade");
 
       LCD_ShowString(90,300,200,16,16,"2017/6/5");

 
    while(1)
 { 
  LCD_ShowxNum(150,210,led0pwmval,4,16,0);     //计数值
  duty_cycle=led0pwmval/9;                     //占空比定义
  if(led0pwmval==50)   duty_cycle=5;
  if(led0pwmval==100)  duty_cycle=9;
  if(led0pwmval==900)  duty_cycle=99;
  LCD_ShowxNum(160,230,duty_cycle,2,16,0);
  
     if(duty_cycle>=0 &&duty_cycle<33)    k=1;
     if(duty_cycle>=33&&duty_cycle<66)    k=2;
           if(duty_cycle>=66&&duty_cycle<99)    k=3;
  
     switch(k)
        {
   case 1:LCD_ShowString(160,250,200,16,16,"Little");break;
        case 2:LCD_ShowString(160,250,200,16,16,"Middle");break;
  case 3:LCD_ShowString(160,250,200,16,16,"Heavy");break;
  default:LCD_ShowString(160,250,200,16,16,"Little");break;
        } 
  
  
  
  
  t=KEY_Scan(0);                  //得到键值
     switch(t)
     {      
   case KEY0_PRES:      //1
    {
    LED0=!LED0;
//PWM
        led0pwmval=led0pwmval-50;       
     if(led0pwmval>5000)
     {
     led0pwmval=0;  
     }
              TIM_SetCompare1(TIM1,led0pwmval);
    delay_ms(60);
//显示 
     }
    break;  
   case KEY1_PRES:     //2   
    {
          LED1=!LED1;
    delay_ms(10); 
    led0pwmval=led0pwmval+50;
    if(led0pwmval>850)
     {
     led0pwmval=899;                             
     }
    TIM_SetCompare1(TIM1,led0pwmval); 
    delay_ms(60);  
    }
    break;    
   case WKUP_PRES:      //3 
   {    
    LED0=!LED0;
    LED1=!LED1;
//PWM
         led0pwmval=0;
    TIM_SetCompare1(TIM1,led0pwmval); 
//显示                       
   }    
    break;
   default:
    delay_ms(10); 
                            break;        
           }
       }
}


Keywords:STM32 Reference address:The STM32 DC fan can adjust the speed by pressing PWM and the parameters are displayed on the LCD.

Previous article:STM32 common IO port simulates serial port
Next article:STM32 study notes: basic timing functions of general timers

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号