Como utilizar MCP23017 en ccs?

Observando por internet no hay mucha informacion de como utilizar el MCP23017 con un pic en CCS, hay pero siempre con arduino algo que no utilizo. Incluyendo aqui en el foro hay solo unos pocos items de este Integrado y el que podria llegar a servir hay un ejemplo que no funciona y en Basic, cosa que tampoco me sirve. La consulta es alguien utilizo este integrado con un pic, y con la libreria mcp23017.c de ccs, porque no logro hacerlo funcionar, estoy utilizando el proteus para probar, pero si no logro hacerlo no lo compro. Necesitaria ver aunque sea un ejemplo de encer leds o algo, nose como comenzar?

asi tengo configurado mi pic
HTML:
#include <18F46K22.h>
#device ADC=10

#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PROTECT                  //Code protected from reads

#use delay(internal=4MHz)
//#use FIXED_IO( A_outputs=PIN_A3 )
#use FIXED_IO( B_outputs=PIN_B3 )
#use FIXED_IO( C_outputs=PIN_C0 )
#use FIXED_IO( E_outputs=PIN_E2,PIN_E1,PIN_E0 )
//#define rs   PIN_A0
//#define rw   PIN_A1
//#define enable   PIN_A2
//#define BKLT   PIN_A3
//#define D4   PIN_A4
//#define D5   PIN_A5
//#define D6   PIN_A6
//#define D7   PIN_A7
#define INT00   PIN_B0
#define INT01   PIN_B1
#define INT02   PIN_B2
#define RB3   PIN_B3
#define IN_RB4   PIN_B4
#define IN_RB5   PIN_B5
#define IN_RB6   PIN_B6
#define IN_RB7   PIN_B7
#define SPKR   PIN_C0
#define RS_EN   PIN_C1
#define ADC14   PIN_C2
#define I2C_CLK   PIN_C3
#define I2C_DTA   PIN_C4
#define STS   PIN_C5
#define TX1   PIN_C6
#define RX1   PIN_C7
#define SPI2_CL   PIN_D0
#define SPI2_DI   PIN_D1
#define SPI2_DG   PIN_D2
#define SPI2_EN   PIN_D3
#define SPI2_DO   PIN_D4
#define SPI2_LD   PIN_D5
#define TX2   PIN_D6
#define RX2   PIN_D7
#define RE0   PIN_E0
#define RE1   PIN_E1
#define RE2   PIN_E2
#define MCLR   PIN_E3

#use rs232(baud=9600,parity=N,xmit=TX1,rcv=RX1,bits=8,stream=PORT1)



//port2
#define RS485_USE_EXT_INT FALSE
#define RS485_RX_PIN RX2
#define RS485_TX_PIN TX2
#define RS485_ENABLE_PIN RS_EN
//#define RS485_RX_ENABLE None
#define RS485_RX_BUFFER_SIZE 10
#define RS485_ID 10
#include <rs485.c>//NOTE: only one RS485 port is supported by the driver

//#include <rs485.c>//NOTE: only one RS485 port is supported by the driver

#use i2c(Master,Fast,sda=I2C_DTA,scl=I2C_CLK,force_hw)

asi es la libreria que utilizo, donde coloque // para anular la parte de reset porque sino tengo que configurar otro pic del pic cosa que no quiero.

HTML:
/*
                               mcp23017.c

   Driver for Microchip MCP23017 16bit I/O expander using I2C.
   
   This device also has weak internal pull-ups and interrupt on change.
   
   MCP23017_I2C_ADDRESS - user can overwrite this #define before including
      this library.  It user doesn't, the driver will use the default value
      of 0x40 (the value if A1, A2 and A3 are ground)

   MCP23017_I2C_STREAM - if this is defined, the library will use this I2C
      stream identifier and won't create a new one with #use i2c().
   
   MCP23017_IC2_PINS - if this is defined and MCP23017_I2C_STREAM isn't
      defined, then this will be used when creating #use i2c().
      Some examples:
         #define MCP23017_IC2_PINS SCL=PIN_C3 SDA=PIN_C4
         #define MCP23017_IC2_PINS I2C1, FORCE_HW
   
   MCP23017_I2C_BAUD - user can overwrite this #define before including
      this library.  Max speed at 1.7M at 5V, 400K at 2.7V, 100K for all
      other voltages.  If user doesn't specify, will use 100K speed.
   
   MCP23017_PIN_RESET - user must define this to the ~RESET pin of the chip.
   
   mcp23017_init() - initialize chip and library.
   
   mcp23017_set_tris(int16 tris) - set tris of port A and port B.  port A
      is the LSB, port B is the MSB.
   
   int16 mcp23017_input() - read both port A and port B at the same 
      time.  A will be LSB, B will be MSB.
      
   int8 mcp23017_input_a() - read port A
   
   int8 mcp23017_input_b() - read port B
   
   mcp23017_output(int16 val) - set output bits for port A and port B.  Port A
      is the LSB and port B is the MSB.
      
   int16 mcp23017_get_latch() - get the output bits for port A and port B.
      Port A is the LSB and port B is the MSB.
   
   mcp23017_write_register(int8 register, int8 val) - write to one of the
      21 internal registers of the chip
   
   int8 mcp23017_read_register(int8 register) - read from one of the 21
      internal registers of the chip
*/
///////////////////////////////////////////////////////////////////////////
////       (C) Copyright 1996,2014 Custom Computer Services            ////
////                  http://www.ccsinfo.com                           ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                               ////
///////////////////////////////////////////////////////////////////////////

