MSP430G2553共阴数码管通过按键显示
时间: 2024-05-13 22:20:50 浏览: 90
以下是MSP430G2553共阴数码管通过按键显示的示例代码:
```c
#include <msp430g2553.h>
#define LED1 BIT0
#define LED2 BIT6
#define KEY1 BIT3
#define KEY2 BIT4
#define KEY_DELAY 30000
const unsigned char DIGITS[10] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x07, 0x7F, 0x6F
};
unsigned char digit = 0;
void delay(unsigned int n)
{
unsigned int i;
for(i = 0; i < n; i++);
}
void display(unsigned char n)
{
P1OUT &= ~(BIT1 + BIT2 + BIT4 + BIT5);
P2OUT &= ~(BIT0 + BIT1 + BIT2 + BIT3);
switch(n) {
case 0: P1OUT |= BIT1 + BIT2 + BIT4 + BIT5; break;
case 1: P2OUT |= BIT0 + BIT1; break;
case 2: P1OUT |= BIT1 + BIT2 + BIT5; P2OUT |= BIT0 + BIT3; break;
case 3: P1OUT |= BIT1 + BIT2 + BIT4 + BIT5; P2OUT |= BIT0 + BIT3; break;
case 4: P1OUT |= BIT2 + BIT4; P2OUT |= BIT0 + BIT1 + BIT3; break;
case 5: P1OUT |= BIT1 + BIT2 + BIT4; P2OUT |= BIT1 + BIT3; break;
case 6: P1OUT |= BIT1 + BIT4; P2OUT |= BIT1 + BIT2 + BIT3 + BIT0; break;
case 7: P1OUT |= BIT1 + BIT2 + BIT4 + BIT5; P2OUT |= BIT0 + BIT1; break;
case 8: P1OUT |= BIT1 + BIT2 + BIT4 + BIT5; P2OUT |= BIT0 + BIT1 + BIT2 + BIT3; break;
case 9: P1OUT |= BIT1 + BIT2 + BIT4; P2OUT |= BIT0 + BIT1 + BIT3; break;
}
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= BIT0 + BIT1 + BIT2 + BIT4 + BIT5;
P2DIR |= BIT0 + BIT1 + BIT2 + BIT3;
P1OUT &= ~(BIT0 + BIT1 + BIT2 + BIT4 + BIT5);
P2OUT &= ~(BIT0 + BIT1 + BIT2 + BIT3);
P1DIR &= ~(KEY1 + KEY2);
P1REN |= KEY1 + KEY2;
P1OUT |= KEY1 + KEY2;
while(1) {
if((P1IN & KEY1) == 0) {
digit++;
if(digit > 9) digit = 0;
delay(KEY_DELAY);
}
if((P1IN & KEY2) == 0) {
digit--;
if(digit < 0) digit = 9;
delay(KEY_DELAY);
}
display(DIGITS[digit]);
delay(10000);
}
}
```
此代码可以让您使用KEY1和KEY2按键来增加和减少数字,然后在数码管上显示它。您可以更改数字的初始值,以便在开始时显示您想要的数字。
阅读全文