⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c1.c

📁 基于Freescale Codewarrior6.2环境下的针对qe8 单片机的LM75A温度传感器的驱动程序,可以直接使用
💻 C
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : I2C1.C
**     Project   : LM75A
**     Processor : MC9S08QE8CFM
**     Beantype  : InternalI2C
**     Version   : Bean 01.196, Driver 01.20, CPU db: 3.00.026
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2009-1-13, 15:18
**     Abstract  :
**          This bean encapsulates the internal I2C communication 
**          interface. The implementation of the interface is based 
**          on the Philips I2C-bus specification version 2.0. 
**          Interface features:
**          MASTER mode
**            - Multi master communication
**            - The combined format of communication possible 
**              (see "Automatic stop condition" property)
**            - 7-bit slave addressing (10-bit addressing can be made as well)
**            - Acknowledge polling provided
**            - No wait state initiated when a slave device holds the SCL line low
**            - Holding of the SCL line low by slave device recognized as 'not available bus'
**            - Invalid start/stop condition detection provided
**          SLAVE mode
**            - 7-bit slave addressing
**            - General call address detection provided
**     Settings  :
**         Serial channel              : IIC
**
**         Protocol
**             Mode                    : MASTER
**             Auto stop condition     : yes
**             SCL frequency           : 104.858 kHz
**
**         Initialization
**
**             Slave address           : 8
**             Bean function           : Enabled
**             Events                  : Enabled
**
**         Registers
**             Input buffer            : IICD      [$0034]
**             Output buffer           : IICD      [$0034]
**             Control register        : IICC1     [$0032]
**             Status register         : IICS      [$0033]
**             Baud setting reg.       : IICF      [$0031]
**             Address register        : IICA      [$0030]
**
**         Interrupt
**             Vector name             : Viic
**             Priority                : 
**
**         Used pins                   : 
**       ----------------------------------------------------------
**            Function    | On package |    Name
**       ----------------------------------------------------------
**              SDA       |     24     |  PTA2_KBIP2_SDA_ADP2
**              SCL       |     23     |  PTA3_KBIP3_SCL_ADP3
**       ----------------------------------------------------------
**     Contents  :
**         SendChar        - byte I2C1_SendChar(byte Chr);
**         RecvChar        - byte I2C1_RecvChar(byte *Chr);
**         SendBlock       - byte I2C1_SendBlock(void* Ptr, word Siz, word *Snt);
**         RecvBlock       - byte I2C1_RecvBlock(void* Ptr, word Siz, word *Rcv);
**         GetCharsInTxBuf - word I2C1_GetCharsInTxBuf(void);
**         SelectSlave     - byte I2C1_SelectSlave(byte Slv);
**         GetCharsInRxBuf - word I2C1_GetCharsInRxBuf(void);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2008
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/


/* MODULE I2C1. */


#pragma MESSAGE DISABLE C4002          /* Disable warning C4002 "Result not used" */

#include "Events.h"
#include "I2C1.h"

/*SerFlag bits*/
#define OVERRUN_ERR      0x01          /* Overrun error flag bit   */
#define WAIT_RX_CHAR     0x02          /* Wait for received char. flag bit (Master)  */
#define CHAR_IN_TX       0x04          /* Char is in TX buffer (Master)    */
#define CHAR_IN_RX       0x08          /* Char is in RX buffer     */
#define FULL_TX          0x10          /* Full transmit buffer     */
#define IN_PROGRES       0x20          /* Communication is in progress (Master) */
#define FULL_RX          0x40          /* Full receive buffer      */
#define MSxSL            0x80          /* Master x Slave flag bit  */

