PDA

View Full Version : more i2c and mcp23017



queenidog
- 23rd May 2018, 23:36
I have a bunch of LEDs with anodes tied to Vcc through resistors. The cathodes are tied to the outputs of an MCP23107 port expander. I'm using the same setup I had for Arduino that worked, so I know this setup works.

Trying to get it going in Pic Basic Pro is not working. I have simplified my program to very, very basics to eliminate any "distractions". Here it is:

#config
__CONFIG _CONFIG1, _HS_OSC & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _CPD_OFF & _BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG _CONFIG2, _BOR40V & _WRT_OFF
#endconfig
define LOADER_USED 1
DEFINE OSC 4 '4 MHz crystal on LabX1, not the processor speed


'TRISC = %00000000
SDA VAR PORTC.4 'i2c data line, 16F887
SCL VAR PORTC.3 'i2c clock line 16F887
addr var byte 'address of port expander

INIT:
addr = $42
I2CWRITE SDA,SCL,addr,[$00,$00] 'set for all output

MAIN:
I2CWRITE SDA,SCL,addr,[$09, $00]
pause 100
I2CWRITE SDA,SCL,addr,[$09, $ff]
pause 100
goto main

Why doesn't this blink the LEDs I have attached to the PE. When I check voltage, it never drops to zero. I've scoped my SDA and SCL lines and they are working fine. I have 5 volts (4.9 actually) on the chip, and the address for this one is $42 (address A1 is high).

