This is another example of the Audio Circuit for my Hearing Guard System using the MSP-EXP432P401RMSP-EXP432P401R with the 430BOOST-SHARP96430BOOST-SHARP96 LCD showing the audio input I'm using TI Energia MT for MSP432 to prototype the display output but will use Code Composer Studio with TI-RTOS for the final code
Energia is TI's take on the Arduino IDE and works pretty much the same as the Arduino version but is only supported with the TI MSP432, MSP430, Tiva C (TM4C123, TM4C129) and CC1310 and CC1350 Launchpads. There are a number of examples that carry over from the Arduino examples and it is fairly simple to take an existing Arduino Example and port it over to a TI device in Energia. One cool feature of Energia MT is that is offers Multitasking capabilities for the MSP432 by way of TI-RTOS. With this, separate tasks can be created to handle different parts of the code such as LCD, ADC input, and GPIO processing. For my example, I created a separate task for the Sharp96 LCD, the Audio Input, and for the LED Bar Graph. Each of these run at different intervals with the Audio input running more frequently than the others.
For example, the initial tab in my Energia project is left blank so there is no over all managing task:
/* * Hear Guard System * Demo of Audio Input with Sharp96 LCD output using multitasking * * */
Then, I have a separate tab for the Audio Input Task:
/*
* Add shared libraries
*/
#include "MultiAnalogInput.h"
const int audioInput = 33; // select the input pin for the potentiometer
uint16_t audioValue = 0; // variable to store the value coming from the sensor
void setupAnalog2() {
Serial.begin(115200);
}
void loopAnalog() {
// read the value from the sensor:
audioValue = analogRead(audioInput);
Serial.print("Value read by audio task: ");
Serial.println(audioValue);
delay(10);
}
There is a separate task to handle the LED output (NOTE: This will eventually control the RGB LED color)
/*
* LEDOutput.ino
* Output to LED Bar Graph
*/
//const int ledPin = 40; // the code will flash the LED connected to pin 13
const uint16_t threshold = 500; // mic threshold sound level
const uint8_t ledCount = 10; // The number of LEDs in the bar graph
uint16_t ledPins[] = {23, 24, 25, 26, 27, 28, 29, 30, 31, 32}; // an array of pin numbers
void setupLEDOutput() {
Serial.begin(115200);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loopLEDOutput() {
int ledLevel = map(audioValue, 500, 1023, 0, ledCount);
Serial.print("LED Output = ");
Serial.println(ledLevel); // print out the data read in
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
} else { // turn off all pins higher than the ledLevel
digitalWrite(ledPins[thisLed], LOW);
}
}
for (uint8_t i=0; i<2; i++) delay(20);
}
And a separate task for the Sharp96 LCD
// Include application, user and local libraries
#include "SPI.h"
#include "OneMsTaskTimer.h"
#include "LCD_SharpBoosterPack_SPI.h"
#include "MultiAnalogInput.h"
LCD_SharpBoosterPack_SPI myScreen;
const int ledPin = 40; // the code will flash the LED connected to pin 40
const int maxVal = 1023;
int maxAudioVal = 0;
void setup() {
pinMode(ledPin, OUTPUT); // sets digital pin 40 as output
Serial.begin(115200);
myScreen.begin();
myScreen.clearBuffer();
myScreen.setFont(1);
myScreen.text(10, 10, "Hello!");
myScreen.flush();
for (uint8_t i=0; i<20; i++) delay(100);
}
void loop() {
Serial.print("Sample = ");
Serial.println(audioValue); // print out the data read in
myScreen.clearBuffer();
myScreen.setFont(0);
myScreen.text(15, 10, "READ AUDIO", LCDWrapNone);
myScreen.setFont(0);
myScreen.setCharXY(10, 40);
myScreen.text(10, 40, "Sample = ");
myScreen.setCharXY(60, 40);
myScreen.println(audioValue, DEC);
if (maxAudioVal < audioValue && audioValue != maxVal)
{
maxAudioVal = audioValue;
}
myScreen.setFont(0);
myScreen.text(10, 60, "MaxVal = ");
myScreen.setCharXY(60, 60);
myScreen.println(maxAudioVal, DEC);
myScreen.flush();
for (uint8_t i=0; i<2; i++) delay(100);
}
I had to include a header file to handle the shared variables otherwise the code would not compile:
/* * Header file to share sensor and sensor pin variables */ #include <stdint.h> extern const int audioInput; extern uint16_t audioValue;
When this is running, the Serial output can be used to view how often the tasks are running:
Value read by audio task: 462
Value read by audio task: 485
Value read by audio task: 484
Sample = 484
LED Output = 0
To see an example of this running, here be a link to a vid of this:
Top Comments