Digital Fever

Enter Your Electronics & Design Project for a chance to win a $200 shopping cart!

Submit an EntrySubmit an Entry  Back to homepage
Project14 Home
Monthly Themes
Monthly Theme Poll

 

Intro

I always had a lot of fun designing digital circuits that used Boolean logic, but these days it is rare to see designs with lots of discrete gates. With this little project I'm going to have some fun designing a quadrature decoder and display using logic gates and flip-flops. It will actually use 9 inverters, 19 AND gates, 13 OR gates, 4 T  flip-flops, and 2 SR flip-flops for a total of 46 logic devices. If it was implemented with TTL or 4000 series chips it could take a dozen chips, but in this project I will implement all of these gates and their interconnections in a single PSoC 4 chip listed at $2.52 by the manufacturer. The 44+ pin PSoC 4 modulePSoC 4 module below allows one of these chips to plug into a breadboard.

Most of the connections to the logic module in this image are just for demonstration purposes. The decoder only really needs 2 input wires and the 7 output resistors driving the 7 segment display.

I actually tried several different implementations of the quadrature decoder function because it was easy to make changes without any physical rewiring.

The complete project implements:

  • a quadrature encoding wheel and circuit
  • a quadrature decoding circuit
  • a 4 bit binary counter
  • a BCD to 7 segment decoder

I ordered digital chips to do the counting and 7 segment decoding, but they have not arrived, so all of the logic is implemented in the PSoC device.

This blog will cover the following topics:

  • an explanation of how the circuit is designed and programmed into the PSoC chip using PSoC Creator
  • a description of a simple quadrature decoder circuit
  • a Visual Basic simulation of a simple quadrature decoder
  • a description of quadrature signals and a more accurate quadrature decoder circuit
  • a brief description of a 4 bit binary counter
  • a brief description of a BCD to 7 segment decoder

 

Demo

This first video shows the system in operation so it is clear what is being attempted and the components involved.

The slotted opto-couplers did not arrive so I used a pair of phototransistors instead.

1st Schematic Operation

The second video describes the first (simple) quadrature decoder I devised by using a visual Basic program to simulate the circuit. VB6 is a great tool for quickly implementing this simulation since it is can be used as a visual object oriented, event driven language. Each gate and its inputs and output are all visual and the code is a simple one line indicating what happens to the output based on what the inputs are. The gate code is automatically executed whenever an input changes so the program does not need a state machine. If an output changes because an input changed, and this output happens to be the input to another gate, it will automatically trigger the gate code for that next stage. No real program planning is needed:

2nd Schematic Operation

I was only going to do the circuit above, but couldn't resist trying to design a quadrature decoder that could handle transition noise better.

I did implement the circuit above and it works okay, but it is possible to get counting pulses by rocking the wheel without rotating it.

The third video explains this improved design and delves into the signals involved in quadrature decoding with the more accurate decoder circuit:

PSoC Creator

This circuit counts pulses accurately in both directions and does not count pulses unless the wheel is actually rotating.

The last video shows how easy it is to design with logic in PSoC Creator:

 

PSoC Creator makes designing with logic easy and fun. It is very visual, very fast, drag-and-drop, performs a lot of sanity checks when compiling, and any changes needed can be implemented without doing any physical rewiring. I wouldn't normally use it strictly for implementing hardware gates since the software side is so easy and powerful as well, but it is a pretty cost effective solution even if only used for hardware logic, especially because it can handle a large range of supply voltages and has a nice strong output current capability.

 

Schematics:

 

Software (VB6 Emulator):

Private Sub A_Click()
    If A.Text = "0" Then A.Text = "1" Else A.Text = "0"
    DoEvents
End Sub
Private Sub B_Click()
    If B.Text = "0" Then B.Text = "1" Else B.Text = "0"
    DoEvents
End Sub


Private Sub A_Change()
    If A.Text = "1" Then AI.Text = "0" Else AI.Text = "1"
    If A.Text = "1" And B.Text = "1" Then Clock.Text = "1" Else Clock.Text = "0"
    DoEvents
End Sub
Private Sub B_Change()
    If B.Text = "1" Then BI.Text = "0" Else BI.Text = "1"
    If A.Text = "1" And B.Text = "1" Then Clock.Text = "1" Else Clock.Text = "0"
    DoEvents
End Sub


Private Sub AI_Change()
    If AI.Text = "1" Then AII.Text = "0" Else AII.Text = "1"
    If BII.Text = "1" And AI.Text = "1" Then BIIN.Text = "0" Else BIIN.Text = "1"
    If AI.Text = "1" And AIINN = "1" And BI.Text = "1" Then Up.Text = "0" Else Up.Text = "1"
    If AI.Text = "1" And BIINN = "1" And BI.Text = "1" Then Down.Text = "0" Else Down.Text = "1"
    DoEvents
End Sub


Private Sub BI_Change()
    If BI.Text = "1" Then BII.Text = "0" Else BII.Text = "1"
    If AII.Text = "1" And BI.Text = "1" Then AIIN.Text = "0" Else AIIN.Text = "1"
    If BI.Text = "1" And AIINN = "1" And AI.Text = "1" Then Up.Text = "0" Else Up.Text = "1"
    If AI.Text = "1" And BIINN = "1" And BI.Text = "1" Then Down.Text = "0" Else Down.Text = "1"
    DoEvents
End Sub
Private Sub AII_Change()
    If AII.Text = "1" And BI.Text = "1" Then AIIN.Text = "0" Else AIIN.Text = "1"
    DoEvents
End Sub
Private Sub BII_Change()
    If BII.Text = "1" And AI.Text = "1" Then BIIN.Text = "0" Else BIIN.Text = "1"
    DoEvents
End Sub
Private Sub AIIN_Change()
    If AIIN.Text = "1" And BIINN.Text = "1" Then AIINN.Text = "0" Else AIINN.Text = "1"
    DoEvents
End Sub
Private Sub BIIN_Change()
    If BIIN.Text = "1" And AIINN.Text = "1" Then BIINN.Text = "0" Else BIINN.Text = "1"
    DoEvents
End Sub
Private Sub AIINN_Change()
    If BIIN.Text = "1" And AIINN.Text = "1" Then BIINN.Text = "0" Else BIINN.Text = "1"
    If BI.Text = "1" And AIINN = "1" And AI.Text = "1" Then Up.Text = "0" Else Up.Text = "1"
    DoEvents
End Sub
Private Sub BIINN_Change()
    If AIIN.Text = "1" And BIINN.Text = "1" Then AIINN.Text = "0" Else AIINN.Text = "1"
    If AI.Text = "1" And BIINN = "1" And BI.Text = "1" Then Down.Text = "0" Else Down.Text = "1"
    DoEvents
End Sub
Private Sub DownDir_Click()
    Direction.Text = "0"
    Timer1.Enabled = True
End Sub


Private Sub StopCmd_Click()
    Timer1.Enabled = False
End Sub


Private Sub Timer1_Timer()
    State = Val(State.Text)
    If Direction.Text = "0" Then
        If State = 0 Then
            A.Text = "0"
            B.Text = "0"
        End If
        If State = 1 Then
            A.Text = "1"
            B.Text = "0"
        End If
        If State = 2 Then
            A.Text = "1"
            B.Text = "1"
        End If
        If State = 3 Then
            A.Text = "0"
            B.Text = "1"
        End If
    Else
        If State = 0 Then
            A.Text = "0"
            B.Text = "0"
        End If
        If State = 1 Then
            A.Text = "0"
            B.Text = "1"
        End If
        If State = 2 Then
            A.Text = "1"
            B.Text = "1"
        End If
        If State = 3 Then
            A.Text = "1"
            B.Text = "0"
        End If
    End If
    State = State + 1
    If State > 3 Then State = 0
    State.Text = Str(State)
    DoEvents
    DoEvents
End Sub




Private Sub UpDir_Click()
    Direction.Text = "1"
    Timer1.Enabled = True
End Sub

 

Summary

This project was a lot of fun. I got to design and print an encoder wheel, whip up a VB program and play with logic design - all in the process of figuring out a bit of a brain teaser circuit.

Although the slotted opto-couplersslotted opto-couplers never arrived, a couple of 40 year old phototransistors from the parts bin worked well enough to demonstrate quadrature encoding. I wasn't originally planning to implement a binary counter or 7 segment decoder within the PSoC, but the logic chips did not arrive either. Fortunately the PSoC had plenty of resources to implement these functions in addition to the quadrature decoder. This extra custom logic made the design and troubleshooting work a bit more involved, but it simplified the hardware build, and correcting logic issues was actually very easy.

PSoC Creator has a very nice user interface which made implementing the circuit and making changes really intuitive. I didn't touch on its software programming capabilities because this project was specifically avoiding software, but that aspect of PSoC Creator is also very slick and it allows very productive code generation and debugging.

 

Relevant Links:

Digital Fever

Project14 | Winners Announcement: Digital Fever: From Simple Gates to FPGA and Beyond!