I'm using a LabX1 with 16F887 mcu. I'm expecting all the lights to blink but none do. (I've played with the GPI0 register, making it $12 instead of $09)

What am I missing? Arduino spoiled me by providing me with libraries that did all the work!

tumbleweed
- 24th May 2018, 00:53
The chip powers up with the IOCON register = 0, so you need to use the "IOCON.BANK=0" column of table 3-1 for the register addresses.

That puts GPIOA at address $12, not $09.

richard
- 24th May 2018, 05:26
I'm using the same setup I had for Arduino that worked, so I know this setup works.

a largely meaningless statement, an Arduino lib could do all sorts of unmentionable things to anything it comes into contact with

it needs to be
I'm using this <library> found here <link to lib> with this setup init() function connected this way <schematic or description>

eg

#include <RFM12B.h>
// By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
#define NODEID 1 //network ID used for this uniy

#define NETWORKID 99 //the network ID we are on

//lib https://github.com/LowPowerLab/RFM12B

radio.Initialize(NODEID, RF12_433MHZ, NETWORKID);

queenidog
- 24th May 2018, 14:35
I'm using this library: Centipede, found here: http://macetech.com/store/index.php?main_page=product_info&products_id=23, with this setup: (this code for 5 different boards). PCB board layout attached.8677 I have 5 of these RGB boards using the code below without issue, but I can't get a simpler version (the PBP code) with monochrome LEDs working.

/*PointsRGBTEST This is working copy.
RGB test BF29April2018
Code to check new RGB Points PCB as precursor to the big Star.
Test of matrix RGB activation. Uses MCP23017 I2C port expanders
at addresses 20 to 24 and special RGB Star point model. Sequences
through 7 colors (see below) for 16 lights. Does all one color
for 16, then next color for same 16. Note RED overwhelms other colors
so blue or green mixed in show red, instead of magenta and yellow.
*/
#include <Wire.h>
#include <Centipede.h>
Centipede CS;
const int pins[3] = {//9,10,11 are DIO pins for RGB transistors. Active high turns transistor on.
9, 10, 11 //use 9, 10, 11 because these are PWM pins on UNO
};

int x = 50;//delay between LEDs on/off //
int i;
byte b;
byte p;
const byte rgb[7] = {0x02, 0x04, 0x08, 0x05, 0x06, 0x0C, 0x0E,}; //00000010= 0x02
//Red overpowers other lights when lit in combination, eg with green or blue.
/*const byte Red = ~0x02; //0B00000010 Red on PB1 pin 9 of Uno invert bits 1 here is on, but must be zero for relay
const byte Green = ~0x04; //0B00000100 Green on PB2 pin 10
const byte Blue = ~0x08; //0B00001000 Blue on PB3 pin 11
const byte Magenta = ~0x05; //0B00001010 red and blue
const byte Yellow = ~0x06; //0B00000110 red and green
const byte Cyan = ~0x0C; //0B00001100 blue and green
const byte All = ~0x0E; //0B00001110 all lights should be white
const byte Seg0 = ~0x01; //0B00000100 Segment 0 on PD0, pin 0 of UNO
const byte Seg1 = ~0x02; //0B00001000 Segm*/

void setup() {
Wire.begin(); // start I2C
CS.initialize();
for (byte p = 0; p < 5; p++)//initialize all 5 port expanders
{ CS.portMode(p, 0x00);} //
// initialize the I/O pins as outputs
for (int i = 0; i < 3; i++) {
pinMode(pins[i], OUTPUT);
}
}

void loop()
{
for (byte b = 0; b < 7; b++)
PORTB = rgb[b]; //cycle 7 colors on pins 9,10,11
for (int p = 0; p < 16; p++)//16 needed to display LED 15
{CS.portMode(1, 0x00);
CS.digitalWrite(p, HIGH);//toggle light ON
delay(x);
CS.digitalWrite(p, LOW); //turn light OFF
delay(x);
}
}

queenidog
- 24th May 2018, 14:38
Yeah, I saw that but $12 didn't work either. Output just won't go low. I measure 3.5 volts or so with my DMM on all GPA pins.

queenidog
- 24th May 2018, 14:42
8678

This is what I get on SDA and SCL with that short program.

queenidog
- 24th May 2018, 16:43
I put a label at the end of my i2C write statement to flash an LED if there is no acknowledge from the chip.

there is NO acknowledge...the LED is flashing. So that's a result I will work with. I have extra port expanders (all pretested) and 16F887s.

Update:
Changed out port expanders: no change
Changed out 16F887: no change
Changed SDA and SCL port from PortC.4 and PortC.3 to PortA.5 and PortA.4: works. Acknowledge led is OFF>

Any insight into why PortC didn't work but PortA does? ON the LabX1, C.4 and C.3 are designated for I2C.

richard
- 25th May 2018, 01:46
for what its worth

this works for me

sda VAR PORTA.1
scl VAR PORTA.2
trisa.5=0
' pause 2000
' Debug "Start",13 ,10
addr var byte ;mcp control address
reg var byte ;mcp register
da var byte ;gpioa data
da=$f8
addr = $40
reg=0
I2CWRITE SDA,SCL,addr,reg,[$00] 'set gpioa all output
reg=9
LOOOP:
I2CWRITE SDA,SCL,addr,reg,[da]
pause 500
da=da+1
GOTO LOOOP

queenidog
- 27th May 2018, 19:44
Despite the superiority of PBP and Mecanique IDE, working with the i2c commands is a bear! Struggled for a week on it, still no go. Went back to Arduino, and still cursing at all the syntax. However, a quick hookup and I have 80 LEDS spread across 5 boards (PCB above), running all kinds of patterns in COLOR without issue. It's basically the code I submitted (for Arduino) except the CS.digitalWrite is repeated 5 times, once for each board. I've got 20 pages of code done in Arduino, couldn't get one light to light up consistently with PBP.

Thanks anyway. Going to the Arduino forum now to find out how to create functions (subroutines) to make my code trim and proper.

richard
- 28th May 2018, 00:47
working with the i2c commands is a bear!

would you care to provide some code to demonstrate a i2cread/write command that exhibits some problem

I find it always works as documented .


I suspect that you have no idea of what the centipede library actually does behind the scenes and that
you are attempting pbp code based on that total lack of understanding. maybe if you posted some code
help would be offered all we have seen so far is just totally wrong.

good luck with your endeavours on the Arduino forum , they just love posts with no code

Scampy
- 28th May 2018, 13:49
good luck with your endeavours on the Arduino forum , they just love posts with no code

I also found that they get arsy if you give them too much. I didn't get much joy from the three arduino forums I posted on when porting my thermostat code to the platform. They also don't provide code for you if you're stuck, they expect you to work that part out when they've told you what to do, and if you are new to the platform and still find the syntax strange.

I found the guys on this forum a lot more friendlier and supportive. It's just a shame that PBP lacks the native support for most modern things - but I2C is one of the basics and I've never had issues when using it to talk to LED driver chips etc.

Demon
- 4th October 2024, 03:23
Deleted, reformulated with complete code in next post.

Demon
- 4th October 2024, 04:57
I cleaned my code up, so it's really apparent what I'm trying to do.

I still won't blink the 16 LEDs.


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONreg var byte : IOCONreg = $05
IOCONdat var byte : IOCONdat = %10110000
' bit 7 1 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

I2CWRITE PinSDA2,PinSCL2,addr,IOCONreg,[IOCONdat]

IODIRAreg var byte : IODIRAreg = $00
IODIRAdat var byte : IODIRAdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRAreg,[IODIRAdat] 'set GPIOA all output

IODIRBreg var byte : IODIRBreg = $10
IODIRBdat var byte : IODIRBdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRBreg,[IODIRBdat] 'set GPIOB all output

GPIOAreg var byte : GPIOAdat var byte
GPIOBreg var byte : GPIOBdat var byte

LOOOP:
GPIOAreg = $09 : GPIOAdat = %00000000
I2CWRITE PinSDA2,PinSCL2,addr,GPIOAreg,[GPIOAdat]

GPIOBreg = $19 : GPIOBdat = %00000000
I2CWRITE PinSDA2,PinSCL2,addr,GPIOBreg,[GPIOBdat]

pause 250

GPIOAreg = $09 : GPIOAdat = %11111111
I2CWRITE PinSDA2,PinSCL2,addr,GPIOAreg,[GPIOAdat]

GPIOBreg = $19 : GPIOBdat = %11111111
I2CWRITE PinSDA2,PinSCL2,addr,GPIOBreg,[GPIOBdat]

pause 250

GOTO LOOOP
end



The HPWM is for a IRFZ44N that i use to pulse the LED grounds. I have a simple LED on it to confirm that it's operating.

Demon
- 4th October 2024, 05:08
Tried with OLAT registers, no joy.


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONreg var byte : IOCONreg = $05
IOCONdat var byte : IOCONdat = %10110000
' bit 7 1 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

I2CWRITE PinSDA2,PinSCL2,addr,IOCONreg,[IOCONdat]

IODIRAreg var byte : IODIRAreg = $00
IODIRAdat var byte : IODIRAdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRAreg,[IODIRAdat] 'set GPIOA all output

IODIRBreg var byte : IODIRBreg = $10
IODIRBdat var byte : IODIRBdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRBreg,[IODIRBdat] 'set GPIOB all output

OLATAreg var byte : OLATAdat var byte
OLATBreg var byte : OLATBdat var byte

LOOOP:
OLATAreg = $0A : OLATAdat = %00000000
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATAdat]

