sanpai88 wrote on Saturday, August 24, 2013:
Hello All,
I’m able to run three different tasks using RTOS but have problems when i have to Blink an Led on an interrupt.The LED blinks with interrupts perfectly fine without the RTOS.When i introduce the task with interrupts none of the other tasks work ,the interrupt first triggers once the code is loaded and all the other tasks freeze .When i disable the task with interrupts the other tasks work fine .I guess there’s an issue on the software side ,i have posted the code please let me know where i’m going wrong .
#include <FreeRTOS_ARM.h>
#include <Servo.h>
Servo servo;
#undef F
#define F(str) str
float temp=0;
float val=0;
int in1Pin = 22;
int in2Pin = 23;
int state=LOW;
int interuptpin=24;
xSemaphoreHandle sem=NULL;
static void CriticalTask(void *pvParameters) {
portTickType xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
for (;;) {
vTaskDelayUntil( &xLastWakeTime,20);
analogReadResolution(10);
val=analogRead(A0);
temp = ( 3.3 * val * 100.0) /1024;
if(temp>20.00)
{
digitalWrite(in1Pin,HIGsH);
digitalWrite(in2Pin,LOW);
}
else
{
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
}
}
}
static void ServoMotor(void *pvParameters) {
servo.attach(9);
for (;;) {
servo.write(90);
vTaskDelay((1000L * configTICK_RATE_HZ) / 1000L);
servo.write(0);
vTaskDelay((1000L * configTICK_RATE_HZ) / 1000L);
}
}
static void vLEDFlashTask(void *pvParameters) {
pinMode(13, OUTPUT);
// Flash led every 200 ms.
for (;;) {
// Turn LED on.
digitalWrite(13, HIGH);
// Sleep for 50 milliseconds.
vTaskDelay((50L * configTICK_RATE_HZ) / 1000L);
// Turn LED off.
digitalWrite(13, LOW);
// Sleep for 150 milliseconds.
vTaskDelay((150L * configTICK_RATE_HZ) / 1000L);
}
}
static void LedTurnOn(void *pvParameters) {
pinMode(25, OUTPUT);
for (;;) {
xSemaphoreTake(sem,5);
state = !state;
digitalWrite(25,state);
}
}
void InteruptHandler()
{
static signed portBASE_TYPE xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
Serial.println("\n Inside Interrupt");
xSemaphoreGiveFromISR(sem,&xHigherPriorityTaskWoken);
}
void setup() {
Serial.begin(9600);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(interuptpin,INPUT);
attachInterrupt(interuptpin,InteruptHandler,CHANGE);
sem = xSemaphoreCreateCounting(1, 0);
/* xTaskCreate(ServoMotor,
(signed portCHAR *)"ServoTask",
configMINIMAL_STACK_SIZE + 50,
NULL,3,NULL);*/
xTaskCreate(vLEDFlashTask,
(signed portCHAR *)"Blinker",
configMINIMAL_STACK_SIZE + 50,
NULL,
1,
NULL);
/*xTaskCreate(CriticalTask,
(signed portCHAR *)"Critical task",
configMINIMAL_STACK_SIZE + 50,
NULL,
4,
NULL); */
xTaskCreate(LedTurnOn,
(signed portCHAR *)"Working With Interrupt",
configMINIMAL_STACK_SIZE + 50,
NULL,
2,
NULL);
noInterrupts();
vTaskStartScheduler();
interrupts();
}
void loop() {
// put your main code here, to run repeatedly:
}