Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. /**
  2. * @file
  3. * @brief Main program logic
  4. *
  5. * S1 Button: push to change states
  6. * - State 1: Reads POT value, writes to LEDs and send the value to UART \n
  7. * Push S2 button to invert LEDs
  8. * - State 2: Read TOUCH value then change the LED states if touched. \n
  9. * Send the readed values via UART
  10. * - State 3: Read character from UART then put the value to LEDs \n
  11. * Send the readed character ASCII code back to UART
  12. * @test
  13. * MPLABX 3.40 XC16 v1.26, XC8 v1.38
  14. * X2C Development board REV.C
  15. * dsPIC33EP256MC502 and PIC16F18855
  16. *
  17. * @author Mark Wendler
  18. * @author Jozsef KOPJAK
  19. * @see info@microstickplus.com
  20. * @see http://www.microstickplus.com
  21. *
  22. * @date 20-09-2016
  23. */
  24.  
  25. #include <xc.h>
  26. #include <stdint.h>
  27. #include "HardwareProfile.h"
  28. #include "Drivers/functions.h"
  29.  
  30. /* FreeRTOS.org includes. */
  31. #include "FreeRTOS.h"
  32. #include "task.h"
  33. #include "semphr.h"
  34. /**
  35. * @brief Initialise the hardware
  36. * - Init LED
  37. * - Init Buttons
  38. * - Init AD
  39. * - Init UART
  40. **/
  41. void vInitApp(void);
  42.  
  43. /**
  44. * @brief Task invert LED1 (1 Hz)
  45. **/
  46. void vTask1( void *pvParameters );
  47. void vTask2( void *pvParameters );
  48. void vTask3( void *pvParameters );
  49. void vTask4( void *pvParameters );
  50.  
  51. /**
  52. * @brief Main program logic
  53. * 1. Configure the hardware
  54. * 2. Start tasks
  55. * */
  56. xSemaphoreHandle xBinarySemaphore1;
  57. xSemaphoreHandle xBinarySemaphore2;
  58. xSemaphoreHandle xBinarySemaphore3;
  59. xSemaphoreHandle xBinarySemaphore4;
  60.  
  61. int main(void) {
  62.  
  63. vSemaphoreCreateBinary(xBinarySemaphore1);
  64. vSemaphoreCreateBinary(xBinarySemaphore2);
  65. vSemaphoreCreateBinary(xBinarySemaphore3);
  66. vSemaphoreCreateBinary(xBinarySemaphore4);
  67. xSemaphoreTake( xBinarySemaphore1, 10 );
  68. xSemaphoreTake( xBinarySemaphore2, 10 );
  69. xSemaphoreTake( xBinarySemaphore3, 10 );
  70. xSemaphoreTake( xBinarySemaphore4, 10 ) ;
  71.  
  72.  
  73.  
  74. vInitApp(); /* Hardware initialization */
  75.  
  76. xTaskCreate( vTask1, "Task1", 256, NULL, 1, NULL );
  77. xTaskCreate( vTask2, "Task2", 256, NULL, 1, NULL );
  78. xTaskCreate( vTask3, "Task3", 256, NULL, 1, NULL );
  79. xTaskCreate( vTask4, "Task4", 256, NULL, 1, NULL );
  80.  
  81. xSemaphoreGive( xBinarySemaphore1 );
  82.  
  83. vTaskStartScheduler();
  84.  
  85. for( ;; );
  86. }
  87.  
  88. // Task Function
  89. void vTask1 ( void *pvParameters) {
  90. for(;;)
  91. {
  92.  
  93. if( xBinarySemaphore1 != NULL )
  94. {
  95.  
  96. if( xSemaphoreTake( xBinarySemaphore1, 10 ) == pdTRUE )
  97. {
  98. mTurnOnLED(0);
  99.  
  100. vTaskDelay( 250 / portTICK_PERIOD_MS );
  101. mTurnOffLED(0);
  102. xSemaphoreGive( xBinarySemaphore2 );
  103.  
  104. }
  105. }
  106. }
  107. }
  108. void vTask2 ( void *pvParameters) {
  109.  
  110. for(;;)
  111. {
  112.  
  113. if( xBinarySemaphore2 != NULL )
  114. {
  115.  
  116. if( xSemaphoreTake( xBinarySemaphore2, 10 ) == pdTRUE )
  117. {
  118.  
  119. mTurnOnLED(1);
  120.  
  121. vTaskDelay( 250 / portTICK_PERIOD_MS );
  122. mTurnOffLED(1);
  123. xSemaphoreGive( xBinarySemaphore3 );
  124.  
  125. }
  126. }
  127.  
  128. }
  129. }
  130. void vTask3 ( void *pvParameters) {
  131.  
  132. for(;;)
  133. {
  134.  
  135. if( xBinarySemaphore3 != NULL )
  136. {
  137.  
  138. if( xSemaphoreTake( xBinarySemaphore3, 10 ) == pdTRUE )
  139. {
  140. mTurnOnLED(2);
  141.  
  142. vTaskDelay( 250 / portTICK_PERIOD_MS );
  143. mTurnOffLED(2);
  144. xSemaphoreGive( xBinarySemaphore4 );
  145. }
  146.  
  147.  
  148. }
  149. }
  150. }
  151. void vTask4 ( void *pvParameters) {
  152.  
  153. for(;;)
  154. {
  155.  
  156. if( xBinarySemaphore4 != NULL )
  157. {
  158.  
  159. if( xSemaphoreTake( xBinarySemaphore4, 10 ) == pdTRUE )
  160. {
  161. mTurnOnLED(3);
  162.  
  163. vTaskDelay(250 / portTICK_PERIOD_MS );
  164. mTurnOffLED(3);
  165. xSemaphoreGive( xBinarySemaphore1 );
  166. }
  167. }
  168. }
  169.  
  170. }
  171.  
  172.  
  173.  
  174. /**
  175. * @brief Initialise the hardware
  176. * - Init LED
  177. * - Init Buttons
  178. * - Init AD
  179. * - Init UART
  180. **/
  181. void vInitApp(void) {
  182. mInitLEDs(); /* Initialisation of LEDs */
  183. mInitButtons(); /* Initialisation of Button */
  184. mTurnOffAnalogMode(); /* Turn off analog mode */
  185. vInitAD(); /* Initialisation of AD module */
  186. vInitUART(); /* Initialisation of UART1 module */
  187. }
  188.  
  189. void __attribute__ ((__interrupt__, __auto_psv__)) _StackError(void)
  190. {
  191. while(1){
  192. Nop();
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement