Sie sind auf Seite 1von 6

/**************************************************************************** * * This file contains MQX only stationery code. * ****************************************************************************/ #include <mqx.h> #include <bsp.h> #include <adc.

h> #include <io_gpio.h> #if ! BSPCFG_ENABLE_IO_SUBSYSTEM #error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option. #endif #ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED #error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option. #endif #if ! BSPCFG_ENABLE_ADC #error This application requires BSPCFG_ENABLE_ADC defined non-zero in user_config.h. Please recompile BSP with this option. #endif /* Task IDs */ #define MAIN_TASK 5 FILE_PTR port_file1,port_file2, port_file3; variables globales para utilisar en el gpio /* Function prototypes */ extern void main_task(uint_32); const TASK_TEMPLATE_STRUCT MQX_template_list[] = { /* Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice */ {MAIN_TASK, main_task, 800, 5, "Main", MQX_AUTO_START_TASK, 0, 0 }, {0} }; /* ADC device init struct */ const ADC_INIT_STRUCT adc_init = { ADC_RESOLUTION_DEFAULT, }; /* resolution */

/* Logical channel #1 init struct */ const ADC_INIT_CHANNEL_STRUCT adc_channel_param1 =

{ BSP_ADC_CH_POT, /* physical ADC channel */ ADC_CHANNEL_MEASURE_LOOP | ADC_CHANNEL_START_TRIGGERED, /* runs continuously after IOCTL trigger */ 10, /* number of samples in one run sequence */ 0, /* time offset from trigger point in us */ 600000, /* period in us (= 0.6 sec) */ 0x10000, /* scale range of result (not used now) */ 10, /* circular buffer size (sample count) */ ADC_TRIGGER_1, /* logical trigger ID that starts this ADC channel */ };

/*TASK*----------------------------------------------------** ** Task Name : main_task ** Comments : ** *END*-----------------------------------------------------*/ void main_task ( uint_32 initial_data ) { #if defined BSP_LED2 /* si esta definida BSP_LED2 en el hardware */ #define PINS1 /* define PINS1 como variable para controlar la estructura del LED2 */ GPIO_PIN_STRUCT pins2[] = * se asigna a GPIO_PIN_STRUCT pins2 para controlar el estado del LED2 */ { BSP_LED2 | GPIO_PIN_STATUS_0 , /* se inicializa el LED2 en estado 0 (apagado) */ GPIO_LIST_END /* se cierra la lista de inizialisacion del GPIO*/ }; #endif #if defined BSP_LED3 hardware */ #define PINS3 variable para controlar la estructura del LED3 */ GPIO_PIN_STRUCT pins3[] = pins3 para controlar el estado del LED3 */ { BSP_LED3 | GPIO_PIN_STATUS_0 , inicializa el LED3 en estado 0 (apagado) */ GPIO_LIST_END }; #endif /* si esta definida BSP_LED3 en el /* define PINS3 como /* se asigna a GPIO_PIN_STRUCT

/* se coloca cual led se va a utilizar y se /* se cierra la lista del GPIO*/

#if defined BSP_BUTTON1 && defined BSP_BUTTON2 /* si esta definida BSP_BUTTON1 y BSP_BUTTON2 en el hardware */ #define PINS4 /* define PINS4 como variable para controlar los botones 1 y 2 */ GPIO_PIN_STRUCT pins4[] = /* se asigna a GPIO_PIN_STRUCT pins4 para controlar el estado de los botones 1 y 2 */ { BSP_BUTTON1, /* se agregan cuales botones van hacer utilisados en este caso el 1 y el 2*/ BSP_BUTTON2, GPIO_LIST_END /* se cierra la lista del GPIO*/ }; #endif ADC_RESULT_STRUCT data; ADC_RESULT_STRUCT */ FILE_PTR f, f1; del adc*/ _mqx_int i = 0; /* se le asigna la variable data a /* se crean los punteros f y f1 que seran utilisados para el control

printf("\n\n-------------- Begin ADC-------------- \n\n");

printf("Opening ADC device ..."); f = fopen("adc:", (const char*)&adc_init); /* se abre un archivo utilisando el puntero f para inicalisar el adc*/ if(f != NULL) /* si se realizo bien la operacion despliega done*/ { printf("done\n"); } else /* si no realizo la operacion bloquea la tarea y nos muestra que ha fallado*/ { printf("failed\n"); _task_block(); } printf("Opening channel #1 ..."); f1 = fopen("adc:1", (const char*)&adc_channel_param1); /* se haber un nuevo archivo en el cual guardaremos la informacion sobre el canal del adc del potenciometro*/ if(f1 != NULL) /* si se realizo bien la operacion despliega done*/ { printf("done, prepared to start by trigger\n"); } else /* si no realizo la operacion bloquea la tarea y nos muestra que ha fallado*/ { printf("failed\n"); _task_block(); }

_time_delay(1000); printf("Triggering channel #1..."); ioctl(f, IOCTL_ADC_FIRE_TRIGGER, (pointer) ADC_TRIGGER_1); /* con la funcion ioctl se activa el tigger que dispara el adc*/ printf("triggered!\n"); if (NULL == (port_file3 = fopen("gpio:read", (char_ptr) &pins4 ))) /* se crea el archivo en port_file3 que sera el que controla los botones*/ { printf("Opening file3 GPIO with associated pins failed.\n"); _mqx_exit(-1); } if (NULL == (port_file1 = fopen("gpio:write", (char_ptr) &pins2 ))) /* se crea el archivo en port_file1 que sera el que controla el LED2*/ { printf("Opening file1 GPIO for pins2 failed.\n"); _mqx_exit(-1); } if (NULL == (port_file2 = fopen("gpio:write", (char_ptr) &pins3 ))) /* se crea el archivo en port_file2 que sera el que controla el LED3*/ { printf("Opening file1 GPIO for pins2 failed.\n"); _mqx_exit(-1); }

while(TRUE) { /* read pin/signal status to pin list */ if (IO_OK != ioctl(port_file3, GPIO_IOCTL_READ, (char_ptr) &pins4)) /* se utiliza el ioctl para poner a leer lo que hay en el port_file 3 (botones)*/ { printf("Reading pin status from file3 to pins4 list failed.\n"); _mqx_exit(-1); } if (pins4[0] & GPIO_PIN_STATUS) si esta en 0 se enciende un led rojo presionado para ctivar el adc*/ { ioctl(port_file2, GPIO_IOCTL_WRITE_LOG1, NULL); se esta esperando a que se presione el boton para activar el adc*/ /* clear pin to 0 (fast) */ /* enciende el led rojo indicando que /* esto se utiliza para controlar el boton 3 el cual indica que esta esperando a que sea

if (IO_OK != ioctl(port_file1, GPIO_IOCTL_WRITE_LOG0, NULL)) { printf("Clearing pins2 to log 0 failed.\n"); _mqx_exit(-1); } } else { /* si se presiono el boton se activa el adc*/ /* apaga el led rojo indicando que esta

ioctl(port_file2, GPIO_IOCTL_WRITE_LOG0, NULL); leyendo el adc*/ for(i = 0; i<=70 ; i++) adc*/ {

/* ciclo for de 70 lecturas que seran las que hara el

/* channel 1 sample ready? */ if (read(f1, &data, sizeof(data) )) /* le lo que se encuentra en al archivo f1(potenciometro) */ printf("ADC ch 1: %4d \n", data.result); /* imprime los valores que son tomados del adc por cada iteracion del ciclo*/ else printf(" "); /* si no se leyo bien el archivo del adc no imprime nada*/ if(data.result>=1000 ) /* si el dato en el adc es mayor que 1000 se enciende el led verde */ { ioctl(port_file1, GPIO_IOCTL_WRITE_LOG1, NULL); /* se enciende el led verde */ } if(data.result<=1000) /* si el dato en el adc es menor que 1000 se apaga el led verde */ { ioctl(port_file1, GPIO_IOCTL_WRITE_LOG0, NULL); verde */ }

/* se apaga el led

_time_delay(700); adc */ } }

/* retardo para que cada iteracion haya una lectura del

_time_delay(100); torre con la hyperterminal */ /* close all ADC channels */ printf("Closing channels..."); fclose(f1); fclose(f); printf("done!\n"); } _mqx_exit(0);

/* retardo para evitar problemas de velocidad de la

/* EOF */

Das könnte Ihnen auch gefallen