Incremental encoders help

Bernard

Joined Aug 7, 2008
5,784
My understanding is that there is some separation between sensor & air jet so it is either a distance or time measurement requirement ? I'd go for an IR beam break & 555 timer, assuming constant belt speed.
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
How do you intend registering them in the micro, have you considered the T1CKI to use TMR1 as a counter?
Max.
Let us speak about the connection of the encoders.

Encoder: A will connect to RC0 of pic16f877A

Is this correct?

How to program timer as a counter in pic16f877a to count encoder pulses?
 

MaxHeadRoom

Joined Jul 18, 2013
30,697
Look up Timer1 in counter mode. Pulse in to RC0.
Read up in the manual there is details on the subject, I only program in Assembly now.
If familiar with timers, you can preload the register and also use pre-scaler if need be.
Also I left the 16F series behind in favour of the 18F series, some nicer commands added which particularly help using Assembly.
You will need to use 5v for the encoder supply.
Max.
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
I believe yours is RS422 if so it is 5vTTL to 30v supply.
It shows 5v to 30v in your PHOTO!.
Max.
I think I should try by writing code

This page gave me good information https://exploreembedded.com/wiki/PIC16f877a_Timer

C:
int Count = 0;
void main (void)
{
T1CON = 0b00000001;
while (1)
{
     while (! TMR1IF);
     TMR1IF = 0;
     Count ++;
       if (Count == 32000)
        {
          Count = 0;
        }
  }
}
Note : should I start new thread ? because it's suitable for Embedded system forum
 
Last edited:

LesJones

Joined Jan 8, 2017
4,509
As you said in post #24 with a 24 volt supply to the encoder the output is a 0 to +24 volt square wave. If you want to supply the encoder with 24 volts then you can divide the output down to about 5 volts using a potential divider. (For example a 1K resistor and a 3.9 k resistor this will give a 4.9 volt signal which is close enough to 5 volts.) I think trying to power the encoder wit 5 volts as Max suggests is a better method.
I would not use TMR1 the way you are using it. When the object is detected I would load TMR1 with it's overflow value (65536 decimal 10000 hex) minus the count you want. It will then generate an interrupt when it overflows so the interrupt service routine can trigger the air jet. Doing it that way saves the cpu from being tied up in a loop comparing two numbers.

Les.
 

MaxHeadRoom

Joined Jul 18, 2013
30,697
These encoder are typically used in CNC applications using 5vdc, so I don't see that as a problem.
If used in a electrically noisy situation and/or a long run from encoder to processor, you may want to take advantage of the RS422 and place a suitable receiver IC at the micro end, there are many out there.
You would only need it for the pair you are using, A, A/ etc.
e.g. UA9637
Max.
 
Last edited:

Thread Starter

daljeet795

Joined Jul 2, 2018
295
I would not use TMR1 the way you are using it. When the object is detected I would load TMR1 with it's overflow value (65536 decimal 10000 hex) minus the count you want. It will then generate an interrupt when it overflows so the interrupt service routine can trigger the air jet.

Les.
I want to count 32000 Pulses

65536-32000= 33536 = 0x8300

TMR1H=0x8300; // initial count values in timer1 register
TMR1L=0x00;
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
Looks about right to me.
Max.
something like this

C:
void main()
{
   TMR1H=0x8300; // initial count values in timer1 register

   TMR1L=0x00;

   TMR1IE=1; //Enable timer interrupt bit in PIE1 register

   GIE=1; //Enable Global Interrupt

   PEIE=1; //Enable the Peripheral Interrupt

   TMR1ON = 1; //Start Timer1

}

void interrupt timer_isr()
{
  
}
 

Thread Starter

daljeet795

Joined Jul 2, 2018
295
I don't program in C, but wouldn't the TMR1H= 83 ?
Max.
I am using TMR1 to count pulses on pin RC0. Timer1 is just a 16 bit counter it can count up to 65535 (16 bits) and

I want to count 32,000 pulses What would be register values in timer1?

TMR1H=0x00;
TMR1L=0x00;
 
Last edited:

Thread Starter

daljeet795

Joined Jul 2, 2018
295
I hope someone having knowledge of c and pic16f877a would help here
C:
void main()
{
   TMR1H=0x82; // initial count values in timer1 register
   TMR1L=0xff;

   TMR1IE=1; //Enable timer interrupt bit in PIE1 register

   GIE=1; //Enable Global Interrupt

   PEIE=1; //Enable the Peripheral Interrupt

   TMR1ON = 1; //Start Timer1
}

void interrupt timer_isr()
{
    // code to trigger the air jet //
}
 
Last edited:

LesJones

Joined Jan 8, 2017
4,509
TMR1 is a 16 bit counter. As the PIC16F877 is an 8 bit processor it has to be accessed as two bytes. TMR1L (Bits 0 - 7.) and TMR1H (Bits 8 - 15.) So you can't write 0x8300 (Which is 16 bits.) into TMR1H as this is only 8 bits. At the start of the program you will also have to configure TMR1 to be clocked from an I/O pin. You will also have to set the prescaler value. (I assume you would be using a value of 1) I don't know how to do these things using "C"
There is also an alternative way to do your counting. You could use the capture/compare module (In compare mode.) to cause an interrupt when TMR1 reaches the value in the CCPR1 register. This way TMR1 would start with a count of zero.

Les.
 
Top