Update I2C library
This commit is contained in:
parent
f7b0c6f1fc
commit
79159729cd
2 changed files with 18 additions and 24 deletions
|
|
@ -1,17 +1,17 @@
|
||||||
#ifndef _I2CMASTER_H
|
#ifndef _I2CMASTER_H
|
||||||
#define _I2CMASTER_H 1
|
#define _I2CMASTER_H
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Title: C include file for the I2C master interface
|
* Title: C include file for the I2C master interface
|
||||||
* (i2cmaster.S or twimaster.c)
|
* (i2cmaster.S or twimaster.c)
|
||||||
* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
|
* Author: Peter Fleury <pfleury@gmx.ch>
|
||||||
* File: $Id: i2cmaster.h,v 1.10 2005/03/06 22:39:57 Peter Exp $
|
* File: $Id: i2cmaster.h,v 1.12 2015/09/16 09:27:58 peter Exp $
|
||||||
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
|
* Software: AVR-GCC 4.x
|
||||||
* Target: any AVR device
|
* Target: any AVR device
|
||||||
* Usage: see Doxygen manual
|
* Usage: see Doxygen manual
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
#ifdef DOXYGEN
|
|
||||||
/**
|
/**
|
||||||
|
@file
|
||||||
@defgroup pfleury_ic2master I2C Master library
|
@defgroup pfleury_ic2master I2C Master library
|
||||||
@code #include <i2cmaster.h> @endcode
|
@code #include <i2cmaster.h> @endcode
|
||||||
|
|
||||||
|
|
@ -38,7 +38,8 @@
|
||||||
Replaced the incorrect quarter period delays found in AVR300 with
|
Replaced the incorrect quarter period delays found in AVR300 with
|
||||||
half period delays.
|
half period delays.
|
||||||
|
|
||||||
@author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
|
@author Peter Fleury pfleury@gmx.ch http://tinyurl.com/peterfleury
|
||||||
|
@copyright (C) 2015 Peter Fleury, GNU General Public License Version 3
|
||||||
|
|
||||||
@par API Usage Example
|
@par API Usage Example
|
||||||
The following code shows typical usage of this library, see example test_i2cmaster.c
|
The following code shows typical usage of this library, see example test_i2cmaster.c
|
||||||
|
|
@ -77,7 +78,7 @@
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
*/
|
*/
|
||||||
#endif /* DOXYGEN */
|
|
||||||
|
|
||||||
/**@{*/
|
/**@{*/
|
||||||
|
|
||||||
|
|
@ -96,7 +97,6 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief initialize the I2C master interace. Need to be called only once
|
@brief initialize the I2C master interace. Need to be called only once
|
||||||
@param void
|
|
||||||
@return none
|
@return none
|
||||||
*/
|
*/
|
||||||
extern void i2c_init(void);
|
extern void i2c_init(void);
|
||||||
|
|
@ -104,7 +104,6 @@ extern void i2c_init(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief Terminates the data transfer and releases the I2C bus
|
@brief Terminates the data transfer and releases the I2C bus
|
||||||
@param void
|
|
||||||
@return none
|
@return none
|
||||||
*/
|
*/
|
||||||
extern void i2c_stop(void);
|
extern void i2c_stop(void);
|
||||||
|
|
@ -164,7 +163,7 @@ extern unsigned char i2c_readNak(void);
|
||||||
/**
|
/**
|
||||||
@brief read one byte from the I2C device
|
@brief read one byte from the I2C device
|
||||||
|
|
||||||
Implemented as a macro, which calls either i2c_readAck or i2c_readNak
|
Implemented as a macro, which calls either @ref i2c_readAck or @ref i2c_readNak
|
||||||
|
|
||||||
@param ack 1 send ack, request more data from device<br>
|
@param ack 1 send ack, request more data from device<br>
|
||||||
0 send nak, read is followed by a stop condition
|
0 send nak, read is followed by a stop condition
|
||||||
|
|
@ -174,5 +173,6 @@ extern unsigned char i2c_read(unsigned char ack);
|
||||||
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
|
#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**@}*/
|
/**@}*/
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Title: I2C master library using hardware TWI interface
|
* Title: I2C master library using hardware TWI interface
|
||||||
* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
|
* Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
|
||||||
* File: $Id: twimaster.c,v 1.3 2005/07/02 11:14:21 Peter Exp $
|
* File: $Id: twimaster.c,v 1.4 2015/01/17 12:16:05 peter Exp $
|
||||||
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
|
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
|
||||||
* Target: any AVR device with hardware TWI
|
* Target: any AVR device with hardware TWI
|
||||||
* Usage: API compatible with I2C Software Library i2cmaster.h
|
* Usage: API compatible with I2C Software Library i2cmaster.h
|
||||||
|
|
@ -12,13 +12,13 @@
|
||||||
#include <i2cmaster.h>
|
#include <i2cmaster.h>
|
||||||
|
|
||||||
|
|
||||||
/* define CPU frequency in Mhz here if not defined in Makefile */
|
/* define CPU frequency in hz here if not defined in Makefile */
|
||||||
#ifndef F_CPU
|
#ifndef F_CPU
|
||||||
#define F_CPU 16000000UL
|
#define F_CPU 4000000UL
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* I2C clock in Hz */
|
/* I2C clock in Hz */
|
||||||
#define SCL_CLOCK 400000L
|
#define SCL_CLOCK 100000L
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
|
|
@ -26,16 +26,10 @@
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
void i2c_init(void)
|
void i2c_init(void)
|
||||||
{
|
{
|
||||||
/* initialize TWI clock
|
/* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
|
||||||
* minimal values in Bit Rate Register (TWBR) and minimal Prescaler
|
|
||||||
* bits in the TWI Status Register should give us maximal possible
|
|
||||||
* I2C bus speed - about 444 kHz
|
|
||||||
*
|
|
||||||
* for more details, see 20.5.2 in ATmega16/32 secification
|
|
||||||
*/
|
|
||||||
|
|
||||||
TWSR = 0; /* no prescaler */
|
TWSR = 0; /* no prescaler */
|
||||||
TWBR = 10; /* must be >= 10 for stable operation */
|
TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
|
||||||
|
|
||||||
}/* i2c_init */
|
}/* i2c_init */
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue