📄 maxqi2c.h
字号:
/*
* maxqi2c.h v0.1
*
* Author: Paul Holden
*
* Library function for performing I2C communication
*
* See the associated Maxim Appnote for documentation
*
* NOTE: THE TIMING IN THIS CODE ASSUMES THE MAXQ2000
* IS RUNNING FROM A 20MHZ CLOCK AND THE
* OPTIMIZER IS SET TO RUN AT THE SETTING 3
*
* HISTORY
* v0.1 - June 1st, 2005: File created
*
*/
#ifndef __MAXQI2C_H__
#define __MAXQI2C_H__
#include "iomaxq200x.h"
/* USER MUST CUSTOMIZE THE FOLLOWING DEFINE STMTS - START */
// Enter the port used for SDA and SCL
#define SDA_PORT 0
#define SCL_PORT 0
// Enter the pin used for SDA and SCL
#define SDA_PORT_BIT 0
#define SCL_PORT_BIT 1
// Uncomment one of these define statements to select I2C bus speed
#define I2C_400_KHZ
//#define I2C_100_KHZ
// Comment out the following define statement to disable clock
// stretching in i2cRecv()
#define I2C_CLOCK_STRETCHING
/* USER MUST CUSTOMIZE THE FOLLOWING DEFINE STMTS - END */
// The possible values for the i2cDataTerm variable
// i2cSend
#define I2C_TERM_NONE 0x00
#define I2C_TERM_STOP 0x01
//i2cRecv (I2C_TERM_NONE will work but is not recommended during an i2cRecv)
#define I2C_TERM_ACK 0x02
#define I2C_TERM_NACK_AND_STOP 0x03
// The possible return values from the i2cSend and i2cRecv functions
#define I2C_XMIT_FAILED 0x00
#define I2C_XMIT_OK 0x01
// The parameters to the functions:
//
// i2cIsAddrPresent()
// i2cSend()
// i2cRecv
//
// See the comments to the actual functions for the
// usage of these variables.
//
// These are made global and extern to allow any module
// in the application to edit them without the need to
// send them as parameters to the function. This design
// decision was made to increase the speed of the system.
extern volatile unsigned char *i2cData;
extern volatile unsigned int i2cDataLen;
extern volatile unsigned char i2cDataAddr;
extern volatile unsigned char i2cDataTerm;
/*
* i2cInit()
*
* Params:
* none
*
* Returns:
* nothing
*
* Description: Initializes the pins that are used as SDA
* and SCL. This function must be called before any other
* of the i2c functions are called
*
*/
void i2cInit();
/*
* i2cIsAddrPresent()
*
* Params:
* i2cDataAddr (global unsigned char): The i2c address
*
* Returns:
* unsigned char: I2C_XMIT_FAILED or I2C_XMIT_OK
*
* Description: Send the i2c address (with a write indicator)
* to see if a slave sends an acknowledge. The address send
* is taken from the i2cDataAddr global and the result
* should be compared with I2C_XMIT_FAILED and I2C_XMIT_OK
* to determine if the slave was present. The format is
*
* [S] [ADDR+W] [A] [P]
*
*/
unsigned char i2cIsAddrPresent();
/*
* i2cSend()
*
* Params:
* *i2cData (global unsigned char *): Pointer to the i2c data bytes
* i2cDataLen (global unsigned char) : The i2c data length (not incl addr)
* i2cDataAddr (global unsigned char) : The i2c address
* i2cDataTerm (global unsigned char) : The i2c transmit ternimation
*
* Returns:
* unsigned char: I2C_XMIT_FAILED or I2C_XMIT_OK
*
* Description: Sends i2c data to a slave. The format is
*
* [S] [ADDR+W] [A] [DATA0] [A] [DATA1] [A] ... [DATA LEN-1] [A] [TERM]
* ^-Slave ^-Slave ^-Slave ^-Slave
*
* NOTE1: This function allows the header ([ADDR+W] [A]) not to be
* sent by setting the i2cDataAddr to 0x00.
*
* NOTE2: This function allows a custom termination by setting the
* i2cDataTerm to I2C_TERM_NONE (no termination) or I2C_TERM_STOP
* (an actual NACK then a P signal on the I2C bus, where P is a
* stop signal).
*
* NOTE3: It is the responsibility of the user to ensure that the
* array pointed to by i2cData (unsigned char *) has i2cDataLen
* data bytes in it.
*
*/
unsigned char i2cSend();
/*
* i2cRecv()
*
* Params:
* *i2cData (global unsigned char *): Pointer to the i2c data bytes
* i2cDataLen (global unsigned char) : The i2c data length (not incl addr)
* i2cDataAddr (global unsigned char) : The i2c address
* i2cDataTerm (global unsigned char) : The i2c transmit ternimation
*
* Returns:
* unsigned char: I2C_XMIT_FAILED or I2C_XMIT_OK
*
* Description: Receives i2c data from a slave. The format is
*
* [S] [ADDR+R] [A] [DATA0] [A] [DATA1] [A] ... [DATA LEN-1] [TERM]
* ^-Slave ^-Master ^-Master
*
* NOTE1: This function allows the header ([ADDR+R] [A]) not to be
* sent by setting the i2cDataAddr to 0x00.
*
* NOTE2: This function allows a custom termination by setting the
* i2cDataTerm to I2C_TERM_NONE (no termination), I2C_TERM_ACK
* (an A), or a I2C_TERM_NACK_AND_STOP (an actual NACK then a P
* signal on the I2C bus, where P is a stop signal).
*
* NOTE3: It is the responsibility of the user to ensure that the
* array pointed to by i2cData (unsigned char *) has i2cDataLen
* data bytes available.
*
*/
unsigned char i2cRecv();
// THE PROGRAMMER SHOULD NOT EDIT ANYTHING BELOW THIS LINE
#define PORT_OUT_BIT(A,B) PO## A ##_bit.bit## B
#define PORT_DIR_BIT(A,B) PD## A ##_bit.bit## B
#define PORT_IN_BIT(A,B) PI## A ##_bit.bit## B
// Macros required for double expansion
#define PORT_OUT_BIT_EXP(A,B) PORT_OUT_BIT(A,B)
#define PORT_DIR_BIT_EXP(A,B) PORT_DIR_BIT(A,B)
#define PORT_IN_BIT_EXP(A,B) PORT_IN_BIT(A,B)
#define SDA_PULLUP PORT_OUT_BIT_EXP(SDA_PORT,SDA_PORT_BIT)
#define SCL_PULLUP PORT_OUT_BIT_EXP(SCL_PORT,SCL_PORT_BIT)
#define SDA PORT_DIR_BIT_EXP(SDA_PORT,SDA_PORT_BIT)
#define SCL PORT_DIR_BIT_EXP(SCL_PORT,SCL_PORT_BIT)
#define SDA_IN PORT_IN_BIT_EXP(SDA_PORT,SDA_PORT_BIT)
#define SCL_IN PORT_IN_BIT_EXP(SCL_PORT,SCL_PORT_BIT)
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -