This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

External crystal as main clock

Hi

I have very basic questions on clocks and crystals.

1. Iam using MSP430FG4618 controller.For a particular application ihave to give delay of 30usec.The default crystal connected to the controller is 32KHz. But I have a 12MHz crystal which I want to connect externally at XT2. Ihave connect and used the following piece of code for configuring that.

WDTCTL=WDTPW+WDTHOLD;

_BIS_SR(0x0000);

FLL_CTL0=0xB0;

FLL_CTL1=0x14;

P1DIR=0xFF;

P1SEL=0x10;   // To take SMCLK out.

while(1);

When i run this code  i could see 12MHz frequency in oscilloscope at SMCLK pin. But when i remove the wjhile loop i dont see anything. Are there aby other registers missing or do I have to add any delay loop? Please guide me. And is there any way i can use this as main clock system I mean the code execution time is in such a way that it depends on the external crystal frequency.

2. Of three clocks, SMCLK and ACLK is used as clock sign als for timers and MCLK  is used by CPU and system. Does it mean that MCLK is the default clock using which instructions run, I mean if we toggle a pin using "for" loop, the time for the pin toggling depends on MCLK. If so how can i configure MCLK with XT2 for toggling pins using for loop.

Please guide me and if possible please share me some sample codes if u have any.

  • deepu said:
    _BIS_SR(0x0000);

    This makes no sense. It is settign no bits in the status register. It's basically an OR instruction with a zero value. a NOP. If you want to clear the SR for some reason, the proper instruction woudl be

    _BIC_SR(0xffff);

    deepu said:
    FLL_CTL0=0xB0;

    It's better to use ORed labels than a combined value. 0xB0 is just a value and leaves much room for mistakes and confusion.  And requires looking into the users guide to determine the purpose. Using

    FLL_CTL0 = DCOPLUS | XCAP10PF;

    things are obvious.

    deepu said:
    when i remove the wjhile loop i dont see anything.

    Obvious. What do you think what happens when your program continues past the wgile? It will exit the main function. The program ends. The MCU will stop functioning.

    deepu said:
    Does it mean that MCLK is the default clock using which instructions run

    Not, it is THE clock that lets the CPU core execute the program. The one and only, not just teh default. Whatever you feed into MCLK as oscillation source will determine the processing speed of the cpu. if you don't feed something into MCLK, the CPU will stop.

    deepu said:
    I mean if we toggle a pin using "for" loop, the time for the pin toggling depends on MCLK.

    Yes. And on the compiler and the code it generates from your C instructions. And on the loop variable you use (e.g. if you define it as long, the loop will be slower than with a 'short int' or 'unsigned char').

    deepu said:
    If so how can i configure MCLK with XT2 for toggling pins using for loop.

    Besides teh fact that this kind of pin toggling is definitely the wrong way to do such things (except for occasional trigger pulses), the manipulation of MCLK is quite straight: select a clock source for MCLK (SELMx bits in FLL_CTL1). There may be an additional divider added (which also applies to SMCLK then) if you're using the internal DCO as clock source. Thenclear teh DCOPLUS bit in FLL_CTL0 and select a divider with the FLLDx bits in SCFI0.

    The DCO, however, can be adjusted to some extent. Either directly, by setting DCOx,  MODx and the FN_x bits in SCFI0 and SCFI1  . This allows picking one of 5 frequency ranges, setting the DCO to one of 32 possible values and modulatiing it with a pattern between this and the next higher frequency to get an average between the two (invalid for teh 32th DCO frequency).

    This gives you one of 160 discrete DCO frequencies between 0.65 and 46MHz and up to 32 average, jittering frequencies between (almost) each two of them. It is NOT possible to get any frequency you want.

    The most complex and flexible way (yet not much more complex to handle from software) is using the FLL. There you provide a reference signal (usually a 32768Hz signal on the LFXT1, pick a frequency range using FN_x and program amultiplication factor. The FLL hardware will then count the MODx and DCOx bits up and down so the average frequency is reference multiplied with factor.
    This way you can get an average frequency of almost any value (it must be an integral multiple of the reference) but the clock jitter is even higher. At any given moment, the temporary clock frequency (the clock cycle length) is one of 32 frequencies, almost evenly distributed over the seleted frequency range.

    However, toggling a port pin in the meaning of flashing an LED or providing a PWM pulse should be done by using the timers, which will do such things in hardware. Or by setting up a timer interrupt and doing the toggle int he interrupt service routine. The MSP can sleep in the meantime, saving power, or do other things and forget about the blinking LED.

**Attention** This is a public forum