Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Thread Closed: Drifted off-topic

???
05/17/03 14:00
Read: times


 
#45743 - RE: Software Filter
Responding to: ???'s previous message
Waqar:

You cannot understand the filter I described? I can write it again in the detailed way.

Lets take an example to use a single input that will require some filtering. The input may be some signal that comes from a mechanical contact with chatter or it may come from some type of slow but nosiy rise time signal through a comparator type circuit. In either case the signal may change levels multiple times at the transitions. The timing line below will show how this might look on. What can happen is that the microcontroller, if it did a read of the input pin right during the multilevel changing period, it may misintrepret the more broad picture of what is really going on with the signal. One way to deal with this problem in software is to sample and filter the signal.


Click above diagram to see high resolution verson.


I have found that a shift register in a byte works really good for this. It is somewhat necessary to understand the nature of the multilevel changing time of the signal in order to filter it effectively. For the example we look at here lets say that we notice on the oscilloscope that the "bouncing" time (what the multilevel changing is often called) seems to last for 2 -> 3 milliseconds. An effective filter for this may be to impose a 5 milliseconds filter on this input. So here is one way to consider that filter:

1) Pretend that the input is called INTERLOCK SWITCH. Lets just call it INSWT for short and that it is connected to P2.0. In some initialization routine we will have made sure that the output latch bit of P2.0 was written to a '1' so that this pin is an input.

2) Set aside a single byte memory location for the filter. Lets name this byte location INSWT_FILTER. In the initialization part of the program have this filter set to either 00h or FFh. (Initial choice will
depend some what on what you expect the idle state of the INSWT signal to be. At initialization you may decide to read INSWT and set the filter based upon its initial setting. (More on this at the end).

3) Arrange for a timer interrupt that goes off once per millisecond. This interrupt will support 1 KHz timing reference on a periodic basis.

4) Each time the interrupt for the timer goes off have the program execute a short sequence of code that does something like the following:
      MOV C, INSWT       ;or MOV C, P2.0
      MOV A, INSWT_FILTER
      RLC A
      MOV INSWT_FILTER, A

...This results in keeping a picture of the state of the input signal for up to 8 milliseconds in the bits of the filter byte with the most recent state being in the low bit.

5) Now in another part of the program when you want to see if the INSWT signal is actually at a '1' level but certainly there for 5 milliseconds based on what we decided for the filter. You would use the following code to check the INSWT filter.
      MOV A, INSWT_FILTER
      ANL A, #1Fh          ;mask 5 low bits = 5 msec
      CJNE A, #1Fh, LABEL1 ;test if all low bits = '1'
      .... here take action to deal with 
           INSWT being filtered at '1'.
      ....
LABEL1:
      .... continue program on its way


6) Likewise in the main part of the program you want to see if the INSWT signal is actually at a '0' level but certainly there for 5 milliseconds based upon what we wanted the filter to do you would use the following code to check the INSWT filter.
      MOV A, INSWT_FILTER
      ANL A, #1Fh          ;mask 5 low bits = 5 msec
      JNZ LABEL2           ;test if all low bits = '0'
      .... here take action to deal with 
           INSWT being filtered at '0'.
      ....
LABEL2:
      .... continue program on its way


7) Now in many programs it may be necessary to check the state of the INSWT signal in more than one place in the code. In these cases it is inefficient to repeat the two above blocks of code again and again so using the very handy bit variable capability of the 8051 architecture we can set aside a bit variable in one of the bit addressable bytes of internal RAM. (Recall that these are in the address range of 020h to 02Fh). Lets use the first bit for this and setup a name
for the bit as follows:
      BSEG AT 020h
INSWT_BIT DBIT 1           ;allocate our filtered bit

Then in the timer interrupt code you can place code which looks like the following. This makes the bit we setup follow the filtered status of the polled input.
      MOV  C, INSWT         ;or MOV C, P2.0
      MOV  A, INSWT_FILTER
      RLC  A                ;shift new sample into
      MOV  INSWT_FILTER, A  ;..the filter byte
      ANL  A, #1Fh          ;mask 5 low bits = 5 msec
      JZ   LABEL_0          ;input filter = xxx00000b
      CJNE A, #1Fh, LABEL_X ;input not stable
LABEL_1:
      SETB INSWT_BIT        ;input filter = xxx11111b
      JMP  LABEL_X          ;set the filter status bit
;
LABEL_0:
      CLR  INSWT_BIT        ;clr the filter status bit
;
LABEL_X:
      ...
      ...                   ;program continues on


8) Finally in various places within the main program when it is necessary to test the state of the filtered INSWT signal it is possible to use instructions like one of the following:
      JB   INSWT_BIT, SOME_LABEL

  or 

      JNB  INSWT_BIT, SOME_OTHER_LABEL


9) Sometimes when using a filter like this it is a good idea to let the timer interrupt run for a while to let the filter stabilize with input samples before starting to use the filtered state of the signal. In the case we talked here it may be desirable to let at least 5 timer interrupts occur first. Often times this can be simply accomplished by initializing the timer interrupt and filter logic early after program startup but then let other system initialization and first activity occur. Often this takes longer than the 5 timer interrupts and so the filter is stable before you get around to using it.


Now that should be all you ever need to know about a simple one bit filter.

