diff options
author | Alexander <trotsenkoa@gmail.com> | 2022-06-29 11:03:02 +0300 |
---|---|---|
committer | Alexander <trotsenkoa@gmail.com> | 2022-06-29 11:03:02 +0300 |
commit | 1702ce6ce430a66bb7af51644b91b7c196e719d9 (patch) | |
tree | 6039acbdf047873d32aaf55969286c5f0d55958f /Libraries/LightSensor/light_sensor_task.c |
Создаю новый репозиторий. Программа для версии NixieClock_v2. Работает, часы тикают. Есть проблема, что скачет вторая сеносрная кнопка (она в другом канале). Поэтому на нее собираюсь сделать антидребезг.
Diffstat (limited to 'Libraries/LightSensor/light_sensor_task.c')
-rw-r--r-- | Libraries/LightSensor/light_sensor_task.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/Libraries/LightSensor/light_sensor_task.c b/Libraries/LightSensor/light_sensor_task.c new file mode 100644 index 0000000..59a0a3f --- /dev/null +++ b/Libraries/LightSensor/light_sensor_task.c @@ -0,0 +1,225 @@ +#include "light_sensor_task.h" +#include "ltimers.h" + +// FreeRTOS includes +#include "FreeRTOS.h" +#include "task.h" +#include "queue.h" +#include "semphr.h" + +// : +// - ? +// - +// - +// - 50 , 100 . + +// * GL5516 . +// +3.3 10. +// 6, +// 200 , , +// . +// , , , +// . +// +// . +// . +// +// - , +// - /. + +#define LIGHT_THREHSOLD 3300 // , +#define LIGHT 0 +#define DARK 1 + +#define TIME_LIGHT_SENSOR 3000 //ms + +static void ADC_Config(void); + +static uint16_t ADC_ConvertedValue = 0;//, ADC_ConvertedVoltage = 0; +QueueHandle_t queue_light_sensor; + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +void LightSensorInit ( void ) +{ + // - ADC init + ADC_Config (); + queue_light_sensor = xQueueCreate ( 1, sizeof (LightSensorState_t) ); + configASSERT( queue_light_sensor ); +} + + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +static void ADC_Config(void) +{ + ADC_InitTypeDef ADC_InitStructure; + GPIO_InitTypeDef GPIO_InitStructure; + + /* GPIOC Periph clock enable */ + RCC_AHBPeriphClockCmd(LIGHT_SENSOR_RCC_AHBPeriph_GPIOx, ENABLE); + + /* ADC1 Periph clock enable */ + LIGHT_SENSOR_RCC_APBxPeriphClockCmd(LIGHT_SENSOR_RCC_APBxPeriph_ADCx, ENABLE); + + /* Configure ADC Channelx as analog input */ + GPIO_InitStructure.GPIO_Pin = LIGHT_SENSOR_GPIO_PINx ; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; + GPIO_Init(LIGHT_SENSOR_GPIOx, &GPIO_InitStructure); + + /* GPIO pin configuration */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; + GPIO_Init ( GPIOB, &GPIO_InitStructure ); + + /* ADCs DeInit */ + ADC_DeInit(LIGHT_SENSOR_ADCx); + + /* Initialize ADC structure */ + ADC_StructInit(&ADC_InitStructure); + + /* Configure the ADC1 in continuous mode with a resolution equal to 12 bits */ + ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; + ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; + ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; + ADC_Init(LIGHT_SENSOR_ADCx, &ADC_InitStructure); + + /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ + ADC_ChannelConfig(LIGHT_SENSOR_ADCx, LIGHT_SENSOR_ADC_Channelx , ADC_SampleTime_239_5Cycles); + + /* ADC Calibration */ + ADC_GetCalibrationFactor(LIGHT_SENSOR_ADCx); + + /* Enable the ADC peripheral */ + ADC_Cmd(LIGHT_SENSOR_ADCx, ENABLE); + + /* Wait the ADRDY flag */ + while(!ADC_GetFlagStatus(LIGHT_SENSOR_ADCx, ADC_FLAG_ADRDY)); + + /* ADC1 regular Software Start Conv */ + ADC_StartOfConversion(LIGHT_SENSOR_ADCx); + +} + + +// ---------------------------------------------------------------------------- +// +// ---------------------------------------------------------------------------- +void ProcessFSM_LightSensor ( void ) +{ + static uint8_t process_light_state = 0; + LightSensorState_t light_sensor_state; + + // - curr prev light + //static uint8_t curr_light = DARK; + //static uint8_t prev_light = LIGHT; + + /* Test EOC flag */ + // - + while(ADC_GetFlagStatus(LIGHT_SENSOR_ADCx, ADC_FLAG_EOC) == RESET); + + /* Get ADC1 converted data */ + ADC_ConvertedValue = ADC_GetConversionValue(LIGHT_SENSOR_ADCx); + + switch ( process_light_state ) + { + case 0: // + if (ADC_ConvertedValue < LIGHT_THREHSOLD) // + { + // - , + StartLTimer (LTIMER_LIGHT_SENSOR); + // - + process_light_state = 2; + } + break; + + case 1: // + if (ADC_ConvertedValue > LIGHT_THREHSOLD) // + { + // - , + StartLTimer (LTIMER_LIGHT_SENSOR); + // - + process_light_state = 3; + } + break; + + case 2: + if ( GetLTimer (LTIMER_LIGHT_SENSOR) >= TIME_LIGHT_SENSOR ) + { + // - , + // + // - + light_sensor_state = LIGHT_SENSOR_STATE_LIGHT; + xQueueSend ( queue_light_sensor, &light_sensor_state, 0 ); + // - + process_light_state = 1; + } + else + { + // - , + // , , 0 + // () + if (ADC_ConvertedValue < LIGHT_THREHSOLD) + { + // + } + else + { + process_light_state = 0; + } + } + break; + + case 3: + if ( GetLTimer (LTIMER_LIGHT_SENSOR) >= TIME_LIGHT_SENSOR ) + { + // - , + // + // - + light_sensor_state = LIGHT_SENSOR_STATE_DARK; + xQueueSend ( queue_light_sensor, &light_sensor_state, 0 ); + // - + process_light_state = 0; + } + else + { + // - , + // , , 0 + // () + if (ADC_ConvertedValue > LIGHT_THREHSOLD) + { + // + } + else + { + process_light_state = 1; + } + } + break; + + default: + break; + + } + + vTaskDelay (100); + //GPIOB->ODR ^= GPIO_Pin_2; +} + + +// ---------------------------------------------------------------------------- +// , +// ---------------------------------------------------------------------------- +void LightSensor_Task ( void *pvParameters ) +{ + while(1)ProcessFSM_LightSensor (); + //vTaskDelete(NULL); +}
\ No newline at end of file |