Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

Problem with queues - simple program with ADC and leds

Posted by bartx on September 8, 2014

Hello,

I'm working on simple project - tempereature sensor based on thermistor. Measured voltage is 0-3,3v and it's connected to ADC on FRDM KL25Z board (16-bit). So as an output I have 16-bit variable with values 0-65536. Depends on the values, I want to turn on/off led diodes. Code is listed below. I'm using CodeWarrior 10.6

My problem is, that it's not working as I expected. When temperature is rising, voltage is going down and one LED is turned on, after some time another led is also ON. But when voltage raises back to 3.3v leds are still on. I have to reset my board, then diodes are off. Anyone have an idea what's wrong with my code?

xQueueHandle queue_led;

void TaskTemp ( void *pvParameters )
{
while(1)
{
	queue_led = xQueueCreate( 2, sizeof( uint16_t ) );

	uint16_t temp_val;

	(void)AD1_Measure(TRUE);
	(void)AD1_GetValue16(&temp_val);

	xQueueSendToFront(queue_led, &temp_val, 100);
	vTaskDelay(100);
}

}
void TaskLed ( void *pvParameters )
{
while(1)
{

	uint16_t temp_val;

		xQueueReceive(queue_led, &temp_val, 100);

		if (temp_val<59000)
		{
			LED_1_SetVal();
		}
		else if (temp_val>59000)
		{
			LED_1_ClrVal();
		}


		if (temp_val<56000)
		{
			LED_2_SetVal();
		}
		else if (temp_val>56000)
		{
			LED_2_ClrVal();
		}

}
vTaskDelete ( NULL );

}

Code in main():

xTaskCreate(TaskLed, (signed char *)"tl", 150, NULL, 1, NULL);
xTaskCreate(TaskTemp, (signed char *)"tt", 150, NULL, 1, NULL);
vTaskStartScheduler();
return(0);

Problem with queues - simple program with ADC and leds

Posted by davedoors on September 8, 2014

TaskTemp() is creating a queue each time it goes around its while(1) loop, so it is posting to a different queue each time it calls xQueueSendToFront(), and TaskLed() is using a queue handle which is always changing. You will probably just run out of RAM eventually as this will be leaking lots of memory because there is no way to recover the queues that have already been created. Do you have a malloc failed hook defined to trap that happening?

I would recommend creating the queue before the tasks are created. That way the queue will already be created before either task tries to use it. You could also check the return value of the queue read to know if a new value has been received.


Problem with queues - simple program with ADC and leds

Posted by bartx on September 8, 2014

Works fine, thanks for your help :) Do you have any idea how to prevent leds from blinking? I'm trying different settings in TicksToWait parameter in QueueReceive/QueueSendtoFront but they're still blinking when value of the 16-bit variable is near value in the if ().


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner