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

📄 i2ccompass.c

📁 老外开发的机器人的底层单片机代码。比较有参考价值哦!
💻 C
字号:
//  I2C bus and compass    I2Ccompass.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.

/*
   Provides functions for initializing the I2C module, and for starting a 
   message transaction to read the heading value from a CMPS03 compass.
   
   Most of the transaction is performed by interrupts occuring after this 
   function has completed.  The full transaction takes about 550 usec.
   Hence, the updated "compass" data is NOT available at the end of this 
   function. So, the interrupt routine reads the value as "compass", and this
   function loads the data from the previous "compass" transaction into 
   "Compass" for export 16 msec later when the transaction is complete. 
   This results in 16 msec of latency, but I hope that won't matter much 
   for compass data.
*/

#include <hcs12dp256.h>		    //for registers
#include <stdio.h>				//for i/o prototypes

int I2Ccount;		//counter for I2C interrupt sequencing
int compass;		//compass heading scaled 0 to 3599. (9999 = invalid)
					//local(& interrupts) value, not valid at times
int Compass;		//exported value

// I2c initialization   move to initialization section ???
void I2Cinit()
  {
    IBAD = 0xF0;      // Bus Address  in slave mode  Bits 1 - 7  Bit 0 is
                      // reserved. Address must be diff from all others on bus.
    //IBFD = 0x18;    // Prescalers and clock dividers (100Khz from 8Mhz clock)
    IBFD = 0x1F;      // Prescalers and clock dividers (100Khz from 24Mhz clock)
    		  		  //   (per AN2318/D and HCS12IIC block guide)
    IBCR = 0xC0;      // IIC Bus enabled (bit7)  Interrupts enabled (6)
    IBSR = 0x80;	  // default values
  }
  
// Read I2C compass heading  
void I2Ccompass()
  {
    Compass = compass;  	//export last completed value
    IBCR |= 0x30;			//set I2C as master and xmit
	IBCR &=~0x08;			//TXAK cleared from previous message
	I2Ccount = 0;			//set count to first byte
	compass = 9999;			//initialize to invalid
	IICDR = 0xC0;			//compass address in transmit mode
		  					//(rest of transaction is done by I2C_handler
							//   in interrupts.c)
  }
  
//  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 + -