14年8个月前IP:未同步
391570
5楼
楼上的那个程序晶振是多大的,我也复制上来一段代码吧,但这个代码就是只能转一圈,不能回来。悲剧,我也改不好
谁可以帮我把这个程序改一下,可以让舵机左右摇摆的?我改了好久也改不好了
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dr1=P1^0;

uint cnt1;
uchar x = 11;    //X表示脉冲宽度 。开始为1ms

void delay(uint xms);    //x毫秒级延时

void main()
{
    
    TMOD  = 0x01;
    TH0 = 0xff;               //TH0 = (65536-100)/256;      
    TL0 = 0x9c;               //TL0 = (65536-100)%256;    
    EA = 1;
    ET0 = 1;
    TR0 = 1;
    while(1)
    {              
                                
                               x = 15;        //中心位置 1.5ms脉冲
        delay(1000);   //延时1s
                              
                                x = 13;        //左转     1.3ms脉冲
        delay(1000);
                                
                    
          
        x = 17;        //右转     1.7ms脉冲
        delay(1000);
                                        
                                  
                                        
    }
}

void delay(uint xms)          //x毫秒级延时
{
    uint i,j;
    for(i=xms;i>0;i--)
    for(j=120;j>0;j--);
}

void T0_timer() interrupt 1      //0.1ms中断  PPM信号发生器   1-2ms(1.5ms时为舵机中心值) 采用的是51单片机速度不是很快,所以精度不高
{
    TH0 = 0xff;               //TH0 = (65536-100)/256;      
    TL0 = 0x9c;               //TL0 = (65536-100)%256;        
    cnt1++;                      //中断计数
    
    if(cnt1==x)              //高电平为 X*0.1 ms
    {
        dr1=1;            //输出高电平
    }
    else
    {
        dr1 =0;            //高电平结束,剩下的时间执行低电平
        if(cnt1==200)           //周期为20ms
                      
                            {
        cnt1 = 0;          //周期完成,重新计数
        }    
    }
                    
}
评论
文号 / 391570

百炼成钢
名片发私信
学术分 0
总主题 33 帖总回复 844 楼拥有证书:机友 笔友
注册于 2007/08/06 23:04:00最后登录 2026/07/12 20:01:02
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:0

个人简介

暂未填写
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的