MCP23017 Problem

General discussion on mikroC PRO for dsPIC30/33 and PIC24.
Author
Message
Kap
Posts: 11
Joined: 23 Jan 2018 08:15

MCP23017 Problem

#1 Post by Kap » 23 Jan 2018 08:39

Hi,
I have connected MCP23017 to dsPIC33EP512MU810. I can write on it but I have a problem in reading. the chip can be read in first instant that I connect the circuit to a power supply (about 1s) then after the MCU reads only zeros.

The written program is as below:

Code: Select all

//-----------------------I/O Direction Registers(TRIS)--------------------------
#define IODIRA     0x00
#define IODIRB     0x01
//-----------------------General Purpose I/O------------------------------------
#define GPIOA      0x12   //Reading the GPIOn register reads the value on the port.
#define GPIOB      0x13
//-----------------------Output Latches-----------------------------------------
#define OLATA      0x14   //Writing to the OLATn register Writes to the associated port.
#define OLATB      0x15
//-----------------------W/R Control bytes--------------------------------------
#define WRITE      0x40   // 0b0100 + 0b000(Slave address) + 0b0(Write) => 0x40
#define READ       0x41
char DD, DATT;

void EXPANDER_write(char portAddr, char dat)
{
  I2C2_start();
  //while(I2C2_Is_Idle());          //Wait for previous operations to cease.
  I2C2_write(WRITE);
  I2C2_write(portAddr);
  I2C2_write(dat);
  I2C2_stop();
}
//------------------------------------------------------------------------------
char EXPANDER_read(char portAddr){
  char dat;
    I2C2_start();
  // while(I2C2_Is_Idle());
  I2C2_write(WRITE);
  I2C2_write(portAddr);
  I2C2_Restart();
  I2C2_write(READ);
  dat = I2C2_Read(1u);
  I2C2_stop();
  return dat;}

void EXPANDER_init(char portA, char portB)
{
  EXPANDER_write(IODIRA,portA);
  EXPANDER_write(IODIRB,portB);
}
 void main() {
  OSCCON=0x33E0;
  CLKDIV=0x0006;       //PLL's Divider Configuration (divided by CLKDIV<4:0>+2)
  PLLFBD=278;             //PLL's Feedback Congiguration (multiply by PLLFBD+2)
  //OSCTUN=0;
  //REFOCON=0;
  ACLKCON3=0xE4A0;        //Axuliary OSC PLL Configuration 0x00F0 post divider; 0x000F prescale
  ACLKDIV3=7;
 TRISF=0x0000;
  LATF=0x0000;
 LATF.b3=1;                                //RESET bit
  I2C2_init(100000);                       //Give speed in KBps
  EXPANDER_init(0x00,0xFF);              //PortA all Outputs, PortB all Inputs.
  while(1) {
    DATT=EXPANDER_read(GPIOB);
   // EXPANDER_write(OLATB, 0xFF);  //(writing works fine)
    DD=DATT+1;
      EXPANDER_write(OLATA, DD);
    delay_ms(1000);
  }}
I would be grateful if you give me your comments.

Regards,
Last edited by Kap on 29 Jan 2018 08:12, edited 1 time in total.

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: MCP23017 Problem

#2 Post by strahinja.jacimovic » 25 Jan 2018 18:36

Hi,

Data is written to the MCP23017 after every byte transfer.

If a Stop or Restart condition is generated during a data transfer, the data will not be written to the MCP23017.

Perhaps this is the reason.

Kind regards,
Strahinja Jacimovic

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#3 Post by Kap » 25 Jan 2018 19:41

Hi Strahinja,
Thank you for your response.
Writing works fine, the problem is reading. The MCU reads the MCP only the first time the circuit is powered. Afterwards, the writing works, and the MCU just reads zeros from MCP.
what could be wrong?

Regards,

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#4 Post by Kap » 28 Jan 2018 02:36

I tried to insert delay every where in the program after and before each start and stop. It does not work. Even when I program the MCU it does not read the MCU, at first. The only time It reads MCP is the first time I connect power to the circuit. What could be wrong?

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#5 Post by Kap » 29 Jan 2018 07:24

I have run some tests.
I can read some registers like IODIRA, IODIRA; when I try to read GPINTENA and GPINTENB both times GPINTENA is read, but some register cannot be read, above all GPIOB which I need to read in my program.

In another test, I tried to write 0x82 in IOCON register, but the chip acted like the bank 0 is active "IOCON=0x02".

