#Include
#Include
h>
#include "string.h" void UartInit(void); void UartSend(unsigned char* pData, unsigned char Length); // Ham nhan du lieu #pragma vector=USCIAB0RX_VECTOR __interrupt void USCI0RX_ISR(void) { unsigned char Receive; Receive = UCA0RXBUF; // Doc 1 byte tu cong UART if (Receive == 'a') { P1OUT |= BIT0; // Led 1 sang P1OUT &= ~BIT6; // Led 2 tat } else if (Receive == 'b') { P1OUT |= BIT6; // Led 2 sang P1OUT &= ~BIT0; // Led 1 tat } } /* * main.c */ void main(void) { WDTCTL = WDTPW | WDTHOLD;
// Cau hinh xung clock cho chip chay o 1Mhz if (CALBC1_1MHZ==0xFF) // If calibration constant erased { while(1); // do not load, trap CPU!! } DCOCTL = 0; // Select lowest DCOx and MODx settings BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; // Cau hinh cho Uart UartInit(); // Cau hinh cho Den Led1 (P1.0) P1DIR |= BIT0; // P1.0 la output P1OUT &= ~BIT0; // Tat Led // Cau hinh cho Den Led2 (P1.6) P1DIR |= BIT6; // P1.0 la output P1OUT &= ~BIT6; // Tat Led while (1) {
UartSend("Dai Hoc Bach Khoa", strlen("Dai Hoc Bach Khoa")); //P1OUT ^= BIT0; // Dao trang thai cua Led __delay_cycles(1000000); // Delay 1s } } void UartInit(void) { P1SEL = BIT1 + BIT2 ; P1SEL2 = BIT1 + BIT2 ; UCA0CTL1 |= UCSSEL_2; UCA0BR0 = 104; UCA0BR1 = 0; UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; IE2 |= UCA0RXIE; __bis_SR_register(GIE); enabled }
// // // // // // // //
P1.1 = RXD, P1.2=TXD P1.1 = RXD, P1.2=TXD SMCLK 1MHz 9600 1MHz 9600 Modulation UCBRSx = 1 **Initialize USCI state machine** Enable USCI_A0 RX interrupt // Enter LPM0, interrupts
void UartSend(unsigned char* pData, unsigned char Length) { unsigned char i; for (i = 0; i < Length; i++) { while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready? UCA0TXBUF = *pData; // TX -> RXed character pData++; } }