初级会员
- 积分
- 77
- 金钱
- 77
- 注册时间
- 2017-12-30
- 在线时间
- 7 小时
|
发表于 2018-1-7 17:54:38
|
显示全部楼层
#include <reg51.H>
#define SPEEDMAX 1000
sbit ENA = P1^2;
sbit CS = P1^3;
sbit CLK = P1^4;
sbit DO = P1^5;
unsigned char seg_data[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //¹²Òõ¼«ÊýÂë¹Ü¶ÎÖµ
unsigned char bit_tab[4]={0xfe,0xfd,0xfb,0xf7}; //λѡ¿ØÖÆλ
unsigned int mod[4]={1000,100,10,1}; //È¡Ä£¿ØÖÆÁ¿£¬ÎªÁË·½±ã¼ÆËãÊ®½øÖÆÊýµÄÿһλÊý×Ö
float s1,s2,s3,s4,s5; //Ëæ»ú¶¨ÒåµÄËٶȱäÁ¿£¬±¸ÓÃ
unsigned int rate; //δʹÓÃ
unsigned int timer0_cnt; //¶¨Ê±Æ÷0¶¨Ê±ÖжϼÆÊý£¬ÎªÁË¿ØÖÆÕ¼¿Õ±ÈʹÓÃ
int button_set_data; //°´¼üÉèÖÃËÙ¶ÈÖµ
unsigned char button; //°´¼üÖµ
unsigned char flag; //±ê־λ£¬Ä¿Ç°Î´Ê¹ÓÃ
unsigned char timer2_cnt; //¶¨Ê±Æ÷2¼ÆÊýÆ÷£¬ÓÃÀ´¼Æʱ500ms
unsigned char pot; //»¬¶¯±ä×èÆ÷µçѹֵ
unsigned int motor_plus; //1sÄÚ£¬µç»ú±àÂëÆ÷Âö³å¸öÊý
unsigned int driver_data; //Õ¼¿Õ±ÈÇý¶¯µ÷Õû
unsigned int pot_data; //»¬¶¯±ä×èÆ÷µçѹֵ¶ÔÓ¦µÄתËÙ
unsigned int speed; //µç»úתËÙ
int pid_val; //PID¿ØÖÆÊä³öÖµ
typedef struct
{
unsigned int set_point;
float P;
float I;
float D;
unsigned int last_error;
unsigned int prev_error;
unsigned int sum_error;
}
PID; //PIDËã·¨½á¹¹Ìå
PID motor_pid; //¶¨Òåµç»úPID¿ØÖƱäÁ¿
//PID¼ÆËã
int PidCalculate(PID *ppid, int next_point) //PID¼ÆË㺯Êý
{
float derror,error;
float val;
error=ppid->set_point-next_point; //µ±Ç°Ê±¿Ì²îÖµ
ppid->sum_error+=error; //»ý·ÖÖµ£¬»ý·ÖÖµ¾ÍÊDzîÖµµÄÀÛ¼Ó
derror=ppid->last_error - ppid->prev_error; //΢·ÖÖµ£¬Î¢·Ö¾ÍÊǵ±Ç°Ê±¿ÌµÄ²îÖµ-ǰһʱ¿ÌµÄ²îÖµ
ppid->prev_error=ppid->last_error; //´æ´¢µ±Ç°Ê±¿ÌµÄ²îÖµ£¬±ä³Éǰһʱ¿Ì²îÖµ£¬ÎªÏ´μÆËã×ö×¼±¸
ppid->last_error=error;
val=ppid->P*error + ppid->I * ppid->sum_error + ppid->D*derror;
return (int)val;
}
void PidInit (PID *ppid) //PID³õʼ»¯º¯Êý
{
ppid->set_point=0;
ppid->P=0.6;
ppid->I=1;
ppid->D=0.2;
}
void delay(unsigned int x) //ÑÓʱº¯Êý
{
unsigned int i;
for(i=0;i<x;i++);
}
unsigned char AD0831_ReadChar(void) //ADC¶ÁÈ¡Êý¾Ý
{
unsigned char val;
unsigned char i;
val=0;
CS=0;
for(i=0;i<9;i++)
{
CLK=1;
delay(1);
CLK=0;
val<<=1;
val|=DO;
}
CS=1;
delay(1);
return val;
}
void main(void)
{
unsigned char i,j;
IT0=1;
EX0=1;
TMOD=0X12;
TH0=256-70;
TL0=256-70;
ET0=1;
EA=1;
TR0=1;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
ET1=1;
TR1=1;
P1=0x00;
DO=1;
PidInit(&motor_pid);
while(1)
{
P1|=0x01;
j++;
if(j==100)
{
pot = AD0831_ReadChar();
pot_data=(unsigned int)(((float)(pot)/256)*1000);
j=0;
}
button=P3;
button=button & 0x03;
if(button!=0x03)
{
delay(10);
button=P3;
button=button & 0x03;
if(button!=0x03)
{
if(button==0x02)
{
button_set_data=button_set_data+100;
}
if(button==0x01)
{
button_set_data=button_set_data-100;
}
if(button_set_data>1000)
button_set_data=1000;
if(button_set_data<0)
button_set_data=0;
}
while(button!=0x03)
{
button=P3;
button=button & 0x03;
}
}
for(i=0;i<4;i++)
{
P0=seg_data[(speed/mod[i])%10];
P2=bit_tab[i];
delay(100);
P2=0xff;
}
motor_pid.set_point=pot_data;
}
}
void timer0() interrupt 1
{
timer0_cnt++;
if(timer0_cnt==1000)
{
timer0_cnt=0;
}
if(timer0_cnt<driver_data)
{
ENA=1;
}
else
{
ENA=0;
}
}
void int0() interrupt 0
{
motor_plus++;
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
timer2_cnt++;
if(timer2_cnt==10)
{
timer2_cnt=0;
speed=((float)(motor_plus)*(float)60)/(float)24.0;
motor_plus=0;
pid_val=PidCalculate(&motor_pid,speed);
if(pid_val>1000)
pid_val=1000;
if(pid_val<0)
pid_val=0;
driver_data=(unsigned int)pid_val;
}
}
|
|