CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

toggle RB3 - pwm pin on pic16f628

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ozzie2005



Joined: 11 Dec 2006
Posts: 5

View user's profile Send private message

toggle RB3 - pwm pin on pic16f628
PostPosted: Fri Dec 12, 2008 3:27 pm     Reply with quote

Hi!

I have a question about PWM. I need to "toggle" the output on few cycles on RB3 pin (PIC16F628)
keeping the same duty cycle at 50%.
Once it is done, pwm should run normally again to original settings.
I don't have any code yet but I need to have some idea before I tackle the problem.

1) I think a I can write a code to generate 40Khz using pwm.

2) I need to know if at any given time I can toggle (according to some data) the output of RB3 on three or more consecutive cycles, once completed return back to the original PWM signal.

3) I'll use a pic16f628 with internal clock (if possible)


thanks for any help...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 12, 2008 5:01 pm     Reply with quote

Do you have to do anything else in the program besides this ?
Is it OK if the program sits in a loop and creates the waveforms
(even using hardware to do it) ?
Or, should the changes to the waveform be done during an interrupt so
that other things can happen in the program ?

What's the duration of each portion of the waveform ?
i.e., how many cycles (or ms) of normal PWM will occur between
the groups of 50% duty cycle PWM ?
ozzie2005



Joined: 11 Dec 2006
Posts: 5

View user's profile Send private message

thank you
PostPosted: Sat Dec 13, 2008 3:04 am     Reply with quote

thank you PCM programmer,

- The program waits for a signal from a PC (via rs232). If not signal received, the pic generates a 40khz waveform constantly.

- when the PC signals the PIC, the pic modifies the output of RB3.
(see picture) for few cycles. There should be no interrupt with the waveform during that transition.

- When completed, RB3 should continue with the 40khz waveform.

I can use a 555 to get this waveform but as you see I need to include some "logic" and toggle the output when signaled by the PC.

I use timer0 to generate the waveform plus logic but with this timer I can't generate a frequency greater than 1khz (assuming 4Mhz internal Osc is used). Am I right?

So that is why I think I should use PWM but I don't know if I can change the value of RB3 "on-the-fly" keeping the waveform clean with no glitches.

The frequency is not really critical. It could be something around 40khz. The Duty cycle should be ~50%; not critical either.

Does CCS have an interrupt routine for PWM similar to int_rtcc so I can probably manipulate the RB3 latch?

Another idea I have is to generate the PWM (RB3) and connect this output to either RB0 or RA4, write a code using any of these interrupts and output the signal on any of the remaining ports.


thank you for your help/ideas PC programmer!




PCM programmer wrote:
Do you have to do anything else in the program besides this ?
Is it OK if the program sits in a loop and creates the waveforms
(even using hardware to do it) ?
Or, should the changes to the waveform be done during an interrupt so
that other things can happen in the program ?

What's the duration of each portion of the waveform ?
i.e., how many cycles (or ms) of normal PWM will occur between
the groups of 50% duty cycle PWM ?


PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Dec 13, 2008 12:39 pm     Reply with quote

The problem with using interrupts with an oscillator frequency of 4 MHz is
the large amount of overhead. It takes about 50 instruction cycles just
to get in and out of your isr. This is the amount of cycles required by
the CCS interrupt dispatcher code. At 4 MHz, it's 1 us/cycle, so that's
50 us overhead. That doesn't include the code in your CCP1 isr.
A 40 KHz signal has a 25 us period.


Quote:
- The program waits for a signal from a PC (via rs232). If not signal received, the pic generates a 40khz waveform constantly.

- when the PC signals the PIC, the pic modifies the output of RB3.
(see picture) for few cycles. There should be no interrupt with the waveform during that transition.

- When completed, RB3 should continue with the 40khz waveform.

Are these the only things it does ? Can there be a while() loop in main()
which simply polls the UART with kbhit() and gets the command byte if
available, and then changes the waveform ? If so, it's possible that
you could use the "Compare" module of the CCP to create the waveform.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: thank you
PostPosted: Sat Dec 13, 2008 6:27 pm     Reply with quote

Quote:


I looks to me like you don't need to take explicit control of RB3. You just need to introduce a 180 degree phase shift in the PWM signal being generated.

Recall that PWM works like this: Timer 2 start out at 0 and the PWM output is high. Timer 2 counts up. When the timer reaches the value in the CCPR1H, CCPR1L register pair, then the PWM output switches to low. Timer 2 continues to count up until it reaches PR2, at which time it resets back to 0 and the PWM output goes high again.

So it looks like what you are trying to do is go directly from the end of one high period to the beginning of another high period. That could be accomplished by setting Timer 2 back to 0 just before it would have reached CCPR1, so that the PWM output stays high for another half-period. Then the PWM will continue to run from this point for as many cycles as you want to let it. You can monitor its progress by periodically reading Timer 2.

