STM32直流风机通过按键PWM调速同时各参数在LCD上显示
扫描二维码
随时随地手机看文章
依然还记得当时学完STM32之后感觉什么都不会,然后基本上是靠单片机的知识撑过期末的测试的,然后其实一直想把STM32学好,但是感觉学太多也不好吧,总感觉这是样,学得多并不是坏事 ,但是总觉得吧,应该要在你学的技能中有一项是你非常突出的。
//
//Design:直流电机调速,通过按键改变占空比以达到调速目的,同时显示各参数
//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 intduty_cycle=0;
unsigned intk=0;
u8 t=0;
u16 led0pwmval=0;
u8 dir=1;
delay_init();//延时函数初始化
uart_init(9600);//串口初始化为9600
LED_Init();//初始化与LED连接的硬件接口
LCD_Init();
KEY_Init();
TIM1_PWM_Init(899,0);//不分频。PWM频率=72000/(899+1)=80Khz
POINT_COLOR=RED;//设置字体为红色
//固定显示区
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");//分辨率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;
}
}
}