MSP430G2553共阴数码管通过3.6的按键输出,p2控制,是数码管从0显示到9
时间: 2024-05-08 09:17:22 浏览: 95
的循环显示。
下面是代码示例:
```c
#include <msp430g2553.h>
#define DIGIT1 BIT0
#define DIGIT2 BIT1
#define DIGIT3 BIT2
#define DIGIT4 BIT3
#define SEG_A BIT4
#define SEG_B BIT5
#define SEG_C BIT6
#define SEG_D BIT7
#define BUTTON BIT6
const unsigned char digit_map[] = {
/* 0 */ SEG_A | SEG_B | SEG_C | SEG_D,
/* 1 */ SEG_B | SEG_C,
/* 2 */ SEG_A | SEG_B | SEG_D | SEG_E | SEG_G,
/* 3 */ SEG_A | SEG_B | SEG_C | SEG_D | SEG_G,
/* 4 */ SEG_B | SEG_C | SEG_F | SEG_G,
/* 5 */ SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,
/* 6 */ SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,
/* 7 */ SEG_A | SEG_B | SEG_C,
/* 8 */ SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G,
/* 9 */ SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G
};
volatile unsigned int button_state = 0;
volatile unsigned int count = 0;
void init(void)
{
// 停用看门狗
WDTCTL = WDTPW + WDTHOLD;
// 设置时钟为1MHz
DCOCTL = 0;
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
// P2.0-P2.3为数码管的控制端口
P2DIR |= DIGIT1 | DIGIT2 | DIGIT3 | DIGIT4;
P2OUT &= ~(DIGIT1 | DIGIT2 | DIGIT3 | DIGIT4);
// P1.4-P1.7为数码管的段选端口
P1DIR |= SEG_A | SEG_B | SEG_C | SEG_D;
P1OUT &= ~(SEG_A | SEG_B | SEG_C | SEG_D);
// P1.6为按键输入
P1DIR &= ~BUTTON;
P1REN |= BUTTON;
P1OUT |= BUTTON;
P1IES |= BUTTON;
P1IFG &= ~BUTTON;
P1IE |= BUTTON;
// 允许中断
__enable_interrupt();
}
void display(unsigned char digit)
{
P1OUT = digit_map[digit];
}
void main(void)
{
init();
while (1) {
switch (count % 4) {
case 0:
P2OUT |= DIGIT1;
P2OUT &= ~(DIGIT2 | DIGIT3 | DIGIT4);
display(count % 10);
break;
case 1:
P2OUT |= DIGIT2;
P2OUT &= ~(DIGIT1 | DIGIT3 | DIGIT4);
display(count % 10);
break;
case 2:
P2OUT |= DIGIT3;
P2OUT &= ~(DIGIT1 | DIGIT2 | DIGIT4);
display(count % 10);
break;
case 3:
P2OUT |= DIGIT4;
P2OUT &= ~(DIGIT1 | DIGIT2 | DIGIT3);
display(count % 10);
break;
}
__delay_cycles(5000);
if (button_state) {
button_state = 0;
count++;
}
}
}
#pragma vector = PORT1_VECTOR
__interrupt void Port1_ISR(void)
{
P1IFG &= ~BUTTON;
button_state = 1;
}
```
该代码使用P2.0-P2.3控制数码管的四个位,通过循环显示0到9的数字。按下P1.6按键后,计数器加1,数字显示加1。
阅读全文