Similarly, when it is time to switch back to "normal" phase, it looks like you want to go from near the end of a low period directly to the beginning of a low period. So what you can do here is similar to what you did to get into the reversed phase. Monitor Timer 2 and just before it reaches PR2, set it back to a value equal to CCPR1.

All this twidling of Timer 2 while it counting is tricky, but it can be done if you are careful. I would not use any CCS library functions for this part. Just talk directly to the registers involved (using the "#byte" directive). It also depends on how accurate you want the timing to be. It is easier if you will allow a little error. If you let Timer 2 go beyond the point where you were supposed to set it, then the PWM output will change where you didn't want it to change. Setting Timer 2 a little early will make your job easier, but it will also introduce a small timing error, which may or may not matter to you, depending on what you are doing with this shifted waveform.

If you really must change Timer 2 in such a way that no PWM timing errors result, then I suggest that you change Timer 2 several times instead of once. For example, if PR2=1000 and CCPR1=500 (50% duty cycle) then if you want to stay in the first half of the range for 1000 counts instead of 500, then wait until Timer 2 reaches 200, the subtract 100 from it. Repeat the process 5 times. There are other nuances to this method, but hopefully you won't need to pursue it if the small error from the straight-forward method is acceptable. But if you do need absolute timing precision, they I can say more about it.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
ozzie2005



Joined: 11 Dec 2006
Posts: 5

View user's profile Send private message

PostPosted: Sun Dec 14, 2008 3:26 pm     Reply with quote

thanks for your replies guys

Quote:

Are these the only things it does ? Can there be a while() loop in main()
which simply polls the UART with kbhit() and gets the command byte if
available, and then changes the waveform ? If so, it's possible that
you could use the "Compare" module of the CCP to create the waveform.


PCM programmer, this is basically what the PIC does. I can definitely use a while() loop in main(), check for DATA from PC with kbhit() and then change the output of CCP accordingly.
Should I check PR2/TIMER2 constantly to toggle the RB2 output? If so, that means that the PIC will be always checking for
TIMER2/PR2 value indefinitely




Quote:

All this twidling of Timer 2 while it counting is tricky, but it can be done if you are careful. I would not use any CCS library functions for this part. Just talk directly to the registers involved (using the "#byte" directive). It also depends on how accurate you want the timing to be. It is easier if you will allow a little error. If you let Timer 2 go beyond the point where you were supposed to set it, then the PWM output will change where you didn't want it to change. Setting Timer 2 a little early will make your job easier, but it will also introduce a small timing error, which may or may not matter to you, depending on what you are doing with this shifted waveform.


RLScott, accuracy is not really important here. One pulse could be a little bit longer than the other one when these changes
occur. The only thing I would not want to happen is to have a very "small" pulse (high or low) when goes from "normal cycle" a the "toggled cycle" or viceversa.


Guys, I really appreciate your help. With your comments/ideas I have a better understanding on how to tackle this problem.
Now I would start programming the PIC with your ideas.

Once again, thanks a million! If you need to add more information about this project, I would be very appreciated.


thank you!
Ttelmah
Guest







PostPosted: Sun Dec 14, 2008 3:31 pm     Reply with quote

I'd say, add an external XOR gate.
Then one input from PWM, other from a logic line on the PIC. When you want the signal 'toggled', simply set the logic signal from the PIC. When you want to go 'normal', turn it off again.

Best Wishes
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Sun Dec 14, 2008 4:10 pm     Reply with quote

Ttelmah wrote:
I'd say, add an external XOR gate.
Then one input from PWM, other from a logic line on the PIC. When you want the signal 'toggled', simply set the logic signal from the PIC. When you want to go 'normal', turn it off again.

Best Wishes

The trouble with that is unless he toggles the XOR control signal at exactly the right time, he will get a glitch in the output, and it will not look like his diagram.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 14, 2008 4:33 pm     Reply with quote

Quote:
I have a question about PWM. I need to "toggle" the output on few cycles on RB3 pin (PIC16F628) keeping the same duty cycle at 50%.
Once it is done, pwm should run normally again to original settings.

Do you want to tell us what this is for ? Or what device requires this
timing ? It's possible we could come up with a better solution.
Ttelmah
Guest







PostPosted: Mon Dec 15, 2008 3:23 am     Reply with quote

True,
However that can easily be fixed with a latch, so that the signal to the XOR, will toggle at the next falling edge in the PWM stream.
Though he has said that a slightly wider pulse won't matter, he has not said what level of 'glitch' would be acceptable. If a small glitch wouldn't matter, then one possibility would be to have a 'toggle' routine, which clears the CCP interrupt, then sits polling this. When it triggers, it toggles the output line. This would give a few machine cycles 'glitch', but might well be acceptable.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group