Part 2 in a series that gets you RTOS-ready for the MSP432 LaunchPad
When you complete this example, you have a basic RTOS setup that integrates USB, ADC and a blinky. You can use Tasks, Semaphores and a UART interrupt. And you'll know how to register Tasks in the TI-RTOS configuration editor.
Part 1 sets up the project, explains how to configure a task and gets the Blinky running. Part 2 adds an ADC Sample Task. |
What you need:
- MSP432 LaunchPad
- 1 micro-USB cable
- Code Composer Studio
- TI-RTOS for MSP43X
- two potentiometers (say 10K or more, not critical), a little breadboard and a few wires
We're staring where we stopped in part 1. We'll add a task that samples two ADC pins.
The results get logged to the Code Composer Studio console.
We'll use two potentiometers to set and change the voltage on the two ADC pins.
Configure the ADC Driver in RTOS
First, let's set our sample strategy in the source file MSP_EXP432P401R.c.
This file standard has different settings for ADC0 and ADC1. Let's make them the same for this exercise.
/* ADC configuration structure */ const ADCMSP432_HWAttrs adcMSP432HWAttrs[MSP_EXP432P401R_ADCCOUNT] = { { .channel = ADC_INPUT_A0, .gpioPort = GPIO_PORT_P5, .gpioPin = GPIO_PIN5, .gpioMode = GPIO_TERTIARY_MODULE_FUNCTION, .refVoltage = REF_A_VREF2_5V, .resolution = ADC_14BIT }, { .channel = ADC_INPUT_A1, .gpioPort = GPIO_PORT_P5, .gpioPin = GPIO_PIN4, .gpioMode = GPIO_TERTIARY_MODULE_FUNCTION, .refVoltage = REF_A_VREF2_5V, .resolution = ADC_14BIT } };
The ADC reference voltage is 2.5V - when testing the exercise later, don't supply more than 2.5V on the pins.
They can take 3.3V, but you'll get an overflow as soon as you exceed the reference voltage.
In the main() function in datalogger.c, add the ADC driver initialisation.
int main(void) { /* Call board init functions */ Board_initGeneral(); Board_initGPIO(); Board_initADC(); // ...
This is a good time to build and run the project. It should compile without errors and the LED should blink as it did in part 1.
Add the ADC Task
The ADC task will run every 10 seconds, sample the two ADC pins and log the results to the CCS console.
Then it gives control back to RTOS, so that it can spend time to other activities (or go idle and put the controller to a lower power mode).
But first our task configeres the two ADC pins.
Create a new source file adc_sample.c.
Enter this code in that file.
/* XDCtools Header files */ #include <xdc/std.h> #include <xdc/runtime/System.h> #include <xdc/runtime/Error.h> /* BIOS Header files */ #include <ti/sysbios/BIOS.h> #include <ti/sysbios/knl/Task.h> #include <ti/sysbios/knl/Clock.h> /* TI-RTOS Header files */ #include <ti/drivers/ADC.h> /* Example/Board Header files */ #include "Board.h" /* * ======== taskAdcSample ======== * Open an ADC instance and get a sampling result from a one-shot conversion. */ Void taskAdcSample(UArg arg0, UArg arg1) { ADC_Handle adc0; ADC_Handle adc1; ADC_Params params; uint16_t adcValue0; uint16_t adcValue1; ADC_Params_init(¶ms); adc0 = ADC_open(Board_ADC0, ¶ms); adc1 = ADC_open(Board_ADC1, ¶ms); while (1) { Task_sleep(((UInt)arg0) / Clock_tickPeriod); /* Blocking mode conversion */ ADC_convert(adc0, &adcValue0); ADC_convert(adc1, &adcValue1); System_printf("Sample %u:%u\n", adcValue0, adcValue1); /* SysMin will only print to the console when you call flush or exit */ System_flush(); } // theoretically close the ADC driver. This code is never reached // ADC_close(adc0); // ADC_close(adc1); }
Then add the task to the RTOS configuration. Open the editor by double-clicking datalogger.cfg.
Save all files and rebuild.
You can execute the program. It will give random-ish readouts in the console because there's nothing connected to the ADC pins yet.
Hardware
Put your two potentiometers on the breadboard. Their value isn't that important. Anything between 10K and 1M is certainly ok. They don't need to have the same value. Whatever you have at home.
We'll use them to divide the 3V3 level of the launchpad, and send the value at the potentiometer's wiper to ADC0 and ADC1.
Wire the 3V3 of your LaunchPad to one sie of each potentiometer, GND to the other side.
The wiper of one goes to P5.4 and P5.5.
Set both potentiometers to the middle, plug the LaunchPad in the USB and start a debug session.
You'll see the values appear in the console.
Turn the potentiometers to see the values change. Once the wiper is above the reference value 2.5V, the results will not change because you've overrun the range.
Result
[CORTEX_M4_0] Starting the example System provider is set to SysMin. Halt the target to view any SysMin contents in ROV. Sample 5140: 3617 Sample 2620: 1194 Sample 1084: 426 Sample 459: 219 Sample 466: 272 Sample 380: 191 Sample 412: 205 Sample 380: 198 Sample 220: 90 Sample 220: 118 Sample 348: 303 Sample 259: 215 Sample 188: 91 Sample 211: 123 Sample 380: 223 Sample 233: 137 Sample 316: 165 Sample 252: 97 Sample 444: 209 Sample 230: 94 Sample 261: 61 Sample 380: 130 Sample 412: 211 Sample 207: 103 Sample 252: 95 Sample 252: 136
Top Comments