Last active
August 29, 2015 13:58
-
-
Save vjames19/10010615 to your computer and use it in GitHub Desktop.
UART communication with oxygen sensor with msp430 launchpad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include "msp430.h" | |
#define TXD BIT2 | |
#define RXD BIT1 | |
unsigned int CV = 0; | |
unsigned int DV = 0; | |
unsigned int RO = 0; | |
double oxygen = 15; // assign a value greater than 2 | |
char oxygenData[6]; | |
//void setClock(void){ | |
// CSCTL0_H = 0xA5; | |
// CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting | |
// CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = XT1; MCLK = DCO | |
// CSCTL3 = DIVA_3 + DIVS_0 + DIVM_0; // set all dividers | |
//} | |
void setUart(void){ | |
// Configure UART pins | |
// P1SEL |= TXD + RXD; | |
// P1SEL2 |= TXD + RXD; | |
// // Configure UART 0 | |
// UCA0CTL1 |= UCSWRST; | |
// UCA0CTL1 = UCSSEL_2; // Set ACLK = 32768 as UCBRCLK | |
// UCA0BR0 = 27; // 9600 baud | |
// UCA0BR1 = 0; | |
// UCA0MCTL = UCBRS2 + UCBRS0; // 32768/9600 - INT(32768/9600)=0.41 | |
// // UCBRSx value = 0x53 (See UG) | |
// UCA0CTL1 &= ~UCSWRST; // release from reset | |
// //UCA0IE |= UCRXIE; // Enable RX interrupt | |
//// IE2 |= UCA0RXIE; | |
// UC0IE |= UCA0RXIE; | |
DCOCTL = 0; // Select lowest DCOx and MODx settings | |
BCSCTL1 = CALBC1_1MHZ; // Set DCO | |
DCOCTL = CALDCO_1MHZ; | |
P2DIR |= 0xFF; // All P2.x outputs | |
P2OUT &= 0x00; // All P2.x reset | |
P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD | |
P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD | |
P1OUT &= 0x00; | |
UCA0CTL1 |= UCSSEL_2; // SMCLK | |
UCA0BR0 = 26; // 1MHz 115200 | |
UCA0BR1 = 0x00; // 1MHz 1152 | |
// UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5 | |
UCA0MCTL = 0; | |
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine** | |
UC0IE |= UCA0RXIE; // Enable USCI_A0 RX interrupt | |
} | |
void setPorts(void) { | |
P2DIR |= 0xFF; | |
P2OUT &= 0x00; | |
P1OUT &= 0x00; | |
// P1DIR |= TXD; | |
// P1DIR &= ~RXD; | |
P1DIR |= BIT3 + BIT4; | |
P1OUT &= ~(BIT3 + BIT4); | |
} | |
// check oxygen levels and set specific ports. | |
void checkOxygen(double oxygen){ | |
if(oxygen > 2) { | |
RO = 1; | |
P1OUT |= BIT3; | |
P1OUT |= BIT4; | |
} else { | |
RO = 0; | |
P1OUT &= ~BIT3; | |
P1OUT &= ~BIT4; | |
} | |
} | |
// Get the oxygen data from receive buffer | |
double getOxygen() { | |
int i = 0; | |
oxygenData[0] = UCA0RXBUF; // get first byte | |
for(i = 1; i < 5; i++) { // get the remaining 4 bytes | |
while (!(IFG2&UCA0RXIFG)); | |
oxygenData[i] = UCA0RXBUF; | |
} | |
oxygenData[5] = '\0'; | |
return atof(oxygenData); | |
} | |
int main(void){ | |
WDTCTL = WDTPW + WDTHOLD; // stop watchdog | |
setPorts(); | |
// setClock(); | |
setUart(); | |
// __delay_cycles(2000); | |
__bis_SR_register(GIE); | |
while(1) {} | |
} | |
#pragma vector=USCIAB0RX_VECTOR | |
__interrupt void USCI0RX_ISR(void) | |
{ | |
// UCA0IFG &=~ UCRXIFG; // Clear interrupt | |
oxygen = getOxygen(); | |
// printf("%3.2f\n",oxygen); | |
checkOxygen(oxygen); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment