C example of using the USCI as a 9600 baud UART to communicate with the HC-06 bluetooth module for the MSP430F5529: Difference between revisions
No edit summary |
No edit summary |
||
Line 38: | Line 38: | ||
*/ |
*/ |
||
void main(void) |
void main(void) |
||
{ WDTCTL = WDTPW|WDTHOLD; |
|||
{ |
|||
/* BCSCTL1 = CALBC1_1MHZ;//Adjust the clock |
|||
WDTCTL = WDTPW|WDTHOLD; |
|||
/* BCSCTL1 = CALBC1_1MHZ;//Adjust the clock |
|||
DCOCTL = CALDCO_1MHZ;*/ |
DCOCTL = CALDCO_1MHZ;*/ |
||
P1DIR = RedLed; //Make P1.0 an output so we can use the red LED |
|||
P4DIR = GreenLed; //Make P4.7 an output so we can use the red LED |
|||
P1OUT &= ~RedLed; //Clear the red LED |
|||
P4OUT &= ~GreenLed; //Clear the green LED |
|||
P3SEL = BIT3+BIT4; // P3.4,5 = USCI_A0 TXD/RXD |
|||
UCA0CTL1 |= UCSWRST; // **Put state machine in reset** |
|||
UCA0CTL1 |= UCSSEL_2; // SMCLK |
|||
UCA0BR0 = 6; // 1MHz 9600 (see User's Guide) |
|||
UCA0BR1 = 0; // 1MHz 9600 |
|||
UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0, |
|||
// over sampling |
|||
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** |
|||
transmit(enter); |
|||
/*UCA0BR0 = 104; |
|||
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt |
|||
//Low bit of UCBRx is 104 |
|||
_enable_interrupts(); |
|||
UCA0BR1 = 0; |
|||
while(1){ |
|||
//High bit of UCBRx is 0 |
|||
if(cnt == 1){ |
|||
UCA0MCTL = UCBRS_1; |
|||
//Check if cnt is 1 |
|||
inputlength = arraylength(input); |
|||
//Baud Rate = 9600 |
|||
//Get your input length |
|||
UCA0CTL1 &= ~UCSWRST; |
|||
passwordlength = arraylength(password); |
|||
//Clear SW reset, resume operation*/ |
|||
//Get your password length |
|||
transmit(enter); |
|||
{difference = compare(input,password);} |
|||
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt |
|||
//Compare the received password with your password |
|||
_enable_interrupts(); |
|||
if(difference == 0){ |
|||
while(1){ |
|||
if |
//Check if they match |
||
{transmit(correct);} |
|||
//Check if cnt is 1 |
|||
//If they match, transmit correct string |
|||
inputlength = arraylength(input); |
|||
P1OUT &= ~RedLed; |
|||
//Get your input length |
|||
P4OUT |= GreenLed; |
|||
passwordlength = arraylength(password); |
|||
// |
//Turn on the green LED |
||
__delay_cycles(5000000); |
|||
{difference = compare(input,password);} |
|||
//Wait for 5 seconds |
|||
//Compare the received password with your password |
|||
P4OUT &= ~GreenLed; |
|||
if(difference == 0){ |
|||
WDTCTL = WDT_MRST_0_064; // Turns off the LED too. |
|||
//Check if they match |
|||
//Reset the system |
|||
{transmit(correct);} |
|||
} else {//If they do not match |
|||
{transmit(incorrect);} |
|||
P1OUT &= ~RedLed; |
|||
//Transmit incorrect string |
|||
P4OUT |= GreenLed; |
|||
P1OUT = RedLed; |
|||
//Turn on the green LED |
|||
//Turn on the red LED |
|||
__delay_cycles(5000000); |
|||
__delay_cycles(2000000); |
|||
//Wait for 5 seconds |
|||
//Wait for 2 seconds |
|||
P4OUT &= ~RedLed; |
|||
WDTCTL = WDT_MRST_0_064; // Turns off the LED too. |
|||
//Turn off the red LED |
|||
{transmit(reenter);} |
|||
} |
|||
}//Transmit reenter string |
|||
else{ |
|||
cnt = 0; |
|||
//If they do not match |
|||
//Reset cnt |
|||
{transmit(incorrect);} |
|||
RXByteCtr = 0; |
|||
//Transmit incorrect string |
|||
}}//Reset Receive Byte counter |
|||
P1OUT = RedLed; |
|||
//Turn on the red LED |
|||
__delay_cycles(2000000); |
|||
//Wait for 2 seconds |
|||
P4OUT &= ~RedLed; |
|||
//Turn off the red LED |
|||
{transmit(reenter);} |
|||
//Transmit reenter string |
|||
} |
|||
cnt = 0; |
|||
//Reset cnt |
|||
RXByteCtr = 0; |
|||
//Reset Receive Byte counter |
|||
}} |
|||
} |
} |
||
//USCI A receiver interrupt |
//USCI A receiver interrupt |
||
// The stuff immediately below is to make it compatible with GCC, TI or IAR |
|||
/*#if defined(__TI_COMPILER_VERSION_) || defined(__IAR_SYSTEMS_ICC__) |
|||
#pragma vector=USCIAB0RX_VECTOR |
|||
__interrupt void USCI0RX_ISR(void) |
|||
#elif defined(__GNUC__) |
|||
void __attribute__ ((interrupt(USCIAB0RX_VECTOR))) USCI0RX_ISR (void) |
|||
#else |
|||
#error Compiler not supported! |
|||
#endif |
|||
*/ |
|||
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) |
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) |
||
#pragma vector=USCI_A0_VECTOR |
#pragma vector=USCI_A0_VECTOR |
||
Line 126: | Line 103: | ||
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void) |
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void) |
||
#else |
#else |
||
#error Compiler not supported! |
|||
#endif |
#endif |
||
{ //Check if the UCA0RXBUF is different from 0x0A |
|||
{ |
|||
//Check if the UCA0RXBUF is different from 0x0A |
|||
//(Enter key from keyboard) |
//(Enter key from keyboard) |
||
if(UCA0RXBUF != 0x0A) input[RXByteCtr++] = UCA0RXBUF; |
if(UCA0RXBUF != 0x0A) input[RXByteCtr++] = UCA0RXBUF; |
||
//If it is, load received character |
//If it is, load received character |
||
//to current input string element |
//to current input string element |
||
else{ |
else{cnt = 1; |
||
//If it is not, set cnt |
|||
cnt = 1; |
|||
input[RXByteCtr] = 0; |
|||
//If it is not, set cnt |
|||
} //Add null character at the end of input string |
|||
input[RXByteCtr] = 0; |
|||
//Add null character at the end of input string |
|||
} |
|||
} |
} |
||
void transmit(char *str){ |
void transmit(char *str){ |
||
while(*str != 0){ |
while(*str != 0){//Do this during current element is not |
||
//Do this during current element is not |
|||
//equal to null character |
//equal to null character |
||
while (!(UCTXIFG&UCA0IFG)); |
while (!(UCTXIFG&UCA0IFG)); |
||
Line 149: | Line 122: | ||
UCA0TXBUF = *str++; |
UCA0TXBUF = *str++; |
||
//Load UCA0TXBUF with current string element |
//Load UCA0TXBUF with current string element |
||
}//then go to the next element |
|||
} |
|||
} |
} |
||
Line 156: | Line 128: | ||
if(a>b) return a; |
if(a>b) return a; |
||
else return b; |
else return b; |
||
} // Find the max between two numbers. |
|||
} |
|||
int compare(char *strin, char *strpass){ |
int compare(char *strin, char *strpass){ |
||
int i=0; |
int i=0; |
||
int result = 0;//Clear result |
int result = 0;//Clear result |
||
/*if(inputlength <= passwordlength){ |
|||
//Check if passwordlength is greater than or |
|||
//equal to inputlength |
|||
while(*strpass != 0){ |
|||
result = result + abs((*strin++)-(*strpass++)); |
|||
//If it is, take the difference between elements of |
|||
//strin and strpass until current element of strpass |
|||
//is equal to null character, abs() is used to ensure |
|||
//that differences do not cancel each other |
|||
}} |
|||
else{ |
|||
while(*strin != 0){ |
|||
result = result + abs((*strin++)-(*strpass++)); |
|||
//If it is not, do the same thing until current element |
|||
//of strin is equal to null character this time |
|||
}}*/ |
|||
int len = max((passwordlength),inputlength); |
int len = max((passwordlength),inputlength); |
||
for(i=0;i<len;i++) { |
for(i=0;i<len;i++) { |
||
result = result + abs((*strin++)-(*strpass++)); |
result = result + abs((*strin++)-(*strpass++)); |
||
} // abs used to make sure differences don't cancel |
|||
} |
|||
return result; |
return result; |
||
}//Return result value |
|||
} |
|||
int arraylength(char *str){ |
int arraylength(char *str){ |
||
int length = 0; |
int length = 0; |
||
Line 192: | Line 148: | ||
//Increase array address |
//Increase array address |
||
length++; |
length++; |
||
} //Increase length value |
|||
} |
|||
return length; |
return length; |
||
}//Return length value |
|||
} |
|||
<\nowiki> |
<\nowiki> |
Revision as of 11:56, 22 July 2015
<nowiki>
/* This program asks you for the password, and then tells
* you if you typed it right. You need to set the bluetooth * terminal to send /n, or /n at the end of a send. The app * I used on my Android phone is from: * https://github.com/Sash0k/bluetooth-spp-terminal * * Originally from the textbook, modified for the MSP430F5529 * by Rob Frohne 7/21/2015. * * The HC-06 bluetoooth module I used to test this is connected * to the +5V and GND on the launchpad. The RXD of the HC-06 is * connected to P3.3 and TXD of the HC-06 is connected to P3.4. * * Note the changes to the interrupt as compared to the G2 Launchpad. */
- include <msp430.h>
- define RedLed BIT0
- define GreenLed BIT7
char password[] = "12345"; //The Password char enter[] = "Enter Your Password\r\n"; char correct[] = "Your password is correct\r\n"; char incorrect[] = "Your password is incorrect\r\n"; char reenter[] = "Please re-enter your password\r\n"; char input[100]; int RXByteCtr = 0; int cnt = 0; int inputlength,passwordlength; int difference; void transmit(char *str); int compare(char *strin, char *strpass); int arraylength(char *str);
/*
* main.c */
void main(void) { WDTCTL = WDTPW|WDTHOLD; /* BCSCTL1 = CALBC1_1MHZ;//Adjust the clock DCOCTL = CALDCO_1MHZ;*/ P1DIR = RedLed; //Make P1.0 an output so we can use the red LED P4DIR = GreenLed; //Make P4.7 an output so we can use the red LED P1OUT &= ~RedLed; //Clear the red LED P4OUT &= ~GreenLed; //Clear the green LED
P3SEL = BIT3+BIT4; // P3.4,5 = USCI_A0 TXD/RXD UCA0CTL1 |= UCSWRST; // **Put state machine in reset** UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 6; // 1MHz 9600 (see User's Guide) UCA0BR1 = 0; // 1MHz 9600 UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0, // over sampling UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** transmit(enter); UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt _enable_interrupts(); while(1){ if(cnt == 1){ //Check if cnt is 1 inputlength = arraylength(input); //Get your input length passwordlength = arraylength(password); //Get your password length {difference = compare(input,password);} //Compare the received password with your password if(difference == 0){ //Check if they match {transmit(correct);} //If they match, transmit correct string P1OUT &= ~RedLed; P4OUT |= GreenLed; //Turn on the green LED __delay_cycles(5000000); //Wait for 5 seconds P4OUT &= ~GreenLed; WDTCTL = WDT_MRST_0_064; // Turns off the LED too. //Reset the system } else {//If they do not match {transmit(incorrect);} //Transmit incorrect string P1OUT = RedLed; //Turn on the red LED __delay_cycles(2000000); //Wait for 2 seconds P4OUT &= ~RedLed; //Turn off the red LED {transmit(reenter);} }//Transmit reenter string cnt = 0; //Reset cnt RXByteCtr = 0; }}//Reset Receive Byte counter }
//USCI A receiver interrupt // The stuff immediately below is to make it compatible with GCC, TI or IAR
- if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
- pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
- elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
- else
- error Compiler not supported!
- endif
{ //Check if the UCA0RXBUF is different from 0x0A //(Enter key from keyboard) if(UCA0RXBUF != 0x0A) input[RXByteCtr++] = UCA0RXBUF; //If it is, load received character //to current input string element else{cnt = 1; //If it is not, set cnt input[RXByteCtr] = 0; } //Add null character at the end of input string } void transmit(char *str){ while(*str != 0){//Do this during current element is not //equal to null character while (!(UCTXIFG&UCA0IFG)); //Ensure that transmit interrupt flag is set UCA0TXBUF = *str++; //Load UCA0TXBUF with current string element }//then go to the next element }
int max(int a, int b){ if(a>b) return a; else return b; } // Find the max between two numbers.
int compare(char *strin, char *strpass){ int i=0; int result = 0;//Clear result int len = max((passwordlength),inputlength); for(i=0;i<len;i++) { result = result + abs((*strin++)-(*strpass++)); } // abs used to make sure differences don't cancel return result; }//Return result value
int arraylength(char *str){ int length = 0; //Clear length while(*str != 0){ //Until null character is reached str++; //Increase array address length++; } //Increase length value return length; }//Return length value
<\nowiki>