element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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 Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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
Personal Blogs
  • Community Hub
  • More
Personal Blogs
NexGen Flight Simuator NexGen: Hacking; Gray 2 BCD Encoder (using TTL)
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: phoenixcomm
  • Date Created: 2 Nov 2022 5:27 PM Date Created
  • Views 1275 views
  • Likes 5 likes
  • Comments 8 comments
  • flight simulator
  • nexgen
  • diy hardware
  • diy cockpit
  • mcp23017
  • 74LS7400
  • 74LS148
  • 74LS08
  • ttl logic
  • Gray Code
  • BCD Code
Related
Recommended

NexGen: Hacking; Gray 2 BCD Encoder (using TTL)

phoenixcomm
phoenixcomm
2 Nov 2022

imageOK, I Have had it up to you know where with the GRAY CODE!! When I have written this decoder in software, it takes a loop or two, It just eats CPU time. This is ok if you can use interrupts. But the Ardunio does not return the interrupt value to you directly. No, it uses what is called a SIDE-EFFECT! Incredibly, this means you have to look for the interrupt number. This is from the Arduino website itself: "ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn't return anything." This is not correct, in fact, the return type must be a VOID.  So I have crawled back to my hardware days and came up with this: (diagram below). Now I can take the decoded BCD and apply it to an MCP23017. Now I have two choices waste a nibble (4-bits) which will handle 2 decoders or 4 decoders but I will have to do some bit shifting.  - CAH Nov 2, 2K22

image

Part Price Count Price
SN74LS00N 0.12 3 0.36
SN74LS08N 0.98 1 0.98
SN74LS148N 2.05 2 4.10
TOTAL 5.44

Prices are from Newark Electronics
(web pricing)

2 of 5 Gray Code BCD
INPUTS OUTPUTS
A B C D E D C B A
0 X X 0 0 0 0
1 X X 0 0 0 1
2 X X 0 0 1 0
3 X X 0 0 1 1
4 X X 0 1 0 0
5 X X 0 1 0 1
6 X X 0 1 1 0
7 X X 0 1 1 1
8 1 0 0 0
9 X X 1 0 0 1
  • Sign in to reply
  • shabaz
    shabaz over 2 years ago in reply to shabaz

    Also, another typo, step (4) should be bcdmap[16], not bcdmap[10]

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to shabaz

    sigh, can't edit the text. The last sentence should read:

    The variable called value now contains the BCD result, and this is super-quick, very few resources used on the microcontroller.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to phoenixcomm

    Oh, wow - not seen that code before. I can't figure out what the pattern is in there. 

    However, I think I now understand the use-case; you will be taking the gray code into a microcontroller, using the MCP IO expander chip. 

    Based on that, the bottleneck on performance will be the bus speed for the I/O expander (I2C or SPI speed). The reading of the gray code (or any code from slow devices like rotary switches) is a very common scenario for the MCP with interrupts. This is the way it is usually done:

    (1) set up the MCP chip to generate an interrupt-on-change, i.e. whether any input goes high or low, you want the interrupt.

    (2) Have the A..E connections wired to the MCP chip, however looking at the code, you could just wire up A..D, or B..F, since 4 of the connections still generates a totally unique code, the 5 connections have some redundancy. It's up to you, but personally I'd just use 4 of them, since there are other points of failure since this isn't going into an aircraft environment. 

    (3) In the code, whenever an interrupt occurs, read the MCP chip into a variable called (say) graycode. There are two choices, to read during the interrupt, or to set a memory location (you're using it like a flag) to indicate to code outside the interrupt to read the MCP chip. If you're using SPI, then it could be OK to just do it in the interrupt, otherwise personally I'd do it outside of the interrupt using the memory location as a flag.

    (4) Make sure you're only using the 4 or 5 bits in your code, i.e. if you're using 4 bits:

        graycode = graycode & 0x0f;

    (4) The code will have a const array. For example, if you're using pins A..D, then the const array will contain this:

      const unsigned char bcdmap[10] = {8, 9, 99, 7, 2, 6, 5, 99, 1, 0, 4, 99, 3, 99, 99, 99};

    The value 99 is an unexpected value, so you can have some error message printed out in those unexpected cases.

    (5) Convert out your rotary switch (or whatever the gray code is coming from) using this:

    value = bcdmap[graycode];

    The variable called value now contains the gray code result, and this is super-quick, very few resources used on the microcontroller.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to shabaz

    Hi thanks for the check-up BUT what you printed out as GRAY is not Gray Code (2 of 5 and negative active. I will have to add pull-up resistors to the Gray code inputs.)) This format is widely used in the aircraft industry. and I think there is a A

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to dougw

    I don't waste the CPU for this.  most of the time its just MHZ and KHZ, in one radio I have 5 groups..:(

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • 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 © 2025 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.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube