以前学习51的时候为了调试机械臂角度编写的程序,用在开发板上也比较方便,但是由于是普通的舵机,不是总线的,所以需要自己洞洞板焊一个供电带电路。
单片机源程序如下:
- #include <reg52.h>
- #include <lcd1602.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit duoji0=P2^0; //PWM输出
- sbit duoji1=P2^1;
- sbit duoji2=P2^2;
- sbit duoji3=P2^3;
- sbit duoji4=P3^1;
- sbit duoji5=P3^0;
- #define GPIO_KEY P1
-
- unsigned int k0;
- unsigned int k1;
- unsigned int k2;
- unsigned int k4;
- unsigned int k3;
- unsigned int k5; //定义下降沿节点
- uint dsip[8]={0};
- uint j=0;
- unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','D',' '};
- void delayms(uint xms)
- {
- uchar j;
- uint i;
- for(i=xms;i>0;i--)
- for(j=112;j>0;j--);
- }
- void time0() interrupt 1
- {
- static uint ii0=0;
- TH0=(65536-100)/256; //以20ms为一个周期,以0.1ms为一个脉冲宽度
- TL0=(65536-100)%256;
- ii0++;
-
- if(ii0<=k0)
- duoji0=1; //0号舵机pwm输出
- else
- duoji0=0;
-
- if(ii0<=k1) //1号舵机pwm输出
- duoji1=1;
- else
- duoji1=0;
- if(ii0<=k2) //2号舵机pwm输出
- duoji2=1;
- else
- duoji2=0;
- if(ii0<=k3) //3号舵机pwm输出
- duoji3=1;
- else
- duoji3=0;
-
- if(ii0<=k4) //4号舵机pwm输出
- duoji4=1;
- else
- duoji4=0;
-
- if(ii0<=k5) //5号舵机pwm输出
- duoji5=1;
- else
- duoji5=0;
-
- if(ii0==200)
- ii0=0;
- }
- void time0init()
- {
- TMOD=0x01;
- TH0=(65536-100)/256;
- TL0=(65536-100)%256;
- TR0=1;
- ET0=1;
- EA=1;
- }
- char KeyDown()
- {
- uchar KeyValue=20;
- uchar a=0;
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)//读取按键是否按下
- {
- delayms(10);//延时10ms进行消抖
- if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
- {
- //测试列
- GPIO_KEY=0X0F;
- switch(GPIO_KEY)
- {
- case(0X07): KeyValue=0;break;
- case(0X0b): KeyValue=1;break;
- case(0X0d): KeyValue=2;break;
- case(0X0e): KeyValue=3;break;
- }
- //测试行
- GPIO_KEY=0XF0;
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+4;break;
- case(0Xd0): KeyValue=KeyValue+8;break;
- case(0Xe0): KeyValue=KeyValue+12;break;
- }
- while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键松手检测,当按键等于0xf时候跳出循环
- {
- delayms(100);
- a++;
- }
- }
- }
- return KeyValue;
- }
- void count()
- {
-
- LcdWriteData(ASCII[0]);
- LcdWriteData(ASCII[11]);
- LcdWriteData(ASCII[k0/10]);
- LcdWriteData(ASCII[k0%10]);
- LcdWriteData(ASCII[12]);
-
- LcdWriteData(ASCII[1]);
- LcdWriteData(ASCII[11]);
- LcdWriteData(ASCII[k1/10]);
- LcdWriteData(ASCII[k1%10]);
- LcdWriteCom(0x80+0x40);
-
- LcdWriteData(ASCII[2]);
- LcdWriteData(ASCII[11]);
- LcdWriteData(ASCII[k2/10]);
- LcdWriteData(ASCII[k2%10]);
- LcdWriteData(ASCII[12]);
-
- LcdWriteData(ASCII[3]);
- LcdWriteData(ASCII[11]);
- LcdWriteData(ASCII[k3/10]);
- LcdWriteData(ASCII[k3%10]);
-
- }
- void main()
- {
- time0init();
- k0=15;
- k1=9;
- k2=16;
- k3=14;
- k4=8;
- k5=8;
- LcdInit();
- count();
-
- while(1)
- {
- uchar KeyValue1=20;
- KeyValue1=KeyDown();
-
-
-
-
-
-
-
- if(KeyValue1==0&&k0<=20&&k0>=8) //0号舵机高电平时间增加
- {
- k0++;
- if(k0==21)
- k0=20;
- KeyValue1=20;LcdWriteCom(0x01); count();
- }
-
- if(KeyValue1==4&&k0<=20&&k0>=8) //0号舵机高电平时间减少
- {
-
- k0--;
- if(k0==7)
- k0=8;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==1&&k1<=20&&k1>=8) //1号舵机高电平时间增加
- {
-
- k1++;
- if(k1==21)
- k1=20;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==5&&k1<=20&&k1>=8)
- {
-
- k1--;
- if(k1==7)
- k1=8;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==2&&k2<=20&&k2>=8) //2号舵机高电平时间增加
- {
-
- k2++;
- if(k2==21)
- k2=20;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==6&&k2<=20&&k2>=8)
- {
-
- k2--;
- if(k2==7)
- k2=8;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==3&&k3<=20&&k3>=8) //3号舵机高电平时间增加
- {
-
- k3++;
- if(k3==21)
- k3=20;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==7&&k3<=20&&k3>=8)
- {
-
- k3--;
- if(k3==7)
- k3=8;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==8&&k4<=20&&k4>=8) //4号舵机高电平时间增加
- {
-
- k4++;
- if(k4==21)
- k4=20;
- KeyValue1=20;LcdWriteCom(0x01); count();
- }
-
- if(KeyValue1==12&&k4<=20&&k4>=8)
- {
-
- k4--;
- if(k4==7)
- k4=8;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- if(KeyValue1==9&&k5<=20&&k5>=8) //5号舵机高电平时间增加
- {
-
- k5++;
- if(k5==21)
- k5=20;
- KeyValue1=20;LcdWriteCom(0x01); count();
- }
-
- if(KeyValue1==13&&k5<=20&&k5>=8)
- {
-
- k5--;
- if(k5==7)
- k5=8;
- KeyValue1=20; LcdWriteCom(0x01);count();
- }
-
- }
- }
-
复制代码
所有资料51hei提供下载:
51arm1602pwm.rar
(44.41 KB, 下载次数: 94)
|