1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
6个回答
|
|
|
|
#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; } } |
|
|
|
大佬,有没有stm32的?
|
|
|
|
想要stm32的,红外光循迹还能走十字路口的
|
|
|
|
百度一大把
|
|
|
|
什么代码,什么功能啊
|
|
|
|
只有小组成员才能发言,加入小组>>
请问下图大疆lightbridge2遥控器主板电源芯片型号是什么?
4483 浏览 1 评论
使用常见的二极管、三极管和mos做MCU和模组的电平转换电路,但是模组和MCU无法正常通信,为什么?
358浏览 2评论
为了提高USIM卡电路的可靠性和稳定性,在电路设计中须注意的点有哪些?
367浏览 2评论
383浏览 2评论
383浏览 2评论
439浏览 2评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-31 00:46 , Processed in 1.020730 second(s), Total 87, Slave 71 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号