PDA

View Full Version : Wrong Frequency



gadelhas
- 4th December 2010, 12:27
Hi Everyone;

I've got this code, and it's working fine, however the frequency of the pwm mesured with the oscilocope it's half the frequency configured.
It is suposed to have 5Khz, however i've got 2.5Khz.

Can anybody tell me what i'm doing wrong in the code?

PIC12F683

Define OSC 8
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF


' PINOUT 1 = IN; 0 = OUT
' ================================================== ==================
'76543210
TRISIO = %00000001

'76543210
GPIO = %00000000

ANSEL = 1 'GP0 ANALOG
VRCON = 0 'VREF OFF
CMCON0 = 7 'COMPARATOR OFF
ADCON0 = %10000000 'RIGHT JUSTIFY - 128

PR2 = %00011000 'SET 24 - 5000Hz - 8Bits
T2CON = %00000110 'TIMER2 ON - PRESCALER 1:16 - Max Duty Value 100 - 1% Step
CCPR1L = %00000000
CCP1CON = %00001100 'PWM ON


Thanks

aratti
- 4th December 2010, 12:52
You have to set the OSCCON register, since by default is set to 4 MHz and you define the osc as an 8 MHz one.

Cheers

Al.

gadelhas
- 4th December 2010, 13:07
Thank you aratti for the fast answer to my problem. I changed the OSCCON register, and it worked at the desired frequency.

Thank you once again!

rsocor01
- 5th December 2010, 03:31
Hmmm :confused:, I thought the DEFINE OSC x takes care of the frequency registers. I'm just curious, will it work if the internal oscillator is defined first and then the frequency is defined after that? In the next code, I just swapped the order of the first two lines in gadelhas original code.


@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF

Define OSC 8

' PINOUT 1 = IN; 0 = OUT
' ================================================== ==================
'76543210
TRISIO = %00000001

'76543210
GPIO = %00000000

ANSEL = 1 'GP0 ANALOG
VRCON = 0 'VREF OFF
CMCON0 = 7 'COMPARATOR OFF
ADCON0 = %10000000 'RIGHT JUSTIFY - 128

PR2 = %00011000 'SET 24 - 5000Hz - 8Bits
T2CON = %00000110 'TIMER2 ON - PRESCALER 1:16 - Max Duty Value 100 - 1% Step
CCPR1L = %00000000
CCP1CON = %00001100 'PWM ON

Robert

mackrackit
- 5th December 2010, 11:15
The configs and the OSCCON register if using an internal OSC is for setting the hardware.
The PIC will then run at that speed. Run at the speed of the external OSC or the internal setup.

All
DEFINE OSC X
does is to tell PBP how fast the chip is running. DEFINE OSC X does not set the chips speed.

Because the two are completely different things the order does not matter.

rsocor01
- 6th December 2010, 02:19
The configs and the OSCCON register if using an internal OSC is for setting the hardware.
The PIC will then run at that speed. Run at the speed of the external OSC or the internal setup.

All
DEFINE OSC X
does is to tell PBP how fast the chip is running. DEFINE OSC X does not set the chips speed.

Because the two are completely different things the order does not matter.

Ooh, I see. Dave thank you for your help.

Robert