805 字
4 分钟
Motor模块相关功能和考点
-
电机PWM的频率及占空比修改
-
题目及思路


-
代码底层
void Select_HC573(unsigned char channel,dat){unsigned char temp;temp = P2 & 0x1f;P2 = temp;P0 = dat;switch(channel){case 4:temp = (P2 & 0x1f) | 0x80;P2 = temp;break;case 5:temp = (P2 & 0x1f) | 0xa0;P2 = temp;break;case 6:temp = (P2 & 0x1f) | 0xc0;P2 = temp;break;case 7:temp = (P2 & 0x1f) | 0xe0;P2 = temp;break;case 0:temp = (P2 & 0x1f) | 0x00;P2 = temp;break;}temp = P2 & 0x1f;P2 = temp;}void Motor(bit enable){if(enable) temp_0 |= 0x20;else temp_0 &= ~0x20;if(temp_0!= temp_old_0){Select_HC573(5,temp_0);temp_old_0 = temp_0;}} -
项目代码
#include <STC15F2K60S2.H>#include "stdio.h"/*自己编写的头文件*/#include "Set_HC573.h"#include "Key.h"#include "Uart.h"/*数码管变量*/unsigned char seg_Pos = 0;//数码管索引unsigned char seg_Buf[8] = {0,0,0,0,0,0,0,0};//数码管显示缓存区,填入位选值unsigned int seg_slow_down = 0;//数码管减速变量/*按键变量*/unsigned char key_slow_down = 0;//按键扫描减速变量unsigned char Key_Val,Key_Old,Key_Down,Key_Up;unsigned char Key;/*电机PWM波所需变量*/bit Volat_Flag = 0;unsigned int Freq_Motor = 1000;unsigned char Duty_cycle = 10;unsigned int period_us = 0;unsigned int Time_High = 0;unsigned int Time_Low = 0;unsigned char duty_count = 0;bit Set_finish_flag = 0;/*===========按键处理函数===========*/void Key_Proc(){if(key_slow_down <= 10) return;key_slow_down = 0;Key_Val = Key_Read();Key_Down = Key_Val & (Key_Old ^ Key_Val);Key_Up = ~Key_Val & (Key_Old ^ Key_Val);Key_Old = Key_Val;switch(Key_Down){case 10:Freq_Motor = (Freq_Motor == 10000)?1000:Freq_Motor+1000;break;case 11:Duty_cycle = (Duty_cycle == 90)?10:Duty_cycle+10;Key = 11;// temp = duty + '0';// SBUF = temp;// while(TI==0);// TI = 0;// SBUF = '0';// while(TI==0);// TI = 0;// SBUF = '%';// while(TI==0);// TI = 0;break;}}/*============脉冲处理函数============*/void Motor_Proc(){period_us = (1000000/Freq_Motor);Time_High = ((period_us * Duty_cycle)/100 + 5)/10;Time_Low = (period_us - Time_High * 10)/10;if(Volat_Flag == 0 && Set_finish_flag == 0){Motor(0);Set_finish_flag = 1;}else{Motor(1);Set_finish_flag = 0;}}/*==============数码管处理函数===============*/void seg_Proc(){if(seg_slow_down <= 400) return;seg_slow_down = 0;// seg_Buf[0] = Time_High / 100;// seg_Buf[1] = Time_High / 10 % 10;// seg_Buf[2] = Time_High % 10;//// seg_Buf[3] = Time_Low / 100;// seg_Buf[4] = Time_Low / 10 % 10;// seg_Buf[5] = Time_Low % 10;//// seg_Buf[6] = period_us / 100 % 10;// seg_Buf[7] = period_us / 10 % 10;seg_Buf[0] = 11;seg_Buf[1] = seg_Buf[2] = 10;seg_Buf[5] =seg_Buf[6] =seg_Buf[7] = 0;seg_Buf[3] = (Freq_Motor / 10000 == 0)?10:Freq_Motor / 10000;seg_Buf[4] = Freq_Motor / 1000 % 10;}/*==============串口处理函数================*/void Uart_Proc(){if(Key == 11){printf("%bu%%",Duty_cycle);Key = 0;}}/*===============定时器初始化函数===============*/void Timer0_Init(void) //1毫秒@12.000MHz{AUXR &= 0x7F; //定时器时钟12T模式TMOD &= 0xF0; //设置定时器模式TL0 = 0x18; //设置定时初始值TH0 = 0xFC; //设置定时初始值TF0 = 0; //清除TF0标志TR0 = 1; //定时器0开始计时ET0 = 1;}void Timer1_Init(void) //10微秒@12.000MHz{AUXR &= 0xBF; //定时器时钟12T模式TMOD &= 0x0F; //设置定时器模式TL1 = 0xF6; //设置定时初始值TH1 = 0xFF; //设置定时初始值TF1 = 0; //清除TF1标志TR1 = 1; //定时器1开始计时ET1 = 1;}/*=========定时器中断函数=========*/void Timer0Servce() interrupt 1{key_slow_down++;seg_slow_down++;if(++seg_Pos == 8) seg_Pos = 0;if(seg_Buf[seg_Pos] > 20) SMG_Disp(seg_Pos,seg_Buf[seg_Pos] - ',',1);else SMG_Disp(seg_Pos,seg_Buf[seg_Pos],0);}void Timer1Servce() interrupt 3{duty_count++;if(Volat_Flag == 1){if(duty_count >= Time_High){//Motor(0);Set_finish_flag = 0;Volat_Flag = 0;duty_count = 0;}}else{if(duty_count >= Time_Low){//Motor(1);Set_finish_flag = 1;Volat_Flag = 1;duty_count = 0;}}}/*=========主函数=========*/void main(){Init_Sys();Timer0_Init();Timer1_Init();Uart1_Init();while(1){Key_Proc();Motor_Proc();seg_Proc();Uart_Proc();}}
-
Motor模块相关功能和考点
https://mizuki.mysqil.com/posts/蓝桥杯单片机/motor模块相关功能和考点/ 部分信息可能已经过时














