PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : XMega Clock,PLL, Prescaler einstellen in Bascom Demo



chr-mt
02.05.2010, 01:54
Hi,
vielleicht hilft es dem einen oder anderen beim Start.
Getestet auf XPlain Board.
Ist nicht viel Code, aber viel Registerbeschreibung :)
Ich habe die Register direkt gesetzt, man braucht also keine xmega.lib!

Zum testen habe ich den internen 2 MHz Oszillator auf die PLL gegeben und mit 16 multipliziert.
Das heißt: 2 MHz rein, 32 MHz raus.

(der Code ist nach einer Frage im MCS-Forum entstanden, deshalb sind die Kommentare auf englisch) :)





$regfile = "xm128a1def.dat"

$crystal = 32000000 '
Ddre = &B11111111 'All Pins on PortE as Output

Dim A As Byte '

'If we use a Crystal we have to set up Frequency Range and Startup Time first
'If we use a external Clock we have to write the XOSCSEL Bit
'*************** XOSCCTRL = XOSC Register (Adress &H52) **********************
'* *
'* 7 6 Bits 7-6 FRQRANGE Crystal Frequency Range *
'* 0 0 04TO2 0.4 MHz to 2 MHz *
'* 0 1 2TO9 2 MHz to 9 MHz *
'* 1 0 9TO12 9 MHz to 12 MHz *
'* 1 1 XOSC External Oszillator *
'*-----------------------------------------------------------------------------*
'* BIT 5 32.768 Crystal Oscillator Low Power Mode *
'* 0 = OFF 1 = Low Power *
'*-----------------------------------------------------------------------------*
'*-----------------------------------------------------------------------------*
'* BIT 4 Reserved, always write to zero *
'*-----------------------------------------------------------------------------*
'* 3 2 1 0 XOSCSEL External Oscillator Select *
'* 0 0 0 1 EXTCLK External Clock *
'* 0 0 1 0 32KHZ 32.768 TOSC Startup Time 16K CLK *
'* 0 0 1 1 XTAL_256CLK 0-16 MHz Crystal Startup Time 256K CLK*
'* 0 1 1 1 XTAL_1KCLK 0-16 MHz Crystal Startup Time 1K CLK*
'* 1 0 1 1 XTAL_16KCLK 0-16 MHz Crystal Startup Time 16K CLK*
'************************************************* ******************************

'For Example, if we use 4MHz Crystal:
'Out &H52 , &B01101011 ' Freq 2 to 9 MHz, Startup Time 16K Clocks, 32KHz Osc in low power Mode

'In this Example we use the internal 2MHz Oscillator

'**** CTRL = Oscillator Control Register (Adress &H50) bits 7-3 Reserved *******
'* Enables or disables Oscillators or PLL *
'* *
'* Bit 0 = RC2MEN 2MHz internal *
'* Bit 1 = RC32MEN 32MHz internal *
'* Bit 2 = RC32KEN 32KHz internal *
'* Bit 3 = XOSCEN external Oscillator *
'* Bit 4 = PLLEN PLL *
'************************************************* ******************************
'Out &H50 , &B00000010 ' enable 32MHz Oscillator
'Out &H50 , &B00000001 ' enable 2MHz Oscillator
'Out &H50 , &B00000011 ' enable 32MHz AND 2MHz Oscillators
'Out &H50 , &B00001000 ' enable external Oscillator

Out &H50 , &B00000001 ' enable 2MHz Oscillator