OLATBreg = $1A : OLATBdat = %00000000
I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATBdat]

pause 250

OLATAreg = $0A : OLATAdat = %11111111
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATAdat]

OLATBreg = $1A : OLATBdat = %11111111
I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATBdat]

pause 250

GOTO LOOOP
end

Demon
- 4th October 2024, 05:10
It won't let me add info to my previous post above:

16F18877
MCP23017

Using 1K/130pF on SCL/SDA as mentioned on p.5 of datasheet (they use 135pF).

Demon
- 4th October 2024, 05:47
I didn't quite understand what they meant with the Bank A/B at first.

I've now tried with both versions, and still no blinky.


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONreg var byte : IOCONreg = $05
IOCONdat var byte : IOCONdat = %00110000 ' Bank=0
'IOCONdat var byte : IOCONdat = %10110000 ' Bank=1
' bit 7 1 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

I2CWRITE PinSDA2,PinSCL2,addr,IOCONreg,[IOCONdat] : pauseus 1

IODIRAreg var byte : IODIRAreg = $00 ' Bank=0
'IODIRAreg var byte : IODIRAreg = $00 ' Bank=1
IODIRAdat var byte : IODIRAdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRAreg,[IODIRAdat] : pauseus 1 'set GPIOA all output

IODIRBreg var byte : IODIRBreg = $01 ' Bank=0
'IODIRBreg var byte : IODIRBreg = $10 ' Bank=1
IODIRBdat var byte : IODIRBdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRBreg,[IODIRBdat] : pauseus 1 'set GPIOB all output

OLATAreg var byte : OLATAdat var byte
OLATBreg var byte : OLATBdat var byte

LOOOP:
OLATAreg = $14 : OLATAdat = %00000000 ' Bank=0
' OLATAreg = $0A : OLATAdat = %00000000 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATAdat] : pauseus 1

