use HPWM instead.
You set the frequency, set the duty cyle (motor speed)... and you're done. It runs in background with the internal CCP module and it will run as long as you want... or power failure (duh....).
use HPWM instead.
You set the frequency, set the duty cyle (motor speed)... and you're done. It runs in background with the internal CCP module and it will run as long as you want... or power failure (duh....).
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Try this code.
LED is on GPIO.2
Button must be pulled down to GND by a resisitor (10K).
And must be pulled high by the button (to Vdd).
Code:@ DEVICE PIC12F683, FCMEN_OFF, IESO_OFF, INTRC_OSC_NOCLKOUT , WDT_OFF, PWRT_ON, MCLR_OFF, PROTECT_ON, BOD_OFF OSCCON = %01110001 ' Internal 8MHz osc. DEFINE OSC 8 ' ======= Common Settings ================================= ADCON0 = 0 ANSEL = 0 CMCON0 = 7 ' Comparators off. TRISIO = 0 GPIO = 0 ' ============= PWM Settings ========= CCP1CON = %00001100 ' CCP1, PWM mode PR2 = 250 ' T2CON = %00000101 ' TMR2 on, prescaler 1:4 CCPR1L = 0 Speed var CCPR1L Btn Var GPIO.3 ' Assuming ; the pin is pulled down to GND by 10K resistor, and button pulls it high to Vdd. ' LED is connected to GPIO.2 (on board PWM module) pause 10 Start: if btn then if speed + 85 < 256 then ' 0, 85, 170, 255, 0 speed = speed + 85 else speed = 0 endif while btn : pause 20 : wend endif pause 2 goto Start end
"If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte
Thanks Sayzer,
I've had some other issues to resolve before I finally got to try it today. It works fine and does exactly what I want to accomplish.
I do have a few questions about how the voodoo that itdoo works but I want to experiment a bit more before I ask them.
Last edited by AvionicsMaster1; - 19th July 2011 at 02:13. Reason: fat fingered
Since you were able to provide an answer for that, do you have any ideas how I can run two outputs simultaneously but each at different output levels?
What I want to do is have a switch turn on a fan at stepped levels and another switch that controls a light/LED that can be set to a different level.
I know that isn't what the post started with but I assumed I'd be able to use the PWM command for setting the levels and maybe erroneously thought I could accomplish that for two different outputs.
Since you have an onboard PWM module running in the background, you can also have a second pwm by software running in the foreground.
The code would be as follows.
You are now using two buttons, one is for LED and the other is for Fan.
I suggest that you use Fan on GPIO.2 (on board module), and LED on GPIO.5 pin.
Also, you have button on GPIO.3, and you are puling it down .
Nowyou have a second button but is is being pulled high.
Code:@ DEVICE PIC12F683, FCMEN_OFF, IESO_OFF, INTRC_OSC_NOCLKOUT , WDT_OFF, PWRT_ON, MCLR_OFF, PROTECT_ON, BOD_OFF OSCCON = %01110001 ' Internal 8MHz osc. DEFINE OSC 8 ' ======= Common Settings ================================= ADCON0 = 0 ANSEL = 0 CMCON0 = 7 ' Comparators off. TRISIO = 0 GPIO = 0 ' ============= PWM Settings ========= CCP1CON = %00001100 ' CCP1, PWM mode PR2 = 250 ' T2CON = %00000101 ' TMR2 on, prescaler 1:4 CCPR1L = 0 Speed VAR CCPR1L ' Speed variablefor LED brightness. Speed2 VAR BYTE ' Speed2 variable for fan speed. ' GPIO.0 ' Empty. ' GPIO.1 ' Empty. ' GPIO.2 ' LED is connected to GPIO.2 (on board PWM module) Btn VAR GPIO.3 ' Assuming ; the pin is pulled down to GND by 10K resistor, and button pulls it high to Vdd. Btn2 VAR GPIO.4 ' Second button. But the pin is pulled high internally. So no external pull up or pull down resistor is required. ' Have a button between this pin and GND (unlike the first button). Fan VAR GPIO.5 ' Fan motor output. INPUT Btn2 ' Make the second button pin an input pin. OPTION_REG.7 = 0 ' WPU.4 = 1 ' Enable internal pull up resisitor for Second button pin (GP4) PAUSE 10 Speed2 = 0 ' First button, btn, controls the LED via onboard PWM module. ' Second button controls the FAN via software PWM via GPIO.5 output pin and GPIO.4 input pin. ' Having the Fan on GPIO.2 and LED on GPIO.5 would be better I think. Start: IF Btn = 1 THEN ' When this button is pressed, it pulls the pin high. IF Speed + 85 < 256 THEN ' 0, 85, 170, 255, 0 Speed = Speed + 85 ELSE Speed = 0 ENDIF WHILE Btn = 1 ' When this button is being pressed, have no interruption on Fan control. PWM Fan,Speed2,1 WEND ENDIF IF Btn2 = 0 THEN ' When this button is pressed, it pulls the pin low. IF Speed2 + 85 < 256 THEN ' 0, 85, 170, 255, 0 Speed2 = Speed2 + 85 ELSE Speed2 = 0 ENDIF WHILE Btn2 = 0 ' When this button is being pressed, have no interruption on Fan control. PWM Fan,Speed2,1 WEND ENDIF PWM Fan,Speed2,1 ' Always drive the fan output. GOTO Start END
"If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte
You are my hero. I've yet to try it but I really appreciate your help. Thanks
I have a couple of questions I hope you'll answer so I can understand what's going on better.
Why is it important to put the fan on gpio.2? I realize it's the "on-board pwm module" but what does that buy you in either performance or code simplicity? Why couldn't I put it on any pin like the LED?
Why turn all these off: FCMEN_OFF, IESO_OFF, WDT_OFF ?
Are these two statements redundant:
OSCCON = %01110001 ' Internal 8MHz osc.
DEFINE OSC 8
Thanks for your time and patience.
Bookmarks