Article count:10804 Read by:13623255

Account Entry

[MSP430 Fun Talk 7] Use of library function timer

Latest update time:2015-12-28
    Reads:

This time we are going to talk about a very important peripheral in the microcontroller, the timer.

How to say it, the timer is similar to the alarm clock, and its functions are similar to those of the alarm clock. It can realize the timing function, of course, this is only one of its functions, it can also complete many other functions, let's take a look at what magical places it has this time.

It's the same document, I won't mention which one. Open it and find the chapter about timers.

We can see that there are three chapters on timers here. In fact, they are all similar, but their functions are slightly different. In essence, they are still the same as their name, timer, timer, which means timing!

Here we focus on Timer_A, and then we will talk about the differences between the three.

In addition, let's talk about why we need a timer! I don't know if you still remember that we said that the MCU is stubborn and executes one program statement at a time. If there is no situation, it will execute it stubbornly. In fact, this is the same as what we said before about interrupts. The timer can count the clock and complete an interrupt within a fixed time, interrupting the stubbornness of the MCU. Next, let's see how to achieve it.


Last time, we used Grace to configure the clock, which made it easier for everyone to understand the clock. Grace can also be used to configure the timer, but we will not use Grace here. Why? Because although its tool is visual, it cannot help us really learn anything, and not all MCU compilation environments provide such a good development method, and it is not recommended that you always use this tool. Here we will use the previous development method for development. We use the form of library development, but we will talk about the relevant configuration of registers.


For a timer, the basic function is timing. When a certain action is executed, a timer interrupt is generated. In addition, we can use the timer to output PWM waves. In addition, we can also perform input capture.

There are four counting modes for TimerA. As shown in the figure, they are:

1. Stop the pause timer

2. Up counting mode

3. Continuous mode

4. Up or down mode

Let us explain what these four modes are like.

In the up counting mode, our timer is 16 bits, so the maximum value is 0FFFFh, so we have such a mark on the ordinate, and the h behind it means that our value is in hexadecimal. Now if we set the value of the TAxCCR0 register, its count will start from 0, and after counting to the count value of TAxCCR0, it will return to 0 and continue counting.

The continuous counting mode starts from zero and counts to 0FFFFh, and then returns to 0 to continue counting.

From the figure we can clearly see the difference. The up-down counting mode counts from zero up and then counts down instead of counting to TAxCCR0 and then directly turning to zero and continuing counting.


Another mode, stop mode, means that the timer stops counting, which is meaningless and will not be discussed here.


The timer is relatively complicated. How should I put it? We cannot explain it in a few words. It may take a long chapter to explain all the details. So we will not go into details here. We will explain the things we are going to use. You can check the usage of other functions when you need them. I believe that if you can understand what we have said, it will be easier for you to learn other modules.

We will use TIMA to implement two functions, one is to light up the LED by timer interrupt, and the other is to implement the PWM output of the timer.

Now we will implement the timer interrupt. Find our library function reference manual.

MSP430FR5xx_6xx_DriverLib_Users_Guide-2_21_00_08

In the library function attached to our fifth lecture, there is a doc folder under which it can be found.

In Chapter 30 we found a list of all function names, and clicking on each function will take us to the specific function definition.

The first step is to initialize the upward counting mode. Click this function to get its function definition and the selection instructions of related parameters.

We found that we don’t understand the definitions of many things, such as baseadress. Let’s see how to use CCS to help ourselves fill in these things. Let’s see how to do it specifically.

Here we use the clock configuration project in the sixth lesson. In the last lesson, we configured the clock so that we can better judge our timing. One thing to remember is that the timer must ensure the accuracy of timing, and its clock source is very important, so we directly use the last project and configure the clock to 16M.

In the above figure, we can see that there are four clock sources for TIMA. TAXCLK and INCLK are external inputs, while ACLK and SMCLK are the two clock sources we mentioned last time. The sizes we configured are SMCLK 16M and ACLK 32.768K.

Create a project and add our library files. The specific operations will not be explained here.

Where is the baseadress defined above? We found a header file called hw_memmap.h

