Other Parts Discussed in Thread: MSP430G2231, MSP430G2553
Hello all,
I am very new to Micro controller programming. I am using the MSP430G2231 with launchpad. My objective is to sample sound signal and send the analog to digital value to computer where it has to saved in text file. I am communicating through USB port, and using p1.7 as the input channel for the sound signal. I have written the following code,
For MSP430G2231:
/*
* main.c
*/
#include "msp430g2231.h"
#define LED0 BIT0
#define LED1 BIT6
#define TXD BIT1 // TXD on P1.1
// Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz
#define Bitime_5 20 // ~ 0.5 bit length + small adjustment
#define Bitime 52
int value=0;
unsigned char BitCnt; //Bit Counter
int TXByte; //Data to send
void ConfigureTimerUart(void);
void Transmit(void);
void ConfigureAdc(void)
{
/* Configure ADC Channel */
ADC10CTL1 = INCH_7 + ADC10DIV_3 ; // Channel 7, ADC10CLK/4
ADC10CTL0 = SREF_0 + ADC10SHT_3 + ADC10ON + ADC10IE; //Vcc & Vss as reference
ADC10AE0 |= BIT7; //P1.7 ADC option
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO = 1MHz
P1OUT &= ~(LED0 + LED1);
P1SEL |= TXD; // Select I/O pins for UART
P1DIR |= TXD;
ConfigureAdc();
__enable_interrupt();
while(1)
{
//__delay_cycles(1000); // Wait for ADC Ref to settle
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled
value = ADC10MEM;
if(value>255)
{
//flag sent
int n = value/255;
ConfigureTimerUart();
TXByte = n+1;
Transmit();
//Break it into multiple bytes
while(value > 0)
{
int x = value - 255;
if(x < 0)
{
ConfigureTimerUart();
TXByte = value;
Transmit();
}
else
{
ConfigureTimerUart();
TXByte = 255;
Transmit();
}
value = x;
}
}
else
{
ConfigureTimerUart();
TXByte = value;
Transmit();
}
}
}
void ConfigureTimerUart(void)
{
CCTL0 = OUT; // TXD Idle as Mark
TACTL = TASSEL_2 + MC_2 + ID_3; // SMCLK/8, continuous mode
}
// Function Transmits Character from TXByte
void Transmit()
{
while (CCR0 != TAR) // Prevent async capture
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
TXByte |= 0x100; // Add mark stop bit to TXByte
TXByte = TXByte << 1; // Add space start bit
CCTL0 = CCIS0 + OUTMOD0 + CCIE; // TXD = mark = idle
while ( CCTL0 & CCIE ); // Wait for TX completion
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
static unsigned char txBitCnt = 10;
CCR0 += Bitime; // Add Offset to CCR0
if (CCTL0 & CCIS0) // TX on CCI0B?
{
if ( txBitCnt == 0)
{
CCTL0 &= ~ CCIE;
txBitCnt = 10;
// All bits TXed, disable interrupt
}
else
{
CCTL0 |= OUTMOD2; // TX Space
if (TXByte & 0x01)
CCTL0 &= ~ OUTMOD2; // TX Mark
TXByte = TXByte >> 1;
txBitCnt --;
}
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(CPUOFF); // Return to active mode
}
And for the front end in java :
package javaapplication3;
import gnu.io.*;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.TooManyListenersException;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
byte[] readBuffer;
int count=0;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("portList... " + portList);
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("port identified is Serial.. "
+ portId.getPortType());
if (portId.getName().equals("COM9")) {
System.out.println("port identified is COM9.. "
+ portId.getName());
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
} else {
System.out.println("unable to open port");
}
}
}
}
public SimpleRead() {
try {
System.out.println("In SimpleRead() contructor");
serialPort = (SerialPort) portId.open("SimpleReadApp",2400);
System.out.println(" Serial Port.. " + serialPort);
} catch (Exception e) {
System.out.println("Port in use Exception");
}
try {
inputStream = serialPort.getInputStream();
System.out.println(" Input Stream... " + inputStream);
} catch (IOException e) {
System.out.println("IO Exception");
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
System.out.println("Tooo many Listener exception");
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_MARK);
// no handshaking or other flow control
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
System.out.println("................");
} catch (UnsupportedCommOperationException e) {
System.out.println("UnSupported comm operation");
}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
System.out.println("In run() function ");
Thread.sleep(1);
} catch (InterruptedException e) {
System.out.println("Interrupted Exception in run() method");
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
readBuffer = new byte[8];
try {
while (inputStream.available()>0) {
count++;
System.out.println("Count = " + count);
double numBytes = inputStream.read();
if(numBytes == 2)
{
double numBytes1 = inputStream.read();
double numBytes2 = inputStream.read();
numBytes = numBytes2 + numBytes1;
}
if(numBytes == 3)
{
double numBytes1 = inputStream.read();
double numBytes2 = inputStream.read();
double numBytes3 = inputStream.read();
numBytes = numBytes2 + numBytes1 + numBytes3;
}
numBytes = numBytes * 3.48;
DecimalFormat df = new DecimalFormat("#.##");
String output = df.format(numBytes);
FileWriter fwo = new FileWriter( "d:\\test.txt", true );
BufferedWriter bwObj = new BufferedWriter( fwo );
bwObj.write( output );
bwObj.newLine( );
bwObj.close( );
System.out.println(output);
}
} catch (IOException e) {
System.out.println("IO Exception in SerialEvent()");
}
break;
}
}
}
The code work fine, but the sampling rate is too slow. I need to sample at the maximum speed to increase accuracy since it is a sound signal. Please suggest me ways to increase the sampling speed at the maximum rate.
Thank you..