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

📄 drv_i2c.txt

📁 picos18的i2c驱动程序样例
💻 TXT
字号:
 ______________________________________________________________________
>                                                                      <
>                            I2C driver v1.04                          <
>                                 for                                  <
>                         PICos18 release 2.00                         <
>                                                                      <
>             PICos18 - Real-time kernel for PIC18 family              <
>                                                                      <
>                                                                      <
> www.picos18.com                                    www.pragmatec.net <
>______________________________________________________________________<


This file contains all necessary informations to use properly the 
PIC18 MSSP I2C driver for PICos18 v2.00. Download the last PICos18
kernel version from www.picos18.com.


 ______________________________________________________________________
>                                                                      <
>                         I - Zip file content                         <
>______________________________________________________________________<

The drv_i2c zip file contains :
- drv_i2c.c
- drv_i2c.h   : header file
- drv_i2c.txt : this file

To use the PICos18 I2C driver, first unzip the file.
Place the C and H file in your project directory.

This driver is supposed to be use with PICos18 and has been tested with 
MPLAB 6.62 and C18 v2.40. Do not use this driver at a frequency lower 
than 8 Mhz (use the PIC18 PLL to increase the internal frequency).

 ______________________________________________________________________
>                                                                      <
>                        II - Hardware settings                        <
>______________________________________________________________________<

Adapt the SSPADD according to the oscillator frequency to set the correct
baud rate generator (file drv_i2c.c).

	//SSPADD= 0x63;			// Assuming 40MHz speed - 100KHz
	//SSPADD= 0x27;			// Assuming 16MHz speed - 100KHz
	SSPADD  = 0x09;			// Assuming 04MHz speed - 100KHz

Have a look at the PIC18 datasheet for different settings.

 ______________________________________________________________________
>                                                                      <
>                       III - TASCDESC settings (1)                    <
>______________________________________________________________________<

Update also the tascdesc.c file to take into account the new I2C task in
your application :

/**********************************************************************
 * ----------------------- TASK & STACK DEFINITION --------------------
 **********************************************************************/
DeclareTask(TASK0);
DeclareTask(I2C_Drv);
...

volatile unsigned char stack0[DEFAULT_STACK_SIZE];
volatile unsigned char stack_i2c[DEFAULT_STACK_SIZE];
...

 ______________________________________________________________________
>                                                                      <
>                        IV - TASCDESC settings (2)                    <
>______________________________________________________________________<

Finally add the task and alarm descriptor to the tascdesc.c file :

...
/**********************************************************************
 * ---------------------------  task I2C ----------------------------
 **********************************************************************/
rom_desc_tsk rom_desc_taskI2C = {
	DRV_I2C_PRIO,                  /* prioinit from 0 to 15       */
	stack_i2c,                     /* stack address (16 bits)     */
	I2C_Drv,                       /* start address (16 bits)     */
	READY,                         /* state at init phase         */
	I2C_DRV_ID,                    /* id_tsk from 0 to 15         */
	sizeof(stack_i2c)              /* ctx address   (16 bits)     */
};
...


AlarmObject Alarm_list[] = 
  {
   /*******************************************************************
    * -------------------------- First task ---------------------------
    *******************************************************************/
   {
     OFF,                                  /* State                   */
     0,                                    /* AlarmValue              */
     0,                                    /* Cycle                   */
     &Counter_kernel,                      /* ptrCounter              */
     TASK0_ID,                             /* TaskID2Activate         */
     ALARM_EVENT,                          /* EventToPost             */
     0                                     /* CallBack                */
   },
   ...,
   /*******************************************************************
    * ---------------------- Alarm I2C task ---------------------------
    *******************************************************************/
   {
     OFF,                                  /* State                   */
     0,                                    /* AlarmValue              */
     0,                                    /* Cycle                   */
     &Counter_kernel,                      /* ptrCounter              */
     I2C_DRV_ID,                           /* TaskID2Activate         */
     TIMEOUT_EVENT,                        /* EventToPost             */
     0                                     /* CallBack                */
   }
 };



Note :
In the drv_i2c.c, adapt the I2C_TIMEOUT_ALARM definition in order to
match with the above structure position.


 ______________________________________________________________________