OLATBreg = $15 : OLATBdat = %00000000 ' Bank=0
' OLATBreg = $1A : OLATBdat = %00000000 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATBdat] : pauseus 1

pause 250

OLATAreg = $14 : OLATAdat = %11111111 ' Bank=0
' OLATAreg = $0A : OLATAdat = %11111111 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATAdat] : pauseus 1

OLATBreg = $15 : OLATBdat = %11111111 ' Bank=0
' OLATBreg = $1A : OLATBdat = %11111111 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATBdat] : pauseus 1

pause 250

GOTO LOOOP
end

richard
- 4th October 2024, 06:07
The example you refer to used and expected iocon.bank to = 1 , why i chose that and how it got that way are lost into the mystery of time. [probably some weird dev board]
this simulates ok for bank=0



#CONFIG __config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_ON & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_ON
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG


DEFINE OSC 32
ANSELB = 0
PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2
GPIOAdat var byte
GPIOBdat var byte
addr var byte
reg var byte
dat var byte


addr = %01000000 ' $40


; UNCOMMENT EITHER SECTION BANK=0 OR BANK=1 NOT BOTH






;START IOCON.BANK=0


reg = 0
dat = 0


I2CWRITE PinSDA2,PinSCL2,addr,reg,[dat] 'set GPIOA all output


reg = 1
dat = 0


I2CWRITE PinSDA2,PinSCL2,addr,reg,[dat] 'set GPIOB all output






LOOOP:
reg = $12 : GPIOAdat = %00000000
I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOAdat]


reg = $13 : GPIOBdat = %00000000
I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOBdat]


pause 250


reg = $12 : GPIOAdat = %11111111
I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOAdat]


reg = $13 : GPIOBdat = %11111111
I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOBdat]


pause 250


GOTO LOOOP
end ;IOCON.BANK=0








;START IOCON.BANK=1 ; NOT TESTED BANK=1 NOT SUPPORTED IN SIMULATOR
'reg = $A
'dat = %10100000
'I2CWRITE PinSDA2,PinSCL2,addr,reg,[dat]



'reg = 0
'dat = 0


'I2CWRITE PinSDA2,PinSCL2,addr,reg,[dat] 'set GPIOA all output


'reg = $10
'dat = 0


'I2CWRITE PinSDA2,PinSCL2,addr,reg,[dat] 'set GPIOB all output






'LOOOP:
' reg = $9 : GPIOAdat = %00000000
' I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOAdat]


' reg = $19 : GPIOBdat = %00000000
' I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOBdat]


' pause 250


' reg = $9 : GPIOAdat = %11111111
' I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOAdat]


' reg = $19 : GPIOBdat = %11111111
' I2CWRITE PinSDA2,PinSCL2,addr,reg,[GPIOBdat]


' pause 250


' GOTO LOOOP
'end ;IOCON.BANK=1

Demon
- 4th October 2024, 06:07
Argh, won't let me add a comment.

Apparently I still hadn't understood how I'm supposed to set the register for IOCON.

Latest attempt, still no workie.


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONAreg var byte : IOCONAreg = $0A ' Bank=0
IOCONBreg var byte : IOCONBreg = $0B ' Bank=0
IOCONdat var byte : IOCONdat = %00110000 ' Bank=0
'IOCONAreg var byte : IOCONAreg = $05 ' Bank=1
'IOCONBreg var byte : IOCONBreg = $15 ' Bank=1
'IOCONdat var byte : IOCONdat = %10110000 ' Bank=1
' bit 7 0 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

I2CWRITE PinSDA2,PinSCL2,addr,IOCONAreg,[IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,IOCONBreg,[IOCONdat]

IODIRAreg var byte : IODIRAreg = $00 ' Bank=0
'IODIRAreg var byte : IODIRAreg = $00 ' Bank=1
IODIRAdat var byte : IODIRAdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRAreg,[IODIRAdat] 'set GPIOA all output

IODIRBreg var byte : IODIRBreg = $01 ' Bank=0
'IODIRBreg var byte : IODIRBreg = $10 ' Bank=1
IODIRBdat var byte : IODIRBdat = %00000000

I2CWRITE PinSDA2,PinSCL2,addr,IODIRBreg,[IODIRBdat] 'set GPIOB all output

OLATAreg var byte : OLATAdat var byte
OLATBreg var byte : OLATBdat var byte

LOOOP:
OLATAreg = $14 : OLATAdat = %00000000 ' Bank=0
' OLATAreg = $0A : OLATAdat = %00000000 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATAdat]

OLATBreg = $15 : OLATBdat = %00000000 ' Bank=0
' OLATBreg = $1A : OLATBdat = %00000000 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATBdat]

pause 250

OLATAreg = $14 : OLATAdat = %11111111 ' Bank=0
' OLATAreg = $0A : OLATAdat = %11111111 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATAdat]

OLATBreg = $15 : OLATBdat = %11111111 ' Bank=0
' OLATBreg = $1A : OLATBdat = %11111111 ' Bank=1
I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATBdat]

