CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

MCP23008 reading port

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

MCP23008 reading port
PostPosted: Sat May 11, 2013 6:07 pm     Reply with quote

hello friends,

I have the following code:

Code:
   MCP23008_write(MCP23008_3_write, _IODIR, 0xFF); // All Pins are Inputs
   MCP23008_write(MCP23008_3_write, _IPOL, 0x00); // All Outputs are similar to inputs
   MCP23008_write(MCP23008_3_write, _GPINTEN, 0xFF); //All Interrupt on Change
   MCP23008_write(MCP23008_3_write, _DEFVAL, 0x00); //To Compare GPINTEN & INTCON against DEFVAL
   MCP23008_write(MCP23008_3_write, _INTCON 0xFF); //Compare with 0 value
   MCP23008_write(MCP23008_3_write, _IOCON, 0x02); //int active high
   MCP23008_write(MCP23008_3_write, _GPPU, 0x00); // Disable Pull-Up Resistors
   MCP23008_write(MCP23008_3_write, _INTF, 0x00);
   MCP23008_write(MCP23008_3_write, _GPIO, 0x00);
   MCP23008_write(MCP23008_3_write, _OLAT, 0x00);


the problem is that the setup is good but the interrupt is not being fired. I am sending this code to setup the MCP23008 as input (read) on Interrupt change. I read all the datasheet and related application note but still in the same problem. Can someone please guide me if I've setup the MCP23008 correctly?

Thanks
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun May 12, 2013 11:36 am     Reply with quote

without source code for

MCP23008_write()

and how you use it in MAIN()

how can anybody tell what
is going on ??
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Sat Mar 01, 2014 10:26 pm     Reply with quote

dear all,

I continue to work on this chip and still have problems to clear interrupt when read the INTCON register. This is the driver code:

Code:
 FUNCTIONS DEFINITIONS
#define MCP23008_1_write 0x4E
#define MCP23008_1_read 0x4F

#define MCP23008_2_write 0x48
#define MCP23008_2_read 0x49

#define MCP23008_3_write 0x4C
#define MCP23008_3_read 0x4D


// MCP23008 REGISTERS
#define _IODIR       0x00 // determines if the pin is input or output
#define _IPOL        0x01 // the polarity of the input. Can have invert input
#define _GPINTEN     0x02 // interrupt on change input
#define _DEFVAL      0x03 // default comparision
#define _INTCON      0x04 // compare with the DEFVAL during interrupt
#define _IOCON       0x05 // I/O Expander configuration
#define _GPPU        0x06 // pull-up resistors
#define _INTF        0x07 // interrupt flag
#define _INTCAP      0x08 // capture the state of the port during interrupt
#define _GPIO        0x09 // actual input/outputs on the port
#define _OLAT        0x0A // logic level register of the output port

long int MCP23008_read(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();       // Data from LSB or MSB of register
   delay_us(20);
   
   i2c_stop();             // stop condition

   return(data);
}

void MCP23008_write(unsigned char location, unsigned char addr, unsigned char data)
{
   
i2c_start();
delay_us(20);

i2c_write(location);
delay_us(20);

i2c_write(addr);
delay_us(20);

i2c_write(data);
delay_us(20);

i2c_stop();
delay_us(20);

}



void MCP23008_init()
{
   output_float(PIN_C4);
   output_float(PIN_C3); //signals to prepare MCP for operation
   delay_ms(250);   //delay for MCP23008 start-up
   
   MCP23008_write(MCP23008_1_write, _IODIR, 0x00); // All Pins are Outputs
   MCP23008_write(MCP23008_1_write, _IPOL, 0x00); // All Outputs are similar to inputs
   MCP23008_write(MCP23008_1_write, _IOCON, 0x00);
   
   MCP23008_write(MCP23008_2_write, _IODIR, 0x00); // All Pins are Outputs
   MCP23008_write(MCP23008_2_write, _IPOL, 0x00); // All Outputs are similar to inputs
   MCP23008_write(MCP23008_2_write, _IOCON, 0x00);
   
   MCP23008_write(MCP23008_3_write, _IODIR, 0xFF); // All Pins are Inputs
   MCP23008_write(MCP23008_3_write, _IPOL, 0x00); // All Outputs are similar to inputs
   MCP23008_write(MCP23008_3_write, _GPINTEN, 0xFF); //All Interrupt on Change
   MCP23008_write(MCP23008_3_write, _DEFVAL, 0x00); //To Compare GPINTEN & INTCON against DEFVAL
   MCP23008_write(MCP23008_3_write, _INTCON 0xFF); //Compare with Defcal Register
   MCP23008_write(MCP23008_3_write, _IOCON, 0x0A);
   MCP23008_write(MCP23008_3_write, _GPPU, 0x00); // Disable Pull-Up Resistors
   MCP23008_write(MCP23008_3_write, _IOCON, 0x00);
   MCP23008_write(MCP23008_3_write, _GPIO, 0x00);
   MCP23008_write(MCP23008_3_write, _OLAT, 0x00);
}


the main code is:

Code:
#include <program.h>
#include <mcp23008.c>

//VARIABLES FOR THE PROGRAM

int counter1 = 0;
int GP_count = 0;
int8 read_port = 0;


#INT_EXT
void  EXT_isr(void)
{
   output_high(PIN_D1);
   output_high(PIN_D2);
   read_port = MCP23008_Read(0x4C,0x08, 0x4D);
   output_low(PIN_D1);
   mcp23008_write(0x48, 0x09, read_port);
   output_low(PIN_D2);
}



void main()
{
   
   mcp23008_init();
   enable_interrupts(INT_EXT);
   ext_int_edge( H_TO_L );
   enable_interrupts(GLOBAL);

   while(TRUE)
   {
     
   }

}


I also draw the circuit on proteus but this is real simulation and not software simulation. Used proteus only to show the circuit

http://postimg.org/image/47llzictt/
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 01, 2014 10:38 pm     Reply with quote

Quote:

data = i2c_read(); // Data from LSB or MSB of register

This is the same old problem. It's missing the NAK on the last i2c_read
in your MCP23008_read() function.

In your thread here, from 2011, you had the NAK in there. Why did
you take it out in your latest code ? It's essential:
http://www.ccsinfo.com/forum/viewtopic.php?t=46502

In this thread here, which looks like the place you got the driver from,
he makes the missing NAK mistake and I correct it:
http://www.ccsinfo.com/forum/viewtopic.php?t=22639
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Sun Mar 02, 2014 12:09 am     Reply with quote

thanks I solve that problem. I forgot the "0"...thanks

But the problem is that When I press the button, the interrupt is working and it is being written on the second MCP chip. The problem is that when I press another button nothing is happening. ON the datasheet it is mentioned that when the register INTCON (0x08) is being read, the interrupt will be reset. Do I missing something please?
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Sun Mar 02, 2014 9:29 am     Reply with quote

Afterr more time trying, i am still have no success....any help please?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Mar 02, 2014 2:16 pm     Reply with quote

Reduce your code to a few lines so showing us what your real problem is.

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group