Loading
Study Reminders
Support
Text Version

Set your study reminders

We will email you at these times to remind you to study.
  • Monday

    -

    7am

    +

    Tuesday

    -

    7am

    +

    Wednesday

    -

    7am

    +

    Thursday

    -

    7am

    +

    Friday

    -

    7am

    +

    Saturday

    -

    7am

    +

    Sunday

    -

    7am

    +

Introduction to Embedded System DesignProfessor Dhananjay V. GadreNetaji Subhas University of Technology, New DelhiLecture - 24MSP430 Clock System and ResetHello and welcome to a new session for this online course on Introduction to Embedded SystemDesign. I am your instructor Dhananjay Gadre. In this lecture, we are going to look at one of themost important aspect which forms an important part of the ecosystem for a microcontroller tofunction effectively, namely clock and reset sources. We have seen in a previous lecture that amicrocontroller require four very important elements clock, reset, power supply, and an ability todownload code into the memory of the microcontroller.In this lecture, we are going to deal with the first two issues, namely clock and reset. Now, whydoes a microcontroller require a clock? The reason is a microcontroller is an example of asynchronous digital circuit. And because I have used the word synchronous that means it is goingto use a clock signal, and therefore I must provide a, such a signal. Also, the value that is thefrequency of this clock signal will determine lot of things.The frequency, higher the frequency, higher the performance because your microcontroller willbe able to execute more instructions per second if the frequency is high, but it will come with aprice. And the price is that as the frequency is gets higher and higher, the power dissipation willproportionately increase because the power dissipation of a CMOS circuit is directly proportionalto the frequency of operation.And so we must decide what is an optimum frequency for the operation of the microcontroller atany given point of time. Fortunately, and it was one of the salient features which I had mentionedin the salient features of microcontrollers, MSP430 offers the ability to be able to dynamicallychange the clock frequency using a software.Using the user program, the user can decide at any given point of time do they want a higherfrequency for operation because the performance requires so or if there is no work to be done,there is no point in clocking the microcontroller at a high frequency and instead, a lowerfrequency operation could be selected so as to conserve available power. So let us look at theclocking options of MSP430.(Refer Slide Time: 02:49)This is the block diagram of the clock module, which is there inside MSP430. On the left, youhave the sources of clock and on the right of this diagram, you see signals which are derivedfrom these three sources. There are three sources. This is one of them. An internal VLO clockthat is called, an external crystal-based oscillator and an internal digital, digitally controlledoscillator. These are the sources and using a particular combination of these three sources, theMSP430 clock module offers three signals, clock signals.It is called A clock that is auxiliary clock, the main system clock, and a subsystem clock. Let ussee what these clock signals do, what part of the microcontroller do they serve, and how theirfrequency can be changed.(Refer Slide Time: 03:54)This is a simplified block diagram of the clock module as applicable in MSP430G series. Youhave a very low-frequency internal oscillator, it operates at about 60 kilohertz, we will come tothe details. You have a low-frequency crystal oscillator; the oscillator is inside themicrocontroller. The crystal has to be connected to external signals, external pins, and apart fromthat, you also have a digitally controlled oscillator.There are three multiplexers, multiplexer number 1, number 2, and number 3, and eachmultiplexer has two inputs broadly. And you can write software to select one or the other sourceof clock. Once you decide the clock source, it can be further divided with a clock divider usingan option of divide by 1, 2, 4, or 8, to derive three clock signals. And these three clock signalsare auxiliary clock, master clock, and subsystem master clock. Let us see what these clocksignals are used for.(Refer Slide Time: 05:09)So, as I mentioned, the there are three sources, digitally controlled oscillator, low-frequencycrystal oscillator, and very low-frequency oscillator inside the MSP430.(Refer Slide Time: 05:23)The digitally controlled oscillator is based on a RC, internal RC oscillator, and let me give you avery simple circuit that may illustrate how a RC circuit can be made using logic gate such as aninverter here. This will oscillate at a certain frequency as determined by the value of this R andthis C. This is not to suggest that the DCO uses this. This is just an example.Anyway, the digital controlled oscillator allows you to change the frequency operation in thisrange from 60 kilohertz to 16 megahertz for a certain supply voltage. If the supply voltage ischanged, these numbers, these upper and lower limits would change. It is a RC, controlled RCoscillator, as I have mentioned, and because it is a very quick-start oscillator, it can start workingin less than a microsecond, it can be used to get out of low-power modes of operation, we aregoing to consider low-power modes of operation of MSP430 in a subsequent lecture, but thisdigital controlled oscillator allows you to switch from low-power modes into active modes ofoperation.The DCO frequency, digitally controlled oscillator frequency can be adjusted by software. Bywriting appropriate values in various registers in the program, you can change the frequency. Thedefault frequency after reset is 1.1 megahertz. So if you do not do anything and you just resetyour microcontroller, you do not have to make any selections, the value of the DCO frequencywill be 1.1 megahertz.(Refer Slide Time: 07:28)And this is the source of the clock, as you see this part in the block diagram here, lower side ishighlighting the DCO clock and all these names that you see here, these refer to various bits invarious registers that we will see very soon, which allow you to change the frequency of theDCO oscillator, digitally controlled oscillator.Further, you have a multiplexer here, as we had seen in the simplified block diagram, whichallows you to select the SM clock here as well as, as you see here, this signal goes and feeds tothe master clock also. We come to that shortly but so this is the part about the source of clock,clock source that is the DCO.(Refer Slide Time: 08:14)Then, apart from the DCO, we also have the low-frequency crystal oscillator. You can use a lowor a higher frequency crystal, but for the G255 series, you are restricted to a low-frequencyoscillator and that is recommended at 32 kilohertz. And in fact, the exact frequency is 32768hertz. This is a crystal used in real-time clocks and so is very commonly available, and this islow-frequency, which is used with for the low-frequency crystal oscillator, which is inside, theoscillator is inside the microcontroller, this crystal has to be connected to external pins X, as wehave seen.If you had a different microcontroller, apart from other than this, you could use a higherfrequency crystal also.(Refer Slide Time: 09:13)Here is the part highlighting the low-frequency crystal oscillator. These are the XIN and XOUTpins onto which you, between which you connect a crystal, this is the crystal. And in the case ofour current microcontroller MSP430G2553, this crystal can be only 32768 hertz. It can be otherfrequencies also. But it just so happens that you will have to get such a crystal custom made.This is the most common commercially available crystal.(Refer Slide Time: 09:47)The third source of clock is very low-frequency oscillator. It is also internal RC based oscillator,and the typical frequency operation is just a mere 12 kilohertz, and you can imagine that from 16megahertz for the DCO, down to 12 kilohertz here, you can really change the frequencyoperation and therefore change the way the microcontroller performs, functions and the amountof power that it consumes.(Refer Slide Time: 10:22)Here is the source, clock source, and as you see it feeds a multiplexer here.(Refer Slide Time: 10:36)Now the question is, we have seen here that we need auxiliary clock, we need a main systemclock, and we need a subsystem clock.(Refer Slide Time: 10:47)What is the purpose of having multiple clock signals on a microcontroller MSP430? And thereason is low-frequency operation is best for energy conservation, it is also good fortimekeeping. The high clock frequency will allow you to react to external events in a short timeand therefore that is beneficial when you want to respond quickly. And if you want a stableclock, then having a crystal-based oscillator is the best option and MSP430 allows you all thethree options.It offers you very-low-frequency RC based oscillator, it allows you 32.768-kilohertzcrystal-based oscillator, and it allows you another RC based oscillator which whose frequencycan be digitally altered to go from 60 kilohertz on one end to 16 megahertz on the other side, andso you could select that if you would like to have fast response time. And that is the reason whymultiple sources have been provided and the clock signals can be derived from these clocksources.(Refer Slide Time: 12:01)What are the signals that we need? As we mentioned, one of them is master clock. It is used byCPU. CPU has only one clock signal and that is master clock. Master clock signal can also be fedto other peripherals. As I mentioned, after reset the default master clock is derived from the DCOwith a frequency of 1.1 megahertz, but you can select the master clock signal to come fromlow-frequency crystal oscillator, to come from VLO or the DCO and XT2, that is high-frequencycrystal 2, but this option is not available on our microcontroller, this particular microcontrollerpart.(Refer Slide Time: 12:47)This is the part where you are selecting the master system clock here, and as you see, you have adivider which allows you to further divide the frequency, you have a multiplexer which allowsyou to select which source, clock source can be used. A master clock can come from here andthis itself allows you to select either the VLO or the low-frequency crystal and the other part ofthis multiplexer is being fed by the DCO. So master clock can be derived out of VLO orlow-frequency crystal or DCO.(Refer Slide Time: 13:28)Then the other signal that we need is the subsystem master clock. It is distributed only toperipherals, this is not fed to the CPU. Oftentimes it is the same as master clock and if at reset,the value is from the DCO and the frequency is 1.1 megahertz and again, it can be selected fromlow-frequency, it can be sourced from the low-frequency crystal oscillator or the VLO oscillatoror of course, the DCO clock.(Refer Slide Time: 14:04)Here is the part about subsystem master clock.(Refer Slide Time: 14:08)And the third is the auxiliary clock. This is again, only distributed to peripherals and the sourceof auxiliary clock can only be the low-frequency crystal or the internal low-frequency oscillator.If, now both these oscillators are slow oscillators meaning when you turn, apply power to them,they do not start oscillating quickly. They take some time to build up and for the oscillations tostabilize. And because of this, if any peripheral is being fed from the auxiliary clock, you mustmake sure that the oscillators have stabilized.If the oscillator has not stabilized, the microcontroller will not connect the clock source to theclock signal and therefore to the peripheral, and this we will see later how we can detect whetherthe oscillator is stable or not, and if it is not stable, we can wait. And why we can wait? Becausethe CPU which decides which clock is to be fed to which peripheral, the CPU is being fed bymaster clock and we can choose the master clock to be from the DCO so it can continue toperform, the microcontroller can continue to run the program, but in that program, you can waitfor these oscillators to stabilize before you apply to any peripherals.(Refer Slide Time: 15:39)Here is the selection for the auxiliary clock. Again, it has a divider, as you see you can divide thesource, which is either this or this by 1, 2, 4, or 8.(Refer Slide Time: 15:51)Now, in the DCO, apart from the ability to vary the frequencies, it also has four calibratedfrequencies and those are 1 megahertz, 8, 12, and 16. And you can write values into appropriateregisters to select whatever frequency you want. This is a sample code that two registers, one ofthem is called BCS control, the other is called DCO control, by writing appropriate values, theseare basically bitmasks, by writing this into these two registers will result in a DCO frequency of1 megahertz calibrated frequency. Not 1.1 megahertz, but quite accurate 1 megahertz frequency.Now, sometimes and often times when you want to measure time accurately, you may not wantto use the DCO, you may want to use the low-frequency crystal because a crystal is a very stableand accurate source of frequency, in which case you have the option of selecting thelow-frequency crystal oscillator. Traditionally, oscillators, crystal oscillators offer you largerange from 32 kilohertz up to few tens of megahertz.(Refer Slide Time: 17:18)In our MSP430G255 series, we are restricted to 32-kilohertz crystal and it is designed to giveyou the printed frequency at 25 degree centigrades. In the case of MSP430, it has to beconnected between these two pins that is XIN and XOUT.(Refer Slide Time: 17:28)Where do you use crystals? When you want to measure time or when you want to measurefrequencies of events then you should consider using a crystal. A crystal oscillator also requirescapacitors. Some capacitance is already on the microcontroller but you, depending upon therecommendations of the datasheet of the crystal that you use, if it requires more capacitance,then it can be connected to the X, these two pin XIN and XOUT pins. Extra capacitance on thesepins as the requirement may be for a particular crystal that you choose.(Refer Slide Time: 18:12)Okay. Now that we have seen the sources of clock and the clock signals that we need, here arethe registers which allow us to select various sources, decide their frequencies, and route theseclock sources to appropriate auxiliary clock or master clock or subsystem master clock.(Refer Slide Time: 18:38)The most important register is the DCO control register, and it has two sets of bits. One is calledthe DCO bit and these are three bits here, and the, you have five bits here, which are the modbits. The DCO bit selects a frequency, broad frequencies which are dictated by another set ofbits, which is which are these bits which we will see in the other register that we will briefly see.These RSEL bits allow you to go from 16 kilohertz to 16 mega, 60 kilohertz to 16 megahertz,and within that, these bits will tell you what particular frequency you can operate at. If you wantto have a frequency selection even finer than you can get with DCO, DCOx, then you have toplay with the MODx bits. For introductory applications, for simple applications, you do not haveto worry about this.(Refer Slide Time: 19:40)Now, this is the second important register, Basic Clock Control Register. Here, I want to make apoint. Do you notice that some of these bits here, it says, when it says rw means these bits can bewritten to as well as read from? And then it says, rw, here it says rw-0 and here it says rw-0within a bracket.These two refer to different values of zero depending upon the source of reset and we willconsider this when we consider the reset case, reset facilities in MSP430 but I just wanted tobring that to your notice. In this register, basic clock control register, we do not have this optionbecause as I mentioned, it does not offer you high-frequency crystal, you can select this, onlythis option is available, and using this you can decide whether you want to which divider youwant to use for the auxiliary clock. And these are the RSEL bits, these four bits, which decidebroad frequency ranges that run the DCO oscillator.(Refer Slide Time: 20:59)Then you have the Basic Clock Control Register 2. Here this decides whether what is the sourceof the master clock, meaning these bits, these two bits will decide will select the multiplexer,which is feeding the master clock source. This will, these two bits will decide what sort ofdivider do you want to use for the master clock source.This will decide whether do you want, how do you select the sub-master clock and you only havethis option here because this is not available on our MSP430G2553. And then these two bits willallow you to choose the divider for SM clock. And then this bit allows you whether you want tohave an external resistor. This is not available on our G2553 series.(Refer Slide Time: 22:08)This is the third register, Basic Control Register, Clock Control Register 3, and here you haveseveral bits. But the most important bit is these, which allow you to choose what sort offrequency crystal you want to use and whether you want to use internal capacitance, what will bethe value of these capacitance.(Refer Slide Time: 22:32)Now, to set the frequency of the DCO, you have to write into the DCO register as well as thesecond register, that is Basic Clock Control Register 1 and I suggest that you go through thisslide to understand all the options.(Refer Slide Time: 22:52)Based on the bits that you write into these registers, you see, this is part of the datasheet thatallows you to change the frequency from 60 kilohertz to 16 megahertz. Now we want to illustratethe great flexibility that MSP430 offers by dynamically changing the clock frequency and whatare we illustrating here? Let me show our plan.(Refer Slide Time: 23:26)What we are doing is we are going to take a MSP430. As you know that there are three sourcesVLO, crystal low-frequency crystal oscillator, and DCO. We are going to use, select the verylow-frequency oscillator for the master clock, which means the processor will operate at 12kilohertz but the 12-kilohertz basic frequency can be divided by 1 or 2 or 4 or 12, which meansby selecting an appropriate divider, we can reduce the frequency operation for the CPU. Becausewe are, I am saying we will select the master clock from master clock signal will come fromVLO.What we want to show is as follows, we want to have a LED like this, and in fact, we are goingto use the existing LED on the lunchbox, but we want to connect three external switches withpull-up resistors, one more, here VCC. And where are these connected? Where are these threeswitches connected? They are connected to P1.3, 1.4, and 1.5. So let me write here, P1.3 pin,P1.4, and P1.5. And we, as we know, the LED is connected to P1.7.What we will, what do we hope to achieve? That we will start the oscillator, we will select theVLO to go through the multiplexer and provide the signal for the master clock, and then we willhave a program which will continuously poll these three switches and if one switch is, if thisswitch is pressed, for example, it will divide the VLO frequency by 1. If the second switch ispressed, it will divide it by 4, and if the third switch is pressed, it will divide it by 8.And thereby you will get these three resultant frequencies. You see? If you divide by 1, thesource is 12 kilohertz, therefore the frequency will be 12 kilohertz. The CPU frequency that ismaster clock frequency will be 12 kilohertz. If you divide it by, if you choose the divide by 4option, you will get 3 kilohertz, and if you choose the 8, you will get 1.5 kilohertz.And you can, how would you know that the oscillator, that the CPU is working at differentfrequencies? Well, what we will do is we are going to blink this LED, we are blinking this LEDat a certain rate which is derived out of the clock frequency. Basically what we are saying is turnthe LED on for some clock cycles and turn the LED off for some clock cycles.Now, if the frequency of operation reduces you will see that the duration of the on and offincreases, that is frequency goes down. And so by operating it at, let us see, P1.3 is, here you geta 1.5-kilohertz clock. Here you get 3-kilohertz clock and here you get 12-kilohertz clock. Ofcourse, we are further used delays so that the LED will be blinking in visible range, you canmake, you can observe that the LED is turning on and off.It is not that it is turning on and off at such a high rate that it is beyond the persistence of vision.No. We have written the code in the manner that it is, you can see that it is turning on and off,but the rate of blinking will be perceptibly different when you choose these clock frequencies.So let us go through the code to understand how it works. At the beginning we see, as usual, wehave included the header file, then we have defined that we are going to put the LED on bit 7,but bit 7 is not telling the actual port pin, bit 7 is simply a mask bit as we have seen. Then we areputting one switch at bit 3, bit 4, and bit 5, and these are all P1.X. So here it is P1.7, 1.7, 1.3, 1.5.Then we have a function which we call as switch input, I will come to that later. We haveanother function called register settings for GPIO, I will come to that also. And we have thirdfunction, which is called register settings for the VLO.(Refer Slide Time: 29:00)And then we have the main program, so the main program is really very simple. What is itdoing? The, when you reset the system, the first instruction that is executed is to stop thewatchdog timer. We do not want to be bothered by watchdog timer overflowing and resetting us,which we will see in the next part of this lecture.Then we are calling this subroutine, which is basically selecting the VLO, and based on theswitches, it is going to select a particular frequency operation, and then we are calling anothersubroutine where we are deciding the direction of the pins of the microcontroller. The P1.7 pinhas to be output pin and the other three pins have to be input pin that will be done in this secondfunction.And then we have a infinite loop, while 1, and we are saying read the switch. So I am going to goexecute this function in which I am reading, waiting whether switch one is pressed or two ispressed or three is pressed. If a particular switch is pressed, it will go and change the frequencyoperation of the VLO oscillator and will come back here, and then it is going to, so this part ofthe code is simply toggling the LED.So you toggle it once, go back, wait for the switch to be pressed if it is not pressed, you comeback, again toggle the LED. So you keep on doing it, which means most of the time you aretoggling. But after every toggle, you are going and checking whether the any switch has beenpressed. If any switch has been pressed, you wait for it to be released and then based on whichswitch was pressed you are changing the VLO frequency.And so after that when it comes here, you will see that the LED toggle rate changes. Why?Because the CPU clock itself has changed and this is therefore a great example to illustrate howMSP430 offers you dynamic clock stability. Let us go through the code again, switch input.(Refer Slide Time: 31:12)Here we are waiting whether switch one is pressed, if it is pressed, this is to debounce and thenyou are selecting that the VLO divider should be by 8, therefore you will get a frequency of 1.5kilohertz. If switch two is pressed, then you debounce it again and select this divider so that yourresultant clock frequency CPU frequency is 3 kilohertz, and the third option is, if the third switchis pressed, your VLO will not be divided, it will be the same as the VLO frequency. Therefore,the CPU clock will be 12 kilohertz.Then this is very simple, it simply turns the pin which is driving the LED as output and the otherthree pins P1.3, 4, and 5 as inputs. It does not do anything else and then this second function,which allows you to select various, select VLO as the source. As we saw earlier, you have toplay in the Basic Clock Source Register 3 to select the VLO oscillator.Now, as I mentioned, this is a slow oscillator, and therefore after power on it may not quicklyturn on, and if you go through the datasheet of MSP430, it will tell you to wait for a certainamount of time before you can expect this clock to work.And the way to check is to reset a particular flag in this register we, which we will see in thereset part, and you turn this bit to zero and wait for some time, and if this bit remains zero thatmeans the oscillator is stable. If the oscillator is not working properly, the microcontroller willset OFIFG bit again. And so you are going to wait in this loop till this bit is reset to 0. If this bitremains reset to 0, that means the oscillator is stable.Now, you can make this setting will allow you to route the VLO oscillator as the master clock.So you are doing those two things. One, you are selecting VLO to come out of the firstmultiplexer, and then you are using the clock signal multiplexer to select master clock fromVLO.(Refer Slide Time: 34:01)And so here is the implementation we have collected, as you see these are the three switches one,two, and three, these are the pull-up resistors, and this LED, the user LED on P1.7 will blink. SoI suggest that you have downloaded this code, rebuild it and upload it into the lunchbox and seehow this the blinking rate of this LED changes as you press this or this or the other one. The rateis quite perceptibly different and this is the proof that by, the program can decide what can be thefrequency operation for the CPU, also, what can be the frequency operation of other peripherals.(Refer Slide Time: 34:49)So this is as far as the clock frequency operation, the clock sources, and clock signals of MSP430microcontroller are concerned, we have covered that. Now we are going to look at the reset part.Now, why does a microcontroller require reset? It is because a microcontroller is a logic circuitand logic circuit uses flip-flops and the flip-flops may have arbitrary values when power is firstapplied to them. And this may lead to non-uniform operation. And therefore it is very importantthat all the internal registers are initialized to a known value when the system is powered on forthe first time. And that is the function of reset.There are broadly two types of resets, one is called power-on reset, and the other is called powerup reset. There is a fine difference between the two. Power-on reset happens, as the namesuggests, when the power is applied for the first time. But power up clear can happen from othersources also.(Refer Slide Time: 35:50)Now, power-on reset is generated whenever you turn power for the first time. It can also begenerated because there is a brownout. Brownout means that the supply voltage to themicrocontroller is not stable. If it dips below a certain value, it will reset the, it will generate apower-on reset for the microcontroller as well as if you press the reset NMI pin that will alsogenerate a power-on reset signal.Now, what reset the system? Is it possible to find out what was the source of reset? Was itbrownout, was it a power-on reset that is turning the power off and on, or was it the user pressingthe switch on the RST/NMI pin? Is it possible to find out? Yes, MSP430 microcontroller hasregisters, which capture the source of that reset. And in fact, in this segment, we are going towrite up code which will show which sources of reset was the reason the system was reset.(Refer Slide Time: 37:04)Power up clear on the other hand, is generated because of software conditions. So the primarydifference between power-on reset and power up clear is that power-on reset is because ofexternal conditions. Why? You are powering the device up, so it generates a power-on reset orthe supply voltage is not stable, so it generates a reset or a user se four events could lead to a power up clear signal being generat