pause 250

GOTO LOOOP
end

tumbleweed
- 4th October 2024, 14:25
For the MCP23017 28-pin DIP package make sure you have connections to:
RESETn (pin 18)
A2, A1, A0 (pins 17, 16, 15) - these should be pulled low (GND) for address $40... they should not be left floating

There should also be pullups to VDD on SCL (pin 12) and SDA (pin 13)

Demon
- 4th October 2024, 18:50
For the MCP23017 28-pin DIP package make sure you have connections to:
RESETn (pin 18)
A2, A1, A0 (pins 17, 16, 15) - these should be pulled low (GND) for address $40... they should not be left floating

There should also be pullups to VDD on SCL (pin 12) and SDA (pin 13)

Yup, reset, a0, a1 and a2 pulled down.

I had sda and scl pulled up as in the datasheet, even tried 4K7 like is commonly used, nothing worked

Demon
- 4th October 2024, 19:25
Streamlined code even more:


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONAreg var byte
IOCONBreg var byte
IODIRAreg var byte
IODIRBreg var byte
OLATAreg var byte
OLATBreg var byte

IOCONdat var byte
IODIRdat var byte
OLATdat var byte

IOCONdat = %00110000
' bit 7 0 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

if IOCONdat.7 = 0 then ' Bank=0
IOCONAreg = $0A
IOCONBreg = $0B
IODIRAreg = $00
IODIRBreg = $01
OLATAreg = $14
OLATBreg = $15
else ' Bank=1
IOCONAreg = $05
IOCONBreg = $15
IODIRAreg = $00
IODIRBreg = $10
OLATAreg = $0A
OLATBreg = $1A
endif

IODIRdat = %00000000 'set GPIO all output

I2CWRITE PinSDA2,PinSCL2,addr,IOCONAreg,[IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,IODIRAreg,[IODIRdat]

I2CWRITE PinSDA2,PinSCL2,addr,IOCONBreg,[IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,IODIRBreg,[IODIRdat]

LOOOP:
OLATdat = %00000000 ' LEDs ON
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATdat]

I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATdat]

pause 200

OLATdat = %11111111 ' LEDs OFF
I2CWRITE PinSDA2,PinSCL2,addr,OLATAreg,[OLATdat]

I2CWRITE PinSDA2,PinSCL2,addr,OLATBreg,[OLATdat]

pause 200

GOTO LOOOP
end


No longer need to mess with comments to change BANK setting. I just flip bit in IOCONdat and IF takes care of the rest.

Quadruple checked everything, still no blinky blink.

Demon
- 4th October 2024, 19:43
Changed to GPIO instead of LAT as per Dave's suggestion here:

https://www.picbasic.co.uk/forum/showthread.php/8732-PIC-18F4550-and-MCP23017?p=96693#post96693

Also played with the syntax (register now part of the list of data).


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONAreg var byte
IOCONBreg var byte
IODIRAreg var byte
IODIRBreg var byte
GPIOAreg var byte
GPIOBreg var byte

IOCONdat var byte
IODIRdat var byte
GPIOdat var byte

IOCONdat = %10110000
' bit 7 0 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

if IOCONdat.7 = 0 then ' Bank=0
IOCONAreg = $0A
IOCONBreg = $0B
IODIRAreg = $00
IODIRBreg = $01
GPIOAreg = $12
GPIOBreg = $13
else ' Bank=1
IOCONAreg = $05
IOCONBreg = $15
IODIRAreg = $00
IODIRBreg = $10
GPIOAreg = $09
GPIOBreg = $19
endif

IODIRdat = %00000000 'set GPIO all output