#ifndef __MCP23017_C__
#define __MCP23017_C__

// these register defines are only valid if you leave the BANK bit in IOCON 
// clear.  this library will always leave this bit clear.
#define MCP23017_REG_IODIRA 0x00
#define MCP23017_REG_IODIRB 0x01
#define MCP23017_REG_IPOLA 0x02
#define MCP23017_REG_IPOLB 0x03
#define MCP23017_REG_GPINTENA 0x04
#define MCP23017_REG_GPINTENB 0x05
#define MCP23017_REG_DEFVALA 0x06
#define MCP23017_REG_DEFVALB 0x07
#define MCP23017_REG_INTCONA 0x08
#define MCP23017_REG_INTCONB 0x09
#define MCP23017_REG_IOCON 0x0a
//#define MCP23017_REG_IOCON 0x0b
#define MCP23017_REG_GPPUA 0x0c
#define MCP23017_REG_GPPUB 0x0d
#define MCP23017_REG_INTFA 0x0e
#define MCP23017_REG_INTFB 0x0f
#define MCP23017_REG_INTCAPA 0x010
#define MCP23017_REG_INTCAPB 0x11
#define MCP23017_REG_GPIOA 0x12
#define MCP23017_REG_GPIOB 0x13
#define MCP23017_REG_OLATA 0x14
#define MCP23017_REG_OLATB 0x15

#ifndef MCP23017_I2C_ADDRESS
#define MCP23017_I2C_ADDRESS  0x40
#endif

#ifndef MCP23017_I2C_STREAM
   #ifndef MCP23017_IC2_PINS
      #define MCP23017_IC2_PINS  I2C1, FORCE_HW
   #endif
   
   #ifndef MCP23017_I2C_BAUD
      #define MCP23017_I2C_BAUD 100000
   #endif
   
   #define MCP23017_LIBRARY_CREATED_STREAM
   
   #use i2c(MASTER, stream=MCP23017_I2C_STREAM, MCP23017_IC2_PINS, fast=MCP23017_I2C_BAUD, NOINIT)
#endif

void mcp23017_init(void)
{
  #if defined(MCP23017_LIBRARY_CREATED_STREAM)
   i2c_init(MCP23017_I2C_STREAM);
  #endif
   
//   output_low(MCP23017_PIN_RESET);
//   delay_us(2);
//   output_high(MCP23017_PIN_RESET);
//   delay_us(2);
}

static void mcp23017_write_bytes(unsigned int8 reg, unsigned int8 *p, unsigned int8 n)
{
   i2c_start();
   i2c_write(MCP23017_I2C_ADDRESS);
   i2c_write(reg);
   while(n--)
   {
      i2c_write(*p++);
   }
   i2c_stop();
}

static void mcp23017_read_bytes(unsigned int8 *p, unsigned int8 reg, unsigned int8 n)
{
   i2c_start();
   i2c_write(MCP23017_I2C_ADDRESS);
   i2c_write(reg);
   i2c_start();
   i2c_write(MCP23017_I2C_ADDRESS | 1);
   while(n--)
   {
      *p++ = i2c_read(n!=0);
   }
   i2c_stop();
}

void mcp23017_set_tris(unsigned int16 tris)
{
   mcp23017_write_bytes(MCP23017_REG_IODIRA, &tris, sizeof(tris));
}  

unsigned int16 mcp23017_input(void)
{
   unsigned int16 ret;
   
   mcp23017_read_bytes(&ret, MCP23017_REG_GPIOA, sizeof(ret));
   
   return(ret);
}
      