/*SerFlag2 bits*/
#define ADDR_COMPLETE      0x01        /* 10-bit address transmission complete   */
#define REP_ADDR_COMPLETE  0x02        /* repeated address transmission complete */
#define GENERAL_CALL       0x04        /* General call flag */
#define ADDR_10            0x08        /* 10-bit addr flag */
#define ADDR_7             0x10        /* 7-bit addr flag */
static byte I2C1_SlaveAddr;            /* Variable for Slave address */
static word InpLenM;                   /* Length of input bufer's content */
static byte *InpPtrM;                  /* Pointer to input buffer for Master mode */
static word OutLenM;                   /* Length of output bufer's content */
static byte * OutPtrM;                 /* Pointer to output buffer for Master mode */
volatile word I2C1_SndRcvTemp;         /* Temporary variable for SendChar (RecvChar) when they call SendBlock (RecvBlock) */
static byte ChrTemp;                   /* Temporary variable for SendChar method */
volatile byte I2C1_SerFlag;            /* Flags for serial communication */
                                       /* Bits: 0 - OverRun error */
                                       /*       1 - Wait for received char. flag bit (Master) */
                                       /*       2 - Char is in TX buffer (Master) */
                                       /*       3 - Char in RX buffer */
                                       /*       4 - Full TX buffer */
                                       /*       5 - Running int from TX */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Master x Slave */
/*
** ===================================================================
**     Method      :  I2C1_Interrupt (bean InternalI2C)
**
**     Description :
**         The method services the interrupt of the selected peripheral(s)
**         and eventually invokes event(s) of the bean.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define RXAK 0x01
#define SRW  0x04
#define IBAL 0x10
#define IAAS 0x40

#define ON_ARBIT_LOST 0x01
#define ON_FULL_RX    0x02
#define ON_RX_CHAR    0x04
#define ON_FREE_TX    0x08
#define ON_TX_CHAR    0x10
#define ON_OVERRUN    0x20
#define ON_TX_EMPTY   0x40

ISR(I2C1_Interrupt)
{
  byte Status = IICS;                  /* Safe status register */
  IICS_IICIF = 1;                      /* Clear interrupt flag and ARBL flag if set (by means of the read modify write effect) */
  if(IICC1_MST) {                      /* Is device in master mode? */
    if(IICC1_TX) {                     /* Is device in Tx mode? */
      if(Status & RXAK) {              /* NACK received? */
        IICC1_MST = 0;                 /* Switch device to slave mode (stop signal sent) */
        IICC1_TX = 0;                  /* Switch to Rx mode */
        OutLenM = 0;                   /* No character for sending */
        InpLenM = 0;                   /* No character for reception */
        I2C1_SerFlag &= ~(CHAR_IN_TX|WAIT_RX_CHAR|IN_PROGRES); /* No character for sending or reception*/
      }
      else {
        if(OutLenM) {                  /* Is any char. for transmitting? */
          OutLenM--;                   /* Decrease number of chars for the transmit */
          IICD = *(OutPtrM)++;         /* Send character */
        }
        else {
          if(InpLenM) {                /* Is any char. for reception? */
            if(InpLenM == 1) {         /* If only one char to receive */
              IICC1_TXAK = 1;          /* then transmit ACK disable */
            }
            else {
              IICC1_TXAK = 0;          /* else transmit ACK enable */
            }
            IICC1_TX = 0;              /* Switch to Rx mode */
            (void)IICD;                /* Dummy read character */
          }
          else {
            I2C1_SerFlag &= ~IN_PROGRES; /* Clear flag "busy" */
            IICC1_MST = 0;             /* Switch device to slave mode (stop signal sent) */
            IICC1_TX = 0;              /* Switch to Rx mode */
            I2C1_OnTransmitData();     /* Invoke OnTransmitData event */
          }
        }
      }
    }
    else {
      InpLenM--;                       /* Decrease number of chars for the receive */
      if(InpLenM) {                    /* Is any char. for reception? */
        if(InpLenM == 1) {
          IICC1_TXAK = 1;              /* Transmit ACK disable */
        }
      }
      else {
        IICC1_MST = 0;                 /* If no, switch device to slave mode (stop signal sent) */
        IICC1_TXAK = 0;                /* Transmit ACK enable */
      }
      *(InpPtrM)++ = IICD;             /* Receive character */
      if(!InpLenM) {                   /* Is any char. for reception? */
        I2C1_OnReceiveData();          /* Invoke OnReceiveData event */
      }
    }
  }
  else {
    if(Status & IBAL) {                /* Arbitration lost? */
      OutLenM = 0;                     /* No character for sending */
      InpLenM = 0;                     /* No character for reception */
      I2C1_SerFlag &= ~(CHAR_IN_TX|WAIT_RX_CHAR|IN_PROGRES); /* No character for sending or reception*/
      IICC1_TX = 0;                    /* Switch to Rx mode */
    }
  }
}