I2CWRITE PinSDA2,PinSCL2,addr,[IOCONAreg,IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,[IODIRAreg,IODIRdat]

I2CWRITE PinSDA2,PinSCL2,addr,[IOCONBreg,IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,[IODIRBreg,IODIRdat]

LOOOP:
GPIOdat = %00000000 ' LEDs ON
I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]

I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]

pause 200

GPIOdat = %11111111 ' LEDs OFF
I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]

I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]

pause 200

GOTO LOOOP
end



Tried with both Bank settings, still no good.

I'm quickly running out of crazy ideas. :D

Demon
- 4th October 2024, 20:05
Logic Probe readings of SCL/SDA lines.


#CONFIG
__config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_OFF & _FCMEN_ON
__config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF
__config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC
__config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_OFF
__config _CONFIG5, _CP_OFF & _CPD_OFF
#ENDCONFIG

DEFINE OSC 32

define CCP1_REG 0 ' Must clear unused CCP pins or else unpredictable results
DEFINE CCP1_BIT 0
define CCP2_REG 0
DEFINE CCP2_BIT 0
DEFINE CCP3_REG PORTB
DEFINE CCP3_BIT 5
define CCP4_REG 0
DEFINE CCP4_BIT 0
define CCP5_REG 0
DEFINE CCP5_BIT 0

ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00001000

PinSCL2 VAR PortB.1
PinSDA2 VAR PortB.2

HPWM 3, 8, 1953

addr var byte : addr = %01000000 ' $40

IOCONAreg var byte
IOCONBreg var byte
IODIRAreg var byte
IODIRBreg var byte
GPIOAreg var byte
GPIOBreg var byte

IOCONdat var byte
IODIRdat var byte
GPIOdat var byte

IOCONdat = %10110000
' bit 7 0 = The registers associated with each port are separated into different banks.
' bit 6 0 = The INT pins are not connected. INTA is associated with PORTA and INTB is associated with PORTB
' bit 5 1 = Sequential operation disabled, address pointer does not increment.
' bit 4 1 = Slew rate disabled
' bit 3 0 = Disables the MCP23S17 address pins.
' bit 2 0 = Active driver output (INTPOL bit sets the polarity.)
' bit 1 0 = Active-low
' bit 0 Unimplemented: Read as ‘0’

if IOCONdat.7 = 0 then ' Bank=0
IOCONAreg = $0A
IOCONBreg = $0B
IODIRAreg = $00
IODIRBreg = $01
GPIOAreg = $12
GPIOBreg = $13
else ' Bank=1
IOCONAreg = $05
IOCONBreg = $15
IODIRAreg = $00
IODIRBreg = $10
GPIOAreg = $09
GPIOBreg = $19
endif

IODIRdat = %00000000 'set GPIO all output
' Image 1
I2CWRITE PinSDA2,PinSCL2,addr,[IOCONAreg,IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,[IODIRAreg,IODIRdat]

pause 50
' Image 2
I2CWRITE PinSDA2,PinSCL2,addr,[IOCONBreg,IOCONdat]
I2CWRITE PinSDA2,PinSCL2,addr,[IODIRBreg,IODIRdat]

pause 100

LOOOP: ' Image 3
GPIOdat = %00000000 ' LEDs OFF
I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]
I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]

pause 200
' Image 4
GPIOdat = %11111111 ' LEDs ON
I2CWRITE PinSDA2,PinSCL2,addr,[GPIOAreg,GPIOdat]
I2CWRITE PinSDA2,PinSCL2,addr,[GPIOBreg,GPIOdat]

pause 400

GOTO LOOOP
end



Image 1:

9782


Image 2:

9783

(next 2 images in next post)

Weird how 2 statements generate 3 groups of pulses (I expected 1 to 1 ratio).

Demon
- 4th October 2024, 20:06
Image 3:

9784


Image 4:

9785

tumbleweed
- 4th October 2024, 22:52
Yup, reset, a0, a1 and a2 pulled down.
Typo? RESETn is a low-active input so it wants a pullup, not pulldown...

The traces show you're getting a NAK to the first byte (chip address), so that needs to get fixed before anything else.

Demon
- 5th October 2024, 00:02
...RESETn is a low-active input so it wants a pullup...


...insert facepalm emote here...

Who knows how long this thing has been working. :wink:

Demon
- 5th October 2024, 00:40
Weeeeee!

https://youtu.be/gvKmfPcCTvg?si=-a1qpOElWniaTyAb