>                                                                      <
>                         V - TASK Settings                            <
>______________________________________________________________________<
	
Open define.h and add this 3 definitions with correct value :
 
        #define I2C_NEW_MSG             0x02
        #define TIMEOUT_EVENT           0x04
        #define BUSERROR_EVENT          0x08
        #define IDLE_EVENT              0x10
	
        #define I2C_QUEUE_EMPTY         0x20
        Note : this last define shoulb be different of any other defines
               in the application task. Modify it if you're already use 
               the 0x20 value.
	
        #define I2C_DRV_ID                 7
        #define DRV_I2C_PRIO              12

The I2C task is considered like a driver, that means you have to give 
one of the highest priority to the I2C driver to let it run each time the 
I2C buffer is updated.

 ______________________________________________________________________
>                                                                      <
>                        VI - ISR connection                           <
>______________________________________________________________________<
	
Open the file int.c and declare the external I2C ISR function :
 
  extern void AddOneTick(void);
  extern void I2C_INT(void);                        <= here
  void InterruptVector(void);

And complete the InterruptVector function :

void InterruptVector(void)
{
  EnterISR();

  if (INTCONbits.TMR0IF == 1)
    AddOneTick();
  if ((PIR2bits.BCLIF == 1) ||   // Check bus collision
      (PIR1bits.SSPIF == 1))     // Check I2C interrupt
    I2C_INT();

  LeaveISR();
}

 ______________________________________________________________________
>                                                                      <
>                       VII - I2C driver usage                         <
>______________________________________________________________________<

In the application task, include the driver header:

#include "drv_i2c.h"

And declare an I2C message (type message_t):
message_t My_I2C_Message; 

In the task, set the I2C Message to match the specification of the target
connected to the I2C bus. Have a look at the following example.


See hereafter a basic example to acces an extenal EEPROM from the I2C bus:


#include "define.h"
#include "drv_i2c.h"

/**********************************************************************
 * Definition dedicated to the local functions.
 **********************************************************************/
#define ALARM_TSK0      0

/**********************************************************************
 * ------------------------------ TASK0 -------------------------------
 *
 * First task of the tutorial.
 *
 **********************************************************************/
TASK(TASK0) 
{
  I2C_message_t TC74_I2C;
  unsigned char Value;

  // R/W bit (LSB) and 7 bytes for the target address
  TC74_I2C.control         = 0b11100000;

  // Sub address of the target (for EEPROM)
  TC74_I2C.addr_high       = 0;
  TC74_I2C.addr_low        = 0x00;

  // Buffer to receive data from TC74
  TC74_I2C.ram_data        = &Value;
  TC74_I2C.num_bytes       = 1;

  // Option flags : short addr, SMBUS mode
  TC74_I2C.flags.long_addr = 0;
  TC74_I2C.flags.SMBus     = 1;
	
  // Send the message to the I2C buffer
  I2C_enqMsg(&TC74_I2C);
  SetEvent(I2C_DRV_ID, I2C_NEW_MSG);

  // Wait for the answer from I2C driver
  // if data expected (read operation)
  WaitEvent(I2C_QUEUE_EMPTY);
  ClearEvent(I2C_QUEUE_EMPTY);

  // Data has been received in the EEPROM_value buffer
  // Call a function here to manage the EEPROM data
  // You can check the "TC74_I2C.error" field to check
  // if transfert is successfull or not.
  // If the target does not respond some time to time 
  // you can use the automatic retry with the 
  // "TC74_I2C.retry_counter" field.

  TerminateTask();
}


 ______________________________________________________________________
>                                                                      <
>                      VIII - Conclusion                               <
>______________________________________________________________________<

This I2C driver is provided from the PICos18 web site with other drivers
dedicated to toher peripherals. Some zip files are also provided to be 
used with a driver. For instance a "TC74 temperature sensor" file should
be delivered to get the temperature from this I2C chip.
Do not hesitate to check periodicaly the www.picos18.com web site in order
to download the last versions of PIC18 drivers for PICos18.



/* End of File : drv_i2c.txt */


⌨️ 快捷键说明

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