基于proteus的直流电机仿真
单片机源代码:
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit open = P2^0; //启动电机按键
- sbit close = P2^1; //关闭电机按键
- sbit swap = P2^2; //正反转按键
- sbit sub_speed = P2^3; //减速按键
- sbit add_speed = P2^4; //加速按键
-
- sbit PWM1 = P3^0;
- sbit PWM2 = P3^1;
- /*液晶显示*/
- sbit E=P3^7;
- sbit RW = P3^6;
- sbit RS = P3^5;
- sbit test = P3^4;
-
-
- int time = 0;
- int high = 20;
- int period = 30;
- int change = 0;
- int flag = 0;
- int num_medium = 0;
- int num_display = 0;
- int count_speed = 0;
- uchar wword[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
-
- /*延时t毫秒*/
- void delay(uchar t)
- {
- uint i;
- while(t)
- {
- /*对于11.0592MHz时钟,延时1ms*/
- for(i=0;i<125;i++);
- t--;
- }
- }
- //写命令函数LCD
- void wc51r(uchar j)
- {
- //fbusy();
- RS=0;
- RW=0;
- P1=j;
- E=1;
- E=0;
- delay(3);
- }
- //写数据函数LCD
- void wc51ddr(uchar j)
- {
-
- RS=1;
- RW=0;
- P1=j;
- E=1;
- E=0;
- delay(2);
- }
- //初始化函数LCD
- void init()
- {
- wc51r(0x01); //清屏
- wc51r(0x38); //使用8位数据,显示两行,使用5*7的字型
- wc51r(0x0c); //显示器件,光标开,字符不闪烁
- wc51r(0x06); //字符不动,光标自动右移一格
- }
-
- /***********T0中断服务程序************PWM波的生成**********/
- void time0_int(void) interrupt 1
- {
-
- time++;
- TH0 = 0xec;
- TL0 = 0x78; //60536
-
- if(change == 0) //正转
- {
- PWM2 = 1;
- if(time == high) //high初始值20
- PWM1=0;
- else if(time == period) // period初始值30
- {
- PWM1 = 1;
- time = 0;
- }
-
- }
-
- else //反转
- { PWM1 = 1;
- if(time == high)
- PWM2=0;
- else if(time == period)
- {
- PWM2 = 1;
- time = 0;
- }
- }
-
- }
- /*************************************************************/
- /****T1中断服务程序********单位时间(S)方波的个数*************/
- void time1_int(void) interrupt 3
- {
- count_speed++; //count_speed初始为0
- if(count_speed == 20)
- { count_speed = 0;
- num_display = num_medium;
- num_medium = 0;
- }
-
- }
- /*************************************************************/
- /************************速度显示的数据处理********************/
- void datamade()
- { uint data MM,NN;
-
- wc51r(0xc2);
- wc51ddr('S');
- wc51ddr('p');
- wc51ddr('e');
- wc51ddr('e');
- wc51ddr('d');
- wc51ddr(0x3a);
- NN = num_display%100;
- MM = num_display/100;
- wc51ddr(wword[MM]);
-
- MM = NN/10;
- NN = NN%10;
- wc51ddr(wword[MM]);
- wc51ddr(wword[NN]);
-
- }
- /*****************************************************************/
- /**********通过按键实现对电机开关、调速、转向的控制***************/
- void motor_control()
- {
- if(open == 1)
- EA = 1;
-
- if(close == 1)
- EA = 0;
-
- if(swap == 1)
- {
- change = ~change;
- while(swap != 0)
- {}
-
- }
-
- if(sub_speed == 1)
- {
- high++;
- if(high == 30)
- EA=0;
- while(sub_speed != 0)
- {}
- }
-
-
- if(add_speed == 1)
- {
- high--;
- if(high == 5)
- high = 5;
- while(add_speed != 0)
- {}
- }
- }
- /******************************************************************/
- /***************************主函数*********************************/
- void main()
- {
- P2 = 0x00;
-
- ET0 = 1;
- ET1 = 1;
- TMOD = 0x11;
- TH0 = 0xec;
- TL0 = 0x78;
- TH1 = 0x3c;
- TL1 = 0xb0; //15536
- TR0 = 1;
- TR1 = 1;
-
- init(); //液晶显示初始化程序
-
- while(1)
- {
-
- wc51r(0x84);
- wc51ddr('H');
- wc51ddr('e');
- wc51ddr('l');
- wc51ddr('l');
- wc51ddr('o');
-
- if(test == 0)
- num_medium++;
- datamade();
- motor_control();
-
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
下载:
电机.zip
(75 KB, 下载次数: 161)
|