summaryrefslogtreecommitdiff
path: root/Libraries/LightSensor
diff options
context:
space:
mode:
authorAlexander <trotsenkoa@gmail.com>2022-06-29 11:03:02 +0300
committerAlexander <trotsenkoa@gmail.com>2022-06-29 11:03:02 +0300
commit1702ce6ce430a66bb7af51644b91b7c196e719d9 (patch)
tree6039acbdf047873d32aaf55969286c5f0d55958f /Libraries/LightSensor
Создаю новый репозиторий. Программа для версии NixieClock_v2. Работает, часы тикают. Есть проблема, что скачет вторая сеносрная кнопка (она в другом канале). Поэтому на нее собираюсь сделать антидребезг.
Diffstat (limited to 'Libraries/LightSensor')
-rw-r--r--Libraries/LightSensor/light_sensor_task.c225
-rw-r--r--Libraries/LightSensor/light_sensor_task.h26
2 files changed, 251 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
diff --git a/Libraries/LightSensor/light_sensor_task.h b/Libraries/LightSensor/light_sensor_task.h
new file mode 100644
index 0000000..f797ecc
--- /dev/null
+++ b/Libraries/LightSensor/light_sensor_task.h
@@ -0,0 +1,26 @@
+#ifndef LIGHT_SENSOR_TASK_INCLUDED
+#define LIGHT_SENSOR_TASK_INCLUDED
+
+#include "stm32f0xx_conf.h"
+
+#define LIGHT_SENSOR_GPIO_PINx GPIO_Pin_6
+#define LIGHT_SENSOR_GPIOx GPIOA
+#define LIGHT_SENSOR_RCC_AHBPeriph_GPIOx RCC_AHBPeriph_GPIOA
+
+#define LIGHT_SENSOR_ADCx ADC1
+#define LIGHT_SENSOR_ADC_Channelx ADC_Channel_6
+#define LIGHT_SENSOR_RCC_APBxPeriphClockCmd RCC_APB2PeriphClockCmd
+#define LIGHT_SENSOR_RCC_APBxPeriph_ADCx RCC_APB2Periph_ADC1
+
+typedef enum {
+
+ LIGHT_SENSOR_STATE_LIGHT,
+ LIGHT_SENSOR_STATE_DARK
+
+} LightSensorState_t;
+
+void LightSensorInit ( void );
+
+void LightSensor_Task ( void *pvParameters );
+
+#endif //LIGHT_SENSOR_TASK_INCLUDED \ No newline at end of file