'*************** PLLCTRL = PLL Control Register (Adress &H55) *****************
'* *
'* 7 6 Bits 7-6 PLLSCR PLL Clocksource *
'* 0 0 RC2MHz 2MHz internal *
'* 0 1 Reserved *
'* 1 0 RC32MHz 32MHz internal *
'* 1 1 XOSC External Oszillator *
'*-----------------------------------------------------------------------------*
'* BIT 5 Reserved, always write to zero *
'*-----------------------------------------------------------------------------*
'* 4 3 2 1 0 PLLFAC Pll Multiplication Factor *
'* 0 0 0 0 1 Multiplication x 1 *
'* 0 0 0 1 0 Multiplication x 2 *
'* 0 0 0 1 1 Multiplication x 3 *
'* 0 0 1 0 0 Multiplication x 4 *
'* 0 0 1 0 1 Multiplication x 5 *
'* 0 0 1 1 0 Multiplication x 6 *
'* 0 0 1 1 1 Multiplication x 7 *
'* 0 1 0 0 0 Multiplication x 8 *
'* 0 1 0 0 1 Multiplication x 9 *
'* 0 1 0 1 0 Multiplication x 10 *
'* 0 1 0 1 1 Multiplication x 11 *
'* 0 1 1 0 0 Multiplication x 12 *
'* 0 1 1 0 1 Multiplication x 13 *
'* 0 1 1 1 0 Multiplication x 14 *
'* 0 1 1 1 1 Multiplication x 15 *
'* 1 0 0 0 0 Multiplication x 16 *
'* 1 0 0 0 1 Multiplication x 17 *
'* 1 0 0 1 0 Multiplication x 18 *
'* 1 0 0 1 1 Multiplication x 19 *
'* 1 0 1 0 0 Multiplication x 20 *
'* 1 0 1 0 1 Multiplication x 21 *
'* 1 0 1 1 0 Multiplication x 22 *
'* 1 0 1 1 1 Multiplication x 23 *
'* 1 1 0 0 0 Multiplication x 24 *
'* 1 1 0 0 1 Multiplication x 25 *
'* 1 1 0 1 0 Multiplication x 26 *
'* 1 1 0 1 1 Multiplication x 27 *
'* 1 1 1 0 0 Multiplication x 28 *
'* 1 1 1 0 1 Multiplication x 29 *
'* 1 1 1 1 0 Multiplication x 30 *
'* 1 1 1 1 1 Multiplication x 31 *
'************************************************* ******************************

Out &H55 , &B00010000 'PLL Clocksource = 2MHz internal Osc.
' ,Multiplicator = 16 to get 32 MHz Out


' Some Registers are protected by the "Configuration Change Protect Mechanism" (CCP Register)
' We have to disable that Protection, then write to the Register within 4 clockcycles

'*********** Configuration Change Protection Register (Adress &H34) ************
'* &H9D SMP Protected SPM/LPM *
'* &HD8 IOREG Protected IO Register *
'************************************************* ******************************


Out &H34 , &HD8 'No write protection for 4 cycles '

'We can use 3 different Prescalers
'************* PSCTRLSystem Clock Prescaler Register (Adress &H34) ************
'* 7 6 5 4 3 2 1 0 (Bit7 Reserved, write to zero) *
'* R 0 0 0 0 0 PSADIV Prescaler A Div Div by 1 (NoDiv) *
'* R 0 0 0 0 1 PSADIV Prescaler A Div Div by 2 *
'* R 0 0 0 1 1 PSADIV Prescaler A Div Div by 4 *
'* R 0 0 1 0 1 PSADIV Prescaler A Div Div by 8 *
'* R 0 0 1 1 1 PSADIV Prescaler A Div Div by 16 *
'* R 0 1 0 0 1 PSADIV Prescaler A Div Div by 32 *
'* R 0 1 0 1 1 PSADIV Prescaler A Div Div by 64 *
'* R 0 1 1 0 1 PSADIV Prescaler A Div Div by 128 *
'* R 0 1 1 1 1 PSADIV Prescaler A Div Div by 256 *
'* R 1 0 0 0 1 PSADIV Prescaler A Div Div by 512 *
'* Bits 1 0 PSBCDIV Prescaler B and C *
'* 0 0 B=1 (no division) C =1 (no division) *
'* 0 1 B=1 (no division) C Div by =2 *
'* 1 0 B=4 Div by 4 C =1 (no division) *
'* 1 1 B=2 Div by 2 C = 2 Div by =2 *
'************************************************* ******************************

'Out &H41 , &B00001100 'Prescaler= Prescaler A=4, B =0, C =0
Out &H41 , &B00000000 'Prescaler= No division Prescaler A, B and C