there are some pictures that may give you clue.
photo_2018-01-28_19-41-11.jpg
photo_2018-01-28_19-41-11.jpg (155.5 KiB) Viewed 5511 times
photo_2018-01-28_19-40-43.jpg
photo_2018-01-28_19-40-43.jpg (158.27 KiB) Viewed 5511 times
0xo05 register should be 0x05
photo_2018-01-28_19-40-23.jpg
photo_2018-01-28_19-40-23.jpg (157.09 KiB) Viewed 5511 times
I would be grateful if you give me hint.

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#6 Post by Kap » 29 Jan 2018 07:29

There are two other pictures:
photo_2018-01-28_19-41-11.jpg
photo_2018-01-28_19-41-11.jpg (155.5 KiB) Viewed 5511 times
photo_2018-01-28_19-40-05.jpg
photo_2018-01-28_19-40-05.jpg (160.25 KiB) Viewed 5511 times

Thanks for your help.

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: MCP23017 Problem

#7 Post by strahinja.jacimovic » 30 Jan 2018 16:40

Hi,

Could you provide a schematic of your circuitry?

Kind regards,
Strahinja Jacimovic

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#8 Post by Kap » 30 Jan 2018 20:32

Thank you for your response. Please find this section schematics below. The whole circuit is rather large so I am posting just this section. Please do not hesitate to ask if there is any question.

Thank you for your help.

I am not sure, but I think there is some bugs with I2C functions of dsPIC33EP512MU810 in MikroC because "while(I2C2_Is_Idle());" keeps the program in infinite loop.
Attachments
Untitled.jpg
Untitled.jpg (294.88 KiB) Viewed 5481 times

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#9 Post by Kap » 04 Feb 2018 01:08

could some body please check the program on a similar hardware? I have checked every thing, I did not find any problem.
I really don't know what's wrong with the system.
Thank you.

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: MCP23017 Problem

#10 Post by strahinja.jacimovic » 05 Feb 2018 17:27

Hi,

The connections seem to be ok.

However, I think you have to remap your pins for I2C using peripheral pin select.

Consult the datasheet.

Kind regards,
Strahinja Jacimovic

Kap
Posts: 11
Joined: 23 Jan 2018 08:15

Re: MCP23017 Problem

#11 Post by Kap » 06 Feb 2018 00:28

Thanks for the response.
I am using I2C2 not AI2C2, so I think it is ok. Furthermore, sending procedure works fine. It seems totally fine. did you check the I2C reading function of the corresponding library?

Thanks

User avatar
strahinja.jacimovic
mikroElektronika team
Posts: 200
Joined: 18 Dec 2017 18:19

Re: MCP23017 Problem

#12 Post by strahinja.jacimovic » 08 Feb 2018 17:41

Hi,

I2C is all master driven so you can use software channels with no significant down side. This will let you choose whatever pins you want to use.

Looking at the datasheet I can only see the pin connections for the ASDA1/ASCL1 pins i.e. the alt pins.

Kind regards,
Strahinja Jacimovic

Virtual Gamer
Posts: 59
Joined: 20 Aug 2014 09:22

Re: MCP23017 Problem

#13 Post by Virtual Gamer » 23 Aug 2019 09:41

How you solved?

same problem I can't read

thanks

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: MCP23017 Problem

#14 Post by stefan.filipovic » 26 Aug 2019 13:14

Hi,

Are you using our Expand 2 click board?

Could you please provide us with more details about this issue?

Kind regards,
Stefan Filipović

ua100k
Posts: 27
Joined: 05 Jul 2017 22:37

Re: MCP23017 Problem

#15 Post by ua100k » 28 Jan 2021 23:42

I am using MikroeC Pro for FT813 and having the same problem. The read fails. I do a read for 20 times and I can see the mcp23017 incrementing the registers to be read. However the way it should work is the read should be directly on the register of the first write.

There is something wrong with the Mikroe Implementation of the I2C.

The only way to read and not get all zeros is to do a full write to a register. Then after then do a read. It seems the read begins after 2 registers from the last write. So MikroeC is doing more reads after the write when it should not.

I don't know what the problem is with Mikroe but you cannot do a read after only writing the register address of the mcp23017 which is what the mcp23017 data sheet says to do.

This whole program works on arduino. and the reads work exactly as described in the datasheet.

So once again the Mikroe I2C is not working correctly.

I am not able to compile and use the Expand2 click software even though it should work. The display goes into a cycle of resetting itself.

The documentation of the Expand2 is terrible and barely existent.

Please fix this very important command library.

Post Reply

Return to “mikroC PRO for dsPIC30/33 and PIC24 General”