Part Number: MSP432P401R
Other Parts Discussed in Thread: MSP432WARE
Tool/software: TI-RTOS
My code below works just fine. It takes an ADC reading and turns an LED on if my sonar sensor has an object close to it and turns the LED off if an object is far away. It also has 2 interrupts for the on board buttons that are supposed to change the threshold values, this was from the example code and I am not using it. My question here is...I have 7 sonar sensors. I would like to set up an ISR for each Sonar sensor that will trigger based on a specified range for each one ( that way i can control the motors if objects are close ). My question is how do I set up an interrupt on an ADC reading based on a range I specify. I am trying to make an efficient system to where I have an infinite loop that does nothing and if any of the sonar sensors detect an object at a range I specify it will trigger it's own ISR and change motor speed and so on. I am sure I can accomplish this doing a bunch of if else statements for each reading that change the motor speeds, but I feel this would be a slow inefficient system. If I am wrong please let me know. Example code or API explanations on how to accomplish this would be useful. I am using the reference code from Simplelink
TI Drivers Lab GPIO Interrupt.
/*
* ======== empty.c ========
*/
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/ADC.h>
#include <ti/display/Display.h>
// #include <ti/drivers/I2C.h>
// #include <ti/drivers/SDSPI.h>
// #include <ti/drivers/SPI.h>
// #include <ti/drivers/UART.h>
// #include <ti/drivers/Watchdog.h>
/* Board Header file */
#include "Board.h"
/* GLOBAL VARIABLES FOR GUI COMPOSER */
uint16_t adcValue = 0;
uint16_t threshold = 500;
uint16_t trigger = 0;
/*
* ======== gpioButtonFxn0 ========
* Callback function for the GPIO interrupt on Board_GPIO_BUTTON0.
*/
void gpioButtonFxn0(uint_least8_t index)
{
/* Clear the GPIO interrupt and decrement threshold */
if(threshold < 250){ // Ensure threshold doesn't go below zero
threshold = 0;
} else {
threshold -= 250; // decrement by 250
}
}
/*
* ======== gpioButtonFxn1 ========
* Callback function for the GPIO interrupt on Board_GPIO_BUTTON1.
* This may not be used for all boards.
*/
void gpioButtonFxn1(uint_least8_t index)
{
/* Clear the GPIO interrupt and increment threshold */
if(threshold > 16133){ // Ensure threshold doesn't go above max ADC range
threshold = 16383;
} else {
threshold += 250; // increment by 250
}
}
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
/* ~10 loops/second */
uint32_t time = 100000;
/* Call driver init functions */
GPIO_init();
ADC_init();
// I2C_init();
// SDSPI_init();
// SPI_init();
// UART_init();
// Watchdog_init();
/* Open Display Driver */
Display_Handle displayHandle;
Display_Params displayParams;
Display_Params_init(&displayParams);
displayHandle = Display_open(Display_Type_UART, NULL);
/* Open ADC Driver */
ADC_Handle adc;
ADC_Params params;
ADC_Params_init(¶ms);
adc = ADC_open(Board_ADC6, ¶ms);
if (adc == NULL) {
// Error initializing ADC channel 0
while (1);
}
/* install Button callback..... 2 parameters, the IO that will
* trigger the interrupt & the callback function that we want to
* execute when the interrupt occurs. */
GPIO_setCallback(Board_GPIO_BUTTON0, gpioButtonFxn0);
GPIO_setCallback(Board_GPIO_BUTTON1, gpioButtonFxn1);
/* Enable interrupts */
GPIO_enableInt(Board_GPIO_BUTTON0);
GPIO_enableInt(Board_GPIO_BUTTON1);
while (1) {
int_fast16_t res;
res = ADC_convert(adc, &adcValue);
if (res == ADC_STATUS_SUCCESS) {
Display_printf(displayHandle, 1, 0, "ADC Reading %d", adcValue);
if(adcValue < threshold){
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
trigger = 1;
} else{
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);
trigger = 0;
}
}
usleep(time);
}
}