MCP23017 ile ilgili örnek uygulama

Başlatan elektromagma, 27 Mart 2018, 13:23:35

elektromagma

Merhaba arkadaşlar,

MCP23017 ile ilgili örnek uygulama paylaşabilirmisiniz?

Daha önce tecrübesi olanlar paylaşabilir mi?

Teşekkür ederim.
Anadolu MakerSpace http://www.anadolums.com/

baran123

I2C ile 3-5 register kontrol edeceksin neyin uygulamasını verelim ? :)

elektromagma

Yapmaya çalıştığım program şu şekilde ;

main.c

#include <main.h>
#include <16f887.h>

#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
#include "mcp23017.C" 

int8 count;
byte text;

#int_TIMER1
void  TIMER1_isr(void)
{

}

#int_EXT
void  EXT_isr(void)
{

}



void main()
{

  setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);      //104 ms overflow
// setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(INT_EXT);
  enable_interrupts(GLOBAL);
  set_tris_A(0x00);
  set_tris_B(0x00);
  output_A(0x02);
  output_B(0x00);
  init_mcp();
  count = 255;
  
    //Example blinking LED program
    while(true){
      count--;
      output_high(PIN_A0);
      write_MCP(MCP23017_I2C_WRITE, _GPIOA, count);
      delay_ms(1000);
      output_low(PIN_A0);
      delay_ms(1000);
      text = read_MCP(MCP23017_I2C_WRITE, _IODIRB, MCP23017_I2C_READ);
    }
}


mcp23017.C


#define MCP23017_I2C_WRITE 0x40
#define MCP23017_I2C_READ 0x41


// MCP23017 REGISTERS
#define _IODIRA      0x00
#define _IPOLA        0x01
#define _GPINTENA    0x02
#define _DEFVALA      0x03
#define _INTCONA      0x04
#define _IOCON        0x05
#define _GPPUA        0x06
#define _INTFA        0x07
#define _INTCAPA      0x08
#define _GPIOA        0x09
#define _OLATA        0x0A

#define _IODIRB      0x10
#define _IPOLB        0x11
#define _GPINTENB    0x12
#define _DEFVALB      0x13
#define _INTCONB      0x14
#define _GPPUB        0x16
#define _INTFB        0x17
#define _INTCAPB      0x18
#define _GPIOB        0x19
#define _OLATB        0x1A

int ldata;

// ----------------------------------------------------------------------------
void write_MCP(unsigned char WriteAddress, unsigned char cmdByte, unsigned char data)
{

  i2c_start();                  // start condition
  delay_us(20);
  
  i2c_write(WriteAddress);    // Send slave address and clear (R/W_)
  delay_us(20);

  i2c_write(cmdByte);      // Command byte and register to be written.
  delay_us(20);

  i2c_write(data);          // First data byte pair as per command byte(cmd)
  delay_us(20);

  i2c_stop();            // stop condition  
  delay_us(50);
}

// ----------------------------------------------------------------------------
long int read_MCP(unsigned int WriteAddress, unsigned int cmdByte, unsigned int ReadAddress)
{
  unsigned int data;

  i2c_start();            // start condition
  delay_us(20);
  
  i2c_write(WriteAddress);    // Send slave address and clear (R/W_)
  delay_us(20);
  
  i2c_write(cmdByte);      // Command byte and register to be written.
  delay_us(50);
  
  i2c_start();
  delay_us(20);            // restart condition
  
  i2c_write(ReadAddress);      // Send slave address and clear (R_/W)
  delay_us(20);
  
  data = i2c_read(0);      // Data from LSB or MSB of register
  delay_us(20);
  
  i2c_stop();            // stop condition

  return(data);
}

// ----------------------------------------------------------------------------
void init_mcp()
{
  output_float(PIN_C3);
  output_float(PIN_C4);

  // Wait for MCP23017 Expander Device to power-up.
  delay_ms(250);


  // Set-up selected I/O expander unit PORTA
  write_MCP(MCP23017_I2C_WRITE, _IOCON, 0b10000010); // Direction of all data is output.
  write_MCP(MCP23017_I2C_WRITE, _IODIRA, 0b00000000); // Direction of all data is output.
  write_MCP(MCP23017_I2C_WRITE, _IPOLA, 0b00000000);  // NonInvert all input polarities if active low
  write_MCP(MCP23017_I2C_WRITE, _GPINTENA, 0b00000000); // INTERRUPT ON CHANGE OF PIN.  
  write_MCP(MCP23017_I2C_WRITE, _DEFVALA, 0b00000000); // Direction of all data is output.  
  write_MCP(MCP23017_I2C_WRITE, _INTCONA, 0b00000000); // Direction of all data is output.
  write_MCP(MCP23017_I2C_WRITE, _GPPUA, 0b00000000);  // Enable pullups
  //write_MCP(MCP23017_I2C_WRITE, _INTFA, 0xF0);  // READ-ONLY 
  //write_MCP(MCP23017_I2C_WRITE, _INTCAPA, 0x00);  // READ-ONLY 
  //write_MCP(MCP23017_I2C_WRITE, _GPIOA, 0xFF);  // READ-ONLY  
  //write_MCP(MCP23017_I2C_WRITE, _OLATA, 0x00);  // Update o/p latch that controls the output.

  // Set-up selected I/O expander unit PORTB
  write_MCP(MCP23017_I2C_WRITE, _IODIRB, 0b11111111); // Direction of all data is output.  
  write_MCP(MCP23017_I2C_WRITE, _IPOLB, 0b00000000);  // NonInvert all input polarities if active low
  write_MCP(MCP23017_I2C_WRITE, _GPINTENB, 0b11111111); // INTERRUPT ON CHANGE OF PIN.  
  write_MCP(MCP23017_I2C_WRITE, _DEFVALB, 0b00000000); // Direction of all data is output.  
  write_MCP(MCP23017_I2C_WRITE, _INTCONB, 0b00000000); // Direction of all data is output.
  write_MCP(MCP23017_I2C_WRITE, _GPPUB, 0b00000000);  // Enable pullups
  //write_MCP(MCP23017_I2C_WRITE, _INTFB, 0xFF);  // Enable pullups  
  //write_MCP(MCP23017_I2C_WRITE, _INTCAPB, 0x00);  // READ-ONLY 
  //write_MCP(MCP23017_I2C_WRITE, _GPIOB, 0x00);  // READ-ONLY  
  //write_MCP(MCP23017_I2C_WRITE, _OLATB, 0x00);  // Update o/p latch that controls the output.
}


Bilgisini paylaşacak arkadaşlara teşekkür ederim.


Kaynak: https://ccsinfo.com/forum/viewtopic.php?t=47936&postdays=0&postorder=asc&start=0

------------------------------------



Yardımın için çok teşekkür ederim.


Alıntı yapılan: baran123 - 27 Mart 2018, 21:50:41I2C ile 3-5 register kontrol edeceksin neyin uygulamasını verelim ? :)
Anadolu MakerSpace http://www.anadolums.com/