chr-mt
02.05.2010, 02: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
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