将I2C寄存器设置为高



我有我的老板要我要这样做的项目,第一步是弄清楚如何使用硅实验室库将给定的I2C寄存器设置为高或低点,如果有人知道任何有关此类问题的良好资源,请给他们谢谢。我使用的图片是PIC16F1823,我已经查看了图片的文档,但只指出了如何读写和写入I2C。

我将其用作标头文件,并且似乎适用于PIC16F1827,该文件基本上与1823年相同。它使用了PIC的外围。只需在您要使用I2C中的任何C文件中包含即可。请确保您#Define FOSC以计算正确的BAUD率。还要仔细检查端口和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!rn");
      else txstring("nACK!rn");
      i2c_Start();
      i2c_Address( slaveaddress, I2C_READ);
      data = i2c_Read(0);
      i2c_Stop();
      return data;
  }

相关内容

  • 没有找到相关文章

最新更新