| ??? 06/09/08 23:41 Read: times |
#155645 - Well that's not gonna happen.... Responding to: ???'s previous message |
Worms, in a Keil file....<laughing>
Here's the source main.c
#include "REG936.h"
#include "i2c.h"
#include "i2c_callback.h"
void main (void)//main program
{
while(1)//Loop this section forever
{
EA=1;
i2c_isr();
i2c_init(0x80, 0);
i2c_master_getbyte(0x03);
i2c_transmit(0xEE);
STO = 1;
i2c_transmit(0xDD);
STO = 1;
i2c_transmit(0xCC);
STO = 1;
i2c_transfer_finished();
}
}
i2c.c
/***********************************************************************
MODULE: I2C
VERSION: 1.03
CONTAINS: Routines for controlling the I2C Peripheral on the Philips
P89LPC936
COPYRIGHT: Embedded Systems Academy, Inc. - www.esacademy.com
LICENSE: May be freely used in commercial and non-commercial code
without royalties provided this copyright notice remains
in this file and unaltered
WARNING: IF THIS FILE IS REGENERATED BY CODE ARCHITECT ANY CHANGES
MADE WILL BE LOST. WHERE POSSIBLE USE ONLY CODE ARCHITECT
TO CHANGE THE CONTENTS OF THIS FILE
GENERATED: On "Jun 09 2008" at "11:54:01" by Code Architect 2.12
***********************************************************************/
// SFR description needs to be included
#include "reg936.h"
#include "i2c.h"
#include "i2c_callback.h"
// MODULE INTERNAL VARIABLES
// internal copy of slave address
static unsigned char mslaveaddress;
// I2C transfer status
static unsigned char mi2cstatus;
// current number of byte transferred
static unsigned int mbytenum;
// MODULE INTERNAL FUNCTIONS
/***********************************************************************
DESC: I2C interrupt service routine. handles the transfer and
calls the relevent callback functions. Changes mi2cstatus
RETURNS: Nothing
************************************************************************/
void i2c_isr
(
void
) interrupt 6 using 1
{
unsigned char status;
status = I2STAT & 0xF8;
switch(status)
{
case 0x08:
case 0x10:
I2DAT = mslaveaddress;
STA = 0;
STO = 0;
mbytenum = 0;
break;
// MASTER TRANSMITTER
// ACK for slave address + W
case 0x18:
I2DAT = i2c_master_getbyte(mbytenum);
STA = 0;
STO = 0;
break;
// no ACK for slave address + W
case 0x20:
// stop condition
STA = 0;
STO = 1;
mi2cstatus = I2C_ERROR;
i2c_transfer_finished();
break;
// ACK for data byte
case 0x28:
if (i2c_master_islasttxbyte(mbytenum))
{
// stop condition
STA = 0;
STO = 1;
mi2cstatus = I2C_OK;
i2c_transfer_finished();
}
else
{
mbytenum++;
I2DAT = i2c_master_getbyte(mbytenum);
STA = 0;
STO = 0;
} // if
break;
// no ACK for data byte
case 0x30:
// stop condition
STA = 0;
STO = 1;
mi2cstatus = I2C_ERROR;
i2c_transfer_finished();
break;
// arbitration lost
case 0x38:
// start condition - try again
STA = 1;
STO = 0;
break;
// MASTER RECEIVER
// ACK for slave address + R
case 0x40:
STA = 0;
STO = 0;
if (i2c_master_islastrxbyte(mbytenum))
{
// return NACK for data byte
AA = 0;
}
else
{
// return ACK for data byte
AA = 1;
} // if
break;
// no ACK for slave address + R
case 0x48:
STA = 0;
// stop condition
STO = 1;
mi2cstatus = I2C_ERROR;
i2c_transfer_finished();
break;
// ACK for data byte
case 0x50:
i2c_master_receivedbyte(mbytenum, I2DAT);
mbytenum++;
STA = 0;
STO = 0;
if (i2c_master_islastrxbyte(mbytenum))
{
// return NACK for next data byte
AA = 0;
}
else
{
// return ACK for next data byte
AA = 1;
} // if
break;
// no ACK for data byte
case 0x58:
i2c_master_receivedbyte(mbytenum, I2DAT);
STA = 0;
// stop condition
STO = 1;
mi2cstatus = I2C_OK;
i2c_transfer_finished();
break;
// SLAVE RECEIVER
// slave address + W received
// or general call address received
// or lost arbitration, slave address + W received
// or lost arbitration, general call address received
case 0x60:
case 0x68:
case 0x70:
case 0x78:
STA = 0;
STO = 0;
mbytenum = 0;
if (i2c_slave_islastrxbyte(mbytenum))
{
// we don't want to receive more bytes
AA = 0;
}
else
{
// want to receive more bytes
AA = 1;
} // if
break;
// data byte received
case 0x80:
case 0x90:
i2c_slave_receivedbyte(mbytenum, I2DAT);
mbytenum++;
STA = 0;
STO = 0;
if (i2c_slave_islastrxbyte(mbytenum))
{
// we don't want to receive more bytes
AA = 0;
}
else
{
// want to receive more bytes
AA = 1;
} // if
break;
// data received, NACK returned
case 0x88:
case 0x98:
i2c_slave_receivedbyte(mbytenum, I2DAT);
mbytenum++;
mi2cstatus = I2C_OK;
STA = 0;
STO = 0;
// go back to slave mode waiting to be addressed
AA = 1;
i2c_transfer_finished();
break;
// stop condition received
case 0xA0:
mi2cstatus = I2C_OK;
STA = 0;
STO = 0;
// go back to slave mode waiting to be addressed
AA = 1;
i2c_transfer_finished();
break;
// SLAVE TRANSMITTER
// slave address + R received, ACK returned
// arbitration lost, slave address + R received, ACK returned
// data byte transmitted, ACK received
case 0xA8:
case 0xB0:
mbytenum = 0;
case 0xB8:
I2DAT = i2c_slave_getbyte(mbytenum);
STA = 0;
STO = 0;
if (i2c_slave_islasttxbyte(mbytenum))
{
// no more data bytes to transmit
AA = 0;
}
else
{
// more data bytes to transmit
AA = 1;
} // if
mbytenum++;
break;
// data byte transmitted, NACK received
// last data byte transmitted, NACK received
case 0xC0:
case 0xC8:
mi2cstatus = I2C_OK;
STA = 0;
STO = 0;
// go back to slave move waiting to be addressed
AA = 1;
i2c_transfer_finished();
break;
// unknown state
default:
mi2cstatus = I2C_ERROR;
STA = 0;
STO = 0;
// go back to slave mode waiting to be addressed
AA = 1;
i2c_transfer_finished();
break;
} // switch - status
// clear interrupt flag
SI = 0;
} // i2c_isr
// EXTERNAL INTERFACE FUNCTIONS
/***********************************************************************
DESC: initializes the I2C peripheral and interrupt
sets the device's I2C address and whether it will
respond to the general call address
Uses a data rate of 100kHz
RETURNS: Nothing
************************************************************************/
void i2c_init
(
unsigned char address, // The 7-bit I2C address to use
bit generalcall // 1 = respond to general call, 0 = ignore
// general call
)
{
// set pins to open-drain
P1M1 |= 0x0C;
P1M2 |= 0x0C;
EA = 1;
// configure I2C address
I2ADR = 0x00;
I2ADR = address << 1;
if (generalcall)
{
I2ADR |= 0x01;
} // if
// configure internal SCL generator
I2SCLH = 0x1E;
I2SCLL = 0x1E;
// configure I2C interface
// use internal SCL generator
I2CON = 0x44;
// set interrupt priority to 0
IP1 &= ~0x01;
IP1H &= ~0x01;
// initial status
mi2cstatus = I2C_IDLE;
// enable interrupt
EI2C = 1;
} // i2c_init
/***********************************************************************
DESC: attempts to start an I2C transmission as a master to a
device with a specific address. If successful then
master callback functions will be called to handle the
data for the transfer
RETURNS: I2C_BUSYTX if I2C is already busy transmitting data
I2C_BUSYRX if I2C is already busy receiving data
I2C_OK if transmission started
NOTES: Can check if I2C busy by ANDing returned value with
I2C_BUSY
unsigned char i2c_transmit (unsigned char address)
************************************************************************/
unsigned char i2c_transmit
(
unsigned char address // address of device to transmit to
)
{
// if already busy then return current status
if (mi2cstatus & I2C_BUSY) return mi2cstatus;
// now we are busy performing a transfer
mi2cstatus = I2C_BUSYTX;
// store slave address + W for use in ISR
mslaveaddress = address << 1;
// transmit start condition
STA = 1;
// transmission started
return I2C_OK;
} // i2c_transmit
/***********************************************************************
DESC: attempts to start an I2C reception as a master from a
device with a specific address. If successful then
master callback functions will be called to handle the
data for the transfer
RETURNS: I2C_BUSYTX if I2C is already busy transmitting data
I2C_BUSYRX if I2C is already busy receiving data
I2C_OK if reception started
NOTES: Can check if I2C busy by ANDing returned value with
I2C_BUSY
************************************************************************/
unsigned char i2c_receive
(
unsigned char address // address of device to receive from
)
{
// if already busy then return current status
if (mi2cstatus & I2C_BUSY) return mi2cstatus;
// now we are busy performing a transfer
mi2cstatus = I2C_BUSYRX;
// store slave address + R for use in ISR
mslaveaddress = (address << 1) | 0x01;
// transmit start condition
STA = 1;
// reception started
return I2C_OK;
} // i2c_receive
/***********************************************************************
DESC: returns the current status of the I2C peripheral.
allows polling to be used to determine if a transfer
has been completed
RETURNS: I2C_BUSYTX if I2C is already busy transmitting data
I2C_BUSYRX if I2C is already busy receiving data
I2C_OK if no transfer in progress and last transfer
was successful
I2C_ERROR if no transfer in progress and last transfer
encountered an error
I2C_IDLE if no transfer in progress
NOTES: Can check if I2C busy by ANDing returned value with
I2C_BUSY. Example:
i2c_transmit(0x00);
while (i2c_getstatus() & I2C_BUSY);
if (i2c_getstatus() == I2C_OK)
...
************************************************************************/
unsigned char i2c_getstatus
(
void
)
{
// wait until any interrupt completed
while(SI);
// return status
return mi2cstatus;
} // i2c_getstatus
i2c.h
/***********************************************************************
MODULE: I2C
VERSION: 1.03
CONTAINS: Routines for controlling the I2C Peripheral on the Philips
P89LPC936
COPYRIGHT: Embedded Systems Academy, Inc. - www.esacademy.com
LICENSE: May be freely used in commercial and non-commercial code
without royalties provided this copyright notice remains
in this file and unaltered
WARNING: IF THIS FILE IS REGENERATED BY CODE ARCHITECT ANY CHANGES
MADE WILL BE LOST. WHERE POSSIBLE USE ONLY CODE ARCHITECT
TO CHANGE THE CONTENTS OF THIS FILE
GENERATED: On "Jun 09 2008" at "11:54:01" by Code Architect 2.12
***********************************************************************/
#ifndef _I2CH_
#define _I2CH_
// possible states for the I2C peripheral
#define I2C_IDLE 0x00
#define I2C_BUSY 0x01
#define I2C_BUSYTX 0x03
#define I2C_BUSYRX 0x05
#define I2C_OK 0x02
#define I2C_ERROR 0x04
/***********************************************************************
DESC: initializes the I2C peripheral and interrupt
sets the device's I2C address and whether it will
respond to the general call address
Uses a data rate of 100kHz
RETURNS: Nothing
************************************************************************/
extern void i2c_init
(
unsigned char address, // The 7-bit I2C address to use
bit generalcall // 1 = respond to general call, 0 = ignore
// general call
);
/***********************************************************************
DESC: attempts to start an I2C transmission as a master to a
device with a specific address. If successful then
master callback functions will be called to handle the
data for the transfer
RETURNS: I2C_BUSYTX if I2C is already busy transmitting data
I2C_BUSYRX if I2C is already busy receiving data
I2C_OK if transmission started
NOTES: Can check if I2C busy by ANDing returned value with
I2C_BUSY
************************************************************************/
extern unsigned char i2c_transmit
(
unsigned char address // address of device to transmit to
);
/***********************************************************************
DESC: attempts to start an I2C reception as a master from a
device with a specific address. If successful then
master callback functions will be called to handle the
data for the transfer
RETURNS: I2C_BUSYTX if I2C is already busy transmitting data
I2C_BUSYRX if I2C is already busy receiving data
I2C_OK if reception started
NOTES: Can check if I2C busy by ANDing returned value with
I2C_BUSY
************************************************************************/
extern unsigned char i2c_receive
(
unsigned char address // address of device to receive from
);
/***********************************************************************
DESC: returns the current status of the I2C peripheral.
allows polling to be used to determine if a transfer
has been completed
RETURNS: I2C_BUSYTX if I2C is already busy transmitting data
I2C_BUSYRX if I2C is already busy receiving data
I2C_OK if no transfer in progress and last transfer
was successful
I2C_ERROR if no transfer in progress and last transfer
encountered an error
I2C_IDLE if no transfer in progress
NOTES: Can check if I2C busy by ANDing returned value with
I2C_BUSY. Example:
i2c_transmit(0x00);
while (i2c_getstatus() & I2C_BUSY);
if (i2c_getstatus() == I2C_OK)
...
************************************************************************/
extern unsigned char i2c_getstatus
(
void
);
#endif // _IC2H_
i2c.callback.c
/***********************************************************************
MODULE: I2C Callback
VERSION: 1.03
CONTAINS: Routines for controlling the I2C Peripheral on the Philips
P89LPC936
COPYRIGHT: Embedded Systems Academy, Inc. - www.esacademy.com
LICENSE: May be freely used in commercial and non-commercial code
without royalties provided this copyright notice remains
in this file and unaltered
WARNING: IF THIS FILE IS REGENERATED BY CODE ARCHITECT ANY CHANGES
MADE WILL BE LOST. WHERE POSSIBLE USE ONLY CODE ARCHITECT
TO CHANGE THE CONTENTS OF THIS FILE
GENERATED: On "Jun 09 2008" at "11:54:02" by Code Architect 2.12
***********************************************************************/
#include "REG936.h"
#include "i2c.h"
#include "i2c_callback.h"
// MASTER CALLBACK FUNCTIONS
/***********************************************************************
DESC: gets the next byte to be transmitted when operating as
a master
RETURNS: byte to transmit
************************************************************************/
unsigned char i2c_master_getbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
)
{
// insert code here
return 0x00;
} // i2c_master_getbyte
/***********************************************************************
DESC: processes the bytes received when operating as a master
RETURNS: Nothing
************************************************************************/
void i2c_master_receivedbyte
(
unsigned int bytenum, // number of the byte in this transfer
// (0 = first byte)
unsigned char value // value of byte received
)
{
// insert code here
} // i2c_master_receivedbyte
/***********************************************************************
DESC: determines if a byte will be the last one to be
transmitted when operating as a master
called before i2c_master_getbyte for each byte
RETURNS: return 1 if the byte will be the last one to transmit
return 0 if the byte will not be the last one to transmit
************************************************************************/
unsigned char i2c_master_islasttxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
)
{
// insert code here
return 0;
} // i2c_master_islasttxbyte
/***********************************************************************
DESC: determines if a byte will be the last one to be
received when operating as a master
called before i2c_master_receivedbyte for each byte
RETURNS: return 1 if the byte will be the last one to receive
return 0 if the byte will not be the last one to receive
************************************************************************/
unsigned char i2c_master_islastrxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
)
{
// insert code here
return 0;
} // i2c_master_islastrxbyte
// SLAVE CALLBACK FUNCTIONS
/***********************************************************************
DESC: gets the next byte to be transmitted when operating as
a slave
RETURNS: byte to transmit
************************************************************************/
unsigned char i2c_slave_getbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
)
{
// insert code here
return 0x00;
} // i2c_slave_getbyte
/***********************************************************************
DESC: processes the bytes received when operating as a slave
RETURNS: Nothing
************************************************************************/
void i2c_slave_receivedbyte
(
unsigned int bytenum, // number of the byte in this transfer
// (0 = first byte)
unsigned char value // value of byte received
)
{
// insert code here
} // i2c_slave_receivedbyte
/***********************************************************************
DESC: determines if a byte will be the last one to be
transmitted when operating as a slave
called after i2c_slave_getbyte for each byte
RETURNS: return 1 if the byte will be the last one to transmit
return 0 if the byte will not be the last one to transmit
************************************************************************/
unsigned char i2c_slave_islasttxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
)
{
// insert code here
return 0;
} // i2c_slave_islasttxbyte
/***********************************************************************
DESC: determines if a byte will be the last one to be
received when operating as a slave
called before i2c_slave_receivedbyte for each byte
RETURNS: return 1 if the byte will be the last one to receive
return 0 if the byte will not be the last one to receive
************************************************************************/
unsigned char i2c_slave_islastrxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
)
{
// insert code here
return 0;
} // i2c_slave_islastrxbyte
/***********************************************************************
DESC: called when an I2C transfer is finished
mi2cstatus may be read to determine if the transfer was
successful (I2C_OK) or failed (I2C_ERROR)
RETURNS: Nothing
************************************************************************/
void i2c_transfer_finished
(
void
)
{
// insert code here
} // i2c_transfer_finished
i2c_callback.h
/***********************************************************************
MODULE: I2C Callback
VERSION: 1.03
CONTAINS: Routines for controlling the I2C Peripheral on the Philips
P89LPC936
COPYRIGHT: Embedded Systems Academy, Inc. - www.esacademy.com
LICENSE: May be freely used in commercial and non-commercial code
without royalties provided this copyright notice remains
in this file and unaltered
WARNING: IF THIS FILE IS REGENERATED BY CODE ARCHITECT ANY CHANGES
MADE WILL BE LOST. WHERE POSSIBLE USE ONLY CODE ARCHITECT
TO CHANGE THE CONTENTS OF THIS FILE
GENERATED: On "Jun 09 2008" at "11:54:02" by Code Architect 2.12
***********************************************************************/
#ifndef _I2CCALLBACKH_
#define _I2CCALLBACKH_
/***********************************************************************
DESC: gets the next byte to be transmitted when operating as
a master
RETURNS: byte to transmit
************************************************************************/
extern unsigned char i2c_master_getbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
);
/***********************************************************************
DESC: processes the bytes received when operating as a master
RETURNS: Nothing
************************************************************************/
extern void i2c_master_receivedbyte
(
unsigned int bytenum, // number of the byte in this transfer
// (0 = first byte)
unsigned char value // value of byte received
);
/***********************************************************************
DESC: determines if a byte will be the last one to be
transmitted when operating as a master
called before i2c_master_getbyte for each byte
RETURNS: return 1 if the byte will be the last one to transmit
return 0 if the byte will not be the last one to transmit
************************************************************************/
extern unsigned char i2c_master_islasttxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
);
/***********************************************************************
DESC: determines if a byte will be the last one to be
received when operating as a master
called before i2c_master_receivedbyte for each byte
RETURNS: return 1 if the byte will be the last one to receive
return 0 if the byte will not be the last one to receive
************************************************************************/
extern unsigned char i2c_master_islastrxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
);
/***********************************************************************
DESC: gets the next byte to be transmitted when operating as
a slave
RETURNS: byte to transmit
************************************************************************/
extern unsigned char i2c_slave_getbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
);
/***********************************************************************
DESC: processes the bytes received when operating as a slave
RETURNS: Nothing
************************************************************************/
extern void i2c_slave_receivedbyte
(
unsigned int bytenum, // number of the byte in this transfer
// (0 = first byte)
unsigned char value // value of byte received
);
/***********************************************************************
DESC: determines if a byte will be the last one to be
transmitted when operating as a slave
called after i2c_slave_getbyte for each byte
RETURNS: return 1 if the byte will be the last one to transmit
return 0 if the byte will not be the last one to transmit
************************************************************************/
extern unsigned char i2c_slave_islasttxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
);
/***********************************************************************
DESC: determines if a byte will be the last one to be
received when operating as a slave
called before i2c_slave_receivedbyte for each byte
RETURNS: return 1 if the byte will be the last one to receive
return 0 if the byte will not be the last one to receive
************************************************************************/
extern unsigned char i2c_slave_islastrxbyte
(
unsigned int bytenum // number of the byte in this transfer
// (0 = first byte)
);
/***********************************************************************
DESC: called when an I2C transfer is finished
mi2cstatus may be read to determine if the transfer was
successful (I2C_OK) or failed (I2C_ERROR)
RETURNS: Nothing
************************************************************************/
extern void i2c_transfer_finished
(
void
);
#endif // _I2CCALLBACKH_
|
| Topic | Author | Date |
| Hardware I2C on 8051 NXP....I'm stuck | 01/01/70 00:00 | |
| divide and conquer | 01/01/70 00:00 | |
| where would I find that? | 01/01/70 00:00 | |
| what is a "tut file" ?? | 01/01/70 00:00 | |
| FIle is in the link in first post | 01/01/70 00:00 | |
| I would NEVER NEVER NEVER ..... | 01/01/70 00:00 | |
| Well that's not gonna happen.... | 01/01/70 00:00 | |
| looks to me as staright CodeArchitect | 01/01/70 00:00 | |
| accidential double, ignore | 01/01/70 00:00 | |
| Programming is not guesswork | 01/01/70 00:00 | |
| blunt is fine.....and I'll say it again. | 01/01/70 00:00 | |
| Forum posts and reality... | 01/01/70 00:00 | |
| you will break a leg | 01/01/70 00:00 | |
does it have to be C? | 01/01/70 00:00 |



