为什么我仿真的无刷直流电机转的好慢好慢啊,

2019-07-18 11:24发布

难道是我的程序问题吗,谁帮我看看啊,程序好长好长
1.  Main.c#define uchar unsigned char#define uint unsigned int#include <reg51.h>#include  "led.h"#include  "botton.h"#include "delay.h"#include "controlfunction.h"#include "feedback.h"sbit RC=P3^0;sbit A0=P3^1;sbit CE=P2^3;sbit CS=P3^3;sbit LED0=P2^0;sbit LED1=P2^1;sbit MOTOR=P2^2;extern uchar code hall[6];extern uchar code zz[6];extern uchar code fz[6];extern uchar hal;extern uchar code seglednode[8];extern uchar hal;char r=0;void main(){ P1=0xff; P3=0xff;P2=0;P0=0;motor_set();IE=0x83;IP=0;TMOD=0x01;led0(seglednode[0]);  led1(seglednode[0]);sl(0);TR0=0;while(1){}} 2.  Led.c#include <reg51.h>#include  "led.h"#include  "botton.h"#include "delay.h"uchar codeseglednode[8]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x01};uchar codespeednode[11]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0xff,0xfb};extern char r;sbit LED0=P2^0;sbit LED1=P2^1;sbit SL1=P2^4;sbit SL2=P2^5;sbit SL3=P2^6;sbit SL4=P2^7; void led0(uchar seg){    LED0=1; P0=seg; LED0=0; }void led1(uchar seg){   LED1=1;P0=seg;LED1=0; }void sl(int result){int buff;buff=result/1000;SL1=1;P0=speednode[buff];SL1=0;buff=(result%1000)/100;SL2=1;P0=speednode[buff];SL2=0;buff= (result%100)/10;SL3=1;P0=speednode[buff];SL3=0;                     buff=result%10;SL4=1;P0=speednode[buff];SL4=0; 3.  Led.h#ifndef uchar#define uchar unsigned char#endif#ifndef uint#define uint unsigned int#endifvoid led0(uchar);void led1(uchar);void sl(int); 4.  Feedback.c#include <reg51.h>#include "delay.h"#include "led.h"#include "math.h"#define uchar unsigned char#define uint unsigned intsbit RC=P3^0;sbit A0=P3^1;sbit CE=P2^3;sbit CS=P3^3;sbit MOTOR=P2^2;sbit SL1=P2^4;sbit SL2=P2^5;sbit SL3=P2^6;sbit SL4=P2^7;float result=0.0;int bdata temp=0;extern char r;extern char oldr;extern uchar code seglednode[8];void inti(){MOTOR=0;P0=0xff;CS=1;CE=0;} void adread(){ inti(); CS=0;A0=0;RC=0;CE=1;{CE=0;RC=1;A0=0;CE=1;temp=P0;temp=temp<<4;CE=0;RC=1;A0=1;CE=1;temp+=P0>>4;}CE=0;CS=1;if(temp&0x800){temp=temp&0x7ff;result=((float)temp)*0.837053571;}else{temp=~temp+1;temp=temp&0x7ff;result=0-((float)temp)*0.837053571;}} 5.  Feedback.hvoid adread(); extern float result; 6.  Delay.asmPUBLIC _DELAY12_1MSSEG_US SEGMENT CODERSEG SEG_US _DELAY12_1MS:MOV R6,#250DELAYL0:NOPNOPDJNZ R6,DELAYL0DJNZ R7,_DELAY12_1MSRET PUBLIC _DELAY12_100MS_DELAY12_100MS:MOV R5,#100 DELAYL1:MOV R6,#250DELAYL2:NOPNOPDJNZ R6,DELAYL2DJNZ R5,DELAYL1DJNZ R7,_DELAY12_100MSRETPUBLIC NOP1NOP1: NOPRETEND 7.  delay.hextern void delay12_1ms(char);extern void delay12_100ms(char);extern void nop1(void);#ifndef unchar#define unchar unsigned char#endif#ifndef unint#define unint unsinged int#endif 8.  button.c#include <reg51.h>#include  "led.h"#include  "botton.h"#include "delay.h"#include "pwm.h"extern char r;extern uchar code seglednode[8];sbit MOTOR=P2^2;void check() interrupt 0{      //判断按键算法if(OPEN==0&&r==0){ delay12_1ms(70);  //防抖处理if(OPEN==0){r=1;TH0=65436/256;TL0=65436%256;TR0=1;}}else if(UP==0&&r>0){ delay12_1ms(100); if(UP==0)r++;if(r>=6)r=6;if(r<=-6)r=-6; }else if(UP==0&&r<0){delay12_1ms(100); if(UP==0)r--;if(r>=6)r=6;if(r<=-6)r=-6; }else if(DOWN==0&&r>0){delay12_1ms(100);if(DOWN==0)r--;if(r>=6)r=6;if(r<=-6)r=-6;}else if(DOWN==0&&r<0){delay12_1ms(70);if(DOWN==0)r++;if(r>=6)r=6;if(r<=-6)r=-6; }else if(CPL==0){delay12_1ms(100); if(CPL==0)r=0-r; }else if(CLOSE==0){r=0;}elser=r;P1=0xff;if(r>=0){led0(seglednode[0]);  led1(seglednode[r]);}else{ led0(seglednode[7]);  led1(seglednode[0-r]);}} 9.  button.h#ifndef uchar#define uchar unsigned char#endif#ifndef uint#define uint unsigned int#endifsbit OPEN=P1^0;sbit UP= P1^1;sbit DOWN=P1^2;sbit CPL=P1^3;sbit CLOSE=P1^4; 10.controlfunction.c#define uchar unsigned char#define uint unsigned int#include <reg51.h>#include  "led.h"#include  "botton.h"#include "delay.h"#include "pwm.h"#include "math.h"sbit LED0=P2^0;sbit LED1=P2^1;sbit MOTOR=P2^2;uchar codehall[6]={0x0a0,0x20,0x060,0x40,0xc0,0x80};uchar codefz[6]={0x24,0x06,0x12,0x18,0x09,0x21};uchar codezz[6]={0x18,0x09,0x21,0x24,0x06,0x12};uchar hal;extern char r;extern float result;extern int sp;                      void motor_set(){uchar i;hal=P1&0xe0;for(i=0;i<6;i++) if(hall==hal)    break;MOTOR=1;if(r>0)P0=zz;else if(r<0)P0=fz;elseP0=0;MOTOR=0; 11.controlfunction.hvoid motor_set(); 12.pid.c#include"reg51.h"#include "math.h"float error[3]={0,0,0};extern float sum;int pid(int speed,float result){float kp=0.9,ki=0.75,kd=0.05,ut=0;error[0]=(float)speed-fabs(result);sum=sum+(ki*error[0]);ut=sum+kp*error[0]+kd*(error[0]-error[1]);//ut=kp*(1.0+2.0*ki+0.5*kd)*(error[0])-kp*(1.0+kd)*error[1]+0.5*kp*kd*error[2];//error[2]=error[1];error[1]=error[0];return (int)ut; 13.pwm.c#include <reg51.h>#include  "botton.h"#include "controlfunction.h"#include "delay.h"#include  "led.h"#include "feedback.h"#include "math.h"#include <math.h>#define uchar unsigned char#define uint unsigned intsbit RC=P3^0;sbit A0=P3^1;sbit CE=P2^3;sbit CS=P3^3;sbit LED0=P2^0;sbit LED1=P2^1;sbit MOTOR=P2^2;sbit SL1=P2^4;sbit SL2=P2^5;sbit SL3=P2^6;sbit SL4=P2^7;sbit check=P3^5;extern uchar code seglednode[8];extern char r;extern uchar code hall[6];extern uchar code zz[6];extern uchar code fz[6];extern uchar hal;extern float result;uchar count=0;uchar sc=0;uchar zys[6]={90,17,12,8,6,4};uchar fys[6]={145,84,59,39,30,23};intspeed[7]={0,220,370,530,750,960,1200};int ys=100;int flag=100;int sp;float sum=0.0;extern float error[3];int pid(int speed,float result);void dianji() interrupt 1{char c; TR0=0;adread(); if(r>0){c=r;sp=speed[c];}else{c=0-r;sp=0-speed[c];}ys=flag-pid(speed[c],fabs(result));//ys=ys-pid(speed[c],fabs(result));//ys=ys+(int)(0.8*((float)(result)-(float)(speed[c])));//////if(ys<=1&&r!=0){ys=3;sum=0;flag=3;error[1]=0;error[0]=0;}if(ys>=150){ys=100;flag=100;sum=0;error[1]=0;error[0]=0;}if((result>0&&sp<0)||result<0&&sp>0)ys=3;if(speed[c]==0&&((int)(fabs(result))-speed[c])<=10){sl(0);ys=100;TR0=0;MOTOR=1; P0=0;MOTOR=0;}TH0=65436/256;TL0=65436%256;if(count==ys){motor_set();count=0;sc++;}                           count++;if(sc==2){sl(abs((int)result));sc=0;}check=~check;TR0=1;}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
1条回答
武力戡乱
1楼-- · 2019-07-18 16:30
我去了,这代码没法看!
前两天遇到这样的一个,完全是复制粘贴的!亲,要命啊!

一周热门 更多>