View previous topic :: View next topic |
Author |
Message |
demonspells
Joined: 06 Jan 2011 Posts: 26
|
PWM speed control / rpm meter problem |
Posted: Thu Jan 20, 2011 6:48 am |
|
|
hi
I'm using pic16f877a to drive a motor at different speeds using PWM, and it works fine.
Now I'm using the same PIC to measure the motor speed using a pair of LED/PD, so I am measuring the speed depending on the frequency of the output signal, and that also works fine when I use a pulse wave to simulate the output signal (when I change the pulse wave frequency the speed reading changes).
My problem is that I need to control this speed depending on a threshold value of the speed, and I can't do that on simulation.
So I need a way to convert my variable PWM signal to a variable frequency signal.
i.e. when I change the duty cycle, the output frequency should change.
Is that possible ?? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Thu Jan 20, 2011 7:03 am |
|
|
Simple answer, yes.
If your 'speed sensor' routine (input speed), is timer based, you'll get a value for the period between the optocoupler 'blips'. Configure a timer to max out at max speed (ie 16 bits = max rotation). Then use the 'counts' as an input to the PWM portion of your program. You should already have a table of how PWM values control the motor.
Now you'll have a similar table for the speed sensor.
Obviously it won't be a simple replacement, some math will be involved to get the right motor speed for your setup, a simple lookup table might work.
Don't ask me how to setup your 'simulator' for this though, I only work in the real world as all simulators are full of 'bugs'. |
|
|
demonspells
Joined: 06 Jan 2011 Posts: 26
|
|
Posted: Fri Jan 21, 2011 7:52 am |
|
|
thanks
I'm using the following routine, can't figure out a way to follow your instructions:
Code: |
#INT_EXT
void ext_isr()
{
N++;
}
int get_speed(void)
{
float speed,m;
long t=0;
N=0;
set_timer1(0);
while(t<=62500){ // delay 0.5s
t=get_timer1();
}
m=N;
speed=m*0.5732;
return(speed);
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Fri Jan 21, 2011 9:30 am |
|
|
Just check all the examples that CCS gives us! There's one about measuring 'pulse width' or search the forum here for 'tachometer', or 'pulse width'.
First sensor pulse triggers the ISR, 2nd stops it, the timer registers will be the 'count' of timer ticks for that duration.Use that number to configure your PWM function.
Obviously you'll have to do some math to get the right speed vs. sensor input and set limits ! ie: when no pulses arrive, you're stopped NOT full speed !! |
|
|
|