⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 无线控制的pwm方式控制直流电机c51程序.txt

📁 嵌入式编程
💻 TXT
字号:
#include "regx51.H"
#ifndef MOTOR_PWM
 #define MOTOR_PWM
//Type define for the project:
typedef unsigned long  u32;
typedef unsigned short  u16;
typedef unsigned char  u08; 
//System hardware settings:
static float   osc  = 20.0000;
static float  machine_cycle  = 1;
static float  VOL_MAX  = 28;
static float  VOL_MIN  = 9;

//the code table for the controller:
#define KEY_START 0xFA//B11111010
#define KEY_MODE1 0xFE//B11111110
#define KEY_MODE2 0xF1//B11110001
#define KEY_MODE3 0xF9//B11111001
#define KEY_INC_SP 0xF5//B11110101
#define KEY_DEC_SP 0xFD//B11111101
#define KEY_STOP 0xF3//B11110011


//Global variable:
#define bit_is_clear P0_0 == 0
#define bit_is_set P0_0 == 1
#define set_bit() P0_0 = 1
#define clr_bit() P0_0 = 0
#define set_enable() enable_flag = 1;
#define set_disable() enable_flag = 0;
#define ENABLE  enable_flag == 1
#define DISABLE  enable_flag == 0

#define  there_is_no_int int_flag == 0
#define  there_is_int int_flag == 1
#define  clr_int() int_flag = 0
#define  set_int() int_flag = 1
#define  PWM_frequency 2000
u16 PWM_cycle = 500;//(1/PWM_frequency)*1000000;
//#define  mt_times(X) ((X)*PWM_frequency) //flip times for the certain time

u08  flag    = 0;
u08  tmp_Data   = 0;
u08  Running_Mode = 0; //to record the running mode
u32  counter_PWM  = 0;
u08  int_flag  = 0;//indicate the soft interrupt status
u08  enable_flag;
u08  key_press;
float ratio  = 0;
 
u16 mt_times(int X)
{
 return (X*2000);//2000 is PWM_frequency 
}
/*u08 GetKeys(void)
{
 key_press=P1;
 return key_press;
}*/
//Interrupts:
//This part is for the keys input (Not for serial controller)
void INT0_Interrupt_Server() interrupt 0 using 0
{
 //make the
 u08 tmp=3;
  while(--tmp);
 P2_1 = 1;
 key_press = P1;
 int_flag = 1;
 P3 = 0xFF;
}
//Timer0 Interrupt function:
//
/*
void T0_Interrupt_Server() interrupt 1 using 1
{
 
}*/
//COM1 Interrupt server:
/*void COM1_server() interrupt 4 using 2
{
 if(RI)
 {
  RI = 0;
  key_press=SBUF;
  set_int();
 }
}*/
//Sub-Functions:
//TimeMaintain() to make the CPU busy for a certain time
//ref to the article in the motor directory
void TimeMaintain(u16 mt_time)
//the parameter mt_time is measured by us
{
 while(--mt_time);
}
//change the certain velocity to DC voltage, velocity is measured by r/s
float Ve2DC(float velocity)
{
 u08 tmp;
 tmp=(int)(velocity*100);
 switch(tmp)
 {
  case 100:
   return 3.0;
  case 200:
   return 6.0;
  case 300:
   return 9.0;
  case 400:
   return 12.0;
  case 500:
   return 15.5;
  case 600: 
   return 20.2;
 }//This part should be re-measured if wanna be used for other cases
}
//change the certain DC voltage to Ratio of PWM
float DC2Ratio(float dc_vol)
{
 float compe = 0.02;//compe is the compesation of the system voltage wastage
 return (dc_vol+compe)/VOL_MAX;
}
//PWM generate from Duty-ratio and fixed frequency and the maintain time(using the conter times);
void generate_PWM(float ratio, u08 time)
//para time is measured by unit 's'
{
/*
 u16 ON_Time;
 u16 OFF_Time;
 ON_Time = (int)(PWM_cycle * ratio);
 OFF_Time = PWM_cycle - ON_Time;
 if(ENABLE)
 {
  while ((counter_PWM < mt_times(time)))
  {
   if(there_is_no_int)
   {
    set_bit();
    TimeMaintain(ON_Time);//asked by the delay function
    clr_bit();
    TimeMaintain(OFF_Time);
    counter_PWM++;
   }
   else
   {
    counter_PWM = 0;
    break;
   }
  }
 }
 counter_PWM = 0;
*/
//below for test:
while(there_is_no_int)
{
 set_bit();
 TimeMaintain(30);
 clr_bit();
 TimeMaintain(30);
}
}
void generate_PWM_Run(float velocity, u08 time)
{
 float DC_Vol, Ratio_tmp;
 DC_Vol=Ve2DC(velocity);
 Ratio_tmp=DC2Ratio(DC_Vol);
 //ratio=Ratio_tmp;
 generate_PWM(Ratio_tmp,time);
}
//The 2 modes:
void running_at_mode1(void)
/*2/s(10s)→3/s(10)→4/s(5)→3/s(5)*/
{
 Running_Mode = 1;
 while(there_is_no_int&&ENABLE)
 {
  generate_PWM_Run(2,10);
  generate_PWM_Run(3,10);
  generate_PWM_Run(4,5);
  generate_PWM_Run(3,5);
 }
}
void running_at_mode2(void)
/*2/s(7)→6/s(5)→4/s(5)→6/s(3)*/ 
{
 Running_Mode = 2;
 while(there_is_no_int&&ENABLE)
 {
  generate_PWM_Run(2,7);
  generate_PWM_Run(6,5);
  generate_PWM_Run(4,5);
  generate_PWM_Run(6,3);
 }
}
void running_at_mode3(void)
/*no-level speed control*/
{
 Running_Mode = 3;
 while(there_is_no_int&&ENABLE)
 {
  generate_PWM_Run(5, 1);
 }
}
//The no-level speed control:
void speed_inc(void)
{
 //generate_PWM(ratio+0.15,);//This value should be revised when using
 clr_int();
 while(there_is_no_int&&Running_Mode==3)
 {
  generate_PWM(ratio+0.01,1);
 }
 ratio=ratio+0.01;
}
void speed_dec(void)
{
 clr_int();
 while(there_is_no_int&Running_Mode==3)
 {
  generate_PWM(ratio-0.01,1);
 }
 ratio=ratio-0.01;
}
//Main function:
void main(void)
{
/*This part is for the final product...*/
 tmp_Data = 4000;
 while(--tmp_Data);
 EA = 1;
 IT0 = 1;
 EX0 = 1;
 P3 = 0xFF;
 P0 = 0xAA;
 P2 = 0x00;
 while(1)
 {
  if(int_flag == 1)
  {
 //  tmp_Data=1;
   P2_2 = 1;
   int_flag = 0;
   key_press=key_press|0xF0;
   switch(key_press)
   {
    case KEY_START://5
     set_enable();
     P2_0 = ~P2_0;
     break;
    case KEY_MODE1://7
     running_at_mode1();
     break;
    case KEY_MODE2://8
     running_at_mode2();
     break;
    case KEY_MODE3://9
     running_at_mode3();
     break;
    case KEY_INC_SP://10
     speed_inc();
     break;
    case KEY_DEC_SP://11
     speed_dec();
     break;
    case KEY_STOP://12
     set_disable();
     P2_1 = 0;
     break;
    default:
     P2_6 = ~P2_6;
   }
  }
 }

}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -