CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

2-bit quadrature encoder ISR
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah
Guest







PostPosted: Sun Jul 29, 2007 2:37 am     Reply with quote

The interrupt will only trigger, if a level changes after the latch has been read. It should already do this (the way the hardware works, if the state of the pins does not match the latched state, there should be an interrupt). However as with all things, there are timing limitations on detecting changes, and for example, as it stands, if a spike occurred between reading the port, and clearing the interrupt, which then returned their signals to the original state, this would probably be ignored. Now, this should not be a problem, but I was trying to rule out things (part of debugging...), by reducing the time over which this can happen.
I really do feel that hardware 'cleaning' of the signals is probably going to be the only solution that works....


Best Wishes
Zer0flag
Guest







PostPosted: Sun Jul 29, 2007 7:45 am     Reply with quote

Here is what I did, unfortunately the same problem occurs again (or even more often):

Code:

#INT_RB noclear
void encoder_ISR()
{
   
   port_b_content = input_b();
   
   clear_interrupt(INT_RB);
   
   enc_a_state = bit_test(port_b_content, 4);
   enc_b_state = bit_test(port_b_content, 3);
   
   if (enc_a_state != enc_b_state)
      enc_dir = 1;   // CW
   else
      enc_dir = -1; // CCW
   
   encoder_int_fired=TRUE;
}
Ttelmah
Guest







PostPosted: Sun Jul 29, 2007 2:14 pm     Reply with quote

OK.
This probably points to where the problem lies.
My suspicion is that the main code, is not seeing multiple triggers from the 'handler'. Imagine that two successive triggers occur with different direction results, one after the other. The main will only see the last one...
Though the normal rule is 'do as little as possible in the handler', for quadrature decoding, I'd maintain the position in the handler. So:
Code:

int16 position;

#INT_RB noclear
void encoder_ISR()
{
   
   port_b_content = input_b();
   
   clear_interrupt(INT_RB);
   
   enc_a_state = bit_test(port_b_content, 4);
   enc_b_state = bit_test(port_b_content, 3);
   
   if (enc_a_state != enc_b_state)
      position++;   // CW
   else
      position--; // CCW
   
   encoder_int_fired=TRUE;
}

Now 'fired', says that the position _has_ changed. The main no longer needs to update this, and will do it even if rapid calls occur one after the other.

Best Wishes
Zer0flag
Guest







PostPosted: Sun Jul 29, 2007 3:01 pm     Reply with quote

Again, thanks a lot for that hint. I'll try to implement and test it tomorrow!

Best regards,
Anguel
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 29, 2007 4:14 pm     Reply with quote

Also, if there's some bounce or noise in the encoder output just after
the edge occurs, it could cause a problem. You didn't say what the
frequency of the encoder waveform is, but try adding a short delay
before you do anything in the isr. Add the line shown in bold below.
It will act as a debounce delay.
Quote:

