Then it must be 1).
Put this line at the top of your program.
ADCON1 = $F ; set all pins to digital
<br>
Then it must be 1).
Put this line at the top of your program.
ADCON1 = $F ; set all pins to digital
<br>
DT
Copy-Pasted that ADCON1... and that could have helped but unfortunately it did not help, nothing changed![]()
The problem is somewhere else, but where?
Does that 65KHz sound correct? On what frequency should the clock run under those two I2C routines (I2CREAD I2CWRITE), where could one find any info on that. Tried also DEFINE I2C_SLOW, but again without success... What can I do? The expander is kind of alive because it does not throw you to the error label....![]()
No 65khz does not sound right, and ...
It doesn't throw you to the error label because it always reads 0's on the pin.
An ack from the device is a low pulse. As long as it sees a low when it expects an ack, it will never jump to the label.
OK, it's time for you to pony up some of the info you've been leaving out.
What are your CONFIGs?
What cyrstal are you using?
What DEFINE OSC are you using?
What pins are the SCL and SDA on?
And the rest of the code.
<br>
DT
So, back to the basics.
If not 65kHz is correct, then what should it be?
If I change the device address from $40 to something else for ex. $45 it will throw the program to the error label. That behavior sounds "healthy", doesn't it?
By CONFIGs you mean? See post #1
The crystal is 12MHz
DEFINE OSC 48
The SDA,SCL pins are connected correctly to pins 33,34 + 2x 2k2 pull-ups
Using also Microchip USB HID bootloader v2.2
I2Creg = OLAT1 : I2Cbyte = $ff : gosub writeioextender: pause 20
I2Creg = IODIR1 : I2Cbyte = $ff : gosub writeioextender: pause 20
The current test loop is here:
Loop:
gosub getbattery
if onoffpressed = 1 then powerout = 0
I2Creg = GP1 : gosub readioextender' to IOEbyte
pause 20
lcdout cmd,home3, dec battery/10, ".",dec battery//10,"V ", dec w/100," ", _
dec onoffpressed,cmd,home4, hex2 IOEbyte," "
goto Loop
No look at this thread.
http://www.picbasic.co.uk/forum/showthread.php?t=543
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Oops, sorry... the question was about all this here below
Here are the configurations settings:
DEFINE RESET_ORG 1000h ' Room for Microchip USB Bootloader
define ADC_BITS 10 ' 10 bit A/D
define ADC_CLOCK 3 ' 3=rc
define ADC_SAMPLEUS 50 ' sampling time in us
define I2C_HOLD 1 ' Can't read or write MCP23016 without i2c_hold = 1
'**************** Define LCD registers and bits *********************
DEFINE LCD_DREG PORTD ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4 if 4-bit bus)
DEFINE LCD_RSREG PORTC ' Set LCD Register Select port
DEFINE LCD_RSBIT 2 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTC ' Set LCD Enable port
DEFINE LCD_EBIT 1 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 4 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us, 2000 us
DEFINE LCD_DATAUS 50 ' Set data delay time in us, 50 us
sda var PORTB.0 ' i2c data pin 33
scl var PORTB.1 ' i2c clock pin 34
deviceIO con $40 ' MCP23016
GP0 con $00 ' port GP0 all in
GP1 con $01 ' port GP1 all in
OLAT0 con $02 ' Output latch register 0
OLAT1 con $03 ' 1
IPOL0 con $04 ' input polarity normal, not inverted
IPOL1 con $05 '
IODIR0 con $06 ' all out = 0
IODIR1 con $07 '
INTCAP1 con $09
INTCAPbyte var byte
IOCON1 con $0b '
cmd con $fe ' LCD command
clr con 1 ' LCD clear display
blink con $0f ' LCD blinkinking cursor on
home1 con $80 ' LCD move cursor to beginning of first line
home2 con $c0 ' LCD move cursor to beginning of second line
home3 con $94 ' debug lines
home4 con $d4 ' debug lines
A quick test here on a 4550 at 48mhz, shows ~250khz clk.
For the configs, I didn't know you were using the USB bootloader. The configs will be whatever the bootloader was programmed with. You'd probably need to read back the chip to find out what configs are set.
I was hoping for more of the program, there's a lot of stuff missing, and you never know, 1 statement could change everything.
Since you are getting all 0's. Maybe you should just try to read the RB0 pin without I2C, as a test. There's a pull-up on it, if you only read a 0, then something's still not right.
<br>
DT
I'm note anymore sure about anything...
When I use the following code to show things:
LCDOUT cmd,home3, dec battery/10, ".",dec battery//10,"V ",dec w/100," ", dec onoffpressed, _
cmd,home4, hex2 portb.0, " ", hex2 IOEbyte," ",cmd,home1
I can see on row 4 = 01 00, RB0=1 !! IOEbyte should show the button press on P1.0 but NO it does not come through
Took PIC of the PCB and read it with meProg and attached it here.
By the way, the ~250kHz that you got, are you absolutely sure that you communicated with the MPC23016 device? I'm just wondering because I have an impression from the datasheet of MCP23016 that it couldn't run that fast...Is my impression wrong?
The configs look OK.
The PORTB.0 read looks OK.
In my test, I did not have an MCP23016 connected, I was just testing the clk frequency with I2CWRITE at 48mhz. The MCP23016 is a 400khz device, so 250khz should not be a problem.
The i2cdevice and I2Creg variables weren't shown. Are they both BYTEs?
I assume they are, just asking.
The only other possibility I can see is that the registers are configured as "Pairs", and all examples in the datasheet show reading or writing 2-bytes of data at a time. I don't see anything that says you can't read only 1-byte at a time, but then there's nothing that says you can either.
So this is my last best guess...
Try doing everything with 2 data bytes.
If the address is for GP0 ($0), then i2cbyte1 should have GP0 and i2cbyte2 should have GP1.Code:I2CWrite SDA,SCL,i2cdevice,I2Creg,[i2cbyte1, i2cbyte2],error
If the address is for GP1 ($1), then they will be reversed. GP1 in i2cbyte1 and GP0 in i2cbyte2.
Both write and reads would need 2-bytes.
And be sure to cycle power after changing it, in case it's still locked up.
hth
DT
Bookmarks