'**** STATUS Oscillator Status Register (Adress &H51) bits 7-5 Reserved ********
'* Indicates Ready Flags when Oscillator is stable *
'*-----------------------------------------------------------------------------*
'* 4 3 2 1 0 Bits 4-0 *
'* 1 0 0 0 0 PLLRDY, PLL has locked to PLL Clocksource *
'* 0 1 0 0 0 XOSCRDY. External Clock Source ready *
'* 0 0 1 0 0 RC32KRDY internal 32KHz Oscillator ready *
'* 0 0 0 1 0 RC32MRDY internal 32MHz Oscillator ready *
'* 0 0 0 0 1 RC2MRDY internal 2MHz Oscillator ready *
'************************************************* ******************************

While A.0 = 0 'Wait for 2MHz Osc. to be ready
A = Inp(&H51)
Wend

'Again, we use the CTRL = Oscillator Control Register like at the beginning,
'to additional enable the PLL, (2MHz Oscillator, Bit 0 is still enabled),

Out &H50 , &B00010001 ' enable PLL

'Now we wait until the PLL is locked

While A.4 = 0 'Wait for PLL to be ready
A = Inp(&H51)
Wend

'Multiplication Factor = 16, so we should get 32MHz Clock
'**************** OSCCTRL = System Clock selection (Adress &H40) ***************
'* 2 1 0 Bits 2-0 *
'* *
'* 0 0 0 RC2MHz 2MHz internal *
'* 0 0 1 RC32MHz 32MHz internal *
'* 0 1 0 RC32KHz 32KHz internal *
'* 0 1 1 XOSC External Oszillator *
'* 1 0 0 PLL PLL *
'************************************************* ******************************
Out &H34 , &HD8 'No write protection for 4 cycles '
Out &H40 , &B00000100 'Select PLL as system Clock

'***************** LOCK Clock System Lock Register (Adress &H42) ***************
'* 7 6 5 4 3 2 1 0 (R= Reseved, write to zero) *
'* R R R R R R R 0 = No Lock *
'* R R R R R R R 1 = Locked *
'************************************************* ******************************
Out &H34 , &HD8 'No write protection for 4 cycles '
Out &H42 , &B00000001 'Lock System Clock Settings

'
Porte = &B00001111 'indicate init done
Wait 1

Do
Toggle Porte
Waitms 1000
Loop
End

Macht richtig Spaß mit den Prescalern und der PLL zu spielen :)

Gruß
Christopher

Kampi
02.05.2010, 08:58
Aber das kannst du doch nur mit einer Vollversion benutzen und nicht in der Demo oder?

chr-mt
02.05.2010, 10:45
Hi Kampi,

Aber das kannst du doch nur mit einer Vollversion benutzen und nicht in der Demo oder?
Doch, das geht. Wie schon geschrieben, ist dazu die xmega.lib nicht notwendig, da die Register "zu Fuß" gesetzt werden !

hier nochmal das Ganze ohne die zusätzlichen Kommentare, ist vielleicht etwas übersichtlicher.

$regfile = "xm128a1def.dat"

$crystal = 32000000 '
Ddre = &B11111111 'All Output

'************** config Clock 2MHz internal, PLL x16 ****************************
Dim A As Byte '
Out &H50 , &B00000001 ' enable 2MHz Oscillator
Out &H55 , &B00010000 'PLL Clocksource = 2MHz internal Osc.
Out &H34 , &HD8 'No write protection for 4 cycles '
Out &H41 , &B00000000 'Prescaler= No division Prescaler A, B and C
While A.0 = 0 'Wait for 2MHz Osc. to be ready
A = Inp(&H51)
Wend
Out &H50 , &B00010001 ' enable PLL
While A.4 = 0 'Wait for PLL to be ready
A = Inp(&H51)
Wend
Out &H34 , &HD8 'No write protection for 4 cycles '
Out &H40 , &B00000100 'Select PLL as system Clock
Out &H34 , &HD8 'No write protection for 4 cycles '
Out &H42 , &B00000001 'Lock System Clock Settings

