Em làm cái ma trận 3x3 tương ứng các chân R0,R1,R2,C0,C1,C2 vào BIT0 BIT3 BIT4 BIT5 BIT6 BIT7 và nối VCC, lập trình cho nó khi bấm nút thì UART ra mã code tương ứng nhưng sao không chạy ạ :(...giúp em với
Code:
/******************************************************************************
* MSP430G2553 - USCI_A0, 9600 UART Echo ISR, DCO SMCLK
*
* Description: Echo a received character, RX ISR used. Normal mode is LPM0.
* USCI_A0 RX interrupt triggers TX Echo.
* Baud rate divider with 1MHz = 1MHz/9600 = ~104.2
* ACLK = n/a, MCLK = SMCLK = CALxxx_1MHZ = 1MHz
*
* MSP430G2553
* -----------------
* /|\| XIN|-
* | | |
* --|RST XOUT|-
* | |
* | P1.2/UCA0TXD|------------>
* | | 9600 - 8N1
* | P1.1/UCA0RXD|<------------
*
*******************************************************************************/
/*****************************************************************
* DEFINE
******************************************************************/
#include "msp430g2553.h"
#define KEY 0xF9//BIT0 BIT3 BIT4 BIT5 BIT6 BIT7
/*****************************************************************
* GLOBAL
******************************************************************/
/*****************************************************************
* INTERRUPT
******************************************************************/
/*****************************************************************
* PROTOTYPE
******************************************************************/
void sendbyte(unsigned char *data);
/****************************************************************
* MAIN
****************************************************************/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR &= ~KEY; //KEY is input direction use for read the gamepad 's value
P1REN |= KEY;//KEY pullup resistor is enable
//P1OUT |= KEY;//Use for P1REN- KEY is connected to VCC
//UART Config
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
for(;;)
{
if (!(P1IN & KEY))//KEY is pressed (KEY is connected to VCC)
{
_delay_cycles(10);
if(!(P1IN & KEY))
{
int i;
unsigned char *data;
for (i=0;i<=7;i++)//Write each bit to data
{
if ((P1IN & (BIT0<<i)) == 1)//Read each bit from KEY
{
*data |= (BIT0<<i);//Write bit 1
}
else
{
*data &= ~(BIT0<<i);//Write bit 0
}
}
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = *data;
}
}
}
}
/*****************************************************************
* UART
* UART sending byte
******************************************************************/
void sendbyte(unsigned char *data)
{
int i;
for (i=0;i<=7;i++)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = data[i];
}
}
/*****************************************************************
* UART
* UART interrupt
******************************************************************/
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
//if ( IFG2 & UCA0TXIFG )
//{
//Do Something with the recieved character
//}
}
/*****************************************************************
* END OF main.c
******************************************************************/