#INT_RB noclear
void encoder_ISR()
{
delay_us(100);

port_b_content = input_b();
Zer0flag
Guest







PostPosted: Sun Jul 29, 2007 4:31 pm     Reply with quote

Thanks for the hint! I'm not sure what you mean by frequency but the encoder is turned by hand Smile I'm gonna try the debounce.

PCM programmer wrote:
Also, if there's some bounce or noise in the encoder output just after
the edge occurs, it could cause a problem. You didn't say what the
frequency of the encoder waveform is, but try adding a short delay
before you do anything in the isr. Add the line shown in bold below.
It will act as a debounce delay.
Quote:

#INT_RB noclear
void encoder_ISR()
{
delay_us(100);

port_b_content = input_b();
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 29, 2007 4:39 pm     Reply with quote

In that case it's probably a fairly low frequency, such as a few Hz.
(Unless it's geared, so that it runs at a higher speed).

Just as a test, you could put in a millisecond level delay such as
1 ms, or 5 ms (instead of the 100 us).
Zer0flag
Guest







Still problems
PostPosted: Mon Jul 30, 2007 8:32 am     Reply with quote

@ PCM programmer: I tested inserting the delays but this did not help :(

@Ttelmah: I hope I understood your advice correctly and implemented it in my encoder-driven menu ( coming in the next post).
Unfortunately from time to time the encoder still does not move to the next menu line OR it jumps over one menu line
when I turn it in one direction and it "clicks" from one to the next detent point.
The ALPS EC11B is defined as 30 detents, 15 pulse. It has its detents at 11 (OFF/OFF) and 00 (ON/ON).
The datasheet says: "The stable detent position cannot be identified in phase B." But I'm not sure if this applies
to the EC11B because they describe many models together. Maybe this is the problem???
Zer0flag
Guest







The last modified version
PostPosted: Mon Jul 30, 2007 8:36 am     Reply with quote

Here is my last modified version which still has the problem:

Definition of the variables:

Code:

const int16 enc_init_position = 32768;   // the very initial position of the encoder
int16 enc_position;    // the position updated by the interrupt
int16 enc_temp_position;   // so that I don't have to work with enc_position directly, interrupts may occur


My ISR:

Code:

#INT_RB noclear
void encoder_ISR()
{
   port_b_content = input_b();
   
   clear_interrupt(INT_RB);
   
   enc_a_state = bit_test(port_b_content, 4);
   enc_b_state = bit_test(port_b_content, 3);
   
   if (enc_a_state != enc_b_state)
      enc_position++;
   else
      enc_position--;
   
   encoder_int_fired=TRUE;
}


In the main program for testing purposes I *always* calculate the current position from the *initial* position:

Code:

if (encoder_int_fired)
{
   encoder_int_fired = FALSE;

   int16 i;
   enc_temp_position = enc_position;   // enc_position may be changed by an interrupt, so work with the proxy

   line = 1; // always start counting from line 1 first

   if (enc_temp_position < enc_init_position)   // do we have to count up or down?
   {
      for (i = enc_temp_position; i < enc_init_position; i++)
      {
         line--;
         if (line == 0) // we have lines 1 to 4 in our menu
            line = 4;
      }
   }
   else       // enc_temp_position >= enc_init_position
   {
      for (i = enc_init_position; i < enc_temp_position; i++)
      {
         line++;
         if (line == 5)   // we have only 4 lines in our menu
            line = 1;
      }
   }

   // finally I display an arrow at "line" position ...
Ttelmah
Guest







PostPosted: Mon Jul 30, 2007 8:48 am     Reply with quote

Don't declare 'i' where you show it. This is not legitimate, even in normal 'C' (the declarations must be at the start of a function, or code block), and in CCS, _will_ cause problems, if there is any other variable called 'i' in the same function...
If all you want to do, is move 'line', then do this directly:
Code:

int line=0;
#INT_RB noclear
void encoder_ISR()
{
   port_b_content = input_b();
   
   clear_interrupt(INT_RB);
   
   enc_a_state = bit_test(port_b_content, 4);
   enc_b_state = bit_test(port_b_content, 3);
   
   if (enc_a_state != enc_b_state)
      line=(line+1) & 3;
   else
      line=(line-1) & 3;
   
   encoder_int_fired=TRUE;
}

Line will move from 0 to 3. Just add one to it to index the display. You don't need to protect this from interrupts occurring (here you are only dealing with one byte - with a two byte value, it may be necessary).

Best Wishes
Zer0flag
Guest







PostPosted: Mon Jul 30, 2007 9:19 am     Reply with quote

Thanks for the code optimization Smile
Unfortunately the problem with the hanging or jumping menu arrow still persists. I have the impression that this has something to do with the detent points :(

So I should check the hardware now?

Regards,
Zer0flag
Ttelmah
Guest







PostPosted: Mon Jul 30, 2007 10:18 am     Reply with quote

I'm afraid, you almost certainly need a buffer on the input lines.
What is your PIC?. What voltage is it running at?.
If you look at the input spec for a typical chip, for a TTL input, 'high', is anything over 2v, while 'low', is anything under 0.15Vdd. 0.75v, for 5v operation. The output of the encoder you are using, is only guaranteed to pull the signal, within 1.5v of ground...
I'd suspect that on occasions, the signal is not getting to the 'low' level, on one or the other pin, and the wrong data is seen as a result. Most of the time itwill do better than this, and work...

Best Wishes
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Mon Jul 30, 2007 11:48 am     Reply with quote

PCM programmer wrote:
Also, if there's some bounce or noise in the encoder output just after
the edge occurs, it could cause a problem. You didn't say what the
frequency of the encoder waveform is, but try adding a short delay
before you do anything in the isr...


Bad idea. There is nothing to suggest that the signal bounce is time-limited. Adding delays will only push the problem somewhere else where it may be even harder to find. Also, delays in ISRs are bad on general principles. If there are further changes in the port state after the port is read, it will cause a further interrupt, and then those changes can be handled at the proper time in the normal way.

Robert Scott
Real-Time Specialties
Ypsilanti, Michigan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 30, 2007 12:04 pm     Reply with quote

Typically, noise such as ringing is going to occur on the transistions.
The #INT_RB interrupts occur on any edge. If there is ringing on
the transitions, then putting a delay at the start of the isr will give
enough time for the noise to go away. Then, when the "mis-match"
condition is cleared by reading Port B, you won't get any more interrupts
due to the noise.

My test was designed to see if this was the case.

If it was me, I've have put a scope on the signals and have the whole
problem solved by now.
Zer0flag
Guest







PostPosted: Mon Jul 30, 2007 12:26 pm     Reply with quote

PCM programmer wrote:
If it was me, I've have put a scope on the signals and have the whole
problem solved by now.


Unfortunately I don't know much about electronics and how to do that. So I am waiting for an electronics guy to check the things. Unfortunately he is very busy and I wanted to make sure it is not the software in the meantime. On the other hand I really learned a lot from your tips! Thank you very much once again!

Regarding the encoder I checked again and found out that the guy who designed the whole device (I don't know him) initially used and tested it with a Piher CS11 and probably used the Piher application notes. Then the Piher did not fit well into the front plate so he just replaced it with the ALPS. And maybe there are some important differences the electronics specialist has to check.

Regards,
Zer0flag
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group