⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 microcontroller.c

📁 老外开发的机器人的底层单片机代码。比较有参考价值哦!
💻 C
字号:
//Microcontroller main program   Microcontroller.c			// Rev 9/1/05

//Copyright (C) 2005 Alex Brown  rbirac@cox.net
//This program is free software; See license at the end of this file for details.
		 	   									//Rev 10/18/04
#include "Microcontroller.h"	       

//Global Variables
   unsigned int time;	    // modified by interrupts.c (rollover 17.9 minutes)
   int BiteMode = 0;	 	// flag: Bite mode engaged = 1, off = 0
   int mode     = 0;		// current real time mode
   int ucStatus = 0;		// various status flags
   	   			  			   // bit 15 = drive calib status
							   // bit 14 = config status
							   // bit 13 = calib status
							   // bit 11 = data logging status (1 = on, 0 = off)
   int LogFlag  = 0;		// data logging ready for new data flag
      
//External Variables   
  extern int FwdModeDone;
  extern int TurnModeDone;
//  extern long int enc0,enc2;
//  extern long int DistActL,DistActR;
  
//Prototypes
  void  atdinit(void);
  void  motorinit(void);
  void  accelinit(void);
  void  I2Cinit(void);
  void  USBinit(void);
  void  miscinit(void);
  void  Bite(void);
  void  Analogs(void);
  void  sonar(void);
  void  RCservo(void);
  int   RGnull(void);
  void  DGyro(void);
  void  I2Ccompass(void);
  void  USBupdate(void);
  void  MotorExec(void);
  void  odometry(void);
  void  ProcessCmds(void);
  void  MotorStop(int);
  void  DataLogInit(void);
  void  DataWrite(void);
  void  DataPrint(void);
  void  Tilt(void);
  void  DIOread(void);

//-------------------------------------------------------------------------
void rtexec()
  {	
  ProcessCmds();	//Process new commands (if any)
  DIOread();		//read DIO ports														   
  I2Ccompass();		//read compass 
  Analogs();        //read analog inputs
  sonar();	  		//read sonar sensors
  RCservo();  		//update RC servo commands and positions
  DGyro();			//update gyro rates and headings
  Tilt();			//update accelerometer tilt readings
  USBupdate();	    //receive and transmit USB messages
  odometry();		//read encoders and update position data
  MotorExec();		//calculate motor drive commands

  if(LogFlag == 0)  //Get new set of data to log,if last batch has been written
  {  DataWrite();
     LogFlag = 1;
  }
  	 
  switch (mode)   //select current mode to execute
    {case 0:
		if (BiteMode == 1) break;
	   	if (SCI0SR1 & 0x20)	//check if Bite selected (esc char)
	 	   {if (getchar() == 27) BiteMode = 1; break;
	 	   } 
	 break;  
	   
	}  //end switch   
//printf("rc enc0  %ld  %ld    %ld   %ld\n",enc0,DistActL, enc2,DistActR);
  }    //end rtexec 
//-------------------------------------------------------------------------
void main()
 { 	
 //Set processor clock to 24MHz
	REFDV = 0X01;	  	 	     //PLLCLK = 2 x 8Mhz * (SYNR+1)/(REFDV+1)
	SYNR  = 0X02;
	while (~CRGFLG & 0x08);	  	 //Wait until PLL is in lock
	CLKSEL = 0X80;	  	 		 //Select PLL clock
	 	
//Set baudrate for monitor (serial port 0)
	//SCI0BD = 52;				 //9600 baud at 8 Mhz
	//SCI0BD = 156;  	  		 //9600 baud at 24 Mhz 
	SCI0BD = 13;  	  		 	 //115200 baud at 24 Mhz  
	
	SCI0CR2 = 0x0C;   			 //enable transmitter and receiver
    printf("\n// Processor is running!\n");

//Initialize functions
   atdinit();		  //start up atd converters
   //motorinit();	  //is called in ProcessCmds after MotorType is received
   //timerinit();	  //is called in ProcessCmds after NumSonars is received
   accelinit();
//   RGnull();   		  //calculate yaw rate gyro null
printf("//bypassed RGnull\n");
   I2Cinit();		  //initialize I2C to read compass
   USBinit();
   MotorStop(400);	  //set motor to stopped mode      
   
   FwdModeDone  = 1; //set motor commands to complete
   TurnModeDone = 1; //set turn commands to complete
      
//Enable Real Time Interrupt
   RTICTL  = 0x73; 			//set RTI rate to 61 hz.
   CRGINT |= 0x80; 			//set RTIE bit (must also do INTR_ON();)
   		 	
   INTR_ON(); 				//turn on interrupts
   
   while(1)					//background loop
	{ 
	  if (BiteMode)  Bite();
	  
	  if(LogFlag == 1)  //if a new set of data loaded, print it
	  {
	     DataPrint();
	  	 LogFlag = 0;
	  }
	  //other background tasks here
	  
	}     //end while	  
 }     //end main()

//  OPEN SOURCE SOFTWARE LICENSE
/* Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use, 
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -