lpcware

How to use external crystal oscillator lpc11c24

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by emielzij on Tue May 05 07:05:50 MST 2015
I want to use the external crystal with the lpc11c24 for critical timing applications.

I found some code to do that at: https://eewiki.net/display/microcontroller/Getting+Started+with+NXP%27s+LPC11XX+Cortex-M0+ARM+Microcontrollers

The code is implemented as follows:


#include "LPC11xx.h"

int main(void) {
//CONNECT SYSTEM OSCILLATOR TO CLKOUT PIN: This allows measurement with o-scope.
LPC_IOCON->PIO0_1        &= 0xFFFFFFF8;       //clear FUNC bits (sec. 7.4.4)
LPC_IOCON->PIO0_1        |= 0x01;             //set FUNC bits to CLKOUT function (sec. 7.4.4)
LPC_GPIO0->DIR           |= 0x01;             //set CLKOUT pin to output (sec. 12.3.2)
LPC_SYSCON->CLKOUTCLKSEL = 0x3;               //sec. 3.5.21
LPC_SYSCON->CLKOUTUEN    = 0;                 //"CLKOUTUEN" has to be toggled for "CLKOTUCLKSEL" to accept the value written to it
LPC_SYSCON->CLKOUTUEN    = 1;                 //"CLKOUTUEN" has to be toggled for "CLKOTUCLKSEL" to accept the value written to it
LPC_SYSCON->CLKOUTDIV    = 1;                 //set divider to 1 (sec. 3.5.23)


//USE EXTERNAL CRYSTAL TO GENERATE INTERNAL SYSTEM CLOCK
    LPC_SYSCON->SYSAHBCLKDIV             = 0x1;       //set clock divider for core to 1
    LPC_SYSCON->MAINCLKSEL               &= ~(0x03);  //set “main clock” to IRC oscillator, if not system will lock up when PLL turns off!(sec. 3.5.11)
    LPC_SYSCON->MAINCLKUEN               &= ~(1);     //write a zero to the MAINCLKUEN register (sec. 3.5.12), necessary for MAINCLKSEL to update
    LPC_SYSCON->MAINCLKUEN               |= 1;        //write a one to the MAINCLKUEN register (sec. 3.5.12), necessary for MAINCLKSEL to update
    LPC_SYSCON->SYSPLLCLKSEL             = 0x01;      //connect system oscillator to SYSTEM PLL (sec. 3.5.9)
    LPC_SYSCON->SYSPLLCLKUEN             &= ~(1);     //write a zero to SYSPLLUEN register (sec. 3.5.10), necessary for SYSPLLCLKSEL to update
    LPC_SYSCON->SYSPLLCLKUEN             |= 1;        //write a one to SYSPLLUEN register (sec. 3.5.10), necessary for SYSPLLCLKSEL to update
    LPC_SYSCON->PDRUNCFG                 |= (1<<7);   //power down the PLL before changing divider values (sec 3.5.35)
    LPC_SYSCON->SYSPLLCTRL               = 0x23;      //set MSEL = 0x00011 and PSEL = 0x01 (sec 3.5.3 and table 46 of sec. 3.11.4.1)
    LPC_SYSCON->PDRUNCFG                 &= ~(1<<7);  //power up PLL after divider values changed (sec. 3.5.35)
    while((LPC_SYSCON->SYSPLLSTAT & 1) == 0);         //wait for PLL to lock
    LPC_SYSCON->MAINCLKSEL               = 0x03;      //set system oscillator to the output of the PLL (sec. 3.5.11)
    LPC_SYSCON->MAINCLKUEN               &= ~(1);     //write a zero to the MAINCLKUEN register (sec. 3.5.12), necessary for MAINCLKSEL to update
    LPC_SYSCON->MAINCLKUEN               |= 1;        //write a one to the MAINCLKUEN register (sec. 3.5.12)

     LPC_IOCON->PIO0_7           &= ~(0x10);    //NOT REQUIRED, turn off pull up (sec 7.4.19)
     LPC_GPIO0->DIR              |= (1<<7);     //set pin direction to output (sec 12.3.2)
     unsigned int i = 0;

     while(1){                      //infinite loop

          LPC_GPIO0->DATA           |= (1<<7);    //set pin high (sec 12.3.1)
          for(i=0; i<0xFFFFF; ++i);               //arbitrary delay
          LPC_GPIO0->DATA           &= ~(1<<7);   //set pin low (sec 12.3.1)
          for(i=0; i<0xFFFFF; ++i);               //arbitrary delay

     }
      return 0 ;
}


But the led is not blinking if this code is running. Also there is no clock signal on the 0_1 pin.
If the part of the code where the external crystal is configured is exclude from the code, the led blinks normally and I measure the correct clock frequency of 48MHz at PIO0_1.

Outcomes