element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Noobie Question re MCP23S17 Peter Oakes C# driver
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 11 replies
  • Answers 1 answer
  • Subscribers 706 subscribers
  • Views 1906 views
  • Users 0 members are here
  • bit_banging
  • raspberry-pi2
  • mcp23x17
  • raspberry_pi
  • peteroakes
Related

Noobie Question re MCP23S17 Peter Oakes C# driver

icarson
icarson over 10 years ago

Hi Guys

 

Just finished going through Peter Oakes excellent MCP23S17 driver and after some careful internet trawling I now understand 95% of what he is doing. There is just one element left to understand and I hope someone can explain it to me.

 

The WriteRegister8 method has the following line

 

writeBuffer3[0] = (BaseAddW | (Address << 1));

 

I know that this is the first of three bytes to be sent (Address, Register and Value). I also know that the BaseAddress is 0x40 but I don't understand the use of the " | (Address<<1)"

 

I guess I have 2 questions

 

1. Why is the OR necessary?, and;

2. What does this OR statement do in bit terms?

 

Thanks in advance for your help

Regards

Ian Carson

  • Sign in to reply
  • Cancel
  • mconners
    0 mconners over 10 years ago

    I'm not familiar with peter's code but in that instance

     

    it shifts Address 1 bit to the left

    when it ors it with BaseAddW it sets the corresponding bit if it is not set already

     

    so if

     

    BaseAddrW = 0x40

     

    let's say

    Address = 0x10

     

    After the or

     

    WriteBuffer3[0] would = 0x60

     

    because shifting Address by 1 to the left would be 0x20

     

    or'ing 0x40 and 0x20

     

    would equal

    0x60

     

    BaseAddrW = 0100 0000 biinary

    Address = 0001 0000 binary

     

    Address << 1 = 0010 0000 binary

     

    0100 0000 |

    0010 0000 =

    0110 000 = 0x60

     

     

    Hope this helps,

     

    Mike

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago

    I guess I should be able to answer that image

     

    the address << 1 will shift the address one space to the left leaving bit 0 for use as the r/w bit as per the specifications fo rthe chip (Page 8 of the PDF here: - http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf .

    The address register (3 bits)  gives the chip the ability to have 8 devices  on one spi chip select. the R/W bit is for Read or Write operations, this is the same if the chip was the I2C variety

     

    logical OR works very simply

    each BIT is logicaly or's with the other parameter so for example if i have the following (The example is using arbitary values, not from the code sample)

     

    0 1 0 0 1 0 1 0 <-- base address

    1 1 0 0 1 0 0 0 <-- address left shifted one bit

    1 1 0 0 1 0 1 0 <-- answer

     

    so in summary, 1|0 = 1, 0|1 = 1, 1|1 = 1, 0|0 = 0

     

    a very simple mechanism of taking a number and merging into it a bit mask for some set of a sub register to result int he complete value to write to the register. could this be simplifed yes but in this case it is part of a generic routine and if you simplified it for a single application and configuration it would no longer be generic

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to mconners

    And Mike has added a very good practical example. where my example was more about how the shift and or works in general

     

    Thanks Mike

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to Robert Peter Oakes

    Thanks Peter, I figured you'd jump in.

     

    Mike

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • icarson
    0 icarson over 10 years ago in reply to Robert Peter Oakes

    Thanks to both of you for putting me straight. I think the newbie mistake was reading 0x40 as decimal not a hexadecimal.

     

    Just to confirm. Structure of address byte is - BaseAddr followed by three bit device address followed by R/W bit (which actually comes from BaseAddr in that BaseAddR has a trailing 1 (for Read) which is passed through the mask after the left shift opens the mask for that bit.

    i.e

     

    BaseAddW                  0100 0000

    BaseAddW                  0100 0001

     

    DeviceAddr               0000 0111 (for device 7, is use this since device 0 is trivial and not very informative visually)

    Shifted DeviceAddr   0000 1110

     

    OR'd result  Write             0100 1110

    OR'd result Read              0100 1111

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • icarson
    0 icarson over 10 years ago in reply to icarson

    I wanted to mark you both as correct as it was a combination of the answers which gave me the clues I needed. I had to give it to Peter in the end as it was his original code. Sorry Mike but your help was equally appreciated.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to icarson

    No problem. You could always mark mine as helpful image

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to icarson

    Anytime you see 0x in front of a number, it will indicate hex.

     

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • icarson
    0 icarson over 10 years ago in reply to mconners

    How's that!

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mconners
    0 mconners over 10 years ago in reply to icarson

    Super!!!

     

    Mike

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube