View previous topic :: View next topic |
Author |
Message |
hawkfire
Joined: 15 Oct 2006 Posts: 14 Location: N.Wales U.K.
|
Posted: Sat Nov 13, 2021 12:04 pm Post subject: |
|
|
Here is my modified library...
Code: | copyright =
www =
email =
comment = I2C LCD PCF8574 Adapter Library.
libversion = 001
date = 2021/03/23
statement = A library for the PCF8574 I2C LCD adapter that is on sale.
statement = I quoted the original 'Kent Andersson' and 'Ben Zijlstra' libraries.
history = Edit 2021/03/23 O-Family
;Define a variable named [Pcf8574_lcd] that specifies the I2C slave address of PCF8574. (&H40,&H42,&H44,&H46,&H48,&H4A,&H4C,&H4E)
;Define a variable named [Backlight] that controls the LCD backlight. (0 = OFF, 1 = ON)
;The pinout of the PCF8574 I2C LCD adapter is as follows.
;P0 - RS
;P1 - R/W
;P2 - E
;P3 - Backlight ([L] = OFF, [H] = ON)
;P4 - D4
;P5 - D5
;P6 - D6
;P7 - D7
[_Init_LCD]
_Init_LCD:
call _i2c_start ;bus start condition
ldi r17, $40 ; send hardware address
call _i2c_write ; send it
ldi r17, $00
call _i2c_write ; select ddr reg. 0
ldi r17, $00
call _i2c_write ; send 0 to ddr, all outputs
call _i2c_stop ; generate stop condition
ldi r24, $00
rcall _send_to_mcp23008
LDI R24,$00 ; Set all port outputs of PCF8574 to 0.
RCALL _Send_to_mcp23008
* BASIC: Waitms 50 ' Wait for more than 40ms after VCC rises to 2.7V.
LDI R24,$30 ; Function set (DL=1: Interface is 8 bits long.)
RCALL _Send_to_LCD
* BASIC: Waitms 5 ' Wait for more than 4.1 ms
LDI R24,$30 ; Function set (DL=1: Interface is 8 bits long.)
RCALL _Send_to_LCD
* BASIC: Waitus 100 ' Wait for more than 100 uS.
LDI R24,$30 ; Function set (DL=1: Interface is 8 bits long.)
RCALL _Send_to_LCD
;
LDI R24,$20 ; Function set (DL=0: Set interface to be 4 bits long.) instuction is 8 bits in length.
RCALL _Send_to_LCD
LDI R24,$28 ; Function set (DL=0: Interface is 4 bits long, N=1: 2 lines, F=0: 5x8 dots)
RCALL _Lcd_Control
LDI R24,$08 ; Sets entire display (D=0: display off, C=0: cursor off, B=0: cursor blinking off)
RCALL _Lcd_Control
LDI R24,$01 ; Clears entire display and sets DDRAM address 0 in address counter.
RCALL _Lcd_Control
LDI R24,$06 ; Entry mode set (I/D=1: Increment, S=0: No display shift)
RCALL _Lcd_Control
;
LDI R24,$0C ; Sets entire display (D=1: display on, C=0: cursor off, B=0: cursor blinking off)
RJMP _Lcd_Control
[END]
[_Write_lcd]
_Write_lcd: ; R24 = Write data.
LDI R25,$01 ; [RS] = 1 (CG,DD RAM data)
RJMP _write_lcd_byte
;
_Lcd_Control: ; R24 = Write command.
LDI R25,$00 ; [RS] = 0 (Control command)
_write_lcd_byte:
* LDS R17,{Backlight}
CPI R17,$00
BREQ _write_lcd_byte1 ; Backlight off?
SBR R25,$08
;
_write_lcd_byte1:
PUSH R25
PUSH R24
RCALL _Write_lcd_Nibble ; Send Higher 4bit data + Control bit.
POP R24
POP R25
SWAP R24 ; Send lower 4bit data + Control bit.
_Write_lcd_Nibble:
CBR R24,&H0F ; Clear the lower 4bits.
OR R24,R25 ; R25 = (b3:Backlight, b2:E, b1:R/W, b0:RS)
;Send data to LCD. (R24 = Send data)
_Send_to_LCD:
RCALL _Send_to_mcp23008 ; Send data to mcp23008. (E = [L])
SBR R24,$04
RCALL _Send_to_mcp23008 ; Send data to mcp23008. (E = [H])
CBR R24,$04
RJMP _Send_to_mcp23008 ; Send data to mcp23008. (E = [L])
;Send data to PCF8574. (R24 = Send data)
_Send_to_mcp23008:
PUSH R24
PUSH R24
CALL _i2c_start ; I2C bus start condition.
* LDS R17,{addressw} ; load slave address
CALL _i2c_write ; Write slave address.
* LDS R17,{REG} ; load register 10
CALL _i2c_write ; write to register
POP R17 ; Get R24 (send data)
CALL _i2c_write ; Write data byte.
CALL _i2c_stop ; I2C bus stop condition.
@genus(10) ; Stable time for PCF8574 port output. (tv(Q))
POP R24
RET
[END]
|
|
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Sat Nov 13, 2021 2:33 pm Post subject: |
|
|
The PCF8574 cannot drive a [H] level source current, so a pull-up resistor is required at the base of the transistor.
The [L] sink can be pulled in, so the circuit is set to [L] to turn off the lights.
I think the cause of the LCD not being able to display is a library problem. Are you saying that the output of the MCP23008 is sending out commands to the LCD correctly?
Did you check that with a logic analyzer? |
|
Back to top |
|
|
hawkfire
Joined: 15 Oct 2006 Posts: 14 Location: N.Wales U.K.
|
Posted: Sat Nov 13, 2021 3:15 pm Post subject: |
|
|
O-Family wrote: |
Did you check that with a logic analyzer? |
No, but I can turn the backlight on or off, from Bascom IDE, and it works as seen on my scope.Also, I can see activity on 'E' during runtime.I did check and send data to the mcp23008 before connecting the display, it works.As far as I can tell, the only difference between the PCF1574 and the MCP23008 is the latter has 10 registers but only 2 are important, these are the data direction register(ddr) which needs setting to outputs onetime and the port latch, register (olat) which is the write to port register to output data on the port pins.I have only modified your code library at the beginning to set the mcp23008 ddr and I have included a change to last routine 'send_to_mcp23008' by adding two extra instructions.Everything else is same as your code.
The extra instructions needed for the mcp32008 come about because the programmer has to tell or select the mcp23008 register it is writing to. |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Sun Nov 14, 2021 8:47 am Post subject: |
|
|
Hi
set the GPPU registry.You have poll-up disabled as O-Family says.
RS |
|
Back to top |
|
|
hawkfire
Joined: 15 Oct 2006 Posts: 14 Location: N.Wales U.K.
|
Posted: Sun Nov 14, 2021 6:09 pm Post subject: |
|
|
SZTRAD wrote: | Hi
set the GPPU registry.You have poll-up disabled as O-Family says.
RS |
Hi
I do not understand what you mean, the mcp23008 outputs are push pull only, they do not need pull up resistors and I have set all to outputs by writing to the ddr, there are no open drain outputs with the mcp23008 they are only push pull at their outputs. |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Sun Nov 14, 2021 7:03 pm Post subject: |
|
|
Hi
page 8,13,19 of the manual
RS |
|
Back to top |
|
|
hawkfire
Joined: 15 Oct 2006 Posts: 14 Location: N.Wales U.K.
|
Posted: Sun Nov 14, 2021 8:13 pm Post subject: |
|
|
SZTRAD wrote: | Hi
page 8,13,19 of the manual
RS |
Hi,
Ah!, I finally see what you are getting at, if you read the data sheet you might notice that GPPU set register are for 100k pull up resistors which can be activated if you have the Mcp23008 ports set to inputs.This register is only applicable if the port(s) are set to inputs.
Check the IODIR register, in my application here, the IODIR are all set to zero for push pull outputs. |
|
Back to top |
|
|
hawkfire
Joined: 15 Oct 2006 Posts: 14 Location: N.Wales U.K.
|
Posted: Sun Nov 14, 2021 8:43 pm Post subject: |
|
|
Hi all,
You maybe wondering why I am trying to get the mcp23008 to work, the reason is one of cost, the pcf8574 is twice the cost of the mcp23008.
Also, the mcp23008 has better drive capability and greater flexibility it is also newer than the pcf8574. |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Mon Nov 15, 2021 2:11 am Post subject: |
|
|
I don't have MCP23008, so I tested it with 16bit MCP23017.
With your modified library, it works fine.
I suspect that your wiring is wrong or the LCD is faulty.
PCF8574 is a complete adapter board that can be attached directly to the LCD and is sold at a very low price, so I think the MCP23008 has little advantage. |
|
Back to top |
|
|
hawkfire
Joined: 15 Oct 2006 Posts: 14 Location: N.Wales U.K.
|
Posted: Mon Nov 15, 2021 10:41 pm Post subject: |
|
|
Hi O-family,
Thanks for your extended work, at least we know that my modified library code works, however for some reason I can't get it to work.The lcd is working fine and I have rechecked the wiring connections but everything is right?
I have 3 pcf8574 adapter boards, from ebay arriving soon, so I will have another go at it in the near future.In the meantime thank you for your time.
Cheers for now
Darren |
|
Back to top |
|
|
|
|
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 You cannot attach files in this forum You cannot download files in this forum
|
|