unsigned int8 mcp23017_input_a(void)
{
   unsigned int8 ret;
   
   mcp23017_read_bytes(&ret, MCP23017_REG_GPIOA, sizeof(ret));
   
   return(ret);
}

unsigned int8 mcp23017_input_b(void)
{
   unsigned int8 ret;
   
   mcp23017_read_bytes(&ret, MCP23017_REG_GPIOB, sizeof(ret));
   
   return(ret);
}
   

void mcp23017_output(unsigned int16 val)
{
   mcp23017_write_bytes(MCP23017_REG_OLATA, &val, sizeof(val));
}

unsigned int16 mcp23017_get_latch(void)
{
   unsigned int16 ret;
   
   mcp23017_read_bytes(&ret, MCP23017_REG_OLATA, sizeof(ret));
   
   return(ret);
}
   
void mcp23017_write_register(unsigned int8 reg, unsigned int8 val)
{
   mcp23017_write_bytes(reg, &val, sizeof(val));
}
   
unsigned int8 mcp23017_read_register(unsigned int8 reg)
{
   unsigned int8 val;
   
   mcp23017_read_bytes(&val, reg, sizeof(val));
   
   return(val);
}

#endif


bueno la cosa es que nose como hacer y no quiero utilizar el PCF8574 porque necesito muchas salidas y utilizaria como 8 de esos cosa que con 4 de mcp23017 seria mejor para mi.

bueno esa es mi consulta si tienen algun ejemplo agradeceria.
Capaz es sencillo pero no lo veo nose que me pasa?

tine configurado para uno solo de esos que es el 0x40 y quiero utilizar 4 de esos integrados. si tienen ejemplos gracias.

Pero algo me dice, que la libreria no sirve, como que no esta bien diseñada.

Muchas Gracias
 
Última edición:
Pues deberias tratar de hacer algo de tu programa aunque no funcione, luego a partir de ahi podemos ver donde estan los errores. Para manejar el MCP23017 debes hacer uso de las funciones del archivo MCP23017.c, alli se encuentran las funciones ya implementadas para poder leer y escribir. Luego con la hoja de datos en mano tiene que ver lo que quieres hacer para poder ir configurando el MCP23017.
 
si justamente estoy en eso de subir aunque sea algo del programa, pero antes estoy viendo la libreria, para mi no sirve, lee la libreria, como que la salida, tris y demas esta todo para el puerto A.
El B nada, el programa antes algo bien basico que se yo para ir comenzando encer un led, pero no me convence la libreria. No tiene ni siquiera para controlar otros integrados. si adelanto algo o vea algo escribire. Yo quiero algo sencillo ejemplo
mcp_output(direccion, dato) listo algo asi y que me envie al pin uno pero veo que esta libreria no sirve. . osea ya lo basico que es algo asi no se puede
 
tengo un problema con la interrupcion estoy configurando el mcp23017 puerto a como salida y puerto b como entrada, puse las resistencias pull-up preciono un switch y veo que la interrupcion B pasa de alto a Bajo, pero cuando suelto el switch sigue en bajo, no tendria que volver a alto?
no hagan caso al resto porque estoy usando otros mcp como salida solo uno esta como salida y entrada, pero bueno la cosa es activar algo un switch por ejemplo en la entrada y que el pic lo lea. pero tiene que activar la interrupcion para que el pic haga caso a ese integrado. pero me parece que tendria que volver a alto osea cambia de estado cuando preciono el boton pero este mantiene a bajo.
Que es problema en la configracion no? la verdad no veo el problema no conozco mucho dicho integrado

otra pregunta tiene resistencias internas como se activan? con mcp_write(addrs_mcp,MCP23017_REG_GPIOB,0xF0);

void mcp_init()
{
int i;
for(i=0;i<sizeof(addrs_mcp);i++)
{
mcp_trisa(addrs_mcp,0);
mcp_outputa(addrs_mcp,0xff);
mcp_trisb(addrs_mcp,0);
mcp_outputb(addrs_mcp,0xff);

if(i>3){
mcp_trisa(addrs_mcp,0);
mcp_outputa(addrs_mcp,0);
mcp_trisb(addrs_mcp,0xff);

mcp_write(addrs_mcp,MCP23017_REG_GPINTENB,0x0f);
mcp_write(addrs_mcp,MCP23017_REG_INTCONB,0x0f);
// mcp_write(addrs_mcp,MCP23017_REG_INTCAPB,0xff);

// mcp_outputb(addrs_mcp,1);
}
// else
// mcp_trisb(addrs_mcp,0xff);
}
}//FIN INICIALIZA MCP
 
Última edición:
Atrás
Arriba