51单片机代码转arduino,那位大佬来帮一下忙

2019-11-26 11:25发布

<br> #define PWM6T2L (*(unsigned char vola<a href="http://bbs.elecfans.com/zhuti_715_1.html" target="_blank" class="relatedlink">ti</a>le xdata *)0xff43)<br> #define PWM6CR (*(unsigned char volatile xdata *)0xff44)<br> #define PWM7T1<br> (*(unsigned int volatile xdata *)0xff50)<br> #define PWM7T1H (*(unsigned char volatile xdata *)0xff50)<br> #define PWM7T1L (*(unsigned char volatile xdata *)0xff51)<br> #define PWM7T2<br> (*(unsigned int volatile xdata *)0xff52)<br> #define PWM7T2H (*(unsigned char volatile xdata *)0xff52)<br> #define PWM7T2L (*(unsigned char volatile xdata *)0xff53)<br> #define PWM7CR (*(unsigned char volatile xdata *)0xff54)<br> sfr<br> P_SW2 = 0xba;<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sfr<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> sbit<br> P0M1 = 0x93;<br> P0M0 = 0x94;<br> P1M1 = 0x91;<br> P1M0 = 0x92;<br> P2M1 = 0x95;<br> P2M0 = 0x96;<br> P3M1 = 0xb1;<br> P3M0 = 0xb2;<br> P4M1 = 0xb3;<br> P4M0 = 0xb4;<br> P5M1 = 0xC9;<br> P5M0 = 0xCA;<br> P6M1 = 0xCB;<br> P6M0 = 0xCC;<br> P7M1 = 0xE1;<br> P7M0 = 0xE2;<br> PWMCFG = 0xf1;<br> PWMCR = 0xf5;<br> PWM<a href="http://www.hqchip.com/app/844" target="_blank" class="relatedlink">IF</a> = 0xf6;<br> PWMFDCR= 0xf7;<br> PWM2 = P3^7;<br> PWM3 = P2^1;<br> PWM4 = P2^2;<br> PWM5 = P2^3;<br> PWM6 = P0^7;<br> PWM7 = P0^6;<br> yanjing= P1;<br> dayanjing=P3^3;<br> dang1=P0^0;<br> dang2=P0^1;<br> dang3=P0^2;<br><br> void PWM_config(void);<br> void Delay10ms(unsigned int);<br> void tibian(unsigned short c,d);<br> void tibian1(unsigned short c,d);<br> void next1(void);<br> void next2(void);<br> void next3(void);<br> void zhixing(void);<br> void zhuanwan(void);<br> void PWM2_SetPwmWide(unsigned short Wide);<br> void PWM3_SetPwmWide(unsigned short Wide);<br> void PWM4_SetPwmWide(unsigned short Wide);<br> void PWM5_SetPwmWide(unsigned short Wide);<br> void PWM6_SetPwmWide(unsigned short Wide);<br> void PWM7_SetPwmWide(unsigned short Wide);<br> void IntConfiguration();<br> Unsigned short<br> xianzhuang2,xianzhuang3,xianzhuang4,xianzhuang5,xianzhuang6,xianzhuang7,zhuan<br> jiao2,zhuanjiao3,zhuanjiao4,zhuanjiao5,zhuanjiao6,zhuanjiao7;<br> unsigned short k=0,j,num=0,cishu=0,mingling=0,qianjin=0,buzi=0,KeyValue=0;<br> void main()<br> {<br> xianzhuang2=1450;xianzhuang3=1545;xianzhuang4=1500;//最标准姿势<br> xianzhuang5=1510;xianzhuang6=1453;xianzhuang7=1460;<br> zhuanjiao2=1850;zhuanjiao3=1145;zhuanjiao4=1500;<br> zhuanjiao5=1510;zhuanjiao6=1453;zhuanjiao7=1460;<br> PWM_config();<br> EA=1;//打开总中断<br> P1=0xff;<br> while(1)<br> {<br> PWM2_SetPwmWide(xianzhuang2);<br> PWM3_SetPwmWide(xianzhuang3);<br> PWM4_SetPwmWide(xianzhuang4);<br> PWM5_SetPwmWide(xianzhuang5);<br> PWM6_SetPwmWide(xianzhuang6);<br> PWM7_SetPwmWide(xianzhuang7);<br> PWM6_SetPwmWide(xianzhuang6);<br> zhixing();<br><br> }<br> }<br> void IntConfiguration()<br> {<br> //设置 INT1<br> IT1=1;<br> EX1=1;<br> }<br> void Int0() interrupt 2 //<a href="http://www.elecfans.com/tags/外部中断/" target="_blank" class="relatedlink">外部中断</a> 0 的中断函数<br> { Delay10ms(1);<br> if(dayanjing==0)<br> {<br> EA=0;<br> KeyValue=1;<br> }<br> }<br> void zhixing()<br> {<br> if(xianzhuang2!=zhuanjiao2)<br> {<br> tibian1(xianzhuang2,zhuanjiao2);<br> xianzhuang2=j;<br> }<br> if(xianzhuang3!=zhuanjiao3)<br> {<br> tibian1(xianzhuang3,zhuanjiao3);<br> xianzhuang3=j;<br> }<br> if(xianzhuang4!=zhuanjiao4)<br> {<br> tibian1(xianzhuang4,zhuanjiao4);<br> xianzhuang4=j;<br> }<br> if(xianzhuang5!=zhuanjiao5)<br> {<br> tibian1(xianzhuang5,zhuanjiao5);<br> xianzhuang5=j;<br> }<br> if(xianzhuang6!=zhuanjiao6)<br> {<br><br> tibian1(xianzhuang6,zhuanjiao6);<br> xianzhuang6=j;<br> }<br> if(xianzhuang7!=zhuanjiao7)<br> {<br> tibian1(xianzhuang7,zhuanjiao7);<br> xianzhuang7=j;<br> }<br> Delay10ms(1);<br> }<br> void tibian1(unsigned short c,d)<br> {<br> if(c<d)<br> {<br> c++;<br> }<br> else if(c&gt;d)<br> {<br> c--;<br> }<br> if(c==d)<br> {<br> next();<br> }<br> j=c;<br> }<br> void next ()<br> {<br> if(mingling==0)<br> {<br> switch (num)<br> {<br> case(0):break;<br><br> case(1):zhuanjiao6=1241;zhuanjiao7=1359;break;//起步支撑<br> case(2):break;<br> case(3):zhuanjiao6=1341;break;// 角 度 回 旋<br> case(4):<br> zhuanjiao2=2037;zhuanjiao3=1332;zhuanjiao4=1667;<br> zhuanjiao5=1677;zhuanjiao6=1413;break;//漫右脚同时右脚回<br> 90 调节 2 3 号舵机可使 67 舵机落脚时是否碰地<br> case(5):break;<br> case(6):break;<br> case(7):break;<br> case(8):break;<br> case(9):<br> if(zhuanjiao6!=1413)<br> {<br> zhuanjiao6=1413;num--;<br> }<br> else<br> {<br> zhuanjiao6=1453;zhuanjiao7=1460;<br> }<br> break;<br> case(10):break;<br> case(11):zhuanjiao6=1574;zhuanjiao7=1637;break;//zhuanjiao6=1574;zhuanjiao7=16<br> 37;break;//<br> 起步支撑 6 的偏转角度增大可使其右偏 减小左偏<br> case(12):break;<br> case(13):<br> switch(P1)<br> { case(0xff):num++;break;<br> //处于白线<br> case(0xf7):zhuanjiao7=1700;break;//前面一个进入黑线<br> case(0xf3):zhuanjiao7=1720;break;//前面两个进入黑线<br> case(0xf1):zhuanjiao7=1850;break;//前面三个进入黑线<br> case(0xf0):zhuanjiao7=1950;break;//前面四个进入黑线<br> case(0xb7):zhuanjiao7=1650;break;//前面一个进去黑线<br> 后面一个进入黑线<br> case(0x97):num++;break; //前面一个进入黑线 后面<br> 两个进去黑线<br> case(0x87):num++;break; //前面一个进入黑线 后面<br> 三个进入黑线<br> case(0xb3):zhuanjiao7=1700;break;//前面两个进入黑线<br> 后面一个进入黑线<br> 19山东省高校第五届机器人大赛技术报告<br> case(0x93):zhuanjiao7=1660;break;//前面两个进入黑线<br> 后面两个进去黑线<br> case(0x83):num++;break;<br> 线 后面三个进入黑线<br> case(0xb1):zhuanjiao7=1720;break;//前面三个进入黑线<br> 后面一个进入黑线<br> case(0x91):zhuanjiao7=1700;break;//前面三个进入黑线<br> 后面两个进去黑线<br> case(0x81):zhuanjiao7=1660;break;//前面三个进入黑线<br> 后面三个进入黑线<br> case(0xb0):zhuanjiao7=1720;break;//前面四个进入黑线<br> 后面一个进入黑线<br> case(0x90):zhuanjiao7=1700;break;//前面四个进入黑线<br> 后面两个进去黑线<br> case(0x80):zhuanjiao7=1660;break;//前面四个进入黑线<br> 后面三个进入黑线<br> case(0xbf):num++;break;<br> case(0x9f):num++;break;<br> case(0x8f):num++;break;<br> //后面一个进入黑线<br> // 后面两个进去黑线<br> //后面三个进入黑线<br> case(0x88):zhuanjiao7=1780;break;//前面一个出来黑线 后面三个进入黑线<br> case(0x8c):zhuanjiao7=1950;break;//前面两个出来黑线 后面三个进入黑线<br> case(0x8e):zhuanjiao7=1950;break;//前面三个出来黑线 后面三个进入黑线<br> case(0xc8):zhuanjiao7=1720;break;//前面一个出来黑线 后面一个出来黑线<br> case(0xcc):zhuanjiao7=1740;break;//前面两个出来黑线 后面一个出来黑线 2<br> case(0xce):zhuanjiao7=2050;break;//前面三个出黑线 后面一个出来黑线<br> case(0xcf):zhuanjiao7=2050;break;//前面四个个出黑线 后面一个出来黑线<br> case(0xe8):zhuanjiao7=1720;break;//前面一个出来黑线 后面两个出来黑线<br> case(0xec):zhuanjiao7=1750;break;//前面两个出来黑线 后面两个出来黑线 2<br> case(0xee):zhuanjiao7=1950;break;//前面三个 出黑线 后面两个出来黑线 3<br> case(0xef):zhuanjiao7=1950;break;//前面全出黑线 后面两个出来黑线 3<br> 20<br> //前面两个进入黑山东省高校第五届机器人大赛技术报告<br> case(0xf8):zhuanjiao7=2050;break;//前面一个出来黑线<br> case(0xb8):zhuanjiao7=2100;break;//前面一个出来黑线 后面一个进入黑线<br> case(0x98):zhuanjiao7=2050;break;//前面一个出来黑线 后面两个进去黑线<br> case(0xfc):zhuanjiao7=2100;break;//前面两个出来黑线<br> case(0xbc):zhuanjiao7=2080;break;//前面两个出来黑线 后面一个进入黑线<br> case(0x9c):zhuanjiao7=2050;break;//前面两个出来黑线 后面两个进去黑线<br> case(0xfe):zhuanjiao7=2100;break;//前面三个出来黑线<br> case(0xbe):zhuanjiao7=2080;break;//前面三个出来黑线 后面一个进入黑线<br> case(0x9e):zhuanjiao7=2050;break;//前面三个出来黑线 后面两个进去黑线<br> default: num++;break;<br> }<br> case(14):if(zhuanjiao7==1637)<br> {<br> zhuanjiao2=<a href="http://www.elecfans.com/tags/74ls164/" target="_blank" class="relatedlink">164</a>3;zhuanjiao3=928;zhuanjiao4=1333;zhuanjiao5=1343;<br> zhuanjiao7 =1500;break;<br> }//处于白线 漫左脚 左脚回 90<br> else<br> { zhuanjiao2=1683;zhuanjiao3=938;zhuanjiao4=1333;<br> zhuanjiao5=1343;num++;break;<br> }<br> case(15):break;<br> case(16):break;<br> case(17):break;<br> case(18):break;<br> case(19):<br> if(zhuanjiao7!=1500)<br> {<br> zhuanjiao7=1500;<br> num--;<br> }//如果左脚是摩擦前进 先回 90<br> else<br> {<br> zhuanjiao6=1453;zhuanjiao7=1460;//右脚回 90 落地<br> 21<br> 5<br> 5<br> 5山东省高校第五届机器人大赛技术报告<br> }<br> break;<br> case(20):break;<br> case(21):zhuanjiao6=1296;zhuanjiao7=1360;break;//7 偏转角度减小后右偏<br> case(22):break;<br> case(23):break;<br> case(24):if(zhuanjiao6==1296)<br> {<br> zhuanjiao2=2037;zhuanjiao3=1332;zhuanjiao4=1667;zhuanjiao5=1677;<br> zhuanjiao6=1413;break;//漫右脚同时右脚回 90<br> }//调节 2 3 号舵机可使 67 舵机落脚时是否碰地<br> else<br> {<br> zhuanjiao2=2037;zhuanjiao3=1332;zhuanjiao4=1667;zhuanjiao5=1677;num++;b<br> reak;<br> }<br> }<br> num++;<br> if(num&gt;24)<br> {<br> if(KeyValue==1)<br> {<br> qianjin++;<br> if(qianjin&gt;2)<br> {<br> KeyValue=0;<br> mingling=1;<br> }<br> }<br> if(num==25)<br> num=5;<br> else<br> num=6;<br> cishu++;//多少步后开启中断<br> if(cishu&gt;28)<br> {<br> IntConfiguration();<br> }<br> }<br> }<br><br> }<br> void Delay10ms(unsigned int c)<br> {<br> unsigned char a,b;<br> for(;c&gt;0;c--)<br> for(b=30;b&gt;0;b--)<br> for(a=47;a&gt;0;a--);<br> }<br> //误差 0us<br> void PWM_config(void)<br> {<br> P0M0&amp;=~0xc0;<br> P0M1&amp;=~0xc0;<br> P0&amp;=~0xc0;<br> P1M0=0;<br> P1M1=0;<br> P2M0&amp;=~0x0e;<br> P2M1&amp;=~0x0e;<br> P2&amp;=~0x0e;<br> P3M0&amp;=~0x80;<br> P3M1&amp;=~0x80;<br> P3&amp;=~0x80;<br> P_SW2|=0x80;<br> PWMCKS=0x0b;<br> PWMC=CYCLE;<br> PWM2T1=1;<br> PWM2T2=0;<br> PWM2CR=0x00;<br> PWM3T1=1;<br> PWM3T2=0;<br> PWM3CR=0x00;<br> PWM4T1=1;<br> PWM4T2=0;<br> PWM4CR=0x00;<br> PWM5T1=1;<br> PWM5T2=0;<br> PWM5CR=0x00;<br> 23<br> //设置 P0.6/P0.7 电平<br> //设置 P2.1/P2.2/P2.3 电平<br> //设置 P3.7 电平<br> //PWM<a href="http://www.hqchip.com/app/1045" target="_blank" class="relatedlink">时钟</a>为<a href="http://www.elecfans.com/tags/时钟系统/" target="_blank" class="relatedlink">系统时钟</a>/12<br> //设置 PWM周期<br> //PWM2 输出到 P3.7<br> //PWM3 输出到 P2.1<br> //PWM4 输出到 P2.2<br> //PWM5 输出到 P2.3山东省高校第五届机器人大赛技术报告<br> PWM6T1=1;<br> PWM6T2=0;<br> PWM6CR=0x08;<br> PWM7T1=1;<br> PWM7T2=0;<br> PWM7CR=0x08;<br> PWMCFG=0x00;<br> PWMCR=0x3f;<br> PWMCR|=0x80;<br> P_SW2&amp;=~0x80;<br> }<br> void PWM2_SetPwmWide(unsigned short Wide)<br> {<br> if(Wide==0)<br> {<br> PWMCR&amp;=~0x01;<br> PWM2=0;<br> }<br> else if(Wide==CYCLE)<br> {<br> PWMCR&amp;=~0x01;<br> PWM2=1;<br> }<br> else<br> {<br> P_SW2|=0x80;<br> PWM2T1=Wide;<br> P_SW2&amp;=~0x80;<br> PWMCR|=0x01;<br> }<br> }<br> void PWM3_SetPwmWide(unsigned short Wide)<br> {<br> if(Wide==0)<br> {<br> PWMCR&amp;=~0x02;<br> PWM3=0;<br> }<br> else if(Wide==CYCLE)<br> {<br> PWMCR&amp;=~0x02;<br> 24<br> //PWM6 输出到 P0.7<br> //PWM7 输出到 P0.6<br> //配置 PWM的输出初始电平<br> //使能 PWM信号输出<br> //使能 PWM模块山东省高校第五届机器人大赛技术报告<br> PWM3=1;<br> }<br> else<br> {<br> P_SW2|=0x80;<br> PWM3T1=Wide;<br> P_SW2&amp;=~0x80;<br> PWMCR|=0x02;<br> }<br> }<br> void PWM4_SetPwmWide(unsigned short Wide)<br> {<br> if(Wide==0)<br> {<br> PWMCR&amp;=~0x04;<br> PWM4=0;<br> }<br> else if(Wide==CYCLE)<br> {<br> PWMCR&amp;=~0x04;<br> PWM4=1;<br> }<br> else<br> {<br> P_SW2|=0x80;<br> PWM4T1=Wide;<br> P_SW2&amp;=~0x80;<br> PWMCR|=0x04;<br> }<br> }<br> void PWM5_SetPwmWide(unsigned short Wide)<br> {<br> if(Wide==0)<br> {<br> PWMCR&amp;=~0x08;<br> PWM5=0;<br> }<br> else if(Wide==CYCLE)<br> {<br> PWMCR&amp;=~0x08;<br> PWM5=1;<br> }<br> else<br> {<br> P_SW2|=0x80;<br> PWM5T1=Wide;<br> P_SW2&amp;=~0x80;<br> PWMCR|=0x08;<br> }<br> }<br> void PWM6_SetPwmWide(unsigned short Wide)<br> {<br> if(Wide==0)<br> {<br> PWMCR&amp;=~0x10;<br> PWM6=0;<br> }<br> else if(Wide==CYCLE)<br> {<br> PWMCR&amp;=~0x10;<br> PWM6=1;<br> }<br> else<br> {<br> P_SW2|=0x80;<br> PWM6T1=Wide;<br> P_SW2&amp;=~0x80;<br> PWMCR|=0x10;<br> }<br> }<br> void PWM7_SetPwmWide(unsigned short Wide)<br> {<br> if(Wide==0)<br> {<br> PWMCR&amp;=~0x20;<br> PWM7=0;<br> }<br> else if(Wide==CYCLE)<br> {<br> PWMCR&amp;=~0x20;<br> PWM7=1;<br> }<br> else<br> {<br> P_SW2|=0x80; PWM7T1=Wide; P_SW2&amp;=~0x80; PWMCR|=0x20;<br> } }<br> <br> </d)<br><p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。