MSP430 key debounce

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

buttonjitter

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.
2553datasheet

From the 2553's datasheet, the first 12 official also help us put together 430G2553 interrupt sources on the table, as follows:
2553intsource

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啦

  1. 任何当前执行的指令完成。
  2. 指向下一条指令的PC 被压入堆栈。
  3. SR 被压入堆栈。
  4. 如果在最后一个指令执行期间由多个中断出现,那么具有最高优先级的中断被选中并等待被处理。
  5. 在单一源标志上,中断请求标志自动复位。对于软件处理,多个源标志保持被设定。
  6. SR 被清除。这将终止任何低功耗模式。由于GIE 位被清除,之后的中断被禁用。
  7. 中断矢量的内容被载入到PC:程序继续在中断处理例程所处的地址上执行。

430intflow

3、中断的不正经操作

介绍说了这么多,都快忘记了那个开头的图片,那个是怎么做到的,我们来说说。

3.1、代码展示

main.c代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include "common.h"

int (void)
{

WDTCTL = WDTPW + WDTHOLD;
大专栏  MSP430按键消抖ss="comment">// P1.0和P1.6设置为输出,点灯用
P1DIR |= BIT6 + BIT0;
// 启动时6亮
P1OUT |= BIT6;
// 0灭
P1OUT &= ~BIT0;
// P1.3作为输入,接收按键的输入电压
P1DIR &= ~BIT3;
// P1SEL功能选择,0表示IO功能
P1SEL &= ~BIT3;
// 上拉电阻使能,作用我们后面讲模拟部分再说吧
P1REN |= BIT3;
// P1.3允许中断,默认是关的,不开不行
P1IE |= BIT3;
// 中断边沿选择为下降沿,因为上拉电阻存在,按下按键的时候电压从高变成接地的低,所以是下降沿
P1IES |= BIT3;
// 清零中断标记位,IFG(interrupt flag),一个字节,8位,分别对应P1.0 ~1.7
P1IFG = 0;
// 使能全局中断,相当于中断的总开关,所有中断都受这个总开关的限制
_EINT();
while (1)
{
// 这里随便干点啥
}
}

#pragma vector=PORT1_VECTOR
__interrupt void key_inte(void)
{
// 看下中断标志位,是不是1.3产生的,其他引脚产生的也会进来
// 当然这里我们只开了1.3的中断,不会有其他引脚触发进来
if (P1IFG & BIT3)
{
// 延时20毫秒消抖
delay_ms(20);
// 按照消抖原理,延时结束后,再判断P1.3的输入是否还是0电压
if ((P1IN & BIT3) == 0)
{
// 确实是按键按下,交换两等的亮暗状态
P1OUT ^= (BIT6 + BIT0);
}
}

// 不管是哪个脚触发的中断,本次全部清除标记,继续等待下一次中断
P1IFG = 0;
}

common.h代码,使用的时候在工程中新建一个common.h文件,并添加到工程中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
* common.h
*
* Created on: 2015年6月22日
* Author: xiaoyao
*/

#ifndef COMMON_H_
#define COMMON_H_

#include <msp430g2553.h>

/********************延时*******************************/
#define CPU_F ((double)1000000) //时钟频率0
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))

#endif /* COMMON_H_ */
3.1、代码解释

1、为什么中断后要等20毫秒再判断?
jitter
如上图所示,机械的按键在按下和弹起时,电压的波动不是理想的立即变低然后变高,而是在变化时伴随着很多抖动和毛刺(有示波器的同学可以看下波形,没有的可以去掉延时这一行然后感受一下效果差异)。这也是本文标题按键消抖的来源。

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.

coperatorpri

s

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11710825.html