PIC 16F1827 Counting Pulses
Moderator: Benj
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
PIC 16F1827 Counting Pulses
One would think counting pulses from a Hall sensor would be kind of easy.
Hall sensor delivers 0101010101010101 (8 off and 8 on) per revolution.
In my code I am just looking at the OFF or LOW. Being that this is an AC motor that is driving the hall sensor speed is not constant. I am reading the pulses no problem, using a (IF RA2=0 then Count=Count +1) type statement. But I think my program loops so fast, that it may be seeing a second pulse only because the Hall sensor is still over the low magnet.
Is there an easy way to program in something that will not count the pulse until the pulse line goes high then low again.
I don't think I need an interrupt, the only time I am trying to count the pulses is when the program has turned the motor on.
Any suggestions?
Bob
Hall sensor delivers 0101010101010101 (8 off and 8 on) per revolution.
In my code I am just looking at the OFF or LOW. Being that this is an AC motor that is driving the hall sensor speed is not constant. I am reading the pulses no problem, using a (IF RA2=0 then Count=Count +1) type statement. But I think my program loops so fast, that it may be seeing a second pulse only because the Hall sensor is still over the low magnet.
Is there an easy way to program in something that will not count the pulse until the pulse line goes high then low again.
I don't think I need an interrupt, the only time I am trying to count the pulses is when the program has turned the motor on.
Any suggestions?
Bob
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Yep. my suggestion would be if i/p = 0 then add 1 to count but only if flag=0, then set a flag=1. When detecting a 1 at the i/p then reset flag to 0. so when i/p 0 is detected and flag is 0 then add 1 to count again etc.
Or alternately you can use a port interrupt that only adds to count on detection of falling edge.
If you get stuck with that and you don't mind sharing then I can implement it for you
Or alternately you can use a port interrupt that only adds to count on detection of falling edge.
If you get stuck with that and you don't mind sharing then I can implement it for you
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Interesting idea. I will give that a whirl when I get home later. Ate most my project time today tracing down a bad relay. I am almost 100% sure I am double counting the same pulse. May even try removing the magnetic ring (from CD ROM drive motor) and just use 3 for on,off,on.
- Dan81
- Valued Contributor
- Posts: 268
- Joined: Sun Jan 15, 2006 4:07 pm
- Location: Albi France
- Been thanked: 60 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Hello Bob
As wrote Medelec, I think the best way to count pulses, is to use RB0 interrupt.
(and timer interrupt if you want to get the speed)
Daniel
As wrote Medelec, I think the best way to count pulses, is to use RB0 interrupt.
(and timer interrupt if you want to get the speed)
Daniel
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Dan,
Speed is not what I am after. I need to know distance. I was wrong about the magnets, there are 6 on and 6 off. or one every 60 degrees of rotation of the pulse wheel. will have to manually rotate the thing and see how many revolutions it takes to spin the main output shaft 1 degree.
I am using RA2 as my input for the pulses. According to the data sheet, this pin also has interrupt on change ability.
I will give the set a flag option a try just to see what happens. I have a program that for now just counts the pulses and I can manually energize the motor. will be happy to get the same number when going 10 degrees CW and back CCW 10 degrees.
If the setting a flag idea does not work.
Bob
Speed is not what I am after. I need to know distance. I was wrong about the magnets, there are 6 on and 6 off. or one every 60 degrees of rotation of the pulse wheel. will have to manually rotate the thing and see how many revolutions it takes to spin the main output shaft 1 degree.
I am using RA2 as my input for the pulses. According to the data sheet, this pin also has interrupt on change ability.
I will give the set a flag option a try just to see what happens. I have a program that for now just counts the pulses and I can manually energize the motor. will be happy to get the same number when going 10 degrees CW and back CCW 10 degrees.
If the setting a flag idea does not work.
Bob
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Interrupt on change (need to select PORT interrupt on your flowchart) could be the way to go. The difference between that andBobw wrote:I am using RA2 as my input for the pulses. According to the data sheet, this pin also has interrupt on change ability.
int (need to select INT on your flowchart) interrupt on falling edge is:
The interrupt macro will be accessed when i/p changes from logic 1 to logic 0, and also changes from logic 0 to logic 1.
So what you could do is read the i/p pin within the interrupt macro
if i/p = 0 then count = count + 1
You will not need to use the flag method with that, since interrupt is only accessed on each change.
With non interrupt method of i/p change detection, this is a danger you missing i/p change if i/p pulses are quiet fast.
I think port IOC is 2nd best to Int on falling edge.
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
I may be setting my interrupt wrong.
1) Input from Switch (switch is resistor pulled high) looking for the LOW (Input is on RA2 pin1)
2) Interrupt set to RB0, calls macro flag_Low, set to trip on falling edge.
3) Macro simply adds 1 to the current pulse count
Program, in simulation never sees the interrupt?
I think the problem is FlowCode only works with RB0 not any other port or pin. My LCD D4 is on RB0 right now.
So I wrote in 2 loops.
First loop looks for the input to be 0, IF 0 then goes to second loop that looks for input to go high, only then will it increment the counter and continue on.
Will see.
I could still use RB0, but I would have to rewire my board.
1) Input from Switch (switch is resistor pulled high) looking for the LOW (Input is on RA2 pin1)
2) Interrupt set to RB0, calls macro flag_Low, set to trip on falling edge.
3) Macro simply adds 1 to the current pulse count
Program, in simulation never sees the interrupt?
I think the problem is FlowCode only works with RB0 not any other port or pin. My LCD D4 is on RB0 right now.
So I wrote in 2 loops.
First loop looks for the input to be 0, IF 0 then goes to second loop that looks for input to go high, only then will it increment the counter and continue on.
Will see.
I could still use RB0, but I would have to rewire my board.
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Simulator does not work too well with some of the interrupts.Bobw wrote:I may be setting my interrupt wrong.
Program, in simulation never sees the interrupt?
They should work fine on your hardware.
If you don't mind sharing, can look over flowchart for you.
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Will post what I have when I get home. (love midnight shift)
Counts pulses very accuratly now (for one direction) wont count them for the other.
10 times told to go the same amount and each time marks lined up dead on.
Ran out of time to look things over to see where I messed up.
For others, in a nut shell the program does this.
1) Turn on motor (CW or CCW)
2) Look for input from sensor (Low active)
3) Start loop look for input to be LOW "0"
4) Once LOW go into another loop to look for sensor to go HIGH "1"
5) Once high, exit loop, update pulse count, update LCD
6) Go back into the count loops.
Bob
Counts pulses very accuratly now (for one direction) wont count them for the other.
10 times told to go the same amount and each time marks lined up dead on.
Ran out of time to look things over to see where I messed up.
For others, in a nut shell the program does this.
1) Turn on motor (CW or CCW)
2) Look for input from sensor (Low active)
3) Start loop look for input to be LOW "0"
4) Once LOW go into another loop to look for sensor to go HIGH "1"
5) Once high, exit loop, update pulse count, update LCD
6) Go back into the count loops.
Bob
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC 16F1827 Counting Pulses
The interrupt macro is accessed every time i/p pin assigned to IOC goes from low to high or from high to low.Bobw wrote: For others, in a nut shell the program does this.
3) Start loop look for input to be LOW "0"
4) Once LOW go into another loop to look for sensor to go HIGH "1"
5) Once high, exit loop, update pulse count, update LCD
6) Go back into the count loops.
Bob
So by reading the the current state of input within the interrupt macro:
If i/p = 1 then you know pulse has gone from 0 to 1
If i/p = 0 then you know pulse has gone from 1 to 0
This is only the case for interrupt on change (IOC)
So you can set a flag to = 1 only after port B0 has gone form high to low, then low to high
Then in main routine, if flag = 1, update pulse count and LCD. set flag to 0
Edit: Thinking about it. You may not need to wait for i/p = 0 then i/p = 1
If you read i/p from within the interrupt routine and i/p = 1, then i/p could have only gone
from 0 to 1 ( since is an interrupt on change. Therefore i/p has changed form 0 to 1)
So if i/p is = 1, flag =1. etc.
This should simplify things for you.
Martin
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Well here ya go.
I found my counting problem on the CCW side.
On a full swing from -90 degrees to +450 degrees I over rotate about 7 degrees. And if I swing back to -90 degrees the tick marks are dead on. Think I will play with a delay some where in the code, I have it set to increment the Ant_Pos every 5 pulse changes, in reality I think I need about 5.1 or 5.2 pulse changes. I also want to add a X1--X10 switch for the Set_Pos. To slow when counting by 1. But for now I am happy that I have accuracy in the movement. I mean after moving 540 degrees and only being off about 7 not to bad for a start.
Bob
I found my counting problem on the CCW side.
On a full swing from -90 degrees to +450 degrees I over rotate about 7 degrees. And if I swing back to -90 degrees the tick marks are dead on. Think I will play with a delay some where in the code, I have it set to increment the Ant_Pos every 5 pulse changes, in reality I think I need about 5.1 or 5.2 pulse changes. I also want to add a X1--X10 switch for the Set_Pos. To slow when counting by 1. But for now I am happy that I have accuracy in the movement. I mean after moving 540 degrees and only being off about 7 not to bad for a start.
Bob
- Attachments
-
- RotorComp8.fcf
- (30.19 KiB) Downloaded 253 times
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Opps should have said need to up date things every 4.8 to 4.9 pulses.
-
- Posts: 157
- Joined: Sat Jan 22, 2011 10:39 pm
- Location: Michigan
- Has thanked: 6 times
- Been thanked: 27 times
- Contact:
Re: PIC 16F1827 Counting Pulses
Just a small update.
I have it set to increment the Ant_Pos every 4 pulse changes. I also included an 8Ms delay before going back to counting the pulses again. Playing with the delay time I have gotten it down to a +-2 degree accuracy over a 540 degree rotation. Rare that I would ever go that far in one setting.
I was also worried about the sensor being so far away that I might have needed an amplifier on the pulse signal. Testing it out on the bench with 100 feet of cable is no problem, and that is with the wires hooked together with alligator clips.
I still want to add a X10 switch to the user input switch. but that is an easy one.
Bob
I have it set to increment the Ant_Pos every 4 pulse changes. I also included an 8Ms delay before going back to counting the pulses again. Playing with the delay time I have gotten it down to a +-2 degree accuracy over a 540 degree rotation. Rare that I would ever go that far in one setting.
I was also worried about the sensor being so far away that I might have needed an amplifier on the pulse signal. Testing it out on the bench with 100 feet of cable is no problem, and that is with the wires hooked together with alligator clips.
I still want to add a X10 switch to the user input switch. but that is an easy one.
Bob