Michael Karas



List of 87 messages in thread
TopicAuthorDate
DMTF and Pulse Dialing            01/01/70 00:00      
   RE: DMTF and Pulse Dialing            01/01/70 00:00      
      RE: DMTF and Pulse Dialing            01/01/70 00:00      
         RE: DMTF and Pulse Dialing            01/01/70 00:00      
            RE: DTMF and Pulse Dialing            01/01/70 00:00      
               RE: DTMF and Pulse Dialing            01/01/70 00:00      
            RE: DMTF and Pulse Dialing -- IJAZ            01/01/70 00:00      
               RE: DMTF and Pulse Dialing -- Dan            01/01/70 00:00      
   Just one small example            01/01/70 00:00      
   RE: DMTF and Pulse Dialing-Waqar            01/01/70 00:00      
   RE: DMTF and Pulse Dialing -- IJAZ            01/01/70 00:00      
      RE: DMTF and Pulse Dialing -- IJAZ            01/01/70 00:00      
         RE: DMTF and Pulse Dialing -- IJAZ            01/01/70 00:00      
   forum people            01/01/70 00:00      
   only steps i need            01/01/70 00:00      
      RE: only steps i need            01/01/70 00:00      
         RE: only steps i need            01/01/70 00:00      
   RE: DMTF and Pulse Dialing            01/01/70 00:00      
   RE: DMTF and Pulse Dialing            01/01/70 00:00      
      really its not happening            01/01/70 00:00      
         RE: really its not happening            01/01/70 00:00      
            My Objective in Clear Words.Hal Albach..            01/01/70 00:00      
         does it work, or not?!?!            01/01/70 00:00      
   RE: DMTF and Pulse Dialing            01/01/70 00:00      
   other things            01/01/70 00:00      
      RE: other things            01/01/70 00:00      
         RE: other things            01/01/70 00:00      
            RE: other things            01/01/70 00:00      
               RE: other things            01/01/70 00:00      
      RE: other things            01/01/70 00:00      
         RE: a suggested condition for Waquar            01/01/70 00:00      
            For Erik Malund            01/01/70 00:00      
               RE: For Erik Malund            01/01/70 00:00      
   People try to understand            01/01/70 00:00      
   people try to understand            01/01/70 00:00      
      RE: people try to understand            01/01/70 00:00      
         RE: Michael            01/01/70 00:00      
      RE: people try to understand            01/01/70 00:00      
         RE: people try to understand            01/01/70 00:00      
            try to read it with soft heart            01/01/70 00:00      
               RE: Software Filter            01/01/70 00:00      
   RE: DMTF and Pulse Dialing            01/01/70 00:00      
      come on iam doing this            01/01/70 00:00      
         RE: come on iam doing this            01/01/70 00:00      
    where are you sir erik malund            01/01/70 00:00      
      RE: where are you sir erik malund            01/01/70 00:00      
   RE: DMTF and Pulse Dialing            01/01/70 00:00      
      RE: DMTF and Pulse Dialing            01/01/70 00:00      
      suggest some thing about this code            01/01/70 00:00      
         RE: suggest some thing about this code            01/01/70 00:00      
         RE: suggest some thing about this code            01/01/70 00:00      
   RE: Kai            01/01/70 00:00      
      RE: Hans / Kai            01/01/70 00:00      
         RE: Hans / Kai            01/01/70 00:00      
            RE: Hans / Kai            01/01/70 00:00      
               RE: Hans / Kai            01/01/70 00:00      
                  RE: Hans / Kai            01/01/70 00:00      
      RE: Kai            01/01/70 00:00      
         RE: Kai            01/01/70 00:00      
            RE: Kai            01/01/70 00:00      
               RE: Waqar            01/01/70 00:00      
         the most helpful one            01/01/70 00:00      
            RE: the most helpful one            01/01/70 00:00      
            RE: the most helpful one            01/01/70 00:00      
   software filtering advice needed            01/01/70 00:00      
      RE: software filtering advice needed            01/01/70 00:00      
         RE: software filtering advice needed            01/01/70 00:00      
            RE: software filtering advice needed            01/01/70 00:00      
      RE: software filtering advice needed            01/01/70 00:00      
         RE: software filtering advice needed            01/01/70 00:00      
            oh sorry michael            01/01/70 00:00      
         something is happening            01/01/70 00:00      
            RE: something is happening            01/01/70 00:00      
               i got the point            01/01/70 00:00      
                  RE: i got the point            01/01/70 00:00      
                     RE: i got the point            01/01/70 00:00      
                         Kai Klaas oscilloscope            01/01/70 00:00      
                           RE: Kai Klaas oscilloscope            01/01/70 00:00      
                           RE: oscilloscope            01/01/70 00:00      
   He's up to his old tricks again            01/01/70 00:00      
      RE: Waquar, Michael            01/01/70 00:00      
         RE: Waquar, Michael            01/01/70 00:00      
            RE: Waquar, Michael            01/01/70 00:00      
               RE: Waquar, Michael            01/01/70 00:00      
         erik malund..what happen to you            01/01/70 00:00      
      Donald Catto think before speaking            01/01/70 00:00      
         RE: Donald Catto think before speaking            01/01/70 00:00      

Back to Subject List