This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LM4F120 Reading states from encoders.

Hello,

I need to reed states from 3 encoders using LM4F120. I built a circuit and wrote some code to handle this. When I was in school I checked it via oscilloscope. When phototransistor was off I had something around 3.4V and when phototransistor was on I had something around 300 mV. Please take a look on circuit and my code, and please tell me, is it likely to succeed ?
Thanks in advance for reply.

// Macros
#define ENCODERS_PORT GPIO_PORTD_BASE

#define XENCODER GPIO_PIN_0
#define ZRENCODER GPIO_PIN_1
#define XRENCODER GPIO_PIN_2

void initGPIOs()
{
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
	GPIOPinTypeGPIOInput(ENCODERS_PORT, XENCODER | ZRENCODER | XRENCODER );
	GPIOPortIntRegister(ENCODERS_PORT, PortDIntHandler);
	GPIOIntTypeSet(ENCODERS_PORT,XENCODER | ZRENCODER | XRENCODER, GPIO_FALLING_EDGE );
	GPIOPinIntEnable(ENCODERS_PORT,XENCODER | ZRENCODER | XRENCODER);
}
void PortDIntHandler()
{
	if ( !GPIOPinRead(ENCODERS_PORT, XENCODER))
	{
		if ( xDir == LEFT ) --xDesirablePos;
		else if ( xDir == RIGHT ) ++xDesirablePos;

		GPIOPinIntClear(ENCODERS_PORT,XENCODER);
	}
	if ( !GPIOPinRead(ENCODERS_PORT, ZRENCODER))
	{
		if ( xDir == LEFT ) --zrDesirablePos;
		else if ( xDir == RIGHT ) ++zrDesirablePos;

		GPIOPinIntClear(ENCODERS_PORT,ZRENCODER);
	}
	if ( !GPIOPinRead(ENCODERS_PORT, XRENCODER))
	{
		if ( xDir == LEFT ) --xrDesirablePos;
		else if ( xDir == RIGHT ) ++xrDesirablePos;

		GPIOPinIntClear(ENCODERS_PORT,XRENCODER);
	}
}

I also added apropierate code to **startup_ccs.c

  • An EAGLE user?

    A few notes on the circuit you sketched.

    • As drawn the input is always on
    • You used the same ground and power on both sides of the optoisolator.  This completely removes any need or usefulness of the opto.  BTW you might also want to take a look at digital isolators. They don't suffer the natural variations of opto-couplers.

    Robert

  • Robert Adsett said:
    You used the same ground and power on both sides of the optoisolator.

    Stickler for details - are you not - mon ami?    While you're absolutely correct - circuit as drawn/implemented "will work" - medoubts poster is (alone) in such component, "mis-application."     As poster titles, "Encoder" perhaps light/blockage "modulates" his output...

    We note that "natural variations" (i.e. aging of light source & receiver) fall far down the list of "hurdles" poster likely will face.     (and proper "handling/treatment of non-common grounds" must be enforced w/isolators - too...)

  • That was KiCad.
    I use the same ground and power because I dont really need to isolate it. I only need to detect hole in encoder wheel.
  • cb1- said:
    Robert Adsett
    You used the same ground and power on both sides of the optoisolator.

    Stickler for details - are you not - mon ami?    While you're absolutely correct - circuit as drawn/implemented "will work" - medoubts poster is (alone) in such component, "mis-application."   

    No doubt it would work, but it  could be replaced by a resistor.

    cb1- said:
    As poster titles, "Encoder" perhaps light/blockage "modulates" his output...

    I assume so, but the circuit drawn doesn't show that.

    cb1- said:
    We note that "natural variations" (i.e. aging of light source & receiver) fall far down the list of "hurdles" poster likely will face.  

    That and component to component variations are also significant.  I've seen that be an issue for first time designers.

    Tip to OP: Don't design to typical

    Robert

  • Ah, I haven't seen KiCad's interface for a while.

    Robert
  • Thank you, Sir - appreciated.

    May I suggest that if, "Speed of operation" is of concern - you monitor the encoder's output to insure it remains "proper" at highest expected speeds. Beyond that you should be, "Good to go."
  • Thank you very much.