将i2c寄存器设置为高电平

时间:2017-05-30 16:03:03

标签: pic i2c silicon

我有这个项目,我的老板让我这么做,第一步是弄清楚如何使用硅实验室库将给定的I2C寄存器设置为高或低,如果有人知道这类问题的任何好的来源请提供给他们谢谢。我正在使用的图片是pic16f1823,我已经查看了图片的文档,但仅说明了如何读取和写入I2c。

1 个答案:

答案 0 :(得分:0)

我将它用作头文件,似乎适用于PIC16F1827,它与1823基本相同。它使用了PIC的外设。只需在要使用i2c的任何c文件中包含。确保#define FOSC以计算正确的波特率。同时仔细检查端口和tris分配是否适合您的设备并进行调整。

它使用轮询而不是中断。取消注释中断设置代码并写入中断服务程序以捕获中断。

  #ifndef I2C_H
  #define   I2C_H

  #ifdef    __cplusplus
  extern "C" {
  #endif

  /*
   * Hi-Tech C I2C library for 12F1822
   * Master mode routines for I2C MSSP port to read and write to slave device 
   * Copyright (C)2011 HobbyTronics.co.uk 2011
   * Freely distributable.
  */

  #define I2C_WRITE 0
  #define I2C_READ 1

  // Initialise MSSP port. (12F1822 - other devices may differ)
  void i2c_Init(void){

      // Initialise I2C MSSP
      // Master 100KHz

      TRISB2 = 1;
      TRISB5 = 1;
      SSP1CON1 = 0b00101000; // I2C Master mode
      SSP1CON2 = 0b00000000; 
      SSP1CON3 = 0b00000000; 

      //SSP1MSK  = 0b00000000; 
      SSP1ADD  = I2C_BRG; // clock = FOSC/(4 * (SSPxADD+1))
      //SSP1IE = 1; // enable interrupt
      SSP1STAT = 0b10000000;
  }

  // i2c_Wait - wait for I2C transfer to finish
  void i2c_Wait(void){
      while ( ( SSP1CON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
  }

  // i2c_Start - Start I2C communication
  void i2c_Start(void)
  {
      i2c_Wait();
      SSP1CON2bits.SEN=1;
  }

  // i2c_Restart - Re-Start I2C communication
  void i2c_Restart(void){
      i2c_Wait();
      SSP1CON2bits.RSEN=1;
  }

  // i2c_Stop - Stop I2C communication
  void i2c_Stop(void)
  {
      i2c_Wait();
      SSP1CON2bits.PEN=1;
  }

  // i2c_Write - Sends one byte of data
  void i2c_Write(unsigned char data)
  {
      i2c_Wait();
      SSPBUF = data;
  }

  // i2c_Address - Sends Slave Address and Read/Write mode
  // mode is either I2C_WRITE or I2C_READ
  void i2c_Address(unsigned char address, unsigned char mode)
  {
      unsigned char l_address;

      l_address=address<<1;
      l_address+=mode;
      i2c_Wait();
      SSPBUF = l_address;
  }

  // i2c_Read - Reads a byte from Slave device
  unsigned char i2c_Read(unsigned char ack)
  {
      // Read data from slave
      // ack should be 1 if there is going to be more data read
      // ack should be 0 if this is the last byte of data read
      unsigned char i2cReadData;

      i2c_Wait();
      SSP1CON2bits.RCEN=1;
      i2c_Wait();
      i2cReadData = SSPBUF;
      i2c_Wait();
      if ( ack ) SSP1CON2bits.ACKDT=0;          // Ack
      else       SSP1CON2bits.ACKDT=1;          // NAck
      SSP1CON2bits.ACKEN=1;                    // send acknowledge sequence

      return( i2cReadData );
  }

  #ifdef    __cplusplus
  }
  #endif

  #endif    /* I2C_H */

然后,您可以使用上面定义的更高级别的功能来控制设备,这在从设备的数据表中有所描述。

例如,从eeprom中读取:

  #include <xc.h>
  #define FOSC 16000000
  #include "i2c.h"

  unsigned char i2c_read_eeprom( unsigned char slaveaddress, unsigned char memaddress )
  {
      unsigned char data;

      data = 123;

      i2c_Start();
      i2c_Address( slaveaddress, I2C_WRITE);
      i2c_Write(memaddress);
      if( SSP1CON2bits.ACKSTAT )
           txstring("ACK!\r\n");
      else txstring("nACK!\r\n");

      i2c_Start();
      i2c_Address( slaveaddress, I2C_READ);
      data = i2c_Read(0);

      i2c_Stop();

      return data;
  }
相关问题