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

From Class Wiki
Jump to navigation Jump to search
(Created page with " .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 ...")
 
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
<nowiki>
.nowiki
/* This program asks you for the password, and then tells
/* This program asks you for the password, and then tells
* you if you typed it right. You need to set the bluetooth
* 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
* terminal to send \r\n, at the end of a send. The app
* I used on my Android phone is from:
* I used on my Android phone is from:
* https://github.com/Sash0k/bluetooth-spp-terminal
* https://github.com/Sash0k/bluetooth-spp-terminal
Line 16: Line 15:
* Note the changes to the interrupt as compared to the G2 Launchpad.
* Note the changes to the interrupt as compared to the G2 Launchpad.
*/
*/

#include <msp430.h>
#include <msp430.h>
#define RedLed BIT0
#define RedLed BIT0
#define GreenLed BIT7
#define GreenLed BIT7
char password[] = "12345"; //The Password
const char password[] = "12345"; //The Password
char enter[] = "Enter Your Password\r\n";
const char enter[] = "Enter Your Password\r\n";
char correct[] = "Your password is correct\r\n";
const char correct[] = "Your password is correct\r\n";
char incorrect[] = "Your password is incorrect\r\n";
const char incorrect[] = "Your password is incorrect\r\n";
char reenter[] = "Please re-enter your password\r\n";
const char reenter[] = "Please re-enter your password\r\n";
char input[100];
char input[100];
int RXByteCtr = 0;
unsigned int RXByteCtr = 0;
int cnt = 0;
int cnt = 0;
int inputlength,passwordlength;
int inputlength, passwordlength;
int difference; void transmit(char *str);
int difference;
int compare(char *strin, char *strpass);
void transmit(const char *str);
int arraylength(char *str);
int compare(const char *strin, const char *strpass);
int arraylength(const char *str);

int abs(int a);


/*
/*
Line 38: Line 37:
*/
*/
void main(void)
void main(void)

{
{
WDTCTL = WDTPW | WDTHOLD;

WDTCTL = WDTPW|WDTHOLD;
/* BCSCTL1 = CALBC1_1MHZ;//Adjust the clock
/* 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
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
P4DIR = GreenLed; //Make P4.7 an output so we can use the red LED
Line 48: Line 47:
P4OUT &= ~GreenLed; //Clear the green LED
P4OUT &= ~GreenLed; //Clear the green LED


P3SEL = BIT3+BIT4; // P3.4,5 = USCI_A0 TXD/RXD
P3SEL = BIT3 + BIT4; // P3.3,4 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0CTL1 |= UCSSEL_2; // SMCLK
Line 54: Line 53:
UCA0BR1 = 0; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
UCA0MCTL = UCBRS_0 + UCBRF_13 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
// over sampling
// over sampling
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
/*UCA0BR0 = 104;
//Low bit of UCBRx is 104
UCA0BR1 = 0;
//High bit of UCBRx is 0
UCA0MCTL = UCBRS_1;
//Second modulation stage select is 1
//Baud Rate = 9600
UCA0CTL1 &= ~UCSWRST;
//Clear SW reset, resume operation*/
transmit(enter);
transmit(enter);
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
_enable_interrupts();
_enable_interrupts();
while(1){
while (1) {
if(cnt == 1){
if (cnt == 1) {
//Check if cnt is 1
//Check if cnt is 1
inputlength = arraylength(input);
inputlength = arraylength(input);
Line 75: Line 65:
passwordlength = arraylength(password);
passwordlength = arraylength(password);
//Get your password length
//Get your password length
{
{difference = compare(input,password);}
difference = compare(input, password);
}
//Compare the received password with your password
//Compare the received password with your password
if(difference == 0){
if (difference == 0) {
//Check if they match
//Check if they match
{transmit(correct);}
{
transmit(correct);
}
//If they match, transmit correct string
//If they match, transmit correct string
P1OUT &= ~RedLed;
P1OUT &= ~RedLed;
Line 87: Line 81:
//Wait for 5 seconds
//Wait for 5 seconds
P4OUT &= ~GreenLed;
P4OUT &= ~GreenLed;
WDTCTL = WDT_MRST_0_064; // Turns off the LED too.
//WDTCTL = WDT_MRST_0_064; // Turns off the LED too.
transmit(enter);
//Reset the system
//Reset the system
} else { //If they do not match
}
else{
{
transmit(incorrect);
//If they do not match
}
{transmit(incorrect);}
//Transmit incorrect string
//Transmit incorrect string
P1OUT = RedLed;
P1OUT = RedLed;
Line 100: Line 95:
P4OUT &= ~RedLed;
P4OUT &= ~RedLed;
//Turn off the red LED
//Turn off the red LED
{transmit(reenter);}
{
//Transmit reenter string
transmit(reenter);
}
}
} //Transmit reenter string
cnt = 0;
cnt = 0;
//Reset cnt
//Reset cnt
RXByteCtr = 0;
RXByteCtr = 0;
}
//Reset Receive Byte counter
} //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 114:
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!
#error Compiler not supported!
#endif
#endif
{ //Check if the UCA0RXBUF is different from 0x0A
{
//(Enter key from keyboard)
//Check if the UCA0RXBUF is different from 0x0A
//(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;
cnt = 1;
//If it is not, set cnt
//If it is not, set cnt
input[RXByteCtr] = 0;
input[RXByteCtr-1] = 0;
//Add null character at the end of input string
} //Add null character at the end of input string (on the /r)
}
}
}
void transmit(char *str){
void transmit(const 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))
;
//Ensure that transmit interrupt flag is set
//Ensure that transmit interrupt flag is set
UCA0TXBUF = *str++;
UCA0TXBUF = *str++;
//Load UCA0TXBUF with current string element
//Load UCA0TXBUF with current string element
//then go to the next element
} //then go to the next element
}
}
}


int max(int a, int b){
int max(int a, int b) {
if(a>b) return a;
if (a > b)
else return b;
return a;
else
}
return b;
} // Find the max between two numbers.


int compare(char *strin, char *strpass){
int compare(const char *strin, const char *strpass) {
int i=0;
int i = 0;
int result = 0;//Clear result
int result = 0; //Clear result
/*if(inputlength <= passwordlength){
int len = max((passwordlength), inputlength);
for (i = len; i > 0; i--) {
//Check if passwordlength is greater than or
result = result + abs((*strin++) - (*strpass++));
//equal to inputlength
} // abs used to make sure differences don't cancel
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);
for(i=0;i<len;i++) {
result = result + abs((*strin++)-(*strpass++));
}
return result;
return result;
//Return result value
} //Return result value

}
int arraylength(char *str){
int arraylength(const char *str) {
int length = 0;
int length = 0;
//Clear length
//Clear length
while(*str != 0){
while (*str != 0) {
//Until null character is reached
//Until null character is reached
str++;
str++;
//Increase array address
//Increase array address
length++;
length++;
//Increase length value
} //Increase length value
}
return length;
return length;
//Return length value
} //Return length value

int abs(int a) {
if (a < 0)
a = -a;
return a;
}
}

</nowiki>

Latest revision as of 11:52, 29 November 2016

/* 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 \r\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
const char password[] = "12345"; //The Password
const char enter[] = "Enter Your Password\r\n";
const char correct[] = "Your password is correct\r\n";
const char incorrect[] = "Your password is incorrect\r\n";
const char reenter[] = "Please re-enter your password\r\n";
char input[100];
unsigned int RXByteCtr = 0;
int cnt = 0;
int inputlength, passwordlength;
int difference;
void transmit(const char *str);
int compare(const char *strin, const char *strpass);
int arraylength(const char *str);
int abs(int a);

/*
 * 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.3,4 = 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.
				transmit(enter);
				//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-1] = 0;
	}	//Add null character at the end of input string (on the /r)
}
void transmit(const 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(const char *strin, const char *strpass) {
	int i = 0;
	int result = 0; //Clear result
	int len = max((passwordlength), inputlength);
	for (i = len; i > 0; i--) {
		result = result + abs((*strin++) - (*strpass++));
	}  // abs used to make sure differences don't cancel
	return result;
}  //Return result value

int arraylength(const 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

int abs(int a) {
	if (a < 0)
		a = -a;
	return a;
}