It is currently 19 Jul 2019 03:07

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 26 Sep 2013 16:08 
Offline

Joined: 17 Apr 2010 15:48
Posts: 46
Location: CELJE
Greeting

An example of using the MCP23017

http://embedded-lab.com/blog/?p=7293
Image

I came up with the idea to write the procedure of using

Image

• 16-bit remote bidirectional I/O port
- I/O pins default to input
• High-speed I2C™ interface (MCP23017)
- 100 kHz
- 400 kHz
- 1.7MHz
• High-speed SPI interface (MCP23S17)
- 10 MHz (max.)
• Three hardware address pins to allow up to eight devices on the bus
• Configurable interrupt output pins
- Configurable as active-high, active-low or open-drain
• INTA and INTB can be configured to operate independently or together
• Configurable interrupt source
- Interrupt-on-change from configured register defaults or pin changes
• Polarity Inversion register to configure the polarity of the input port data

Image

LCD_I2C_EXPANDER
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
'' *
'' * Program name:
''     LCD_I2C_EXPANDER (Simple demonstration of the LCD_I2C Module) or io pin
'' * Copyright:
''     S59KB Thank microelectronics, 2013
'' * Revision History:
''     20130919:
''     - initial release.
'' * Description:
''     EXAMPLE INCLUDING USE IN I2C MCP23017
''     This is a simple demonstration of LCD_I2C_MCP23017 library Module.
''
'' * Test configuration:
''     MCU:             P18F4520 or any other
''     I2C:             MCP23017
''     Dev.Board:       EasyPIC5
''     Oscillator:      HS 20.0000 MHz
''     Ext. Modules:    me LCD 4x20 or any other
''     SW:              mikroBasic Pro v 3.2
'' * MCP23017 16 bit general purpose I/O expander
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
program LCD_I2C_EXPANDER
INCLUDE LCD_I2C_MCP23017
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
'       DeviceAddress   (A2,A1,A0) -> Address
  const DeviceAddress_1 = 0x40 '(LO,LO,LO) -> ID_1
  const DeviceAddress_2 = 0x42 '(LO,LO,HI) -> ID_2
  const DeviceAddress_3 = 0x44 '(LO,HI,LO) -> ID_3
  const DeviceAddress_4 = 0x46 '(LO,HI,HI) -> ID_4
  const DeviceAddress_5 = 0x48 '(HI,LO,LO) -> ID_5
  const DeviceAddress_6 = 0x4A '(HI,LO,HI) -> ID_6
  const DeviceAddress_7 = 0x4C '(HI,HI,LO) -> ID_7
  const DeviceAddress_8 = 0x4E '(HI,HI,HI) -> ID_8
'-------------------------------------------------------------------------------
''                       ______   ______
''                      [     [___]     ]
''               GPB0  o[ 1          28 ]o  GPA7---IO
''  LCD_LED_IO---GPB1  o[ 2          27 ]o  GPA6---IO
''      LCD_RS---GPB2  o[ 3          26 ]o  GPA5---IO
''       LCD_E---GPB3  o[ 4     M    25 ]o  GPA4---IO
''      LCD_D4---GPB4  o[ 5     C    24 ]o  GPA3---IO
''      LCD_D5---GPB5  o[ 6     P    23 ]o  GPA2---IO
''      LCD_D6---GPB6  o[ 7     2    22 ]o  GPA1---IO
''      LCD_D7---GPB7  o[ 8     3    21 ]o  GPA0---IO
''          +5v---VDD  o[ 9     0    20 ]o  INTA
''           0v---VSS  o[ 10    1    19 ]o  INTB
''                 NC  o[ 11    7    18 ]o  RESET---to +5v OFF
'' Soft_I2C_Scl---SCL  o[ 12         17 ]o  A2---to 0v ]
'' Soft_I2C_Sda---SDA  o[ 13         16 ]o  A1---to 0v ] ID address is 0x40
''                 NC  o[ 14         15 ]o  A0---to 0v ]
''                      [_______________]
''
'''-----------------------------------------------------------------------------
  dim br as byte
  DIM TXT_BYTE AS STRING[3]
  DIM BUTTONS_0 AS BYTE
  DIM BUTTONS_1 AS BYTE
  DIM BUTTONS_2 AS BYTE
  DIM BUTTONS_3 AS BYTE
  DIM BUTTONS_4 AS BYTE
  DIM BUTTONS_5 AS BYTE
  DIM BUTTONS_6 AS BYTE
  DIM BUTTONS_7 AS BYTE
  DIM LED_0 AS BYTE
  DIM LED_1 AS BYTE
  DIM LED_2 AS BYTE
  DIM LED_3 AS BYTE
  DIM LED_4 AS BYTE
  DIM LED_5 AS BYTE
  DIM LED_6 AS BYTE
  DIM LED_7 AS BYTE
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
' Declarations section
main:
     INTCON = $A0             ' enable GIE, TMR0IE
     CMCON = CMCON or 0x3F    ' turn off comparators
     ADCON1 = ADCON1 or 0x0F  ' turn off analog inputs
     TRISA = 0x00             ' Configure PORTA as output
     PORTA = 0x00
     TRISB = 0x00             ' Configure PORTB as output
     PORTB = 0x00
     TRISC = $04              ' Configure PORTC as output / input
     PORTC = 0x00
     TRISD = 0x00             ' Configure PORTB as output
     PORTD = 0x00
'-----------------------------------------------------------
   ''  I2C1_Init(100000)  ' Initialize I2C1 100 Khz DOES WORK
   ''  I2C1_Init(400000)  ' Initialize I2C1 400 Khz DOES WORK
     I2C1_Init(1000000)   ' Initialize I2C1   1 Mhz DOES WORK
   ''  I2C1_Init(1500000) ' Initialize I2C1 1,5 Mhz DOES WORK
   ''  I2C1_Init(1700000) ' Initialize I2C1 1,7 Mhz DOES NOT WORK WITH ME
     Delay_ms(100)
'-----------------------------------------------------------
     INIT_MCP23017_GPA(DeviceAddress_1, 0xFF)      ' set all of port GPA to input or output
     INIT_MCP23017_GPB(DeviceAddress_1, 0x00)      ' set all of port GPB to input or output
     LCD_I2C_INIT(DeviceAddress_1)                 ' Initialize  LCD_I2C
     LCD_I2C_CMD(DeviceAddress_1, _LCD_CURSOR_OFF)
     LCD_I2C_CMD(DeviceAddress_1, _Lcd_Clear)
     Delay_ms(100)
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
WHILE TRUE
'' EXAMPLES
    LCD_LED(DeviceAddress_1, 1)                                   ' LED LCD IS ON
    LCD_I2C_OUT(DeviceAddress_1, 1, 1, "LCD...  I2C-EXPANDER")    ' PRINT LCD
    LCD_I2C_OUT(DeviceAddress_1, 2, 1, "  mikroElektronika  ")    ' PRINT LCD
    LCD_I2C_OUT(DeviceAddress_1, 3, 1, "  LCD_I2C_MCP23017  ")    ' PRINT LCD
    LCD_I2C_OUT(DeviceAddress_1, 4, 1, " TEST INTERRUPT")         ' PRINT LCD

    FOR BR = 0 TO 255
        ByteToStr(BR,TXT_BYTE)
        LCD_I2C_OUT(DeviceAddress_1, 4, 17, TXT_BYTE)             ' PRINT LCD
    NEXT BR

    LCD_LED(DeviceAddress_1, 0)                                   ' LED LCD IS OFF
    Delay_ms(500)
    LCD_I2C_CMD(DeviceAddress_1, _Lcd_Clear)                      ' Lcd Clear
'-------------------------------------------------------------------------------
    '-------------------------------------------------
    ''''''''''''''''VARIOUS EXAMPLES''''''''''''''''''
    '-------------------------------------------------
    ' INIT_MCP23017_GPA(DeviceAddress_X, 0x00)      ' Initialize GPA is OUTPUT
    ' WRITE BYTE GPA
    ' for br = 0 to 255
    '    OUT_GPA_BYTE(DeviceAddress_X, br)
    '    Delay_ms(50)
    ' next br
    '-------------------------------------------------
    ' WRITE BIT OR PIN GPA
    ' for br = 0 to 7
    '    OUT_GPA_BIT(DeviceAddress_X, br, 1)
    '    Delay_ms(50)
    '    OUT_GPA_BIT(DeviceAddress_X, br, 0)
    ' next br
    '-------------------------------------------------
    ' INPUT BYTE STATE FROM GPA PORT
    ' INIT_MCP23017_GPA(DeviceAddress_X, 0xFF)      ' set all of port GPB to input or output
    ' PORTB = IN_GPA_BYTE(DeviceAddress_1)
    '-------------------------------------------------
    ' INPUT BUTTONS STATE FROM PIN GPA PORT
    ' INIT_MCP23017_GPA(DeviceAddress_X, 0xFF)      ' Initialize GPA is INPUT
    ' BUTTONS_0 = IN_GPA_BIT(DeviceAddress_X, 0)
    ' BUTTONS_1 = IN_GPA_BIT(DeviceAddress_X, 1)
    ' BUTTONS_2 = IN_GPA_BIT(DeviceAddress_X, 2)
    ' BUTTONS_3 = IN_GPA_BIT(DeviceAddress_X, 3)
    ' BUTTONS_4 = IN_GPA_BIT(DeviceAddress_X, 4)
    ' BUTTONS_5 = IN_GPA_BIT(DeviceAddress_X, 5)
    ' BUTTONS_6 = IN_GPA_BIT(DeviceAddress_X, 6)
    ' BUTTONS_7 = IN_GPA_BIT(DeviceAddress_X, 7)
    ' IF BUTTONS_1 = 1 THEN
    '   '' EXAMPLES CODE
    ' END IF
    '-------------------------------------------------
    ' SEND HI STATE TO PIN GPA
    ' INIT_MCP23017_GPA(DeviceAddress_X, 0x00)      ' Initialize GPA is OUTPUT
    ' LED_1 = 1
    ' LED_2 = 0
    ' LED_3 = 1
    ' OUT_GPA_BIT(DeviceAddress_X, 1, LED_1) ''  = 1
    ' OUT_GPA_BIT(DeviceAddress_X, 2, LED_2) ''  = 0
    ' OUT_GPA_BIT(DeviceAddress_X, 3, LED_3) ''  = 1
WEND
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
end.
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////


Image

Image

MODULE LCD_I2C_MCP23017
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
MODULE LCD_I2C_MCP23017
'' *
'' * Modul name:
''     LCD_I2C_MCP23017 (Simple demonstration of the LCD_I2C Module)
'' * Copyright:
''     S59KB Thank microelectronics, 2013
'' * Revision History:
''     20130919:
''     - initial release.
'' * Description:
''     EXAMPLE INCLUDING USE IN I2C MCP23017
''     This is a simple demonstration of LCD_I2C_MCP23017 library Module.
''
'' * Test configuration:
''     MCU:             P18F4520 or any other
''     I2C:             MCP23017
''     Dev.Board:       EasyPIC5
''     Oscillator:      HS 20.0000 MHz
''     Ext. Modules:    me LCD 4x20 or any other
''     SW:              mikroBasic Pro v 3.2
'' * MCP23017 16 bit general purpose I/O expander
''------------------------------------------------------------------------------
'' INCLUDE LCD_I2C_MCP23017
'' Library Routines  LCD_I2C_MCP23017
'' Lcd_Init________________________________LCD_I2C_INIT(DeviceAddress...)
'' Lcd_Cmd_________________________________LCD_I2C_CMD(DeviceAddress...)
'' Lcd_Chr_________________________________LCD_I2C_CHR(DeviceAddress...)
'' Lcd_Chr_Cp______________________________LCD_I2C_CHR_CP(DeviceAddress...)
'' Lcd_Out_________________________________LCD_I2C_OUT(DeviceAddress...)
'' Lcd_Out_Cp______________________________LCD_I2C_OUT_CP(DeviceAddress...)
''
''
'' LCD_I2C_INIT(DeviceAddress)                  Initializes LCD NR.1 at port with default pin settings (see the connection scheme at the end of the chapter)
'' LCD_I2C_CMD(DeviceAddress,_Lcd_Clear)        Sends command to LCD. You can pass one of the predefined constants to the function. The complete list of available commands is below.
''                                              _LCD_FIRST_ROW Move cursor to the 1st row
''                                              _LCD_SECOND_ROW Move cursor to the 2nd row
''                                              _LCD_THIRD_ROW Move cursor to the 3rd row
''                                              _LCD_FOURTH_ROW Move cursor to the 4th row
''                                              _LCD_CLEAR Clear display
''                                              _LCD_RETURN_HOME Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected.
''                                              _LCD_CURSOR_OFF Turn off cursor
''                                              _LCD_UNDERLINE_ON Underline cursor on
''                                              _LCD_BLINK_CURSOR_ON Blink cursor on
''                                              _LCD_MOVE_CURSOR_LEFT Move cursor left without changing display data RAM
''                                              _LCD_MOVE_CURSOR_RIGHT Move cursor right without changing display data RAM
''                                              _LCD_TURN_ON Turn Lcd display on
''                                              _LCD_TURN_OFF Turn Lcd display off
''                                              _LCD_SHIFT_LEFT Shift display left without changing display data RAM
''                                              _LCD_SHIFT_RIGHT Shift display right without changing display data RAM
'' LCD_I2C_CHR(DeviceAddress,1,5,51)            Prints character on LCD at specified row and column (parameters row and col). Both variables and literals can be passed as character.
'' LCD_I2C_CHR_CP(DeviceAddress,51)             Prints character on LCD at current cursor position. Both variables and literals can be passed as character.
'' LCD_I2C_OUT(DeviceAddress,1,1,"mikroBasic")  Prints text on LCD at specified row and column (parameters row and col). Both string variables and literals can be passed as text.
'' LCD_I2C_OUT_CP(DeviceAddress,"mikroBasic")   Prints text on LCD at current cursor position. Both string variables and literals can be passed as text.
''
''                       ______   ______
''                      [     [___]     ]
''               GPB0  o[ 1          28 ]o  GPA7---IO
''  LCD_LED_IO---GPB1  o[ 2          27 ]o  GPA6---IO
''      LCD_RS---GPB2  o[ 3          26 ]o  GPA5---IO
''       LCD_E---GPB3  o[ 4     M    25 ]o  GPA4---IO
''      LCD_D4---GPB4  o[ 5     C    24 ]o  GPA3---IO
''      LCD_D5---GPB5  o[ 6     P    23 ]o  GPA2---IO
''      LCD_D6---GPB6  o[ 7     2    22 ]o  GPA1---IO
''      LCD_D7---GPB7  o[ 8     3    21 ]o  GPA0---IO
''          +5v---VDD  o[ 9     0    20 ]o  INTA
''           0v---VSS  o[ 10    1    19 ]o  INTB
''                 NC  o[ 11    7    18 ]o  RESET---to +5v OFF
'' Soft_I2C_Scl---SCL  o[ 12         17 ]o  A2---to 0v ]
'' Soft_I2C_Sda---SDA  o[ 13         16 ]o  A1---to 0v ] ID address is 0x20
''                 NC  o[ 14         15 ]o  A0---to 0v ]
''                      [_______________]
''
'' A0, A1 and A2 are where you set the chips address so the microcontroller can send it the data, usually these are hardwired.
'' If you connect these 3 pins to ground then the address will be '0x20',
'' if you connect them all to VDD ( 5v ) then the address will be '0x27'.
'' If you are used to binary and Hex you can probably see how this works,
'' but to keep it nice and simple here is a table to show you what connects create what addresses :
''        DeviceAddress        '(A2,A1,A0) -> Address
''  const DeviceAddress_1 = 0x40 '(LO,LO,LO) -> 0x40
''  const DeviceAddress_2 = 0x42 '(LO,LO,HI) -> 0x42
''  const DeviceAddress_3 = 0x44 '(LO,HI,LO) -> 0x44
''  const DeviceAddress_4 = 0x46 '(LO,HI,HI) -> 0x46
''  const DeviceAddress_5 = 0x48 '(HI,LO,LO) -> 0x48
''  const DeviceAddress_6 = 0x4A '(HI,LO,HI) -> 0x4A
''  const DeviceAddress_7 = 0x4C '(HI,HI,LO) -> 0x4C
''  const DeviceAddress_8 = 0x4E '(HI,HI,HI) -> 0x4E
''   ( Reset )
''  Normally you will leave this pin tied high ( Note it is a NOT reset so +5V, or GND is reset ).
''------------------------------------------------------------------------------
''Example for the generation of new characters
''  const character1 as byte[8] = (21,14,21,4,21,14,21,0)                                'Character 1 ...    #  #  #
''  SUB PROCEDURE CustomChar1 dim i1 as byte                                                                  # # #
''      DIM i1 AS BYTE                                                                                       #  #  #
''      LCD_I2C_INIT(DeviceAddress)                                                                             #
''      Delay_ms(250)                                                                                        #  #  #
''      LCD_I2C_CMD(DeviceAddress,64)                                                                         # # #
''      for i1=0 to 7                                                                                        #  #  #
''          LCD_I2C_CHR_CP(DeviceAddress,character1[i1])
''      next i1
''      for i1=0 to 7
''          LCD_I2C_CHR_CP(DeviceAddress,character1[i1])
''      next i1
''  END SUB
''-------------------------
''  LCD_I2C_Cmd(DeviceAddress,64)
''  for i1 = 0 to 7
''      LCD_I2C_Chr_Cp(DeviceAddress,character1[i1])
''  next i1        'Character 1 ...
''------------------------------------------------------------------------------
''  LCD_I2C_INIT(DeviceAddress, Port_x, d3, d2, d1, d0, Port_x, rs, ctrl_rw, enable) Not necessary to make the Library.
''------------------------------------------------------------------------------
'' Initializes LCD_1 at PORT_GPB with default pin settings WITH ME
    ''''''PORT_GPB''''''''''''''
    const LED_LCD_PIN = 1  ' PIN GPB.1
    const LCD_RS = 2       ' PIN GPB.2
    const LCD_E  = 3       ' PIN GPB.3
    const LCD_D4 = 4       ' PIN GPB.4
    const LCD_D5 = 5       ' PIN GPB.5
    const LCD_D6 = 6       ' PIN GPB.6
    const LCD_D7 = 7       ' PIN GPB.7
''------------------------------------------------------------------------------
'' Register Definitions (Bank = 0)
    const IODIRA = 0x00 ' register GPIOA
    const IODIRB = 0x01 ' register GPIOB
    const IOPOLA = 0x02
    const IOPOLB = 0x03
    const GPINTENA = 0x04
    const GPINTENB = 0x05
    const DEFVALA = 0x06
    const DEFVALB = 0x07
    const INTCONA = 0x08
    const INTCONB = 0x09
    const IOCONA = 0x0A
    const IOCONB = 0x0B
    const GPPUA = 0x0C
    const GPPUB = 0x0D
    const INTFA = 0x0E
    const INTFB = 0x0F
    const INTCAPA = 0x10
    const INTCAPB = 0x11
    const GPIOA = 0x12 ' address port GPIOA
    const GPIOB = 0x13 ' address port GPIOB
    const OLATA = 0x14
    const OLATB = 0x15
    const MCP_Write = 0x00 ' Write MCP23017
    const MCP_Read  = 0x01 ' Read MCP23017
''------------------------------------------------------------------------------
SUB PROCEDURE INIT_MCP23017_GPA(DIM DeviceAddress, STATE_A AS BYTE)
SUB PROCEDURE INIT_MCP23017_GPB(DIM DeviceAddress, STATE_B AS BYTE)
SUB PROCEDURE OUT_GPA_BYTE(DIM DeviceAddress, DATA_A AS BYTE)
SUB PROCEDURE OUT_GPB_BYTE(DIM DeviceAddress, DATA_B AS BYTE)
SUB FUNCTION IN_GPA_BYTE(DIM DeviceAddress AS BYTE)AS BYTE
SUB FUNCTION IN_GPB_BYTE(DIM DeviceAddress AS BYTE)AS BYTE
SUB PROCEDURE OUT_GPA_BIT(DIM DeviceAddress, BIT_A, STATE_BIT AS BYTE)
SUB PROCEDURE OUT_GPB_BIT(DIM DeviceAddress, BIT_B, STATE_BIT AS BYTE)
SUB FUNCTION IN_GPA_BIT(DIM DeviceAddress, BIT_A AS BYTE)AS BYTE
SUB FUNCTION IN_GPB_BIT(DIM DeviceAddress, BIT_B AS BYTE)AS BYTE
''------------------------------
SUB PROCEDURE LCD_poslji_nib(DIM DeviceAddress, RS, NIB AS BYTE)
SUB PROCEDURE LCD_poslji(DIM ID_DATA, RS, NIB AS BYTE)
SUB PROCEDURE LCD_I2C_INIT(DIM DeviceAddress AS BYTE)
SUB PROCEDURE LCD_RED_POZ_OUT(DIM DeviceAddress, KO, VR AS BYTE)
SUB PROCEDURE LCD_I2C_CMD(DIM DeviceAddress, CMD_X AS BYTE)
SUB PROCEDURE LCD_I2C_CHR(DIM DeviceAddress, KO, VR AS BYTE,DIM ZNAK AS BYTE)
SUB PROCEDURE LCD_I2C_OUT(DIM DeviceAddress, KO, VR AS BYTE, DIM BYREF TEKST as STRING[20])
SUB PROCEDURE LCD_I2C_OUT_CP(DIM DeviceAddress AS BYTE, DIM BYREF ZNAK AS STRING[20])
SUB PROCEDURE LCD_LED(DIM DeviceAddress, STATE AS BYTE)
''------------------------------------------------------------------------------
implements
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE INIT_MCP23017_GPA(DIM DeviceAddress, STATE_A AS BYTE)
'Then to set port A to IO
      I2C1_Start()             ' issue I2C start signal
      I2C1_Wr(DeviceAddress + MCP_Write)' ID MCP23017
      I2C1_Wr(IODIRA)       ' IODIRA register
      I2C1_Wr(STATE_A)         ' set all of port A to input or output
      I2C1_Stop()              ' issue I2C stop signal
      delay_ms(1)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE INIT_MCP23017_GPB(DIM DeviceAddress, STATE_B AS BYTE)
'Then to set port and B  to IO
      I2C1_Start()             ' issue I2C start signal
      I2C1_Wr(DeviceAddress + MCP_Write)' ID MCP23017
      I2C1_Wr(IODIRB)       ' IODIRB register
      I2C1_Wr(STATE_B)         ' set all of port B to input or output
      I2C1_Stop()              ' issue I2C stop signal
      delay_ms(1)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE OUT_GPA_BYTE(DIM DeviceAddress, DATA_A AS BYTE)
'To control port A, we use:
      I2C1_Start()             ' issue I2C start signal
      I2C1_Wr(DeviceAddress + MCP_Write)' ID MCP23017
      I2C1_Wr(GPIOA)        ' address GPIOA
      I2C1_Wr(DATA_A)       ' value to send
      I2C1_Stop()              ' issue I2C stop signal
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE OUT_GPB_BYTE(DIM DeviceAddress, DATA_B AS BYTE)
'To control port A, we use:
      I2C1_Start()             ' issue I2C start signal
      I2C1_Wr(DeviceAddress + MCP_Write)' ID MCP23017
      I2C1_Wr(GPIOB)        ' address GPIOB
      I2C1_Wr(DATA_B)       ' value to send
      I2C1_Stop()              ' issue I2C stop signal
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB FUNCTION IN_GPA_BYTE(DIM DeviceAddress AS BYTE)AS BYTE
'To input port A, we use:
      I2C1_Start()             ' issue I2C start signal
      I2C1_Wr(DeviceAddress + MCP_Write)' ID MCP23017
      I2C1_Wr(GPIOA)                    ' set MCP23017 memory pointer to GPIOA address
      I2C1_Repeated_Start()    ' issue I2C signal repeated start
      I2C1_Wr(DeviceAddress + MCP_Read) ' value to send
      RESULT = I2C1_Rd(0)                ' request one byte of data from MCP20317
      I2C1_Stop()              ' issue I2C stop signal
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB FUNCTION IN_GPB_BYTE(DIM DeviceAddress AS BYTE)AS BYTE
'To input port B, we use:
      I2C1_Start()             ' issue I2C start signal
      I2C1_Wr(DeviceAddress + MCP_Write)' ID MCP23017
      I2C1_Wr(GPIOB)                    ' set MCP23017 memory pointer to GPIOB address
      I2C1_Repeated_Start()    ' issue I2C signal repeated start
      I2C1_Wr(DeviceAddress + MCP_Read) ' value to send
      RESULT = I2C1_Rd(0)                ' request one byte of data from MCP20317
      I2C1_Stop()              ' issue I2C stop signal
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE OUT_GPA_BIT(DIM DeviceAddress, BIT_A, STATE_BIT AS BYTE)
    DIM DATA_GPA_BYTE AS BYTE
    DATA_GPA_BYTE = IN_GPA_BYTE(DeviceAddress)
    DATA_GPA_BYTE.BIT_A = STATE_BIT
    OUT_GPA_BYTE(DeviceAddress,DATA_GPA_BYTE)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE OUT_GPB_BIT(DIM DeviceAddress, BIT_B, STATE_BIT AS BYTE)
    DIM DATA_GPB_BYTE AS BYTE
    DATA_GPB_BYTE = IN_GPB_BYTE(DeviceAddress)
    DATA_GPB_BYTE.BIT_B = STATE_BIT
    OUT_GPB_BYTE(DeviceAddress,DATA_GPB_BYTE)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB FUNCTION IN_GPA_BIT(DIM DeviceAddress, BIT_A AS BYTE)AS BYTE
    DIM DATA_GPA_BYTE AS BYTE
    DATA_GPA_BYTE = IN_GPA_BYTE(DeviceAddress)
    RESULT = DATA_GPA_BYTE.BIT_A
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB FUNCTION IN_GPB_BIT(DIM DeviceAddress, BIT_B AS BYTE)AS BYTE
    DIM DATA_GPB_BYTE AS BYTE
    DATA_GPB_BYTE = IN_GPB_BYTE(DeviceAddress)
    RESULT = DATA_GPB_BYTE.BIT_B
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_poslji_nib(DIM DeviceAddress, RS, NIB AS BYTE)
      OUT_GPB_BIT(DeviceAddress, LCD_RS, RS)
      OUT_GPB_BIT(DeviceAddress, LCD_D4, NIB.0)
      OUT_GPB_BIT(DeviceAddress, LCD_D5, NIB.1)
      OUT_GPB_BIT(DeviceAddress, LCD_D6, NIB.2)
      OUT_GPB_BIT(DeviceAddress, LCD_D7, NIB.3)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 1)
      delay_us(200)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 0)
      delay_us(200)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 1)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_poslji(DIM DeviceAddress, RS, NIB AS BYTE)
      OUT_GPB_BIT(DeviceAddress, LCD_RS, RS)
      OUT_GPB_BIT(DeviceAddress, LCD_D4, NIB.4)
      OUT_GPB_BIT(DeviceAddress, LCD_D5, NIB.5)
      OUT_GPB_BIT(DeviceAddress, LCD_D6, NIB.6)
      OUT_GPB_BIT(DeviceAddress, LCD_D7, NIB.7)
      delay_us(80)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 0)
      delay_us(20)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 1)
      OUT_GPB_BIT(DeviceAddress, LCD_RS, RS)
      OUT_GPB_BIT(DeviceAddress, LCD_D4, NIB.0)
      OUT_GPB_BIT(DeviceAddress, LCD_D5, NIB.1)
      OUT_GPB_BIT(DeviceAddress, LCD_D6, NIB.2)
      OUT_GPB_BIT(DeviceAddress, LCD_D7, NIB.3)
      delay_us(80)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 0)
      delay_us(80)
      OUT_GPB_BIT(DeviceAddress, LCD_E, 1)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_I2C_INIT(DIM DeviceAddress AS BYTE)     ' Initializes LCD I2C at port with default pin settings.
      OUT_GPB_BYTE(DeviceAddress, 0x00)                    ' Initialize GP_portB is low
      OUT_GPB_BIT(DeviceAddress, LCD_E, 0)       delay_ms(200)
      LCD_poslji_nib(DeviceAddress, 0, 0x03) delay_ms(200) ' 3 puta init
      LCD_poslji_nib(DeviceAddress, 0, 0x03) delay_ms(80)
      LCD_poslji_nib(DeviceAddress, 0, 0x03) delay_ms(80)
      LCD_poslji_nib(DeviceAddress, 0, 0x02) delay_ms(12)  ' namesti 4 bita prenos podataka
      LCD_poslji(DeviceAddress, 0, 0x01)     delay_ms(12)  ' obrisi displaj
      LCD_poslji(DeviceAddress, 0, 0x28)     delay_ms(4)   ' nastavi  znakove sa 5x7 font
      LCD_poslji(DeviceAddress, 0, 0x0C)     delay_ms(4)   ' ukljuci displj, izkljuci kursor i blinkanje
      LCD_poslji(DeviceAddress, 0, 0X06)     delay_ms(4)   ' namesti smer pomeranja kursora i shift displaj
      delay_ms(100)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE LCD_RED_POZ_OUT(DIM DeviceAddress, KO,VR AS BYTE)
      if (KO = 1) THEN LCD_poslji(DeviceAddress, 0, 128 + VR) ELSE END IF
      if (KO = 2) THEN LCD_poslji(DeviceAddress, 0, 192 + VR) ELSE END IF
      if (KO = 3) THEN LCD_poslji(DeviceAddress, 0, 148 + VR) ELSE END IF
      if (KO = 4) THEN LCD_poslji(DeviceAddress, 0, 212 + VR) ELSE END IF
      delay_us(100)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_I2C_CMD(DIM DeviceAddress, CMD_X AS BYTE)
    LCD_poslji(DeviceAddress, 0, CMD_X)
    delay_ms(100)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_I2C_CHR(DIM DeviceAddress, KO,VR AS BYTE,DIM ZNAK AS BYTE)
    LCD_RED_POZ_OUT(DeviceAddress, KO, VR-1)
    LCD_poslji (DeviceAddress, 1, ZNAK)
    delay_us(100)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_I2C_CHR_CP(DIM DeviceAddress, ZNAK AS BYTE)
    LCD_poslji (DeviceAddress, 1, ZNAK)
    delay_us(100)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE  LCD_I2C_OUT(DIM DeviceAddress, KO,VR AS BYTE, DIM BYREF TEKST as STRING[20])
    DIM LOOP_INDEX AS BYTE
    DIM INDEX AS BYTE
       INDEX = Strlen(TEKST)- 1
       FOR LOOP_INDEX = 0 TO INDEX
           LCD_I2C_CHR(DeviceAddress, KO, VR + LOOP_INDEX , TEKST[LOOP_INDEX])
       NEXT LOOP_INDEX
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE LCD_I2C_OUT_CP(DIM DeviceAddress AS BYTE, DIM BYREF ZNAK AS STRING[20])
    DIM LOOP_INDEX AS BYTE
    DIM INDEX AS BYTE
       INDEX = Strlen(ZNAK) - 1
       FOR LOOP_INDEX = 0 TO INDEX
           LCD_I2C_CHR_CP(DeviceAddress, ZNAK[LOOP_INDEX])
       NEXT LOOP_INDEX
    delay_us(100)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
SUB PROCEDURE LCD_LED(DIM DeviceAddress, STATE AS BYTE)
    OUT_GPA_BIT(DeviceAddress, LED_LCD_PIN, STATE)
END SUB
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////
END. ' and ENJOY :)
'///////////////////////////////////////////////////////////////////////////////
'*******************************************************************************
'///////////////////////////////////////////////////////////////////////////////


Image

Image

Download:
http://www.libstock.com/projects/view/7 ... r-mcp23017

73 de S59KB


Top
 Profile  
 
PostPosted: 27 Sep 2013 15:52 
Offline
mikroElektronika team
User avatar

Joined: 25 Jan 2008 09:56
Posts: 10254
Hi,

Thank you very much for this contribution. :)

Regards,
Filip.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


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 post attachments in this forum

Search for:
Jump to: