STM32 Mbed Quadrature encoder input

How to use a quadrature encoder with STM32/Mbed online compiler? This code is how I’ve solved it.

At first, surfing the net searching for a solution I’ve found this forum post: POST

Then I’ve found a working example program HERE

The following code is that one modified by me  for my needs:

  • Obtain a number: positive for clockwise turning, negative for conterclockwise.
  • The number must be proportional of the turning speed
  • I’m using the EC11 encoder that outputs 4 pulses each click

Please nothe that in this code I’ve activated the CPU internal pullups on input lines/pins PC_10 and PC_12. You can change the pins according with your needs and cpu or board used.

#include "mbed.h"

// Pin declarations
DigitalIn phA1(PC_10); // phase a of the quadrature encoder 1
DigitalIn phB1(PC_12); // phase b of the quadrature encoder 1

// Global variables declaration
int encoderClickCountA    = 0; // hold the signed value corresponding to the number of clicks left or right since last sample
int previousEncoderStateA = 0; // keep a record of the last actioned sample state of the Qb+Qa outputs for comparison on each interrupt

// Rotation accumulator for the quadrature encoder
void quadratureDecoder1( void )
{
    int currentEncoderStateA = (phB1.read() << 1) + phA1.read(); // create a two bit value out of phaseB and phaseA
    
    if( currentEncoderStateA == previousEncoderStateA )
    {
        return;
    }
    
    switch( previousEncoderStateA )
    {
        case 0:
            if( currentEncoderStateA == 1 )
            {
                encoderClickCountA--;
            }
            else if( currentEncoderStateA == 2 )
            {
                encoderClickCountA++;
            }
            break;
            
        case 1:
            if( currentEncoderStateA == 3 )
            {        
                encoderClickCountA--;
            }
            else if( currentEncoderStateA == 0 )
            {
                encoderClickCountA++;
            }
            break;
            
        case 2:
            if( currentEncoderStateA == 0 )
            {
                encoderClickCountA--;
            }
            else if( currentEncoderStateA == 3 )
            {
                encoderClickCountA++;
            }
            break;
            
        case 3:
            if( currentEncoderStateA == 2 )
            {
                encoderClickCountA--;
            }
            else if( currentEncoderStateA == 1 )
            {
                encoderClickCountA++;
            }
            break;

        default:
            break;
    }
    previousEncoderStateA = currentEncoderStateA;
}

 // read the rotation accumulator, and once read, reset it to zero
int getClicksA( void )
{
   int res = encoderClickCountA; // this allows the knob to be rotated "while im not looking at it' and still return the    
    encoderClickCountA = 0;      // actual number of clicks that have been rotated since last time I was checking it.
    return res;
}
int main()
{
    Serial pc( USBTX, USBRX );
    pc.baud(115200);
    Ticker sampleTicker;                                // create a timer to sample the encoder
    int    ClicksA;                                     // Click encoder A
    
    phA1.mode(PullUp);
    phB1.mode(PullUp);
    
    sampleTicker.attach_us( &quadratureDecoder1, 1000 ); // make the quadrature decoder function check the knob once every 1000us = 1ms
    
    pc.printf("test started\r\n");
    
    //Loop principale infinito
    while( 1 )
    {
        ClicksA = getClicksA()/4;
        
        if(ClicksA != 0 ) // If there is some movement on encoders I display it
        {
      pc.printf("clicks %d\r\n", ClicksA);
        }
        
    }

VEB Robotron 70013

This is the picture of the product from LCI electronics eBay listing

I’ve found this tube from an eBay seller called “LCI electronics”. He’s from Bulgaria. This tube was made in ex DDR, German Democratic Republic, in late 80′ from the well known manufacturer of hi-tech electronic products “VEB Robotron”. This pictures are of the item that I’ve received.

As you can see, the product that I’ve received is the same of that listed. Excellent package indeed! The seller sent me a brief datasheet/comparison chart and and application example.

PRC320 power supply repair

My military HF manpack radio PRC320, 3 months after I’ve bought it, started to make a whistle and was not able to tune on any frequency. After few minutes it went KO. No audio at all. Damn! How to fix it? Let’s take a look inside!

Opening the radio is an easy task:

  • Disconect battery, antena connector and headset cables
  • Unscrew all the allen screws
  • Take apart gently, front and back pannels. They slide out the aluminium enclosure.

This is what I seen inside it

Inside PRC320

The Unit n° 5 is the first suspect. This module is the power supply. As a general rule in electronics servicing, check the power supply first! Let’s open it to verify if it’s doing it’s job.

Now, with extreme care I’ve reconnected front and back pannel and powered the radio via its external power supply cable. After measuring the voltages on 6 and 112V points my suspects where confirmed. No way to have some power supply, there’s no output voltage. I’ve removed the module from the radio to troubleshoot it.

Repair that module is not an easy task because it’s semiconductors are all obsolete and hard to find parts. Still at the moment I don’t have a suitable replacement for TR1: the main 24V to 12V stabilizer, that fried. I’ve found also the BC107 on the step up section KO. Giving a chance to a BD139 in place of TR1 resulted in a semi funcional radio: I could listen to radio stations but i couldn’t transmit! Ok, let’s search for a complete new module! This is not a trivial task: this modules are very hard to find, I’ve found one new, serviced from a guy that does this job, radio servicing, during his military career, on ebay for 80€! It’s not cheap.

Now with this new module in place the radio comes to life again in RX and TX! Nice… but I have still to deal with the old broken module. I’ll update this post when I’ll fix it. At the moment this file could help you, Module 5 schematics