Jump to content
43oh

Msp430g2553 Launchpad v1.5 ADC Sampling Freq Setting Using Energia


Recommended Posts

hi all,

i'm using lauchpad v1.5 G2553 chip on it. I'm reading an anolog signal (150 Hz, 0-1.5Volts). The default sampling frequency for example potentiometer reading codes seems too slow. How can i set the sampling frequency to 1kHz using Energia (0101E0012 version) ?

Any sample code would be appreciated :) 

thanks...

Link to post
Share on other sites

I'm just using sample code that comes with the energia library...I'm monitoring my code on comport and also over Bluetotooth...

I can sample all dc values over a potentiometer, but when it comes to a ac signal (which is offsetted to be read by ADC)..

I really need to find out the way i select my sampling frequency at 1 kHz.

 

code:

---------------------------------------------

const int analogInPin = A0;  // my signal attached here (0-2 Volts, 150 Hz)
const int analogOutPin = 9; // Analog output pin that the LED.......no meaning for me to do this
 
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
 
void setup() {
  // initialize serial communications at 9600 bps:
   Serial.begin(9600);
}
void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 1023);  
  //outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           
 
  // print the results to the serial monitor:
  Serial.print("sensor = " ); 
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   
 
  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);    // i made this also 1 ms, didnt change the result!!                 
  }
-------------------
Link to post
Share on other sites

I think it's the serial 9600bps that's holding the ADC frequency here, when the serial buffer is full, the Serial.print must be waiting until some chars are transmitted before returning.

 

Try without the Serial.print

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 1023);  
  //outputValue = map(sensorValue, 0, 1023, 0, 255); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           
 
  delay(1);
  }
Link to post
Share on other sites

 

I'm just using sample code that comes with the energia library..

 

 

It does not mean that's it's the best for your usage ;)

 

 

 

I think it's the serial 9600bps that's holding the ADC frequency here, when the serial buffer is full, the Serial.print must be waiting until some chars are transmitted before returning.

 

 

+1

 

It was my idea when I asked for the code.

 

~30 bytes are sended each loop + 9600 baud <=> 9600/10 = 960 bytes/s <=> 960/30 => ~30 S/s max

 

So your low speed is normal. If you want to have a better acquisition speed, you may store it in an array during an acquisition period, and then send it by serial after.

 

Another way is to only send significant value : if you are studying a PWM signal, only transition are significant, so no need to send value if the precedent was the same ;)

Link to post
Share on other sites

Thank you all for your stimulating responses...

 

You are right, the serial process and conversion of the data to the integer values makes this slow down. 

 

It does not mean that's it's the best for your usage ;)

 

 

 

+1

 

It was my idea when I asked for the code.

 

~30 bytes are sended each loop + 9600 baud <=> 9600/10 = 960 bytes/s <=> 960/30 => ~30 S/s max

 

So your low speed is normal. If you want to have a better acquisition speed, you may store it in an array during an acquisition period, and then send it by serial after.

 

Another way is to only send significant value : if you are studying a PWM signal, only transition are significant, so no need to send value if the precedent was the same ;)

I think i cannot write the appropriate code to store the data and send it to the serial channel (UART) to make it faster. I may choose a ti kit which can use faster baudrate,like launchpad c2000 piccolo (115200 etc.)

By the same code, i can achieve 115200 / 10 cycle / 30 byte which makes nearly 380 Hz sampling. Am i right with this calculation?

 

- The other issue, 

Unfortunatelly, all my data is significant, its ECG signal from my analog circuit, i have to send all the data.

Link to post
Share on other sites

Thanks dbunet and B@tto,

What if i read the data just on bluetooth module, not over usb? can i achieve 115200 this time? does g2553 support this speed then?

i'll try to change the BT module's (HC 06) baud rate which is 9600 as default....

Link to post
Share on other sites

As I said, G2553 can support even much higher than 115200, so the G2553 is not a problem at all in your case. The problem comes from the conversion from uart out of the g2553 to your computer. So if you want to use BT, the limitation will come from your BT module.

 

Some tricks to improve speed : firstly, do not use texte like you did. You wasted a lot of bandwitch Serial.print("sensor = " ). Secondly use binary transmission. If you want to send your analogRead() value you can use a routine like this :

Measure = analogRead(Sensor);
Serial.write(Measure>>8);
Serial.write(Mesure&0xFF);

And on the computer side, just read the serial buffer as binary, not ASCII, do the invert bitwise and you may get your analog reading ;)

And it takes only two bytes of data so :

 

9600/10 = 960 <=> 960 / 2 = 490 hz , even in 9600 bauds :)

Link to post
Share on other sites

I'm too excited to try this B@tto, thanks a lot...

I'll report the results then...

 

As I said, G2553 can support even much higher than 115200, so the G2553 is not a problem at all in your case. The problem comes from the conversion from uart out of the g2553 to your computer. So if you want to use BT, the limitation will come from your BT module.

 

Some tricks to improve speed : firstly, do not use texte like you did. You wasted a lot of bandwitch Serial.print("sensor = " ). Secondly use binary transmission. If you want to send your analogRead() value you can use a routine like this :

Measure = analogRead(Sensor);
Serial.write(Measure>>8);
Serial.write(Mesure&0xFF);

And on the computer side, just read the serial buffer as binary, not ASCII, do the invert bitwise and you may get your analog reading ;)

And it takes only two bytes of data so :

 

9600/10 = 960 <=> 960 / 2 = 490 hz , even in 9600 bauds :)

Link to post
Share on other sites

For faster reading my new code will be as follows;

 

the output will be binary or the hexadecimal by ANDing 0xFF?

 

edit: just realized it! since i have 10 bits ADC, i have to send the data as 2 bytes as High and Low part. I only use last 2 bits of HSB and all LSB on the computer side...

--------------------------------

const int analogInPin = A0;  // Analog input pin ECG signal attached (150 Hz, 0-2Volts)
int sensorValue = 0;
void setup() {
 // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.write(sensorValue>>8);
Serial.write(sensorValue&0xFF);
  // wait 1 milliseconds before the next loop for the analog-to-digital converter to set after the last reading:
  delay(1);                     
}
Link to post
Share on other sites

Never that everything you do, even if it's written in decimal, hexadecimal ... Is always managed as binary. You would write :

Serial.write(sensorValue&255);

or 

Serial.write(sensorValue&0b11111111);

instead of 

Serial.write(sensorValue&0xFF);

It would exactly the same ;)

 

If your connection is not saturated, I recommand to you to send 255 two times before a new transmission, to ensure that on computer side a new transmission is arriving. Because if for a reason or another, the LSB transmission is lost (it could happen, it's rare but ...) your program will be desynchronised : MSB interpreted as LSB and vice versa.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...