RSS

Clocks and MSP430

05 Oct

hi there everyone,

in this tutorial let us check out the all clocks that msp has got to offer us.After studying 8051 and comparing that to controllers like msp and AVR makes me wonder how much development has taken place over the years.The 8051 controller had just one clock.It was external with maximum range of 16Mhz.If it failed then the controller would fail.So what makes msp better.It has 2 built in clocks and one external.Some have a provision for 2 external clocks and th series 5xx and 6xx have dedicated clock built in for Real time Clock facility.When i first read through i was wondered.What do you do with all these clocks?Do we really need it?

So I read through and yeah we do  need it whn we are thinking of very low power consumption.The MSP430 has usually 3 clock sources.

  1. LFTXT1      or   External low frequency oscillator (usually 32768 Hz but can have a max of 16Mhz.)
  2. DCO            or   Internal Digitally Controlled Oscillator stabilized with Frequency Locked Loop.
  3. VLO            or   Internal  Very Low Power Very Low Frequency Oscillator.

Some of the controllers have a XT2 oscillator which is a external high frequency oscillator.And as mentioned the 5xxx and 6xxx series have a REFOCLK which is stabalized 32768Hz clock internal to the chip to be used in place of the LFTXT1.It consumes lesser power than the LFTXT1.

These Clocks source 4 Clock signals.They are

  1. MCLK       or    Master Clock.
  2. SMCLK     or   Sub-Master Clock.
  3. ACLK        or   Auxiliary Clock.
  4. ACLKn      or  Auxiliary Clock divided by n.

The different registers used to modify the Clocks are shown below.These were taken from the user guide provided by ti.


The Master Clock signal is Used to source the CPU.So the speed of the CPU depends on the MCLK and this signal is switched off during LPM0.By default in the absence of External crystal the DCO sources the MCLK.

The DCOCTL,The range select bits in BCSCTL1 and BCSCTL2 are used to configure the MCLK.

Now let us see how to control the DCO frequency.The DCOx and RSELx are together used to vary the DCO frequency from about few hundreds of hertz to 16Mhz.The MODx bits are used to obtain the intermediate frequency.The SELMx and DIVMx are used to select the source for the MCLK signal and division factor.The following code encompasses the above.

#include "msp430x20x2.h"
int main (void)
{
  WDTCTL=WDTPW+WDTHOLD;               //hold watchdog
  P1DIR=BIT0+BIT6;                   
  P1IE=BIT3;                          //enable interrupts
  int i=0;
  _EINT();            
  while(1)
    {
    for(i=0;i<30000;i++);             //toggle LED1 at PIN 0
    P1OUT^=BIT0;
    }
}

#pragma vector=PORT1_VECTOR           //ISR for PIN 3
__interrupt void pin3 (void)
{
static int range=0,dco=0;             
dco++;                                //increment the dco step
dco=dco%8;
range=range%3;
switch(range)                         // step up range every 8 clicks of S2 switch
  {
  case 2:BCSCTL1=RSEL0+RSEL1+RSEL2+RSEL3;
         break;
  case 1:BCSCTL1=RSEL0+RSEL1+RSEL2;
         break;
  case 0:BCSCTL1&=~(RSEL0+RSEL1+RSEL2+RSEL3);
         break;
  }

switch(dco)                         //step up speed every click
  {
  case 0:DCOCTL&=~(DCO0+DCO1+DCO2);
         break;
  case 1:DCOCTL=DCO0;
         break;
  case 2:DCOCTL=DCO1;
         break;  
  case 3:DCOCTL=DCO0+DCO1;
         break;
  case 4:DCOCTL=DCO2;
         break;
  case 5:DCOCTL=DCO0+DCO2;
         break;
  case 6:DCOCTL=DCO1+DCO2;
         break;
  case 7:DCOCTL=DCO0+DCO1+DCO2;         //increment the range
         range++;
         P1OUT^=BIT6;
         break;
  }
P1IFG&=~BIT3;
}

The above graph explains the variations in the DCO frequency.The above code also checks for the RSEL values and DCO values as in graph. Every time the RSEL value increments the green led toggles and the red led is a indicator of the speed of CPU as the LED is toggled by a delay loop f(or loop) which uses the CPU Registers to count .As i dont have a CRO I’ve limited the code to just the 3 values of the RSEL .Please do check if there are any variations for other values.There are modulation bits which allow us to further vary the frequency.I’ll be putting up separate post on it sometime later.Now let us see how to vary the source for the MCLK from DCO to  other sources and to change the division factor.

#include "msp430x20x2.h"
void main()
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR=BIT0+BIT6;
P1OUT=0X00;
P1IE=BIT3;
_EINT();
int i=0;
while(1)
{
for(i=0;i<32000;i++);
P1OUT^=BIT0;
}
}
//------------------------------------------interrupt isr
#pragma vector=PORT1_VECTOR
__interrupt void pin3(void)
{
static int source=0,divide=0;
divide++;
divide=divide%4;
source=source%3;
switch(source)
{
case 0:BCSCTL2&=~(SELM0+SELM1);         //Select DCO
        break;
case 1:BCSCTL2=SELM1;                   //select XT2 else LFTXT1 or VLO
        break;
case 2:BCSCTL2=SELM0+SELM1;             //select LFTXT1 or VLO
        break;        

}
switch(divide)
{
case 0:BCSCTL2&=~(DIVM0+DIVM1);         //Divide the source / 1
        break;
case 1:BCSCTL2=DIVM0;                   //divide by 2
        break;
case 2:BCSCTL2=DIVM1;                   //divide by 4
        break;        
case 3:BCSCTL2=DIVM0+DIVM1;             //divide by 8
        source++;
        P1OUT^=BIT6;
        break;
}
P1IFG&=~BIT3;
}

So in the above code toggling of the green led is an indicator of the change of source and the red light toggle indicates the frequency due to change in the division factor.The same applies to the SMCLK and ACLK too.The SELSx and DIVx bits are used to change the source and the division factor for the SMCLK.The DIVAx bits are used for the ACLK.The default clock for ACLK in case the external oscillator is not present is VLO.To check the frequency of the ACLK ,SMCLK and MCLK the following code can be used.The ACLK and SMCLK are usually connected to the GPIO’s in case of the launchpad it being the PIN0 and PIN5.The MCLK can be checked by a delay loop.

#include "msp430x20x2.h"
void main()
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR=BIT0+BIT5+BIT6;
P1SEL=BIT0+BIT5;            //Select the secondary pin funcitons i.e ACLK SCLK.

while(1)
{
P1OUT^=BIT6;
}
}

The Frequency will have to be checked on a CRO as the Flicker on the led too fast to see.Also there are default values stored in the memory of the chip which can be used to configure the controller to predefined frequencies.These are given in the library file as CALDCO_16MHZ,CALBC1_16MHZ,.. etc.These can be directly entered on the code to get the desired calibration.Please leave a comment if there is something I can help with or if there is an error.I hope this blog was useful.Thank you for reading.Have a nice day 🙂

Advertisements
 
Leave a comment

Posted by on October 5, 2011 in Flip, MSP430 Tutorials

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: