分类
笔记

51单片机机器人小车走8字程序代码

适用于51单片机的机器人小车走8字程序代码

走8字的原理图:

BoeBot_8

头文件 BoeBot.h

#ifndef __BOEBOT_H__
#define __BOEBOT_H__

#include <at89x52.h>

void delay_nms(int ms); 
void delay_nus(int us);

#endif

const unsigned char CONST_MILLISECONDCOUNT = 115;
const unsigned char CONST_MICROSECONDDIVISER = 12;

void move(int movefx,int movetime,int Velocity,int P10Velocity,int P11Velocity);
void delay_nms(int ms);
void delay_nus(int us);

void delay_nms(int ms) {
  unsigned char i;
  while((--ms) >= 0){
    for(i = 0;i < CONST_MILLISECONDCOUNT;i++);
  }
}

void delay_nus(int us) {
  us /= CONST_MICROSECONDDIVISER;
  while((--us) >= 0){}
}




/*********************************************************************************
*                             move() 函数说明                                     
*--------------------------------------------------------------------------------
*void move(int movefx,int movetime,int velocity,int P10Velocity,int P11Velocity) 
*--------------------------------------------------------------------------------
*参数:                                                                            
*                                                                                
*int movefx: 选择一个移动动作                                                       
*   1 - 前进                                                                  
*   2 - 后退                                                                  
*   3 - 左转                                                                  
*   4 - 右转                                                                  
*   5 - 原地左转                                                               
*   6 - 原地右转                                                              
*                                                                                 
*int movetime: 动作执行时间                                                                                                                                                 *                                             
*int velocity: 前进/后退/原地左右转的移动速度  (0<速度<=200)                            
*              如果使用左右转,这里参数取 0                                                                                                                                *                                                                              
*int P10Velocity: 定义P1_0针脚控制的舵机的速度                                       
*                 如果使用原地左右转,这里参数取 0
*
*int P11Velocity: 定义P1_1针脚控制的舵机的速度
*                 如果使用原地左右转,这里参数取 0
**********************************************************************************/

void move(int movefx,int movetime,int Velocity,int P10Velocity,int P11Velocity) {
    int i;
    switch (movefx) {
        case 1:         
                           for(i=1;i<=movetime;i++) {
                           P1_1=1;delay_nus(1500+Velocity);P1_1=0;
                           P1_0=1;delay_nus(1500-Velocity);P1_0=0;
                           delay_nms(20);
                   }
        break;

                case 2: 
                   for(i=1;i<=movetime;i++) {
                           P1_1=1;delay_nus(1500-Velocity);P1_1=0;
                           P1_0=1;delay_nus(1500+Velocity);P1_0=0;
                           delay_nms(20);
                           }
        break;
        
                case 3:
               for(i=1;i<=movetime;i++) {
                           P1_1=1;delay_nus(1500-P11Velocity);P1_1=0;
                           P1_0=1;delay_nus(1500+P10Velocity);P1_0=0;
                           delay_nms(20);
                           }
        break;
        
        case 4:
               for(i=1;i<=movetime;i++) {
                           P1_1=1;delay_nus(1500-P11Velocity);P1_1=0;
                           P1_0=1;delay_nus(1500+P10Velocity);P1_0=0;
                           delay_nms(20);
                            }
        break;
    
        case 5:
                   for(i=1;i<=movetime;i++) {
                           P1_1=1;delay_nus(1500-Velocity);P1_1=0;
                           P1_0=1;delay_nus(1500-Velocity);P1_0=0;
                           delay_nms(20);
                           }
        break;
        
        case 6:
                   for(i=1;i<=movetime;i++) {
                           P1_1=1;delay_nus(1500+Velocity);P1_1=0;
                           P1_0=1;delay_nus(1500+Velocity);P1_0=0;
                           delay_nms(20);
                           }
        break;
        
        default:break;      
    }
}

 

主程序 main.c

#include "BoeBot.h"
void main(void) {
    move(3,1800,0,100,500);
    move(3,1800,0,500,100);
}

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注