PDA

View Full Version : how can i stop the pic from sending a PWM???



texas5
- 12th October 2008, 12:29
hi all.my pic16f877a did generate the 125kHz with 50% (get from this forum,thanx) but i don't know how to turn it off.what i mean is if i push the button 1 pic will generate the pwm & when i push the button 2 pic will stop from generate it (on/off the PWM).

here is my coding:

define osc 8
TRISC = 0 'PORTC all outputs
TRISB.5 = 1
TRISB.4 = 1

push1 var portb.5
push2 var portb.4

main:

if push1 = 0 then inject
if push2 = 0 then notinject

goto main

inject:

if push2 = 0 then notinject

PR2 = 15 'Load Period Register
CCPR1L = 8 'Set 50% duty cycle
CCP1CON = %1100 'PWM mode
T2CON.2 = 1 'Start TIMER2

if push2 = 0 then notinject

goto inject

notinject:

if push1 = 0 then inject

????????????????????????????
????????????????????????????
????????????????????????????

if push1 = 0 then inject

goto notinject

i don't know what to fill in the "??????"....i have try to set the TMR2=0, CCP1CON=0 & T2CON.2=0 but it did not work...help me please....

Bruce
- 12th October 2008, 15:50
This should work.


DEFINE OSC 8
TRISC.2 = 1 ' CCP1 pin stays input intil PWM on
TRISB.5 = 1 ' button input (external pull-up with switch to ground)
Push1 var PORTB.5

PR2 = 15 'Load Period Register
CCPR1L = 8 'Set 50% duty cycle
CCP1CON = %1100 'PWM mode
T2CON.2 = 1 'Start TIMER2

Main:
TRISC.2=Push1 ' if RB5=0 CCP1 pin is an output, PWM=on
GOTO Main ' if RB5=1 CCP1 pin is an input, PWM=off
The PWM module is still operationg so you shouldn't have any glitches in the signal, and
you only need one button to control it.

When the CCP1 pin is switched to an input, the PWM signal is disconnected. When it's
flipped to an output it's back on.

texas5
- 13th October 2008, 07:05
wow.simple solution.haha.thanx a lot bruce.but are your coding mean i need to hold the push button to PWM on & release the button to PWM off...or push the button once to PWM on & push once again to PWM off????

skimask
- 13th October 2008, 07:08
wow.simple solution.haha.thanx a lot bruce.but are your coding mean i need to hold the push button to PWM on & release the button to PWM off...or push the button once to PWM on & push once again to PWM off????
Read the code! Do you see anything in that code saving the state of the button to remember for the next time the loop checks the buttons state?
The bit in the TRIS register follows the state of the push button...simple as that...just like it's written...

Bruce
- 13th October 2008, 14:27
Yep. Like Ski said, that's how it works.

Here's another flavor.


Main:
IF Push1=0 THEN
GOSUB ChkState
ENDIF
GOTO Main

ChkState:
WHILE Push1=0 ' wait for button release
WEND
TRISC.2=TRISC.2 ^ 1 ' toggle TRISC.2 on button release
RETURN
Endless simple possibilities.

texas5
- 14th October 2008, 17:12
thanx guys.it work....