打印
[华山论剑]

51单片机控制舵机转动(不用按键)自动转动

[复制链接]
1051|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
潘宁不一般|  楼主 | 2022-4-24 21:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
整个程序不就是改变相应的timer1的值控制转动特定角度么,为什么无论我改变什么数,都旋转90度啊,而且有正90有负90,timer1=10的时候还会从-90到-27.6来回转是怎么回事,角度的正和负是怎么控制的
[color=rgba(0, 0, 0, 0.75)]
  • // _nop_()延时一个机器周期

  • [color=rgb(119, 120, 136) !important]

    #include <reg52.h>

  • [color=rgb(119, 120, 136) !important]

    #include <intrins.h>

  • [color=rgb(119, 120, 136) !important]

    sbit PWM = P3^0;  //设定PWM输出的I/O端口

  • [color=rgb(119, 120, 136) !important]

    unsigned char count = 0;

  • [color=rgb(119, 120, 136) !important]

    unsigned char timer1 ;

  • [color=rgb(119, 120, 136) !important]

    /*

  • [color=rgb(119, 120, 136) !important]

    对于180°舵机

  • [color=rgb(119, 120, 136) !important]

    t = 0.5ms——————-舵机会转动 0 °

  • [color=rgb(119, 120, 136) !important]

    t = 1.0ms——————-舵机会转动 45°

  • [color=rgb(119, 120, 136) !important]

    t = 1.5ms——————-舵机会转动 90°

  • [color=rgb(119, 120, 136) !important]

    t = 2.0ms——————-舵机会转动 135°

  • [color=rgb(119, 120, 136) !important]

    t = 2.5ms——————-舵机会转动180

  • [color=rgb(119, 120, 136) !important]

    */

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    /*延时程序*/

  • [color=rgb(119, 120, 136) !important]

    void delay1s(void)   //误差 0us

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

        unsigned char a,b,c;

  • [color=rgb(119, 120, 136) !important]

        for(c=167;c>0;c--)

  • [color=rgb(119, 120, 136) !important]

            for(b=171;b>0;b--)

  • [color=rgb(119, 120, 136) !important]

                for(a=16;a>0;a--);

  • [color=rgb(119, 120, 136) !important]

        _nop_();  //if Keil,require use intrins.h

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

    /*定时器T0初始化*/

  • [color=rgb(119, 120, 136) !important]

    void Timer0_Init()           

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

        TMOD &= 0x00;

  • [color=rgb(119, 120, 136) !important]

        TMOD |= 0x01; //定时器T0设置成方式1

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

        TH0 = 0xff;   //定时常数 0.1ms 晶振为11.0592MHz

  • [color=rgb(119, 120, 136) !important]

        TL0 = 0xa4;

  • [color=rgb(119, 120, 136) !important]



  • [color=rgb(119, 120, 136) !important]

        ET0 = 1;      

  • [color=rgb(119, 120, 136) !important]

        TR0 = 1;

  • [color=rgb(119, 120, 136) !important]

            EA=1;

  • [color=rgb(119, 120, 136) !important]

            }

  • [color=rgb(119, 120, 136) !important]

           

  • [color=rgb(119, 120, 136) !important]

    /*T0中断初始化*/

  • [color=rgb(119, 120, 136) !important]

    void Time0_Init() interrupt 1

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

            TR0 = 0;

  • [color=rgb(119, 120, 136) !important]

            TH0 = 0xff; // 0.1ms

  • [color=rgb(119, 120, 136) !important]

            TL0 = 0xa4;

  • [color=rgb(119, 120, 136) !important]

           

  • [color=rgb(119, 120, 136) !important]

            if(count <= timer1) //5==0° 15==90°

  • [color=rgb(119, 120, 136) !important]

            {

  • [color=rgb(119, 120, 136) !important]

                    PWM = 1;

  • [color=rgb(119, 120, 136) !important]

            }

  • [color=rgb(119, 120, 136) !important]

            else

  • [color=rgb(119, 120, 136) !important]

            {

  • [color=rgb(119, 120, 136) !important]

                    PWM = 0;

  • [color=rgb(119, 120, 136) !important]

            }

  • [color=rgb(119, 120, 136) !important]

            count++;

  • [color=rgb(119, 120, 136) !important]

            if (count >= 200) //T = 20ms清零

  • [color=rgb(119, 120, 136) !important]

            {

  • [color=rgb(119, 120, 136) !important]

                    count = 0;

  • [color=rgb(119, 120, 136) !important]

            }

  • [color=rgb(119, 120, 136) !important]

            TR0 = 1; //开启T0

  • [color=rgb(119, 120, 136) !important]

    }

  • [color=rgb(119, 120, 136) !important]

    void main()

  • [color=rgb(119, 120, 136) !important]

    {

  • [color=rgb(119, 120, 136) !important]

            Timer0_Init();

  • [color=rgb(119, 120, 136) !important]

            while(1)

  • [color=rgb(119, 120, 136) !important]

            {

  • [color=rgb(119, 120, 136) !important]

                   

  • [color=rgb(119, 120, 136) !important]

                    timer1 =5;//舵机恢复到0°的位置

  • [color=rgb(119, 120, 136) !important]

                    count=0;//让定时器重新计数

  • [color=rgb(119, 120, 136) !important]

                    delay1s();

  • [color=rgb(119, 120, 136) !important]

                    timer1 =10;

  • [color=rgb(119, 120, 136) !important]

                    count=0;

  • [color=rgb(119, 120, 136) !important]

                    delay1s();

  • [color=rgb(119, 120, 136) !important]

            }

  • [color=rgb(119, 120, 136) !important]

    }







  • 收起



使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

16

帖子

0

粉丝