So where is this file included? In timer_a.c we can see that it is included here.

Now we know what the first parameter is. The baseaddress corresponds to TIMER_A0_BASE. There is a conditional compilation here. Let's explain its usage. It can realize the applicability of the code. Suppose I write a small piece of code.

#ifdef MSP430

a = 0;

#endif

#ifdef MSP430FR5969

a = 1;

#endif

At this time, if I define a #define MSP430, then the value a I get is 0. On the contrary, if I define a #define MSP430FR5969, the value a I get is 1.

We will not explain here where this is defined. Right click __MSP430_HAS_T0A3__ and select open declaration. You will know it by yourself.

Now let's find the second parameter to fill in. Right-click Timer_A_initUpModeParam and then open declaration. (A shortcut is to hold down Ctrl and click Timer_A_initUpModeParam)

It is a structure definition, which is too long for me to capture. You can see that most of it is comments, so it is relatively easy to understand. Let's see how to use it.

The above is the overall program. Remember to #include <driverlib.h>

I have a problem with my compiler

It has not been solved yet, and I hope someone who knows the problem can help me solve it.

If I find a solution I will tell you how to solve this problem next time.

I think there is something wrong with my Grace, but I can't find out whether it is a configuration problem or a problem with the software itself. After careful inspection, I found that there is no way to generate the Grace.lib file.

Next time we will talk seriously about the library function configuration method of the system clock.

Thanks.


Replenish:

I made a mistake in the code above.

Timer_A_initUpModeStucture.timerInterruptEnable_TAIE= TIMER_A_TAIE_INTERRUPT_ENABLE;

The interrupt enabled by this statement is different from what we imagined. The interrupt we want is to generate an interrupt when the count reaches the value set by CCR0 is 50000, but in practice we find that this is not the case. We compile the above code and download it, and find that although the LED is on, it does not flash. In the simulation process, we also enter a function called IntFault(). Here we need to figure out what interrupt is set by the above statement.

There are two interrupts for TIMER_A, which we can find in the timer section of the data sheet:

One is the CCIE interrupt generated by CCIFG, and the other is the TAIE interrupt generated by TAIFG. These two interrupts are different, as follows:

TAIE and CCIE refer to different events. TAIE refers to the overflow of the TAR counter, the change from 65535 to 0, caused by TAIFG. CCIE refers to the capture of the corresponding signal (in capture mode); the timing time is reached (in comparison mode). It is caused by CCIFG. The interrupt vectors of the two interrupts are different. TAIFG generally enters TIMER_A1_VECTOR; CCIFG depends on which timer is used. If it is CCR0, it enters TIMER_A0_VECTOR, if it is CCR1, CCR2..., it enters TIMER_A1_VECTOR.

The blue words are the interrupts we use, so we need to modify them here to enable the CCR0 interrupt and disable the TAIFG interrupt. Modify them to the following sentence:

Timer_A_initUpModeStucture.captureCompareInterruptEnable_CCR0_CCIE |= TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE;

At this time, download it again and you can see the LED flashing.

At the same time, we saw above that we need to use an interrupt vector, so what does this interrupt vector refer to?

The interrupt vector refers to the entry address of our interrupt program, so where is this address defined? We found that these addresses are defined in msp430fr5969.h:

All interrupt vectors in FR5969 are defined here. Of course, some places will say that this is an interrupt vector table, which refers to the same thing, that is, the definition of all interrupt entry addresses.

So how do we understand this?

We can better understand this process from the following figure.

Some parts may not be rigorous enough, I hope everyone can understand what the specific meaning is.


thanks for your support!!!

For more content and attachment downloads, please click below Read the original text .


Welcome to watch

[Interesting Talk about MSP430] Lecture 1 of MSP430

[Interesting Talk about MSP430] Lecture 2 of MSP430

[Interesting Talk about MSP430] Lecture 3 of MSP430

[Interesting Talk about MSP430] Lecture 4 of MSP430

[Interesting Talk about MSP430] Lecture 5 of MSP430

[Interesting Talk about MSP430] Lecture 6 of MSP430





Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号