Project for Driver Development and Resource Sharing Implementation
#include
#include "LiquidCrystal_I2C.h"
LiquidCrystal_I2C lcd(0x27, 20,4);
int sensorValue1,sensorValue2;
SemaphoreHandle_t adcSemaphore;
void TaskDigitalRead( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
void channel_0()
{
/** Configure Regular Channel
*/
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void channel_1()
{
/** Configure Regular Channel
*/
ADC_ChannelConfTypeDef sConfig = {0};
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.LowPowerAutoPowerOff = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_5;
hadc1.Init.OversamplingMode = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
void setup()
{
MX_ADC1_Init();
adcSemaphore = xSemaphoreCreateBinary();
xTaskCreate(TaskDigitalRead,"DigitalRead",80,NULL,1,NULL);
xTaskCreate(TaskAnalogRead,"AnalogRead",80,NULL,1,NULL);
vTaskStartScheduler();
}
void loop()
{
}
void TaskDigitalRead( void *pvParameters ) // This is a Task.
{
while(1)
{
// read the input pin:
channel_1();
HAL_ADC_Start(&hadc1);
while(HAL_ADC_PollForConversion(&hadc1, 100)!=HAL_OK);
value1=HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
xSemaphoreGive( adcSemaphore );
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void TaskAnalogRead( void *pvParameters ) // This is a Task.
{
while(1)
{
if(xSemaphoreTake(adcSemaphore,portMAX_DELAY)==pdTRUE)
{
channel_0();
HAL_ADC_Start(&hadc1);
while(HAL_ADC_PollForConversion(&hadc1, 100)!=HAL_OK);
value=HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 2);
lcd.print("ADC1 : ");
lcd.print(sensorValue2);
lcd.setCursor(0, 3);
lcd.print("ADC2 : ");
lcd.print(sensorValue1);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}