freertos problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

freertos problem

 
0
  #1
Jul 21st, 2009
I am currently working on a real time operating system(freertos) task,

there are 3 tasks here and the problem im having is with the 1st task. it is basically to have 2 modes, toggled by a single button "RIGHT" (keys = ~PTH & BOTH). this has got to go basically from what it is now, to a mode that just displays "RESET" on the screen. it will toggle between these two modes by pressing the "RIGHT" button. please help in anyway you can!


  1. #define _SCI
  2.  
  3. #include <stdio.h>
  4.  
  5. /* Kernel includes. */
  6. #include "FreeRTOS.h"
  7. #include "task.h"
  8.  
  9. /*-----------------------------------------------------------
  10.   Definitions.
  11. -----------------------------------------------------------*/
  12.  
  13. /* Priorities assigned to demo application tasks. */
  14. #define mainCOM_PRIORITY1 ( tskIDLE_PRIORITY + 1 )
  15. #define mainCOM_PRIORITY2 ( tskIDLE_PRIORITY + 2 )
  16.  
  17. /*-----------------------------------------------------------
  18.   Local functions prototypes.
  19. -----------------------------------------------------------*/
  20. static portTASK_FUNCTION_PROTO( vCOMSendTask1, pvParameters );
  21. static portTASK_FUNCTION_PROTO( vCOMSendTask2, pvParameters );
  22.  
  23. void main( void )
  24. {
  25. // Set up PORTB as output and turn all LEDs off
  26. DDRB = 0xFF;
  27. PORTB = 0xFF;
  28. // Light the top (b7) LED
  29. PORTB &= ~bit(7); /* Clear appropriate bit on port */
  30.  
  31. /* Create two comms tasks.*/
  32. xTaskCreate( vCOMSendTask1, (const signed portCHAR * const)"COM1", configMINIMAL_STACK_SIZE, NULL, mainCOM_PRIORITY1, NULL );
  33. xTaskCreate( vCOMSendTask2, (const signed portCHAR * const)"COM2", configMINIMAL_STACK_SIZE, NULL, mainCOM_PRIORITY2, NULL );
  34.  
  35. /* The tasks have been created - start the scheduler. */
  36. vTaskStartScheduler();
  37.  
  38. /* Should not reach here! */
  39. for( ;; );
  40. }
  41. /*-----------------------------------------------------------*/
  42. static portTASK_FUNCTION( vCOMSendTask1, pvParameters )
  43. {
  44. portTickType xLastWakeTime;
  45. portTickType xFlashRate = 1000 / portTICK_RATE_MS; // Char rate in ms
  46.  
  47. xLastWakeTime = xTaskGetTickCount();
  48.  
  49. for(;;)
  50. {
  51. putchar('1');
  52. vTaskDelayUntil( &xLastWakeTime, xFlashRate );
  53. }
  54. }
  55. /*-----------------------------------------------------------*/
  56. static portTASK_FUNCTION( vCOMSendTask2, pvParameters )
  57. {
  58. portTickType xLastWakeTime;
  59. portTickType xFlashRate = 1500 / portTICK_RATE_MS; // Char rate in ms
  60.  
  61. xLastWakeTime = xTaskGetTickCount();
  62.  
  63. for(;;)
  64. {
  65. putchar('2');
  66. vTaskDelayUntil( &xLastWakeTime, xFlashRate );
  67. }
  68. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: freertos problem

 
0
  #2
Jul 21st, 2009
You set your outputs, but is the default for your ports set to input? If not, no key to read!


Also you talk about inputs but I see no code for it!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: freertos problem

 
0
  #3
Jul 21st, 2009
im soooo sorry, i put up the wrong program, here is the actual program im working on!

  1.  
  2. #define _SCI
  3.  
  4. #include <stdio.h>
  5.  
  6. /* Kernel includes. */
  7. #include "FreeRTOS.h"
  8. #include "task.h"
  9. #include "queue.h"
  10.  
  11. /*-----------------------------------------------------------
  12.   Definitions.
  13. -----------------------------------------------------------*/
  14.  
  15. /* Priorities assigned to application tasks. */
  16. #define Buttons_PRIORITY ( tskIDLE_PRIORITY + 1 )
  17. #define Clock_PRIORITY ( tskIDLE_PRIORITY + 2 )
  18. #define mainLED_PRIORITY ( tskIDLE_PRIORITY + 3 )
  19. #define Osc_PRIORITY ( tskIDLE_PRIORITY + 4 )
  20.  
  21. void lcd_init(void);
  22. void lcd_clear(void);
  23. void lcd_putstr(char *);
  24. void lcd_putxy(char, char, char *);
  25.  
  26. // LED number to flash
  27. #define flashLED 6
  28.  
  29. // Define bit patterns for key values and buttons
  30. #define LEFT 0x02 /* PT1/S3 button */
  31. #define RIGHT 0x01 /* PT0/S2 button */
  32. #define BOTH (LEFT | RIGHT) /* Mask for both keys */
  33.  
  34. // Number of timer interrupts to get 1ms
  35. #define DELAY1ms 750
  36.  
  37. /*-----------------------------------------------------------
  38.   Local functions prototypes.
  39. -----------------------------------------------------------*/
  40. static portTASK_FUNCTION_PROTO( vButtonsTask, pvParameters );
  41. static portTASK_FUNCTION_PROTO( vClockTask, pvParameters );
  42. static portTASK_FUNCTION_PROTO( vLEDflashTask, pvParameters );
  43. static portTASK_FUNCTION_PROTO( vOscDispTask, pvParameters );
  44.  
  45. static void vSetupTimers(void);
  46.  
  47. /* Interrupt routimes */
  48. static void TimerCh4_ISR(void);
  49. static void TimerCh3_ISR(void);
  50.  
  51. static volatile unsigned int osc_ticks; /* oscillator speed in ms */
  52. static volatile unsigned int count1ms; /* Incremented by TC3 1ms interrupt */
  53.  
  54. void main( void )
  55. {
  56. // Set up PORTB as output and turn all LEDs off
  57. DDRB = 0xFF;
  58. PORTB = 0xFF;
  59. // Light the top (b7) LED
  60. PORTB &= ~bit(7); /* Clear appropriate bit on port */
  61.  
  62. // Set up timer interrupts etc
  63. vSetupTimers();
  64.  
  65. lcd_init();
  66. lcd_clear();
  67. // 1111111111
  68. // 01234567890123456789
  69. lcd_putstr(" Oscillator demo ");
  70.  
  71. /* Create the three tasks.*/
  72. xTaskCreate( vButtonsTask, (const signed portCHAR * const)"Btns",
  73. configMINIMAL_STACK_SIZE*4, NULL, Buttons_PRIORITY, NULL );
  74. xTaskCreate( vClockTask, (const signed portCHAR * const)"RTC",
  75. configMINIMAL_STACK_SIZE*4, NULL, Clock_PRIORITY, NULL );
  76. xTaskCreate( vOscDispTask, (const signed portCHAR * const)"RTC",
  77. configMINIMAL_STACK_SIZE*4, NULL, Osc_PRIORITY, NULL );
  78.  
  79. /* Create task to flash LED 6 */
  80. xTaskCreate( vLEDflashTask, (const signed portCHAR * const)"LED",
  81. configMINIMAL_STACK_SIZE, NULL, mainLED_PRIORITY, NULL );
  82.  
  83. /* The tasks have been created - start the scheduler. */
  84. vTaskStartScheduler();
  85.  
  86. /* Should not reach here! */
  87. for( ;; );
  88. }
  89. /*-----------------------------------------------------------*/
  90. // Task to read buttons from Port T
  91. static portTASK_FUNCTION( vButtonsTask, pvParameters )
  92. {
  93. portTickType xLastWakeTime;
  94. portTickType xPause = 50 / portTICK_RATE_MS; // Pause in ms
  95.  
  96. unsigned char keys;
  97.  
  98. xLastWakeTime = xTaskGetTickCount();
  99.  
  100. DDRT &= ~BOTH; // LS 2 bits of Port T inputs
  101.  
  102. for(;;)
  103. {
  104. // 50ms pause
  105. vTaskDelayUntil( &xLastWakeTime, xPause );
  106.  
  107. /* Check for keypad press */
  108. keys = ~PTT & BOTH;
  109.  
  110. if ((keys & LEFT) == LEFT)
  111. {
  112. lcd_putxy(1, 0, "Left");
  113. }
  114. else
  115. {
  116. lcd_putxy(1, 0, " ");
  117. }
  118. if ((keys & RIGHT) == RIGHT)
  119. {
  120. lcd_putxy(1, 15, "Right");
  121. }
  122. else
  123. {
  124. lcd_putxy(1, 15, " ");
  125. }
  126. }
  127. }
  128. /*-----------------------------------------------------------*/
  129. // Task to display clock time
  130.  
  131. static portTASK_FUNCTION( vClockTask, pvParameters )
  132. {
  133. portTickType xPause = 1000 / portTICK_RATE_MS; // Pause in ms
  134. portTickType xLastWakeTime;
  135.  
  136. char time[10]; /* Formatted to show HH:MM:SS */
  137. unsigned char hrs, min, sec; /* Current time */
  138.  
  139. xLastWakeTime = xTaskGetTickCount();
  140.  
  141. hrs = 0;
  142. min = 0;
  143. sec = 0;
  144.  
  145. for(;;)
  146. {
  147. /* Send time to LCD */
  148. sprintf(time, "%02u:%02u:%02u", hrs, min, sec);
  149. lcd_putxy(1, 5, time);
  150. vTaskDelayUntil( &xLastWakeTime, xPause );
  151.  
  152. /* Update count of HH:MM:SS allowing for overflow */
  153. if (++sec == 60)
  154. {
  155. sec = 0;
  156. if (++min == 60)
  157. {
  158. min = 0;
  159. if (++hrs == 24)
  160. {
  161. hrs = 0;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /*-----------------------------------------------------------*/
  168. // Displays oscillator values on LCD display
  169. /*-----------------------------------------------------------*/
  170. static portTASK_FUNCTION( vOscDispTask, pvParameters )
  171. {
  172. unsigned int ticks
  173. unsigned int tocks;
  174. char clock[20];
  175. portTickType xLastWakeTime;
  176. portTickType xWait = 250 / portTICK_RATE_MS;
  177.  
  178. xLastWakeTime = xTaskGetTickCount();
  179.  
  180. lcd_putxy(2, 0, "osc ticks :");
  181. lcd_putxy(3, 0, "count1ms :");
  182.  
  183. for (;;)
  184. {
  185. // Send oscillator values to LCD; exclusive access to global variable
  186. portENTER_CRITICAL();
  187. ticks = osc_ticks;
  188. tocks = count1ms;
  189. portEXIT_CRITICAL();
  190.  
  191. sprintf(clock, "%05u", ticks);
  192. lcd_putxy(2, 12, clock);
  193.  
  194. sprintf(clock, "%05u", tocks);
  195. lcd_putxy(3, 12, clock);
  196.  
  197. vTaskDelayUntil( &xLastWakeTime, xWait );
  198. }
  199. }
  200. /*-----------------------------------------------------------*/
  201. // Simple LED flashing task - confirms RTOS is operating
  202. /*-----------------------------------------------------------*/
  203. static portTASK_FUNCTION_PROTO( vLEDflashTask, pvParameters )
  204. {
  205. portTickType xLastWakeTime;
  206. portTickType xFlashOn = 200 / portTICK_RATE_MS;
  207. portTickType xFlashOff = 800 / portTICK_RATE_MS;
  208.  
  209. xLastWakeTime = xTaskGetTickCount();
  210.  
  211. for(;;)
  212. {
  213. // Turn LED on
  214. portENTER_CRITICAL();
  215. PORTB &= ~bit(flashLED);
  216. portEXIT_CRITICAL();
  217. vTaskDelayUntil( &xLastWakeTime, xFlashOn );
  218. // and off again
  219. portENTER_CRITICAL();
  220. PORTB |= bit(flashLED);
  221. portEXIT_CRITICAL();
  222. vTaskDelayUntil( &xLastWakeTime, xFlashOff );
  223. }
  224. }
  225. /*-----------------------------------------------------------*/
  226. // Setup routine for Timers 3 and 4
  227. /*-----------------------------------------------------------*/
  228. static void vSetupTimers(void)
  229. {
  230. /* Timer channel 3 - 1ms interrupts */
  231. DBug12FNP->SetUserVector(UserTimerCh3, TimerCh3_ISR);
  232. /* Timer channel 4 - oscillator interrupts */
  233. DBug12FNP->SetUserVector(UserTimerCh4, TimerCh4_ISR);
  234.  
  235. // Set up Timers 3 and 4
  236. TIOS |= bit(3); /* Make channel 3 function as an output compare */
  237. TC3 = TCNT + DELAY1ms;
  238. TIE |= bit(3); /* Enable interrupt for Ch 3 */
  239.  
  240. DDRT &= ~bit(4); // Bit 4 of Port T is an input
  241. TCTL3 |= bit(0); /* Rising edge for interrupt on Ch 4 */
  242. TIE |= bit(4); /* Enable interrupts on Ch4 */
  243. }
  244. /*-----------------------------------------------------------*/
  245. // Interrupt routines for Timers 4 and 3
  246. /*-----------------------------------------------------------*/
  247. #pragma interrupt_handler TimerCh4_ISR
  248.  
  249. /* Function to handle interrupts from input compare on TC4, which
  250.  * is the oscillator clock. It simply uses the value
  251.  * of 1ms ticks from the TC3 interrupt routine. The oscillator can
  252.  * give times between edges of approx 8ms to 600ms.
  253.  */
  254. static void TimerCh4_ISR(void)
  255. {
  256. TFLG1 = bit(4); /* Clear the IC4 flag */
  257. osc_ticks = count1ms; /* Get new count value */
  258. count1ms = 0; /* and reset it again */
  259. }
  260. /*-----------------------------------------------------------*/
  261. #pragma interrupt_handler TimerCh3_ISR
  262.  
  263. /* This timer interrupt is used to generate a count of 1ms ticks */
  264. static void TimerCh3_ISR(void)
  265. {
  266. TC3 += DELAY1ms; /* Calculate next target count */
  267. TFLG1 = bit(3); /* Clear the OC3 flag */
  268. count1ms++;
  269. }
  270. /*-----------------------------------------------------------*/
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: freertos problem

 
0
  #4
Jul 21st, 2009
Are you sure your inputs are on port T pins PT#0 and PT#1 ?
What exact processor are you using?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: freertos problem

 
0
  #5
Jul 21st, 2009
Okay so now I'm confused. You have two independent switches left and right. Built in pull-ups so when you close the switch you shunt the pin to ground, thus causing a low input.
It appears to work this way but you have to hold the switch down to keep it on.

Are you looking for latching switch logic? So it edge triggers when you press (or release) it toggles state and when you press it again it toggles again?

Either way, you need a debounce circuit, or do you have a capacitor in your circuit to debounce for you so you don't have to do it in code?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: freertos problem

 
0
  #6
Jul 21st, 2009
a debounce is probably what is needed, as the result should happen when the button is released. I am using a (motorola/freescale) 68HC12 processor, the two buttons being used are pre built onto the board on which the processor is mounted. the buttons left and right have been preprogrammed so other than the latching effect, nothing needs to be done with them.

i just need it to be able to scroll/toggle through 2 modes, but whatever ive tried in the past just doesnt work (it doesnt pick up the change again or it wont stay in a while loop.

anything you think would bring it closer to those goals, epecially a latch or debounce effect in the code would be hugely appreciated
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: freertos problem

 
1
  #7
Jul 21st, 2009
Your key delay is too short 50/1000 = 20/second
Should be around 200. Use a 5

You need edge triggering.

swLast = getbits

loop:
sw = getbits
swEdge = sw ^ swLast
swLast = sw;

So now, a button JUST down, or button JUST up will have their bit in swEdge set.

So for button down

So for button down
swD = swEdge & ~sw
if (swD & 1) then 1st button just pressed

if (swD & 2) then 2nd button just pressed


So for button up
swU = swEdge & sw
if (swU & 1) then 1st button just released

if (swU & 2) then 2nd button just released


So does that help?
Last edited by wildgoose; Jul 21st, 2009 at 7:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 63
Reputation: 666kennedy is an unknown quantity at this point 
Solved Threads: 0
666kennedy 666kennedy is offline Offline
Junior Poster in Training

Re: freertos problem

 
0
  #8
Jul 22nd, 2009
a little,

do u mind explaining what i need to declare and what is C code.

things like swd and swEdge, are they things you just made up or actual code. also is ~sw the port that im reading the button presses on my program.

thank you for your help, this is kind of frustrating as im thinking the code ive been taught to use is slightly different.

also the latch i had been using was:

if(~PTH & RIGHT)
{
while(~PTH & BOTH != 0)
{
//wait until button is released
}
}
Last edited by 666kennedy; Jul 22nd, 2009 at 4:40 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: freertos problem

 
0
  #9
Jul 22nd, 2009
It would be inappropriate to write the code in C or assembly for you.
Waiting in a while loop for input on an embedded application is not a good thing to do.
I practically gave you the code in pseudo code!
Examine what I did on paper!
  1. static char swLast = 0xFF;
  2. char sw, swEdge, swUp, swD;
  3.  
  4. sw = PIT; // Get 8-bits of input from Port P
You should already know how to read the following truth table...

  1. A B ~B A^ B (A^B)&~B (A^B)&B
  2. 0 0 1 0 0 0
  3. 0 1 0 1 0 1
  4. 1 0 1 1 1 0
  5. 1 1 0 0 0 0


... That was more information then I should be giving you!
STUDY the logic table. Using only one switch, A is the last switch state, B is the current switch state. In the pseudo code's case, it was processing up to 8 switches simultaneously on the conditional test was only looking at the first two!
Last edited by wildgoose; Jul 22nd, 2009 at 11:31 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC