PDA

View Full Version : PWM fluctuates on PIC18252



gringo332
- 23rd March 2007, 16:00
Hey guys, I am using PWM on a motor and I have an LED that flashes after I send the pulse to the motor. When ever the LED flashes the motor revs up really fast. If I disable the LED or leave it on all the time there is no problem, eg. the motor gives a constant speed. Any Idea why this might be?

Thanks , heres my code

DEFINE OSC 20
DEFINE LOADER_USED 1 ;bootloader
main

x var byte
TRISB.1 = 0
TRISa.2 = 0
TRISB.2 = 0
high porta.2
low portb.2
pwm portb.1,150,200

low porta.2
pause 500

goto main
end

mister_e
- 23rd March 2007, 16:04
Hey Gringo ;)

Well, you will have to use a Hardware PWM instead. Have a look to HPWM.

It runs in background and whatever else your code do... it shouldn't affect the PWM signal.

skimask
- 23rd March 2007, 16:17
pwm portb.1,150,200


The PWM command in PBP is a 'software' command... Program execution basically stops and that point and the only thing going on is the PWM command. If you need 'real' PWM, you have to use the hardware PWM module, or maybe software/interrupt driven PWM.

gringo332
- 23rd March 2007, 16:26
Thanks for the replies! I will look into it and let yall know what happens. Only thing is I don't remember this being a problem when I ran this same code on a 16f876.

skimask
- 23rd March 2007, 16:41
DEFINE OSC 20
DEFINE LOADER_USED 1 ;bootloader
main

x var byte
counter var byte

TRISB.1 = 0
TRISa.2 = 0
TRISB.2 = 0
high porta.2
low portb.2
pwm portb.1,150,200
counter = counter + 1

if counter > 127 then 'if counter is less than 128 turn led on
low porta.2 'if counter if greater than 127 turn led off
else
high porta.2
endif

goto main
end


Thanks for the replies! I will look into it and let yall know what happens. Only thing is I don't remember this being a problem when I ran this same code on a 16f876.

If I was going to do the same thing and use the 'software' PWM command, I might change your original code to what I put above, or maybe use a word value instead of a byte and change the cutoff point to 32767, or reset the counter if it hits 100, or 10000, or whatever.

gringo332
- 24th March 2007, 03:36
I see what was causing the motor to rev up. If you loop the PWM signal but if there is a PAUSE (i.e flashing an LED) before looping back this causes it to act this way. I think the motor responds this way when it is turned off. But if you loop back quickly the Pulse seems continuous with no hiccups. Yall are right, that I will probably have to use hardware PWM because I would be taking input from Infrared and doing some calculations before looping back.

Thanks for the suggestion again guys!