Forum: Mikrocontroller und Digitale Elektronik PIC18F2550 mit internem Oszillator: immer nur 1MHz?


von Stefan (Gast)


Lesenswert?

Hallo,

ich habe hier einen PIC18F2550, den ich mit dem internen Oszillator 
betreibe.
Laut Datenblatt ist das ein 8 MHz Oszillator der per INTOSC Postscaler 
auf bis zu 31kHz runterskaliert werden kann.
Ich mache gerade meine ersten Schritte mit C (MPLABX und XC8).
Leider bekomme ich den internen Oszillator damit nicht korrekt 
eingestellt.
Wie kann ich denn mit den pragma Anweisungen die Frequenz einstellen?
1
/** Configuration ********************************************************/
2
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port function on RA6, EC used by USB (INTIO))
3
#pragma config PWRT = ON
4
#pragma config BOR = OFF
5
#pragma config WDT = OFF  //Watchdog Timer
6
#pragma config LVP = OFF  //Low Voltage ICSP
7
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)
Mit diesen Einstellungen läuft der PIC immer mit 1MHz.

von Peter C. (peter_c49)


Lesenswert?

Hallo Stefan,

mit den "wenigen" config pragma's wird es wohl (default) 4Mhz ( /4), 
also 1Mhz effectiv bleiben.

Da du XC8 & MPLABX benutzt, wähle in MPLABX -> Window -> PIC Memory 
Views -> Configuration Bits .
in dem Fenster siehst du auch die PLL settings und mehr.
Mit Datanblatt infos und diesen config bits wirst du weiter kommen.

wenn du die config bits dort bearbeitet hast, ctrl-a und copy&paste in 
dein Program bzw dein config header file.

mfG
Peter

: Bearbeitet durch User
von Stefan (Gast)


Lesenswert?

Hallo Peter,

Peter C. schrieb:
> Da du XC8 & MPLABX benutzt, wähle in MPLABX -> Window -> PIC Memory
> Views -> Configuration Bits .
> in dem Fenster siehst du auch die PLL settings und mehr.
genau das habe ich gemacht. Allerdings habe ich alle für mich erstmal 
unwichtigen Einstellungen nicht übernommen.

Nach Datenblatt müsste ich eigentlich das OSCCON-Bits 6 bis 4 auf 111 
setzen. Wie kann ich den das in C machen?

Wenn ich das richtig verstehe komme ich da mit den Configuration Bits 
allein nicht weiter.
Hier die vollständige Liste:
1
// PIC18F2550 Configuration Bit Settings
2
3
// 'C' source line config statements
4
5
#include <xc.h>
6
7
// #pragma config statements should precede project file includes.
8
// Use project enums instead of #define for ON and OFF.
9
10
// CONFIG1L
11
#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
12
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
13
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
14
15
// CONFIG1H
16
#pragma config FOSC = INTOSCIO_EC// Oscillator Selection bits (Internal oscillator, port function on RA6, EC used by USB (INTIO))
17
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
18
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
19
20
// CONFIG2L
21
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT enabled)
22
#pragma config BOR = OFF        // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
23
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
24
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
25
26
// CONFIG2H
27
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
28
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
29
30
// CONFIG3H
31
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
32
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
33
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
34
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR pin disabled)
35
36
// CONFIG4L
37
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
38
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
39
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
40
41
// CONFIG5L
42
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
43
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
44
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
45
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
46
47
// CONFIG5H
48
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
49
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
50
51
// CONFIG6L
52
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
53
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
54
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
55
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
56
57
// CONFIG6H
58
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
59
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
60
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
61
62
// CONFIG7L
63
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
64
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
65
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
66
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
67
68
// CONFIG7H
69
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

von Stefan (Gast)


Lesenswert?

PS: dass ein Zyklus aus 4 Takten besteht habe ich dabei schon 
berücksichtigt. Wenn ich Spruts Beispiel (20MHz externer Takt, 0.25s 
Leuchtzeit LED) direkt übernehme 
(http://www.sprut.de/electronic/pic/c/c18/c18.html#helloworld), dann 
leuchtet meine LED 4s (-> Faktor 20).

von Toxic (Gast)


Angehängte Dateien:

Lesenswert?

Stefan schrieb:
> Wenn ich das richtig verstehe komme ich da mit den Configuration Bits
> allein nicht weiter.

Bei einem Reset wird der interne Oszillator immer auf 1MHz gesetzt und 
muss dann per OSCCON-Register auf den gwuenschten Wert gesetzt werden.
Fuege untenstehende Zeilen ein.Die xxx-Werte(Frequenz) entnimmst Du aus 
dem Anhang oder Datenblatt.

OSCCONbits.IRCF0 = xxx
OSCCONbits.IRCF1 = xxx
OSCCONbits.IRCF2 = xxx

von Peter C. (peter_c49)


Lesenswert?

Hallo Stefan,

danke,jetzt kann man alle pragmas sehen. soweit ich sehe ok.

hier ein auzug von einem PIC18F45k20, sollte ähnlich sein.

    // play on speed
    // 8mhz
    OSCCON = 0x60; //IRCFx = 110
    OSCTUNEbits.PLLEN = 0; // PLL nicht in allen 18Fxxxx vorhanden bei 
FRC!

habe leider keine 2550 beispiele zur hand, meine haben alle Quarz dran 
wegen USB.

im Datenblatt deines 2550 wirst du die werte und erklärung finden.
fange mit IRCF2:IRCF0: Internal Oscillator Frequency Select bits an.
diese im OSCON gemäss deiner wünsche anpassen.
Falls du zeitkritische andere HW aktionen direckt danach machst, im 
OSCON das IOFS bit prüfen ob nach deinen änderungen der neue takt stabil 
ist.

PS. wenn es morgen wieder nur regnet und du nicht vorwärts gekommen 
bist, kann ich mich mal ein meinem 18F2550 versuchen. 18F hab ich schon 
lang nicht mehr benutzt.

mfG
Peter

von Stefan (Gast)


Lesenswert?

Vielen Dank euch beiden!
jetzt funktioniert es so wie es soll.

Wie ich die SFRs per C setzen kann war mir nicht klar. Jetzt da ich 
wusste wonach ich suchen musste, habe ich das auch XC8 im User Guide 
gefunden :-)

Viele Grüße
Stefan

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.