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

MCP23009 Via I2C don't work

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



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

MCP23009 Via I2C don't work
PostPosted: Mon Feb 05, 2024 2:19 pm     Reply with quote

PIC16F1619
ICD3
CCS Version: 5.080

Hello,

I have connected the MCP23009 to the PIC16F1619 via I2C, but only one transmission is possible.
If I only use the i2c_write command, everything works.
As soon as an i2c_read command is in the code, the transmission is aborted after one time. The i2c_read command is executed exactly once.

I did the same thing with a PIC18F4520 for testing purposes, but it doesn't work either.
I've been trying for many hours now and can't get any further.

I hope someone can help.


Code:


// CCS Version 5.080
// Programmer: ICD3


// PIC16F1619 <=> I2C <=> MCP23009

// MCP23009

/*
adress pin = 0V
Reset pin = 5V
*/




#include <16F1619.h>
#device ADC=10
#use delay(internal=16Mhz)


 
#pin_select SCL1=PIN_C7       // PIN  9
#pin_select SDA1=PIN_B6      // Pin 11

#use I2C(master,slow, I2C1, FORCE_HW)   // don't work


//#use I2C(master,slow, sda=PIN_B6, scl=PIN_C7, FORCE_SW)


#fuses PUT               // Power Up Timer
#fuses MCLR             // Master Clear pin enabled
#fuses BROWNOUT        // Reset when brownout detected
#fuses PROTECT        // Code protected from reads


#define     LED        PIN_C4
#define     led_on     output_high(led)   
#define     led_off    output_low(led)
   
   
void main()
{   
     setup_oscillator(OSC_16MHZ); 
      delay_us(50);       
      SETUP_WDT(WDT_OFF);
 
 unsigned int8    GPIOPort=0;
 
 
         led_on;     
         delay_ms(500);     
         led_off;
         delay_ms(500);

 
         output_float(pin_B6);  // SDA
         output_float(pin_C7);  // SCL
     
// configure MCP23009
       
 // Pull-Ups ON       
         i2c_start();
         i2c_write(0b01000000);          // Device address, Bit0=0 = > Write         
         i2c_write(0x06);               // Adress for  Pull up   
         i2c_write(0b11111111);        // All pull ups on
         i2c_stop();
         
             
 
// All Pins Input
         i2c_start();
         i2c_write(0b01000000);         // Device address, Bit0=0 => write         
         i2c_write(0b00000000);        // Adress for  I/O= 00h
         i2c_write(0b11111111);       // GPIO all in
         i2c_stop();

     
       while(1)
       {
     
 // read Port  MCP23009
         //delay_us(20);
         
         i2c_start();
         i2c_write(0b01000000);        // Device address, Bit0=0 => Write
         i2c_write(0x09);             // Adress for GPIO Reg.
         
         i2c_start();                // restart
         
         i2c_write(0b01000001);     // Device address, Bit0=1 => Read
       
         //delay_us(20);                         
 
         GPIOPort = i2c_read(1);
       
         i2c_stop();
         delay_us(80);
         

         
       if( bit_test(GPIOPort,2) == 0)  // Pin2 MCP23009
       led_on;
       else led_off;


     }
   
     
       
     

} // void main


temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 05, 2024 3:13 pm     Reply with quote

comment, while I don't use either...

perhaps you have the wrong address
copy/compile/run PCMP 'I2C Scanner' program in the 'code library'.
It'll find the address of your device

Also be sure to use the correct pullup resistors(4k7 for 5v, 3k3 for 3v..seem to work fine..)
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Tue Feb 06, 2024 2:17 am     Reply with quote

What pull up resistors have you got on the I2C lines?.
How is the ADDR input to the MCP wired?. Directly to 0v, or a resistor?.
Don't use FORCE_HW, and I2C1. The I2C1 setting is automatically selecting
the hardware.
Do not use PROTECT, while trying to debug. This reduces the life of the
chip, and should only be used when your code is complete.

One other glaring thing. Why are you using i2c_read(1)?????
The _last_ read on an i2c_read transaction must use i2c_read(0).
Using 1, is telling the device your read wants to continue......
andresteff



Joined: 21 Mar 2020
Posts: 44

View user's profile Send private message

PostPosted: Wed Feb 07, 2024 4:32 am     Reply with quote

The problem was the "1" in i2c_read(1);
With i2c_read(0); everything works.

Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Feb 07, 2024 10:43 am     Reply with quote

That was why it was the big one!... Very Happy

The others though are worth bearing in mind. A 3.3v device needs 30%
smaller resistors than a 5v device, and just setting 'I2C1', is automatically
forcing the hardware port to be used. The point about PROTECT, is it forces
a full chip erase to be done, even if you are only changing one byte in the
code. result extra erase cycles. Doesn't matter much on modern chips,
but historically this mattered a lot. Also makes the programming a little
slower.
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