'
Porte = &B00001111 'indicate init done
Wait 1

Do 'Blink
Toggle Porte
Waitms 1000
Loop
End


Gruß
Christopher

chr-mt
20.05.2010, 20:16
Hi,
habe mal etwas Overclocking versucht.
Die CPU läuft bei mir bis 50MHz, darüber ist Schluss.
Was die interne Peripherie dazu sagt, oder wie lange der Chip das aushält , ist eine andere Frage. :-b
Ausprobieren auf eigene Gefahr! =;

Es wird mit einem 4MHz Quarz ein 100MHz Takt über die PLL erzeugt und dann über den Prescaler C auf 50MHz CPU Takt geteilt.



$regfile = "xm128a1def.dat"
$crystal = 50000000 'Für Bascoms interne Berechnung der Wartezeiten, etc.

Ddre = &B11111111 'PortE alles auf Output konfigurieren

Dim A As Byte '

' 4MHz Quarz an PortR0 und R1, PLL mit Multiplikationsfaktor 25
' ergibt 100MHz Takt für 2 x Speed tolerante Peripherie (clk2per)
' der CPU Takt wird mit Prescaler C DIV 2 auf 50MHz gebracht.

Out &H52 , &B01101011 ' Freq 2 bis 9 MHz, Startup Time 16K Takte, 32KHz Osc in low power Mode
Out &H50 , &B00001000 ' Externen Oszillator einschalten
Out &H55 , &B11011001 ' Takt für die Pll = externer Oszillator (die 4MHz) Multiplikator = 25
Out &H34 , &HD8 ' Register Schreibschutz für 4 Zyklen aufheben '
Out &H41 , &B00000001 ' Prescaler A und B werden nicht verwendet (:1), Prescaler C teilt durch 2
' Vor dem Prescaler C 100 MHz Peripherietakt, dahinter 50 MHz CPU Takt
While A.3 = 0 ' Warte bis Ext Oszillator stabil
A = Inp(&H51)
Wend
Out &H50 , &B00011000 ' jetzt die PLL dazuschalten

While A.4 = 0 ' und warten, bis sie stabil ist
A = Inp(&H51)
Wend

Out &H34 , &HD8 ' Register Schreibschutz für 4 Zyklen aufheben '
Out &H40 , &B00000100 ' PLL als Systemtakt auswählen
Out &H34 , &HD8 ' Register Schreibschutz für 4 Zyklen aufheben '
Out &H42 , &B00000001 ' weitere Einstellungen sperren

Do ' Port toggeln,
' (nur damit ich auf dem Frequenzzähler kontrollieren konnte, was sich verändert)
Porte = 0 ' 32 MHz CPU = ca. 3.5 MHz auf PortE
Porte = 255 ' 50 MHz CPU = ca. 5.5 MHz auf PortE , scheint also zu funktionieren :)
Loop
End


Gruß
Christopher

Willa
29.11.2021, 14:54
Hallo 2010, danke für diese Erklärung, sie wird sicherlich noch einigen Leuten helfen die per Google suchen. Sie hat mir eben auch wieder sehr geholfen! Bei mir laufen mehrere xmega16A4 seit langer Zeit problemlos auf 64 MHz mit 16 Mhz Quarz.


$regfile = "xm16A4def.dat"
$crystal = 64000000
$hwstack = 256
$swstack = 256
$framesize = 256
$lib "xmega.lib"
Dim A As Byte
Out &H52 , &B10101011
Out &H50 , &B00001000
Out &H55 , &B11000100
Out &H34 , &HD8

Out &H41 , &B00000000

While A.3 = 0
A = Inp(&H51)
Wend
Out &H50 , &B00011000
While A.4 = 0
A = Inp(&H51)
Wend
Out &H34 , &HD8
Out &H40 , &B00000100
Out &H34 , &HD8
Out &H42 , &B00000001


Auch der 128A1 läuft gut auf 64 Mhz (da habe ich aber keine Langzeiterfahrung).