PDA

View Full Version : Setting up a 12F683 to run at 20 Mhz



johnnylynx
- 1st March 2009, 06:56
I'm trying to get the 12F683 to run on an external crystal at 20 mhz...with no luck.

I've played with the OSCON and had it run at 1, 2, 4 and 8 Mhz. The Define command doesn't seem to do this though.

How do I setup the 12F683 for 20 mhz??????:(

Here is some code I'm playing with.


'config_wdt_on
'config_fosc2_off
'config_fosc1_on
'config_fosc0_off
'config_ircf2_on
'config_ircf1_off
'config_ircf0_off
'define osc 20


'cfg %111111111111010
'osccon = %01001000 ' Osc set to 1 MHz
'osccon = %01100000 ' Osc set to 4 MHz
osccon = %01110000 ' Osc set to 8 MHz


trisio = %00011000 '
wpu = %00011000
CMCON0 = 7 ' Analog comparators off
ANSEL = 0 ' Analog select set to digital, pg 69 data
ADCON0 = 0 ' A/D turned OFF, pg 68 of data



pausetime var word
Out0 var gpio.0
Out1 var gpio.1
Out2 var gpio.2
B0 var byte

'input gpio.4

' SET VARIABLES
pausetime = 100
B0 = 0
low 0
low 1
low 2


start:

'button gpio.3,1,10,5,B0,1,notp
if gpio.3 = 1 then goto start

call blink0
call blink2
call blink1


goto start


blink0:
high out0
pause pausetime
low out0
'pause pausetime
return

blink1:
high out1
pause pausetime
low out1
'pause pausetime
return

blink2:
high out2
pause pausetime
low out2
'pause pausetime
return

notp:
goto start

end

mackrackit
- 1st March 2009, 07:35
_HS_OSC
Needs to be used in the configs.
Then
DEFINE OSC 20
needs to be near the top of your code.

Have you read this thread?
http://www.picbasic.co.uk/forum/showthread.php?t=543

johnnylynx
- 1st March 2009, 18:10
_HS_OSC
Needs to be used in the configs.
Then
DEFINE OSC 20
needs to be near the top of your code.

Have you read this thread?
http://www.picbasic.co.uk/forum/showthread.php?t=543

I just looked at the link...Great information! Thanks for that.

I'll try your suggestions above today, really appreciate that too.

Do you know if the crystal and capacitor setup is 'touchy'? I'm trying to paste an image of the setup here...but not sure if you'll see it...

johnnylynx
- 1st March 2009, 19:00
_HS_OSC
Needs to be used in the configs.
Then
DEFINE OSC 20
needs to be near the top of your code.

Have you read this thread?
http://www.picbasic.co.uk/forum/showthread.php?t=543

OK, I tried the above but didn't work.

Trial 1:
- using the default setting of 4 mhz, the LED's flashed at rate 'X'

Trial 2:
- using the osccon = %01110000 (8 mhz)
- The LED's flashed at a rate of '2X'

Trial 3:
- Using the

_hs_osc
Define OSC 20
- the LED flashing rate went back to 'X'

It doesn't seem that the 20 Mhz is working??? Should it not flash at '5X' rate?

Here is what the top end of the code looks like now:




'*************************************
'Configuration Bits

_hs_osc

define osc 20

'osccon = %01001000 ' Osc set to 1 MHz
'osccon = %01100000 ' Osc set to 4 MHz
'osccon = %01110000 ' Osc set to 8 MHz


trisio = %00011000 '
' GPIO.5 output
' GPIO.4 input
' GPIO.3 input
' GPIO.2 output
' GPIO.1 output
' GPIO.0 output

wpu = %00010000
' GPIO.5 Weak pullup disabled
' GPIO.4 Weak pullup enabled
' GPIO.3 Weak pullup disabled
' GPIO.2 Weak pullup disabled
' GPIO.1 Weak pullup disabled
' GPIO.0 Weak pullup disabled

CMCON0 = 7 ' Analog comparators off
ANSEL = 0 ' Analog select set to digital, pg 69 data
ADCON0 = 0 ' A/D turned OFF, pg 68 of data


'***************************************
'Define Variables

pausetime var word
Out0 var gpio.0
Out1 var gpio.1
Out2 var gpio.2
B0 var byte

'***************************************
'Set Variables

pausetime = 100
B0 = 0
low 0
low 1
low 2

Charles Linquis
- 1st March 2009, 19:15
When you set

'DEFINE OSC 20'

You are not "telling" the chip to run at 20MHz, you are telling PBP that
the chip is running at 20MHz (rather than 4MHz), and to adjust all pertinent
timing accordingly.

If the chip is actually running at 4MHz and you have DEFINE OSC 20, all the PBP PAUSES (and a lot of other timing, such as SERIN) will be off by a factor of 5. PBP uses a default define of 4MHz, so if you are actually running at 4MHz, you don't have to change this define. PAUSE 1000 will give a 1 second delay. If the PIC is actually running at 20MHz, then a PAUSE 1000 will give a 200 millisecond delay.
If you are running at any speed other than 4MHz, then you change the define accordingly.

johnnylynx
- 1st March 2009, 19:35
When you set

'DEFINE OSC 20'

You are not "telling" the chip to run at 20MHz, you are telling PBP that
the chip is running at 20MHz (rather than 4MHz), and to adjust all pertinent
timing accordingly.

If the chip is actually running at 4MHz and you have DEFINE OSC 20, all the PBP PAUSES (and a lot of other timing, such as SERIN) will be off by a factor of 5. PBP uses a default define of 4MHz, so if you are actually running at 4MHz, you don't have to change this define. PAUSE 1000 will give a 1 second delay. If the PIC is actually running at 20MHz, then a PAUSE 1000 will give a 200 millisecond delay.
If you are running at any speed other than 4MHz, then you change the define accordingly.

Ah, makes sense.

So in theory, if I use CFG_HS_OSC to get the 12F683 to use the external oscillator on pins 2&3 and use the DEFINE OSC 4 command...then the LED's should blink at the rate of 5X...right?

If so, I just tried that and the rate is still X???

What am I doing wrong?

Better yet...How do I confirm that the chip is running at 20Mhz??

John.

Archangel
- 1st March 2009, 19:59
Read this link: http://www.picbasic.co.uk/forum/showthread.php?t=543
Setting the config statement to HS controls the power supplied to the oscillator to "enable" it to run at high speed. The crystal determines that speed, The Define statement as Charles explained Syncs up the chips internal timing to the supplied crystal and Oscon allows you to alter the oscillator's prescaler in code (more or less ).

johnnylynx
- 2nd March 2009, 00:04
Read this link: http://www.picbasic.co.uk/forum/showthread.php?t=543
Setting the config statement to HS controls the power supplied to the oscillator to "enable" it to run at high speed. The crystal determines that speed, The Define statement as Charles explained Syncs up the chips internal timing to the supplied crystal and Oscon allows you to alter the oscillator's prescaler in code (more or less ).

I'm not sure if you saw the photo I posted above...I'm using 2-27pF capacitors with the crystal. How can I confirm that the chip is running at 20Mhz?

John.

Charles Linquis
- 2nd March 2009, 00:20
Run the program below -

Set the port to output

TOPP:
Toggle LED
PAUSE 1000
GOTO TOPP

If the LED stays on for one second and off for one second, then
you are running the frequency you have set in your DEFINE

For example:

IF have DEFINE OSC 4 -

and the above program turns the LED one second ON and one second OFF
Then you are running at 4MHz

If it is on for 200milliseconds at off for 200 milliseconds. Then you are running
at 20MHz.

johnnylynx
- 2nd March 2009, 02:26
Run the program below -

Set the port to output

TOPP:
Toggle LED
PAUSE 1000
GOTO TOPP

If the LED stays on for one second and off for one second, then
you are running the frequency you have set in your DEFINE

For example:

IF have DEFINE OSC 4 -

and the above program turns the LED one second ON and one second OFF
Then you are running at 4MHz

If it is on for 200milliseconds at off for 200 milliseconds. Then you are running
at 20MHz.



I kinda thought that's what I had to do...just didn't believe it was that simple!

So I did that...even though I've 'told' the chip to run the external crystal and defined osc 4...I'm not blinking @ 200ms :(

Either the crystal isn't oscillating properly...or I should start flippin' burgers;)


BTW: The reason I'm trying to run this 12F683 at 20Mhz is to obtain readings from an accelerometer (aboard an RC aircraft) and control 3 servos all in a relatively smooth way.

In addition, I've bought XBEE 900 Mhz XSC transceivers and will be passing telemetry information from the aircraft to a ground station. I have more Q's about serial communications from the 12F683 to the XBEE...but that's for another thread. I first have to figure out what I don't know.

Cheers,

JOhn.

mackrackit
- 2nd March 2009, 02:54
It does not look like you are setting the config correctly.
The thread that Joe and I pointed you to should have helped.

For now try going to the inc file for this chip in the PBP directory. Below is part of it.
If you are using PM for the assembler change
device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off
to
device pic12F683, hs_osc, wdt_on, mclr_on, protect_off

If you are using MPASM then change
__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
to
__config _HS_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF



device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off

XALL

NOLIST

else

LIST

LIST p = 12F683, r = dec, w = -302

INCLUDE "P12F683.INC" ; MPASM Header

__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF

johnnylynx
- 3rd March 2009, 02:15
It does not look like you are setting the config correctly.
The thread that Joe and I pointed you to should have helped.

For now try going to the inc file for this chip in the PBP directory. Below is part of it.
If you are using PM for the assembler change
device pic12F683, intrc_osc_noclkout, wdt_on, mclr_on, protect_off
to
device pic12F683, hs_osc, wdt_on, mclr_on, protect_off

If you are using MPASM then change
__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
to
__config _HS_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF




Dave, thanks for your help, really appreciate it. I've been spending too many hours (very unproductive ones at that) trying to learn this thing called picbasic, and, as I'm sure you know, after you hit that frustration level...YOU JUST CAN'T GIVE UP! And, of course its down hill from there...Sleep is good, if you can get your mind off the problem.

Anyway,

Here's my Code:


@__config _HS_OSC & _WDT_ON & _MCLRE_ON & _CP_OFF

DEFINE OSC 20


CMCON0 = 7 ' Analog comparators off
ANSEL = 0 ' Analog select set to digital, pg 69 data
ADCON0 = 0 ' A/D turned OFF, pg 68 of data


'***************************************
'Define Variables


'***************************************
'Set Variables


low 0


start:
toggle 0

pause 1000

goto start

end

When I change the DEFINE OSC between 4, 8, 20 it seems to work fine now...but I get compilation errors as follows:


Executing: "C:\PBP\PBPW.EXE" -ampasmwin -oq -z -p12F683 "Blink 12f683 simple.bas"
PICBASIC PRO(TM) Compiler 2.50b, (c) 1998, 2008 microEngineering Labs, Inc.
All Rights Reserved.
Warning[205] E:\USERS\JDROOG\DOCUMENTS\MCS\BLINK 12F683 SIMPLE.ASM 82 : Found directive in column 1. (__config)
Error[118] E:\USERS\JDROOG\DOCUMENTS\MCS\BLINK 12F683 SIMPLE.ASM 82 : Overwriting previous address contents (2007)
Error[118] E:\USERS\JDROOG\DOCUMENTS\MCS\BLINK 12F683 SIMPLE.ASM 82 : Overwriting previous address contents (2007)
Loaded E:\Users\jdroog\Documents\MCS\Blink 12f683 simple.COD.
BUILD SUCCEEDED: Mon Mar 02 19:14:43 2009

What exactly does this mean??

John.

mackrackit
- 3rd March 2009, 02:42
For the error go to the *.inc file in the PBP directory for your chip and comment out this line

__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF

with a ;

The warning is just a warning, you can add this near the top of you code

Asm
ERRORLEVEL -205
Endasm
or put a space between
@ and _
in the config statement in you code.

This stuff is addictive:D

johnnylynx
- 3rd March 2009, 02:46
For the error go to the *.inc file in the PBP directory for your chip and comment out this line

__config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF

with a ;

The warning is just a warning, you can add this near the top of you code

Asm
ERRORLEVEL -205
Endasm
or put a space between
@ and _
in the config statement in you code.

This stuff is addictive:D


It worked. Thanks again. I think I'll read some Tom Clancey now...