Advertisement
Guest User

12LF1552 I2C i2c.h

a guest
Aug 21st, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.25 KB | None | 0 0
  1. #ifndef __I2C_H
  2. #define __I2C_H
  3. /******************************************************************************
  4.  // *                   I2C PERIPHERAL LIBRARY HEADER FILE
  5.  ******************************************************************************
  6.  * FileName:                i2c.h
  7.  * Dependencies:        See include below
  8.  * Processor:               PIC18
  9.  * Compiler:                MCC18
  10.  * Company:                 Microchip Technology, Inc.
  11.  *
  12.  * Software License Agreement
  13.  * The software supplied herewith by Microchip Technology Incorporated
  14.  * (the “Company”) for its PICmicro® Microcontroller is intended and
  15.  * supplied to you, the Company’s customer, for use solely and
  16.  * exclusively on Microchip PICmicro Microcontroller products. The
  17.  * software is owned by the Company and/or its supplier, and is
  18.  * protected under applicable copyright laws. All rights are reserved.
  19.  * Any use in violation of the foregoing restrictions may subject the
  20.  * user to criminal sanctions under applicable laws, as well as to
  21.  * civil liability for the breach of the terms and conditions of this
  22.  * license.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
  25.  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
  26.  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27.  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
  28.  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
  29.  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
  30.  *****************************************************************************/
  31. #define I2C_V1
  32.  
  33. /* SSPCON1 REGISTER */
  34. #define   SSPENB            0b00100000      /* Enable serial port and configures SCK, SDO, SDI*/
  35. #define   SLAVE_7           0b00000110      /* I2C Slave mode, 7-bit address*/
  36. #define   SLAVE_10          0b00000111      /* I2C Slave mode, 10-bit address*/
  37. #define   MASTER            0b00001000      /* I2C Master mode */
  38. #define   MASTER_FIRMW      0b00001011  //I2C Firmware Controlled Master mode (slave Idle)
  39. #define   SLAVE_7_STSP_INT  0b00001110  //I2C Slave mode, 7-bit address with Start and Stop bit interrupts enabled
  40. #define   SLAVE_10_STSP_INT     0b00001111  //I2C Slave mode, 10-bit address with Start and Stop bit interrupts enabled
  41.  
  42. /* SSPSTAT REGISTER */
  43. #define   SLEW_OFF          0b10000000      /* Slew rate disabled for 100kHz mode */
  44. #define   SLEW_ON           0b00000000      /* Slew rate enabled for 400kHz mode  */
  45.  
  46. /* I2C IO pins */
  47. #ifdef __12LF1552
  48. #define I2C_SCL TRISAbits.TRISA1
  49. #define I2C_SDA TRISAbits.TRISA3
  50. #else
  51. #define I2C_SCL TRISCbits.TRISC0
  52. #define I2C_SDA TRISCbits.TRISC1
  53. #endif
  54.  
  55. #if defined (I2C_V1) || defined (I2C_V4)    /* Parts with only one I2C, MSSP */
  56.  
  57. //******** I2C **********
  58.  
  59. /***********************************************************************************
  60. Macro       : EnableIntI2C
  61.  
  62. Include     : i2c.h
  63.  
  64. Description : Macro enables I2C  Interrupt
  65.  
  66. Arguments   : None
  67.  
  68. Remarks     : None
  69. ***********************************************************************************/
  70. #define EnableIntI2C                   (PIE1bits.SSPIE = 1)
  71.  
  72. #define EnableIntI2C1   EnableIntI2C
  73.  
  74. /***********************************************************************************
  75. Macro       : DisableIntI2C
  76.  
  77. Include     : i2c.h
  78.  
  79. Description : Macro disables I2C  Interrupt
  80.  
  81. Arguments   : None
  82.  
  83. Remarks     : None
  84. ***********************************************************************************/
  85. #define DisableIntI2C                   (PIE1bits.SSPIE = 0)
  86.  
  87. #define DisableIntI2C1  DisableIntI2C
  88.  
  89. /***********************************************************************************
  90. Macro       : SetPriorityIntI2C(priority)
  91.  
  92. Include     : i2c.h
  93.  
  94. Description : Macro sets the priority level for I2C interrupt.
  95.  
  96. Arguments   : priority - This input parameter is the level of interrupt priority.
  97.         * 0 -- Low priority (Default Value)
  98.         * 1 -- High Priority
  99.  Remarks     : None
  100. ***********************************************************************************/
  101. #define SetPriorityIntI2C(priority)     (IPR1bits.SSPIP = priority)
  102.  
  103. #define SetPriorityIntI2C1  SetPriorityIntI2C
  104.  
  105. /*******************************************************************
  106. Macro       : I2C_Clear_Intr_Status_Bit
  107.  
  108. Include     : i2c.h
  109.  
  110. Description : Macro to Clear I2C  Interrupt Status bit
  111.  
  112. Arguments   : None
  113.  
  114. Remarks     : None
  115. *******************************************************************/
  116. #define I2C_Clear_Intr_Status_Bit     (PIR1bits.SSPIF = 0)
  117.  
  118. #define I2C1_Clear_Intr_Status_Bit  I2C_Clear_Intr_Status_Bit
  119.  
  120. /*******************************************************************
  121. Macro       : I2C_Intr_Status
  122.  
  123. Include     : i2c.h
  124.  
  125. Description : Macro to return I2C Interrupt Status  
  126.  
  127. Arguments   : None
  128.  
  129. Remarks     : None
  130. *******************************************************************/
  131. #define I2C_Intr_Status     PIR1bits.SSPIF
  132.  
  133. #define I2C1_Intr_Status    I2C_Intr_Status
  134.  
  135. /**************************************************************************
  136. Macro       : getcI2C
  137.  
  138. Description : macro is identical to ReadI2C,#define to ReadI2C in i2c.h
  139.  
  140. Arguments   : None
  141.  
  142. Remarks     : None
  143. ***************************************************************************/
  144. #define  getcI2C  ReadI2C
  145.  
  146. /**************************************************************************
  147. Macro       : putcI2C
  148.  
  149. Description : macro is identical to WriteI2C,#define to WriteI2C in i2c.h
  150.  
  151. Arguments   : None
  152.  
  153. Remarks     : None
  154. ***************************************************************************/
  155. #define  putcI2C  WriteI2C
  156.  
  157. void IdleI2C( void );
  158.  
  159. void OpenI2C(  unsigned char sync_mode,  unsigned char slew );
  160.  
  161. signed char WriteI2C( unsigned char data_out );
  162.  
  163. signed char putsI2C(  unsigned char *wrptr );
  164.  
  165. unsigned char ReadI2C( void );
  166.  
  167. void CloseI2C( void );
  168.  
  169. #endif
  170.  
  171. #if defined (I2C_V1)
  172.  
  173. /* CloseI2C
  174.  * Disable SPI module
  175.  */
  176. //#define CloseI2C()  SSPCON1 &=0xDF
  177.  
  178. /* Idle I2C
  179.  * Test if I2C module is idle
  180.  */
  181. //#define IdleI2C()    while ((SSPCON2 & 0x1F) | (SSPSTATbits.R_W))
  182.  
  183. /*******************************************************************
  184. Macro       : StopI2C()
  185.  
  186. Include     : i2c.h
  187.  
  188. Description : Macro to initiate stop condition
  189.  
  190. Arguments   : None
  191.  
  192. Remarks     : This macro initiates stop condition and waits till the stop signal
  193.         sequence is terminated.This macro is applicable only to master
  194. *******************************************************************/
  195. #define StopI2C()  SSPCON2bits.PEN=1;while(SSPCON2bits.PEN)
  196.  
  197. /*******************************************************************
  198. Macro       : StartI2C()
  199.  
  200. Include     : i2c.h
  201.  
  202. Description : Macro to initiate start condition
  203.  
  204. Arguments   : None
  205.  
  206. Remarks     : This macro initiates start condition and waits till the start signal
  207.         sequence is terminated. This macro is applicable only to master
  208. *******************************************************************/
  209. #define StartI2C()  SSPCON2bits.SEN=1;while(SSPCON2bits.SEN)
  210.  
  211. /*******************************************************************
  212. Macro       : RestartI2C()
  213.  
  214. Include     : i2c.h
  215.  
  216. Description : Macro to initiate Restart condition
  217.  
  218. Arguments   : None
  219.  
  220. Remarks     : This macro initiates Restart condition and waits till the Restart signal
  221.         sequence is terminated. This macro is applicable only to master
  222. *******************************************************************/
  223. #define RestartI2C()  SSPCON2bits.RSEN=1;while(SSPCON2bits.RSEN)
  224.  
  225. /*******************************************************************
  226. Macro       : NotAckI2C()
  227.  
  228. Include     : i2c.h
  229.  
  230. Description : Macro to initiate negetive acknowledgement sequence
  231.  
  232. Arguments   : None
  233.  
  234. Remarks     : This macro initiates negetive acknowledgement condition and
  235.         waits till the acknowledgement sequence is terminated.
  236.         This macro is applicable only to master
  237. *******************************************************************/
  238. #define NotAckI2C()     SSPCON2bits.ACKDT=1;SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)
  239.  
  240. /*******************************************************************
  241. Macro       : AckI2C()
  242.  
  243. Include     : i2c.h
  244.  
  245. Description : Macro to initiate positive acknowledgement sequence
  246.  
  247. Arguments   : None
  248.  
  249. Remarks     : This macro initiates positive acknowledgement condition and
  250.         waits till the acknowledgement sequence is terminated.
  251.         This macro is applicable only to master
  252. *******************************************************************/
  253. #define AckI2C()        SSPCON2bits.ACKDT=0;SSPCON2bits.ACKEN=1;while(SSPCON2bits.ACKEN)
  254.  
  255. /************************************************************************
  256. Macro :  DataRdyI2C()
  257.  
  258. Include            : i2c.h
  259.  
  260. Description        : This Macro provides status back to user if SSPxBUF
  261.             register contain data.
  262.                      
  263. Arguments          : None
  264.  
  265. Remarks            : This Macro determines if there is any byte to read from
  266.             SSPxBUF register.
  267. *************************************************************************/
  268. #define DataRdyI2C()    (SSPSTATbits.BF)
  269.  
  270. /**************************************************************************
  271. Macro       : putcI2C
  272.  
  273. Description : macro is identical to WriteI2C,#define to WriteI2C in i2c.h
  274.  
  275. Arguments   : None
  276.  
  277. Remarks     : None
  278. ***************************************************************************/
  279. #define  putcI2C  WriteI2C
  280.  
  281. signed char WriteI2C(  unsigned char data_out );
  282.  
  283. signed char getsI2C(  unsigned char *rdptr,  unsigned char length );
  284.  
  285.  
  286.  
  287.  
  288. //******************* I2C EEPROM Function Prototype ******************************
  289.  
  290. signed char EEAckPolling(  unsigned char control );
  291.  
  292. signed char EEByteWrite(  unsigned char control,
  293.                             unsigned char address,
  294.                             unsigned char data );
  295.  
  296. signed int  EECurrentAddRead(  unsigned char control );
  297.  
  298. signed char EEPageWrite(  unsigned char control,
  299.                             unsigned char address,
  300.                             unsigned char *wrptr );
  301.  
  302. signed int  EERandomRead(  unsigned char control,  unsigned char address );
  303.  
  304. signed char EESequentialRead(  unsigned char control,
  305.                                  unsigned char address,
  306.                                  unsigned char *rdptr,
  307.                                  unsigned char length );
  308. #endif
  309.  
  310. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement