PDA

View Full Version : Setting up the oscillator



J_norrie
- 2nd October 2007, 11:15
Hi there everyone, I have a problem setting up the external oscillator on the following pics
16f876a and the 18f2220

I have programmed both with the same simple counter led display program.
i wanted it to run faster to give me some processing time so i thought all i would need was a faster crystal which didn't work on the 16f876a so I thought I would try a 18f2220 with the original 4mh crystal or higher frequency. when i tried the program in the faster chip it ran dead slow 1 count per sec instead of the 100 per sec with the 16f876a with 4mhz crystal.

Am I using the internal osc?
what registers do i need to change?

here is the code used for both pic's

PICBASIC PRO program to demonstrate 7-segment LED display. Schematic
' can be found at http://melabs.com/resources/articles/ledart.htm (fig 6).

DEFINE OSC 4
Segments Var PORTB
Digits Var PORTC

i Var Byte
n Var Byte
Value Var Word


TRISB = $80 ' Set segment pins to output
TRISC = $f0 ' Set digit pins to output

mainloop:
For Value = 0 To 9999
GoSub display ' Display the value
Next Value

GoTo mainloop ' Do it forever


' Subroutine to send the number (0 - 9999) in Value to LEDs
display:
For i = 0 To 3 ' Loop through 4 digits
n = Value Dig i ' Get digit to display
GoSub display1 ' Display the digit
Pause 1 ' Leave it on 1 millisecond
Next i ' Do next digit
Return


' Surboutine to display one digit on LED
' i = digit number
' n = number to display
display1:
Digits = $ff ' All digits off to prevent ghosting

' Convert binary number in n to segments for LED
Lookup n, [$40, $79, $24, $30, $19, $12, $02, $78, $00, $18], Segments

' Set digit pin i to 0 (on) and the rest of the pins to 1 (off)
Digits = ~Dcd i

Return

thanks in advance John

mister_e
- 2nd October 2007, 14:53
If you want to use a faster crystal speed, let's say 20 MHz, you also need to alter the configuration fuses before programming your PIC. The most useful way, at least to me, is to set them in your code.

For the 16F876a, make sure the part number finish with -20, unless it's possible that it doesn't work... even if many here, including me already tried it with nice results.

To use any crystal value >4MHz, you need to set the OSC mode to HS. I suggest you to read the following thread... at VERY LEAST the first 5 posts.
http://www.picbasic.co.uk/forum/showthread.php?t=543

Now, for a 20MHz crystal and for a 16F876a, using MPASM, your code header should looks like


@ __config _HS_OSC & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _LVP_OFF & _CP_OFF & _DEBUG_OFF

DEFINE OSC 20

HTH