Introducing breathing light when the typical mode of operation of the CPU is an infinite loop, stop to see whether it should light up LED. However, it has been busy with the same thing is also very boring, you can stop and dry as anything else, this is the interruption of work.
1, showing the effect of
The figure is a key example of the use of interrupt the work, the microcontroller can handle other tasks during normal operation, when the key is pressed, the CPU is interrupted to trigger switching between traffic lights, and then return to processing tasks.
2, interrupt decent introduction
When I started learning interruption, an example of the deepest impression is like a man interrupted the work of his hands are busy processing documents, but suddenly the desk phone rang, and he had to stop hand work to pick up the phone. Process to answer the phone, the leader just told him to deal with an important thing, he later had a brief phone call to the end of a busy leader explain things. After successful completion, he returned to the office to find that a bookmark folder hand, continue to work.
- The very image of this example, like the interruption to the microcontroller attached to a more flexible work ability , is not a rigid staff as step by step in accordance with the work schedule, do not understand the flexible handling urgent task.
- The urgent task, there is a telephone that is not urgent, unimportant tasks, key personnel have required urgent task, such a mechanism in the microcontroller interrupt is called the interrupt priority .
- To busy this tangled burst task, but also easy to look back and find the exact file on hand to see that page, we need to clip a bookmark, make a mark, which is called interruption in the preservation field
Here we try to explain and easy to understand examples to see how the microcontroller are trained to a journeyman.
2.1,430 interruption
Speaking in front of what is an interrupt, the interrupt what good is that this chip MSP430 what interrupt it? Let's look at 430 in the end how many people will come to harass the work of the CPU.
Here we begin to see the datasheet of the chip, datasheet is a chip instruction manual. Since the MSP430 family has too many models, so not every regard all the features of a chip specification to write the whole. So there are a total 430 x2xx series of instructions in this link: the MSP430x2xx Series
User's Guide . Then specific to each chip, hardware indicators, such as the memory size of what is different, these indicators will have a special specification, this link: MSP430G2x53, MSP430G2x13 mixed signal microcontroller Datasheet (Rev. G) . These documents are found in the official website, direct search for a specific chip model, then click below can be found in several places, there are Chinese as well as English, general English is the latest version of a document.
From the 2553's datasheet, the first 12 official also help us put together 430G2553 interrupt sources on the table, as follows:
Can be seen, the chip communicates power, reset, timers, comparators, serial IO pin and so be able to trigger an interrupt, in this example the key is actually utilized IO interrupt pin P1.3. There is also a table that we need to pay attention to whether shielding properties, some simple understanding is interrupted, you can set to ignore it, but some interruption in rivers and lakes, involuntarily, and if it comes, it must Tegretol .
2.2,430 interrupt priority
The table above, see the priority is not the best one, and from top to bottom, excellent (deposit) before (at) level (flu) in descending order. This table is no need to keep in mind, we need to know there is such a thing exists, at the time of writing the code, if it comes to using multiple interrupts, pay attention to proper timing relationships between different arrangements for each other we do not like, this back have the opportunity to introduce, for now forget the right, I remember talking about that analogy bookmarks on OK.
2.3, interruption of site protection
下图是430的一个典型流程图,在430系列文档里有介绍(2553 datasheet不会写)。是不是看着巨麻烦,不用担心,现在没必要搞懂,等到后面有机会讲门电路和寄存器级的代码,我在细细描述下通用的单片机中断处理的机制。讲讲下面这段话究竟在做啥事情。现在先忘掉这些吧,记住我讲的那个书签的比喻就OK啦。
- 任何当前执行的指令完成。
- 指向下一条指令的PC 被压入堆栈。
- SR 被压入堆栈。
- 如果在最后一个指令执行期间由多个中断出现,那么具有最高优先级的中断被选中并等待被处理。
- 在单一源标志上,中断请求标志自动复位。对于软件处理,多个源标志保持被设定。
- SR 被清除。这将终止任何低功耗模式。由于GIE 位被清除,之后的中断被禁用。
- 中断矢量的内容被载入到PC:程序继续在中断处理例程所处的地址上执行。
3、中断的不正经操作
介绍说了这么多,都快忘记了那个开头的图片,那个是怎么做到的,我们来说说。
3.1、代码展示
main.c代码
1 |
|
common.h代码,使用的时候在工程中新建一个common.h文件,并添加到工程中
1 |
/* |
3.1、代码解释
1、为什么中断后要等20毫秒再判断?
如上图所示,机械的按键在按下和弹起时,电压的波动不是理想的立即变低然后变高,而是在变化时伴随着很多抖动和毛刺(有示波器的同学可以看下波形,没有的可以去掉延时这一行然后感受一下效果差异)。这也是本文标题按键消抖的来源。
2、common.h里面的是什么?
个人习惯喜欢在common.h里面封装一些公共的函数和宏定义,这样方便使用,每个工程直接引用就可以了。这里的延时就是。CPU_F是CPU主频率,430默认是1M,所以填1000000,后面如果做其他功能的时候将时钟频率调到16M或者32M或者32768HZ,都记得改一下这里的值,否则延时就不准了。delay_us和delay_ms分别是延时对应的微秒和毫秒。
3, the above code, the wording of these two lines, the first line is fixed, this line tells the compiler, the following function is to prepare the pins P1, P1 after the break, if interrupt open, allow the CPU to jump directly to perform the following functions. As for the second line of the function name, how to play freely, as long preceded __interrupt on the line.
#pragma vector=PORT1_VECTOR
__interrupt void key_inte(void)
4, there is a place above the scene say the protection is not found Why not here? This is because if you use C language rather than assembler, compiler of these things can help us to automatically generate, according to the first point above said wording on it.
5, the top judge interrupted code, there is a pit novice easy to step into, causing no reaction by the key how is this line:
if ((P1IN & BIT3) == 0)
This line is easy to write someone handy if (P1IN & BIT3 == 0) , the result is completely different, because of lower priority than the & ==, the compiler will first determine BIT3 == 0, and then sends the results P1IN and, BIT3 is 0x00001000, certainly not equal to 0, so how function keys are able to get treatment. Annex C language operator precedence table below, there is usually a trick is to write code if not remember a priority, then put every one of your actions are uncertain as above plus brackets , this is snake oil operations.
s