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 CCS Technical Support

PIC18F26K20 - 40 KHz PWM with defined # of periods

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



Joined: 29 Aug 2008
Posts: 7

View user's profile Send private message

PIC18F26K20 - 40 KHz PWM with defined # of periods
PostPosted: Thu Jan 28, 2016 4:43 pm     Reply with quote

Hi,

I need to pulse a red laser diode with a fixed number of 40 KHz,
50 % duty pulses. Clock is a 16 MHz Crystal. The modulation input
of the laser driver is connected to the CCP1 pin. Also I can enable the
Laser Power input via RC3.

Timer2 Setup for 40 KHz and 50 % duty is perfect. I can simply switch
on and off the pwm to generate the desired signals.

My problem is that I am not synchronised with the pwm timers if
I simply switch the pwm on and off. The PWM periods sometimes
are not complete.

The idea was to generate an interrupt at begin or end of the pwm
period but I didn´t find a good solution. It would be perfect to trigger
an additional timer with each period so I can watch this and stop the pwm
after a specified number of cycles.

40 KHz is a lot if the interrupt is called permanently. This could be a
no go for the main program. But if the main program stops during laser
transmission, this does not care.

Does anyone have a good idea how to solve this?

Best wishes,
Chris
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jan 28, 2016 6:11 pm     Reply with quote

Change the duty period to either zero of greater than PWM period.
(Choice depends on the output state you want when stopped.)
Current cycle will run to completion then pulses will stop.
It's all in the data sheet.
Should give you what I understand you've asked for.


Mike
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Jan 28, 2016 7:02 pm     Reply with quote

1- connect the PWM output to the input of Timer0 T0CKI input pin.
setup with prescalar as bypassed and counter in 8 bit mode

2 - before you enable the 40khz PWM pulse generator :
a- preload timer0 with a count of 256-(number_of pwm pulses) you want.
ENABLE the timer0 T0IF interrupt handler.
b- inside the TOIF handler -disable the pwm generator
3- start the pwm generator

this will only make ONE INT at pulse count completion, and cancel the generator until you trigger it again-

i can even see how to use another timer( of which there are plenty on this PIC) to decide how often this complete routine wants to execute- if auto repeat of the same pulse count is needed,

making external connections to other functions of your PIC is a powerful way to get the job done.

rinse, repeat as needed .....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 28, 2016 7:22 pm     Reply with quote

Quote:
I need to pulse a red laser diode with a fixed number of 40 KHz,

What is the number of pulses required ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Fri Jan 29, 2016 2:29 am     Reply with quote

Lots of bits already given.

The simplest I think is to use CCP2.

As Mike says, don't start/stop the PWM. Just change it's duty from 0% to the 50% to start sending, and back to 0% to stop (or use 100% if you want the signal to stay high). When you change the PWM duty cycle, this change occurs on the _next_ PWM cycle.

Now your PWM output uses Timer2.
Setup your PWM 'permanently running', but with the duty cycle set to 0% (or 100%).

CCP's in 'capture' or 'compare' mode, use Timer1 or (on your chip), optionally Timer3.

So feed the CCP1 output, back in to the T13CKI pin.

Program Timer1 for 'T1_EXTERNAL'.

Now Timer1, will count the PWM pulses.

Setup CCP2, with 'CCP_COMPARE_INT'.

Now just before you enable the PWM output, load CCP_2, with the number of pulses you require, and set_timer1(0).

Enable the CCP2 interrupt, and start the PWM output (set duty to 50%).
When the CCP2 interrupt occurs, set the duty cycle back to the 'off' state (0% or 100%). On the _next_ cycle this change will occur (so at the end of the current cycle).

Now the only extra complexity, is if you need more than 65535 pulses?. If so, then you have to let the timer wrap a number of times _before_ setting the CCP to interrupt. Have a look at how I generate a servo pulse in this thread:

<http://www.ccsinfo.com/forum/viewtopic.php?t=54739>

Where I count to 100000 on a CCP.
chrhartz



Joined: 29 Aug 2008
Posts: 7

View user's profile Send private message

PostPosted: Sat Jan 30, 2016 11:25 am     Reply with quote

Many thanks for your answers!

I didn´t know that I can set the duty to 0% or 50% to change
the output behaviour of the pwm.

Feeding back the output to a timer input is a nice idea. This gives
more flexibility without side-effects.

Best wishes,
Chris
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