View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
38kHz PWM |
Posted: Sun Dec 25, 2016 9:26 pm |
|
|
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);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19367
|
|
Posted: Mon Dec 26, 2016 2:52 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Mon Dec 26, 2016 6:56 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Mon Dec 26, 2016 7:39 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19367
|
|
Posted: Mon Dec 26, 2016 7:51 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
benoitstjean
Joined: 30 Oct 2007 Posts: 553 Location: Ottawa, Ontario, Canada
|
|
Posted: Mon Dec 26, 2016 8:24 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Mon Dec 26, 2016 9:18 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19367
|
|
Posted: Mon Dec 26, 2016 9:29 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|