/*
** ===================================================================
**     Method      :  I2C1_SendChar (bean InternalI2C)
**
**     Description :
**         When working as a MASTER, this method writes one (7-bit
**         addressing) or two (10-bit addressing) slave address
**         bytes inclusive of R/W bit = 0 to the I2C bus and then
**         writes one character (byte) to the bus. The slave address
**         must be specified before, by the "SelectSlave" or
**         "SelectSlave10" method or in the bean initialization
**         section, "Target slave address init" property. If
**         interrupt service is enabled and the method returns
**         ERR_OK, it doesn't mean that transmission was successful.
**         The state of transmission is obtainable from
**         (OnTransmitData, OnError or OnArbitLost) events. 
**         When working as a SLAVE, this method writes a character
**         to the internal output slave buffer and, after the master
**         starts the communication, to the I2C bus. If no character
**         is ready for a transmission (internal output slave buffer
**         is empty), the Empty character will be sent (see "Empty
**         character" property).
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - Character to send.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED -  Device is disabled
**                           ERR_BUSY - The slave device is busy, it
**                           does not respond by an acknowledge (only
**                           in master mode and when interrupt
**                           service is disabled)
**                           ERR_BUSOFF - Clock timeout elapsed or
**                           device cannot transmit data
**                           ERR_TXFULL - Transmitter is full (slave
**                           mode only)
**                           ERR_ARBITR - Arbitration lost (only when
**                           interrupt service is disabled and in
**                           master mode)
** ===================================================================
*/
byte I2C1_SendChar(byte Chr)
{
  if((IICS_BUSY)||(InpLenM)||(I2C1_SerFlag&(CHAR_IN_TX|WAIT_RX_CHAR|IN_PROGRES))) { /* Is the bus busy */
    return ERR_BUSOFF;                 /* If yes then error */
  }
  ChrTemp = Chr;                       /* Safe character */
  return (I2C1_SendBlock(&ChrTemp, (word)1, (word*)&I2C1_SndRcvTemp)); /* Send character and return */
}

/*
** ===================================================================
**     Method      :  I2C1_RecvChar (bean InternalI2C)
**
**     Description :
**         When working as a MASTER, this method writes one (7-bit
**         addressing) or two (10-bit addressing) slave address
**         bytes inclusive of R/W bit = 1 to the I2C bus, then reads
**         one character (byte) from the bus and then sends the stop
**         condition. The slave address must be specified before, by
**         the "SelectSlave" or "SelectSlave10" method or in bean
**         initialization section, property "Target slave address
**         init". If interrupt service is enabled and the method
**         returns ERR_OK, it doesn't mean that transmission was
**         finished successfully. The state of transmission must be
**         tested by means of events (OnReceiveData, OnError or
**         OnArbitLost). In case of successful transmission,
**         received data is ready after OnReceiveData event is
**         called. 
**         When working as a SLAVE, this method reads a character
**         from the input slave buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - Received character.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED -  Device is disabled
**                           ERR_BUSY - The slave device is busy, it
**                           does not respond by the acknowledge
**                           (only in master mode and when interrupt
**                           service is disabled)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -