Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.39 KB | None | 0 0
  1. /*** INCLUDES *******************************************************/
  2. #include "HardwareProfile.h"
  3.  
  4. /* FreeRTOS.org includes. */
  5. #include "FreeRTOS.h"
  6. #include "task.h"
  7. #include "croutine.h"
  8. #include "list.h"
  9. #include "semphr.h"
  10. #include "queue.h"
  11. #include "timers.h"
  12.  
  13. /* Standard includes. */
  14. #include <stdio.h>
  15.  
  16. void vInitApp(void);
  17.  
  18. void vLED1Task( void *pvParameters );
  19. void vLED2Task( void *pvParameters );
  20. void vLED3Task( void *pvParameters );
  21. void vLED4Task( void *pvParameters );
  22.  
  23. /* Initialization of Change notification service. */
  24. void vEnableChangeNotification(void);
  25.  
  26. QueueHandle_t xLedQueue1;
  27. QueueHandle_t xLedQueue2;
  28. QueueHandle_t xLedQueue3;
  29. QueueHandle_t xLedQueue4;
  30.  
  31. const TickType_t turnOnTime = 400 / portTICK_RATE_MS;
  32. const TickType_t turnOffTime = 200 / portTICK_RATE_MS;
  33.  
  34. int main(void)
  35. {
  36.     vInitApp();
  37.    
  38.     xLedQueue1 = xQueueCreate( 5, sizeof( portBASE_TYPE ) );
  39.     xLedQueue2 = xQueueCreate( 5, sizeof( portBASE_TYPE ) );
  40.     xLedQueue3 = xQueueCreate( 5, sizeof( portBASE_TYPE ) );
  41.     xLedQueue4 = xQueueCreate( 5, sizeof( portBASE_TYPE ) );
  42.  
  43.     xTaskCreate( vLED1Task, "LED 1", 256, NULL, 2, NULL );
  44.     xTaskCreate( vLED2Task, "LED 2", 256, NULL, 2, NULL );
  45.     xTaskCreate( vLED3Task, "LED 3", 256, NULL, 2, NULL );
  46.     xTaskCreate( vLED4Task, "LED 4", 256, NULL, 2, NULL );
  47.  
  48.     vTaskStartScheduler();
  49.  
  50.     for( ;; );
  51.  
  52.     return 0;
  53. }//end main
  54.  
  55. void vLED1Task( void *pvParameters )
  56. {
  57.     portBASE_TYPE xReceivedValue;
  58.     portBASE_TYPE xTransmittedValue;
  59.  
  60.     for( ;; )
  61.     {
  62.         if( xQueueReceive(xLedQueue1, &(xReceivedValue), portMAX_DELAY ) == pdPASS )
  63.         {
  64.             if(xReceivedValue == 1)
  65.             {
  66.                 xTransmittedValue = 1;
  67.                 vTaskDelay(turnOnTime);
  68.                 mTurnOnLED(0);
  69.                 xQueueSend(xLedQueue2, (void * ) & xTransmittedValue, portMAX_DELAY);
  70.             }
  71.             else
  72.             {
  73.                 vTaskDelay(turnOffTime);
  74.                 mTurnOffLED(0);
  75.             }
  76.         }
  77.     }
  78. }
  79.  
  80. void vLED2Task( void *pvParameters )
  81. {
  82.     portBASE_TYPE xReceivedValue;
  83.     portBASE_TYPE xTransmittedValue;
  84.  
  85.     for( ;; )
  86.     {
  87.         if( xQueueReceive(xLedQueue2, &(xReceivedValue), portMAX_DELAY ) == pdPASS )
  88.         {
  89.             if(xReceivedValue == 1)
  90.             {
  91.                 xTransmittedValue = 1;
  92.                 vTaskDelay(turnOnTime);
  93.                 mTurnOnLED(1);
  94.                 xQueueSend(xLedQueue3, (void * ) & xTransmittedValue, portMAX_DELAY);
  95.             }
  96.             else
  97.             {
  98.                 xTransmittedValue = 0;
  99.                 vTaskDelay(turnOffTime);
  100.                 mTurnOffLED(1);
  101.                 xQueueSend(xLedQueue1, (void * ) & xTransmittedValue, portMAX_DELAY);
  102.             }
  103.         }
  104.     }
  105. }
  106.  
  107. void vLED3Task( void *pvParameters )
  108. {
  109.     portBASE_TYPE xReceivedValue;
  110.     portBASE_TYPE xTransmittedValue;
  111.  
  112.     for( ;; )
  113.     {
  114.         if( xQueueReceive(xLedQueue3, &(xReceivedValue), portMAX_DELAY ) == pdPASS )
  115.         {
  116.             if(xReceivedValue == 1)
  117.             {
  118.                 xTransmittedValue = 1;
  119.                 vTaskDelay(turnOnTime);
  120.                 mTurnOnLED(2);
  121.                 xQueueSend(xLedQueue4, (void * ) & xTransmittedValue, portMAX_DELAY);
  122.             }
  123.             else
  124.             {
  125.                 xTransmittedValue = 0;
  126.                 vTaskDelay(turnOffTime);
  127.                 mTurnOffLED(2);
  128.                 xQueueSend(xLedQueue2, (void * ) & xTransmittedValue, portMAX_DELAY);
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135. void vLED4Task( void *pvParameters )
  136. {
  137.     portBASE_TYPE xReceivedValue;
  138.     portBASE_TYPE xTransmittedValue;
  139.  
  140.     for( ;; )
  141.     {
  142.         if( xQueueReceive(xLedQueue4, &(xReceivedValue), portMAX_DELAY ) == pdPASS )
  143.         {
  144.             if(xReceivedValue == 1)
  145.             {
  146.                 vTaskDelay(turnOnTime);
  147.                 mTurnOnLED(3);
  148.             }
  149.             else
  150.             {
  151.                 xTransmittedValue = 0;
  152.                 vTaskDelay(turnOffTime);
  153.                 mTurnOffLED(3);
  154.                 xQueueSend(xLedQueue3, (void * ) & xTransmittedValue, portMAX_DELAY);
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. void vApplicationIdleHook( void )
  161. {
  162. }
  163.  
  164. void vInitApp(void)
  165. {
  166.     mInitLEDs(); /* Initialisation of LEDs */
  167.     mInitButtons(); /* Initialisation of Button */
  168.     vEnableChangeNotification(); /* Initialisation of CN intrrrupt */
  169. }
  170.  
  171. void vEnableChangeNotification(void)
  172. {
  173.     CNENAbits.CNIEA2 = 1; // Pin : RA2 ( S1 Button )
  174.     CNENAbits.CNIEA3 = 1; // Pin : RA3 ( S2 Button )
  175.     IEC1bits.CNIE = 1; // Enable CNI interrupt
  176.     __builtin_enable_interrupts(); // Enable global Interrupt
  177. }
  178.  
  179. /* Interrupt service routine for the CNI interrupt. */
  180. void __attribute__ (( interrupt, no_auto_psv )) _CNInterrupt ( void )
  181. {
  182.     BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  183.     portBASE_TYPE xTransmittedValue = 0xf;
  184.  
  185.     if(IFS1bits.CNIF == 1)
  186.     {
  187.         IFS1bits.CNIF = 0;
  188.         if(mGetButtonStatePB1())
  189.         {
  190.             xTransmittedValue = 1;
  191.             xQueueSendFromISR(xLedQueue1, (void*) &xTransmittedValue, &xHigherPriorityTaskWoken);
  192.         }
  193.         if(mGetButtonStatePB2())
  194.         {
  195.             xTransmittedValue = 0;
  196.             xQueueSendFromISR(xLedQueue4, (void*) &xTransmittedValue, &xHigherPriorityTaskWoken);
  197.         }
  198.         if( xHigherPriorityTaskWoken )
  199.         {
  200.             taskYIELD ();
  201.         }
  202.  
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement