Skip to content

Instantly share code, notes, and snippets.

@vjames19
Last active April 16, 2016 12:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjames19/9927646 to your computer and use it in GitHub Desktop.
Save vjames19/9927646 to your computer and use it in GitHub Desktop.
Get oxygen sensor data through UART for the msp430fr5739
#include <stdlib.h>
#include <string.h>
#include "msp430.h"
unsigned int CV = 0;
unsigned int DV = 0;
char RO = '0';
const double CRITICAL_OXYGEN_LEVEL = 1.5;
volatile double oxygenThreshold = 2;
volatile double oxygen = 15; // assign a value greater than 2
volatile short belowTwo = 0;
char oxygenData[6];
char AO = 'N';
void setClock(void){
CSCTL0_H = 0xA5; // Set password
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting
CSCTL2 = SELA_0 + SELS_3 + SELM_3; // set ACLK = XT1; MCLK = DCO
CSCTL3 = DIVA_5 + DIVS_3 + DIVM_0; // set all dividers
}
void setTimer(void) {
// TODO: Correct the interval by using the needed divisors.
TA0CCTL0 = CCIE; // TACCR0 interrupt enabled
TA0CCR0 = 0xFFFF;
TA0CTL = TASSEL_1 + MC_3; // SMCLK, UP mode
}
void setUart(void){
// Configure UART pins
P2SEL1 |= BIT0 + BIT1;
P2SEL0 &= ~(BIT0 + BIT1);
// Configure UART 0
UCA0CTL1 |= UCSWRST;
UCA0CTL1 = UCSSEL_2; // Set ACLK = 32768 as UCBRCLK
UCA0BR0 = 1; // 9600 baud
UCA0BR1 = 0;
UCA0MCTLW |= 0x00A1; // 32768/9600 - INT(32768/9600)=0.41
// UCBRSx value = 0x53 (See UG)
UCA0CTL1 &= ~UCSWRST; // release from reset
UCA0IE |= UCRXIE; // Enable RX interrupt
}
void setPorts(void) {
P1DIR |= BIT3 + BIT4 + BIT6;
P1OUT &= ~(BIT3 + BIT4 + BIT6);
}
void setStandbyMode(void) {
char command[] = "E\r";
// Send command.
for(int i=0; i < 2; i++) {
while (!(UCA0IFG&UCTXIFG));
UCA0TXBUF = command[i];
}
}
void sendReadCommand(void) {
char readCommand[] = "R\r";
// Send read command.
for(int i=0; i < 2; i++) {
while (!(UCA0IFG&UCTXIFG));
UCA0TXBUF = readCommand[i];
}
}
// check oxygen levels and set specific ports.
void checkOxygen(double oxygen){
if(oxygen < oxygenThreshold || (belowTwo == 1 && oxygen < 6) ) {
if(oxygen < CRITICAL_OXYGEN_LEVEL) {
AO = '0';
}
belowTwo = 1;
RO = '1';
P1OUT |= (BIT3 + BIT4 + BIT6);
} else {
AO = 'N';
belowTwo = 0;
RO = '0';
P1OUT &= ~(BIT3 + BIT4 + BIT6);
}
}
// Get the oxygen data from receive buffer XX.XX<CR>
double getOxygen() {
oxygenData[0] = UCA0RXBUF; // get first byte
for(int i = 1; i < 5; i++) { // get the remaining 4 bytes
while (!(UCA0IFG&UCRXIFG));
oxygenData[i] = UCA0RXBUF;
}
oxygenData[5] = '\0';
return atof(oxygenData);
}
int main(void){
WDTCTL = WDTPW + WDTHOLD; // stop watchdog
setPorts();
setClock();
setUart();
setStandbyMode();
setTimer();
__bis_SR_register(LPM0_bits + GIE);
// while(1) {
// __delay_cycles(50000000);
// sendReadCommand();
// }
while(1){}
}
#pragma vector = USCI_A0_VECTOR // USCI ISR
__interrupt void USCI_A0_ISR(void){
oxygen = getOxygen();
checkOxygen(oxygen);
}
// Timer A0 interrupt service routine
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
sendReadCommand();
}
@gharis
Copy link

gharis commented Apr 16, 2016

will this UART configurations work for 9600 bound rate UART device, to get a string to RX buffer from the a UART device?im working on a GPS project so i need to get NMEA strings to RX buffer .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment