API  2.3.0
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
ai_read.c

This example shows how to read analog values through an analog input.

/*
* @file ai.c
* @author TST
* @version V0.1
* @date 28/08/2014
* @brief Analog Input (AI) example.
* This example shows users how to initialize and how to read analog values from a selected analog input.
* The example uses the TSMART_AI_Read() function to provide values from 0 (0 volts) to 4095(3.3 volts).
*
*/
/* TSmarT header: This header file includes the necessary headers so that the program can work properly. */
#include "tsmart.h"
#define ANALOG_INPUT tsmart_ai0a /* Analog input in use: ai0a. */
/*
* AI_READ Task.
*
* This task reads the selected analog input every second.
*
*/
void vAI_READ(void *pvParameters){
/* Variables */
int32_t value;
for(;;){
value = TSMART_AI_Read(&ANALOG_INPUT); /* The analog value is saved in the "value" variable. */
vTaskDelay(1000/portTICK_RATE_MS); /* Waits for a second to read again. */
}
}
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_ai_config_t tsmart_ai_config;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Sets the analog input (AI) configuration. */
/* To work with the analog input, it is necessary to set up an analog-digital converter (ADC). */
tsmart_ai_config.ai_mode = TSMART_AI_MODE_INDEPENDENT;
tsmart_ai_config.ai_scan_conv_mode = DISABLE;
tsmart_ai_config.ai_continuous_conv_mode = DISABLE;
tsmart_ai_config.ai_data_align = TSMART_AI_DATAALIGN_RIGHT;
tsmart_ai_config.ai_sample_time = TSMART_AI_SAMPLETIME_41CYCLES5;
tsmart_ai_config.adc = TSMART_AI_ADC1;
/* Initializes an analog input: ANALOG_INPUT (ai0a). */
TSMART_AI_Init(&ANALOG_INPUT, &tsmart_ai_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* AI_READ task creation */
if(xTaskCreate(vAI_READ, "AI_READ", 256, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}