?? i2c.c
字號:
// settings may need to change for frequencies outside this range.
#if ((SYSCLK/SMB_FREQUENCY/3) < 255)
#define SCALE 1
CKCON |= 0x08; // Timer1 clock source = SYSCLK
#elif ((SYSCLK/SMB_FREQUENCY/4/3) < 255)
#define SCALE 4
CKCON |= 0x01;
CKCON &= ~0x0A; // Timer1 clock source = SYSCLK / 4
#endif
TMOD = 0x20; // Timer1 in 8-bit auto-reload mode
// Timer1 configured to overflow at 1/3 the rate defined by SMB_FREQUENCY
TH1 = -(SYSCLK/SMB_FREQUENCY/SCALE/3);
TL1 = TH1; // Init Timer1
TR1 = 1; // Timer1 enabled
}
//-----------------------------------------------------------------------------
// Timer3_Init()
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Timer3 configured for use by the SMBus low timeout detect feature as
// follows:
// - Timer3 in 16-bit auto-reload mode
// - SYSCLK/12 as Timer3 clock source
// - Timer3 reload registers loaded for a 25ms overflow period
// - Timer3 pre-loaded to overflow after 25ms
// - Timer3 enabled
//
void Timer3_Init (void)
{
TMR3CN = 0x00; // Timer3 configured for 16-bit auto-
// reload, low-byte interrupt disabled
CKCON &= ~0x40; // Timer3 uses SYSCLK/12
TMR3RL = -(SYSCLK/12/40); // Timer3 configured to overflow after
TMR3 = TMR3RL; // ~25ms (for SMBus low timeout detect):
// 1/.025 = 40
EIE1 |= 0x80; // Timer3 interrupt enable
TMR3CN |= 0x04; // Start Timer3
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the Crossbar and GPIO ports.
//
// P0.0 digital open-drain SMBus SDA
// P0.1 digital open-drain SMBus SCL
//
// P2.1 digital push-pull LED
//
// all other port pins unused
//
void PORT_Init (void)
{
P0MDOUT = 0x00; // All P0 pins open-drain output
P2MDOUT |= 0x02; // Make the LED (P2.1) a push-pull
// output
XBR0 = 0x04; // Enable SMBus pins
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0 = 0xFF;
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SMBus Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
//
// SMBus ISR state machine
// - Slave only implementation - no master states defined
// - All incoming data is written to global variable <SMB_DATA>
// - All outgoing data is read from global variable <SMB_DATA>
//
void SMBus_ISR (void) interrupt 7
{
static unsigned char sent_byte_counter;
//static unsigned char rec_byte_counter;
if (ARBLOST == 0)
{
switch (SMB0CN & 0xF0) // Decode the SMBus status vector
{
// Slave Receiver: Start+Address received
case SMB_SRADD:
STA = 0; // Clear STA bit
sent_byte_counter = 1; // Reinitialize the data counters
rec_byte_counter = 1;
//if((SMB0DAT&0xFE) == (SLAVE_ADDR&0xFE)) // Decode address
if((SMB0DAT!=0xA0)||((SMB0DAT>0x0F)&&(SMB0DAT<0xF0)))
{ // If the received address matches,
ACK = 1; // ACK the received slave address
SMB_DATA_OUT[sent_byte_counter-1]=SMB0DAT&0xFE;
SMB_DATA_OUT[sent_byte_counter]=SMB0DAT&0xFE;
SMB_DATA_IN[rec_byte_counter++-1]=SMB0DAT&0xFE;
if((SMB0DAT&0x01) == READ) // If the transfer is a master READ,
{
// Prepare outgoing byte
SMB0DAT = SMB_DATA_OUT[sent_byte_counter-1];
sent_byte_counter++;
}
}
else // If received slave address does not
{ // match,
ACK = 0; // NACK received address
}
break;
// Slave Receiver: Data received
case SMB_SRDB:
if (rec_byte_counter < NUM_BYTES_WR)
{
// Store incoming data
SMB_DATA_IN[rec_byte_counter-1] = SMB0DAT;
rec_byte_counter++;
ACK = 1; // ACK received data
}
else
{
// Store incoming data
SMB_DATA_IN[rec_byte_counter-1] = SMB0DAT;
DATA_READY = 1; // Indicate new data fully received
ACK = 1; // ACK received data
}
break;
// Slave Receiver: Stop received while either a Slave Receiver or
// Slave Transmitter
case SMB_SRSTO:
STO = 0; // STO must be cleared by software when
// a STOP is detected as a slave
break;
// Slave Transmitter: Data byte transmitted
case SMB_STDB:
if (ACK == 1) // If Master ACK's, send the next byte
{
if (sent_byte_counter <= NUM_BYTES_RD)
{
// Prepare next outgoing byte
SMB0DAT = SMB_DATA_OUT[sent_byte_counter-1];
//B0DAT = SMB_DATA_OUT[sent_byte_counter-1];
sent_byte_counter++;
}
} // Otherwise, do nothing
break;
// Slave Transmitter: Arbitration lost, Stop detected
//
// This state will only be entered on a bus error condition.
// In normal operation, the slave is no longer sending data or has
// data pending when a STOP is received from the master, so the TXMODE
// bit is cleared and the slave goes to the SRSTO state.
case SMB_STSTO:
STO = 0; // STO must be cleared by software when
// a STOP is detected as a slave
break;
// Default: all other cases undefined
default:
SMB0CF &= ~0x80; // Reset communication
SMB0CF |= 0x80;
STA = 0;
STO = 0;
ACK = 0;
break;
}
}
// ARBLOST = 1, Abort failed transfer
else
{
STA = 0;
STO = 0;
ACK = 0;
}
SI = 0; // Clear SMBus interrupt flag
}
//-----------------------------------------------------------------------------
// Timer3 Interrupt Service Routine (ISR)
//-----------------------------------------------------------------------------
//
// A Timer3 interrupt indicates an SMBus SCL low timeout.
// The SMBus is disabled and re-enabled here
//
void Timer3_ISR (void) interrupt 14
{
SMB0CF &= ~0x80; // Disable SMBus
SMB0CF |= 0x80; // Re-enable SMBus
TMR3CN &= ~0x80; // Clear Timer3 interrupt-pending flag
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -