SOURCE 공개
1. 공개 I2C 처리루틴 |
#define HIGH 1 #define LOW 0
#define TRUE 1 #define FALSE 0
//------------------------------------------------------------------------------ // I2C Functions - Master //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ // Routine: i2c_init // Inputs: none // Outputs: none // Purpose: Initialize I2C for the ADu812C //------------------------------------------------------------------------------ void i2c_init (void) { // To initialize we need the output // data high, the clock high, and // the I2C system in Master mode I2CM = HIGH; // Set master mode MDO = HIGH; // Set the data bit to HIGH MDE = FALSE; // We are not using the pin yet MCO = HIGH; // Set the clock bit to HIGH }
//------------------------------------------------------------------------------ // Routine: i2c_start // Inputs: none // Outputs: none // Purpose: Sends I2C Start Trasfer - State "B" //------------------------------------------------------------------------------ void i2c_start (void) { // An I2C start sequence is defined as // a High to Low Transistino on the data // line as the CLK pin is high MDE = TRUE; // Master Mode Data Output Enable: ON MDO = HIGH; MCO = HIGH; MDO = LOW; // Master Mode Data Output: LOW MDO = LOW; // Repeat for delay MCO = LOW; // Master Mode Clock Output: LOW }
//------------------------------------------------------------------------------ // Routine: i2c_stop // Inputs: none // Outputs: none // Purpose: Sends I2C Stop Trasfer - State "C" //------------------------------------------------------------------------------ void i2c_stop (void) { // An I2C start sequence is defined as // a Low to High Transistino on the data // line as the CLK pin is high MDE = TRUE; // Master Mode Data Output Enable: ON MDO = LOW; // Master Mode Data Output: LOW MCO = HIGH; // Master Mode Clock Output: HIGH MCO = HIGH; // Repeat for delay MDO = HIGH; // Master Mode Data Output: LOW }
//------------------------------------------------------------------------------ // Routine: i2c_write // Inputs: output byte // Outputs: none // Purpose: Writes data over the I2C bus MSB -> LSB write //------------------------------------------------------------------------------ bit i2c_write (unsigned char output_data) { unsigned char index; // An I2C output byte is bits 7-0 // (MSB to LSB). Shift one bit at a time // to the MDO output, and then clock the // data to the I2C Slave
MDE = TRUE; // Master Mode Data Output Enable: ON for(index = 0; index < 8; index++) // Send 8 bits out the port { // Output the data bit to the EEPROM MDO = ((output_data & 0x80) ? 1 : 0); output_data <<= 1; // Shift the byte by one bit MCO = HIGH; // Clock the data into the EEPROM MCO = HIGH; // Repeat for delay MCO = LOW; }
MDE = FALSE; // Put data pin into read mode MCO = HIGH; // Get ACK bit if (!MDI) { MCO = LOW; // Clock Low return TRUE; // ACK from slave } else { MCO = LOW; // Clock Low return FALSE; // No ACK } }
//------------------------------------------------------------------------------ // Routine: i2c_read // Inputs: send_ack (if TRUE send the ACK signal) // Outputs: input byte // Purpose: Reads data from the I2C bus MSB -> LSB read //------------------------------------------------------------------------------ unsigned char i2c_read (bit send_ack) { unsigned char index, input_data;
input_data = 0x00;
MDE = FALSE; // Put data pin into read mode MCO = LOW; // Set clock LOW
for(index = 0; index < 8; index++) // Get 8 bits from the device { input_data <<= 1; // Shift the data right 1 bit MCO = HIGH; // Clock the data into the register MCO = HIGH; // Repeat for delay MCO = LOW; input_data |= MDI; // Read the data bit }
MDE = TRUE; // Put data pin into write mode if (send_ack) MDO = LOW; // Set data pin LOW to Ack the read else MDO = HIGH; // Set data pin HIGH to Not Ack the read
MCO = HIGH; // Set the clock pin HIGH MCO = HIGH; // Repeat for delay MCO = LOW; // Set the clock pin LOW MDO = LOW; // Set the data pin LOW MDO = LOW; // Repeat for delay MDE = LOW; // Put data pin into read mode
return input_data; }
|
'공부 > 8051' 카테고리의 다른 글
[ADUC831] ADUC831 간단 매뉴얼 1 (0) | 2012.11.12 |
---|---|
[ADUC812] ADUC812 설명 8 (0) | 2012.11.10 |
[ADUC812] ADUC812 설명 6 (0) | 2012.11.10 |
[ADUC812] ADUC812 설명 5 (0) | 2012.11.10 |
[ADUC812] ADUC812 설명 4 (0) | 2012.11.10 |