View previous topic :: View next topic |
Author |
Message |
javi.ar
Joined: 17 Feb 2006 Posts: 59 Location: Argentina
|
16F1937 PWM no output |
Posted: Sat Jun 01, 2013 4:10 pm |
|
|
Hi
I have been reading and testing for quite a while, but I did not manage to make it work.
I need 120Khz output in CCP1 (PIN _C2)
But no success no matter what I change.
I can blink the led with the classic output_low / output_high commenting out the setup_ccp1(CCP_PWM); line.
I can also blink the led using the timer 2 overflow.
Output for PIN_C2 is constantly high when using the PWM
I have chosen 7.68Mhz in order to have a precise 120Khz output in CCP1 as required by x10 (+- 2%)
I am using version 4.140. Thanks in advance
Code: |
#include <x10_central.h>
void main(){
setup_lcd(LCD_DISABLED);
delay_ms (10);
output_low(PIN_C2); // Set CCP1 output low
delay_ms (10);
setup_ccp1(CCP_PWM);
set_pwm1_duty(30);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_timer_2(T2_DIV_BY_1,15,1); //8.3 us overflow, 8.3 us interrupt
set_timer2 (0);
while(true){
}
}
///////////////////////////////////////////////////////////////////////////
#include <16F1937.h>
#device *=16
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES WDT_SW //No Watch Dog Timer, enabled in Software
#FUSES PUT //Power Up Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOBROWNOUT //No brownout reset
#FUSES PLL_SW //4X HW PLL disabled, 4X PLL enabled/disabled in software
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(int=7680000)
#ZERO_RAM
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jun 01, 2013 4:22 pm |
|
|
It's usual to put all your fuses etc before the code.
However as posted you've asked for a duty which is greater than the period.
Code: | set_pwm1_duty(30);
.............
setup_timer_2(T2_DIV_BY_1,15,1); //8.3 us overflow, 8.3 us interrupt | Could explain why there's no output.
Mike
EDIT How do you get the internal oscillator to run @7M68?
Last edited by Mike Walne on Sat Jun 01, 2013 4:28 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jun 01, 2013 4:26 pm |
|
|
Quote: |
#FUSES INTRC_IO
#use delay(int=7680000)
I have chosen 7.68Mhz
|
You can't do that. The internal oscillator has specific frequencies that
you can select. See this section of the 16F1937 data sheet for a list
of the supported frequencies:
Quote: | 5.2.2.5 Internal Oscillator Frequency Selection |
The #use delay() frequency must be the same as the oscillator frequency.
16F1937 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/41364E.pdf
Quote: |
set_pwm1_duty(30L);
setup_timer_2(T2_DIV_BY_1, 15 ,1); |
If you use a pwm duty cycle value that is greater than the middle value
in setup_timer_2(), then it means you want to use 10 bit pwm mode. In
that case, you have to tell the compiler that you want to use 10-bit mode.
This is done by specifying the pwm duty cycle as a 16-bit value. One way
to do this is to put an 'L' after the number (which mean 'long' int is used,
which means 16-bits). Change your code as shown above in bold. |
|
|
javi.ar
Joined: 17 Feb 2006 Posts: 59 Location: Argentina
|
|
Posted: Sat Jun 01, 2013 6:31 pm |
|
|
Ok, then, I will review items remarked. Thanks a lot |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Jun 02, 2013 2:37 am |
|
|
As a comment. Think again.....
Even if you correct everything in the software, you are in problems till you rethink your hardware. What is the quoted accuracy of the internal clock in the 16F1937?.....
Best Wishes |
|
|
javi.ar
Joined: 17 Feb 2006 Posts: 59 Location: Argentina
|
|
Posted: Sun Jun 02, 2013 8:39 am |
|
|
Thanks for the recomendation. I will first test with a dummy oscillator (high and low the pin 4.1us during 1ms) The main problem is to get 7.68 Mhz crystal oscillator. Thanks a lot for your help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Jun 02, 2013 1:08 pm |
|
|
Don't get hooked on your frequency choice. 15.36MHz, is an off the shelf crystal, and gives you more processing speed, and is an exact multiple of 120K.
Best Wishes |
|
|
javi.ar
Joined: 17 Feb 2006 Posts: 59 Location: Argentina
|
|
Posted: Wed Jun 05, 2013 9:31 pm |
|
|
Finally worked!!!!
Well I tested with 16Mhz internal oscillator and timer2 was within the range, so for testing I will use the internal.
Now regarding the pwm duty with the suggested values provided by the pic wizard were always higher than the duty cycle so was never going down.
Something to take in account for the future.
Now I have to deal with the amplifier and envelope detector in order to see the presence of the 120khz. But that is a different story.
Thanks a lot for your time, help and concern. GREAT HELP. Thanks a lot. |
|
|
|