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

38kHz PWM

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

38kHz PWM
PostPosted: Sun Dec 25, 2016 9:26 pm     Reply with quote

Hi,

I'm trying to make a 38kHz PWM using #use delay(clock=48000000)
However, based on the formula : (1/clock)*4*t2div*(period+1) ,
i used (period+1)=315 and get 38.095kHz. Is it "period" can be any number larger than 0 ?

What should i do to get a PWM with exact value of 38kHz?

Code:


#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)


void main(void)
{

   /* PWM EXAMPLE */

   char selection;
   unsigned int8 value;

   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM

          //   The cycle time will be (1/clock)*4*t2div*(period+1)
          //   In this program clock=48000000 and period=127 (below)
         
          //     (1/48000000)*4*1*315= 26.25 us or 38.095 kHz


    setup_timer_2(T2_DIV_BY_1, 315, 1);
   
   

  setup_port_a(ALL_ANALOG);
  setup_adc(adc_clock_internal);
  set_adc_channel( 0 );
  printf("%c\r\n",selection);

  while( TRUE ) {
    value=read_adc();

    printf("%2X\r",value);

    set_pwm1_duty(value);         
  }

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 2:52 am     Reply with quote

You don't.

It's simple integer maths. You can only get 'exactly' (beware of course that this is itself impossible - even the best atomic clocks have an error), a particular frequency if it divides as an integer without remainder into the master Fosc/4 value. 12000000/38000 = 315.789. So can't be done.
The PR1 value can only be 0 to 255. Nearest you can get is:

Prescaler 4
PR1 = 78 (/79)

Gives 12MHz/(4*79) = 37974.68 Hz.

So less that 0.06% error.

setup_timer_2(T2_DIV_BY_4, 78, 1);

Comment - don't use ADC_CLOCK_INTERNAL. This is not recommended for use above 1Mhz.
temtronic



Joined: 01 Jul 2010
Posts: 9162
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 6:56 am     Reply with quote

comment.

You need to study/read about the other 38KHz device to see how accurate or stabile it is. The Internet and Google are great at finding this important information.
38KHz sounds like a remote control device, maybe an US xducer ? If the device has a spec of say '38KHZ, +-1%' then the math that Mr. T did is well within the 'spec' required for proper operation.
Also in those specs will be the duty cycle or pulse width specs.Those ARE very important. Get that out of spec and even though you've got 38,000.000 Hz the devices will never reliably communicate.

Jay
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 7:39 am     Reply with quote

Dear Ttelmah and Jay,

Thank you very much for the technical guidance.
Actually i'm trying to make RC for SHARP LCD TV which are using 38kHz frequency. But the actual spec +-% still no idea. Need to google it for more information
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 7:51 am     Reply with quote

If you look at the receiver IC's Sharp sell for their 38KHz IR, they have a 36 to 41KHz pass band. They are low accuracy, because most remote controls don't even bother using a crystal.
benoitstjean



Joined: 30 Oct 2007
Posts: 553
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 8:24 am     Reply with quote

You can also change your crystal to whatever is available from your preferred electronics distributor (e.g. DigiKey)...

Didn't look at the specs for your PIC but is it possible that 48MHz is too high?

Also, unless there's something I'm not understanding, your value of 315 is not possible since the <period+1> can only be a maximum of 255. You should have used <mode=2> and <period+1 = 157> but that will also calculate to 37.974kHz.

Anyhow, do the calculations the other way around, perhaps use Excel to do a multitude of automatic calculations and check what crystal frequencies will give you 38Khz. For instance, <mode=1>, <period=127> gives a 19.456MHz oscillator for perfect 38kHz. Not sure, however, if 19.456MHz is available.

Of if you do like <mode = 2> and <period+1 = 64>, then the oscillator calculates to 19.76MHz.

Ben
temtronic



Joined: 01 Jul 2010
Posts: 9162
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 9:18 am     Reply with quote

Ben's got the perfect solution, working 'backwards'. I did that 30 years ago to get a perfect 24Hz clock for the energy control system. Ended up using three 4000 series CMOS and a 2.457600 MHz xtal.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Mon Dec 26, 2016 9:29 am     Reply with quote

He can't do that if he wants to use USB.

Problem is that ideally you'd choose a frequency that was a multiple of 38000. So (perhaps) something like 45.6Mhz. However to support USB, the master crystal has to be a multiple of 4MHz, so you can't do this.
Honestly though 0.06% is so close that even the most 'picky' IR system is not going to worry about this.
The idea that the timings have to be anything better that 'reasonably close' is wrong. You can even get close enough with a basic RC oscillator.
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