完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#define SPEEDMAX 1000 ***it ENA = P1^2; ***it CS = P1^3; ***it CLK = P1^4; ***it 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 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)%10]; P2=bit_tab; 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 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; } } |
|
相关推荐
4个回答
|
|
|
|
..............一堆代码。还要翻译,,,你一个函数一个函数理解不就好了
|
|
|
|
大神有没有PID怎样控制电机的资料呢
|
|
|
|
注释已经够多了,你是哪个地方不懂呀,你要是都不懂就没法教你了
|
|
|
|
只有小组成员才能发言,加入小组>>
3278 浏览 9 评论
2954 浏览 16 评论
3455 浏览 1 评论
8987 浏览 16 评论
4046 浏览 18 评论
1097浏览 3评论
567浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
564浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2299浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1855浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-21 00:28 , Processed in 1.215453 second(s), Total 84, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号