|
|
View previous topic :: View next topic |
Author |
Message |
waheed
Joined: 19 May 2012 Posts: 26 Location: Pakistan
|
RTOS_Disable() & RTOS Enable() |
Posted: Tue May 22, 2012 12:36 am |
|
|
Controller: PIC 18f452
How to use these Functions? Info Provided in the CCS Manual is insufficient.
I have tried using it in a RTOS Task like this:
Code: |
#task (rate=100us, max=50us)
void read_adc()
{
float32 Volts;
count++;
adc_value = read_adc();
Volts = (float32)adc_value*3000;
Volts = Volts/1024;
Volts_add = Volts_add + Volts;
Volts_final= Volts_add/(float32)count;
}
#task (rate=100us, max=20us)
if (count==100)
{
RTOS_Enable(LCD_Update);
}
void LCD_Update()
{
count=0;
Volts_add=0;
dt_lcd_gotoxy(0,4);
printf(dt_lcd_printchar"Voltage = %fmV ",Volts_final); /* send to LCD*/
dt_lcd_gotoxy(0,3);
printf(dt_lcd_printchar,"Bits = %Lu ",bits);
sq_value=(sqrt(Volts_final)/100)*2.235;
dt_lcd_gotoxy(0,5);
printf(dt_lcd_printchar,"Sq. Vol. = %f ",sq_value);
dt_lcd_gotoxy(0,6);
printf(dt_lcd_printchar,"PWM1 = %f%% ",PWM1_duty);
dt_lcd_gotoxy(0,7);
printf(dt_lcd_printchar,"PWM2 = %f%% ",PWM2_duty);
}
|
What I wanted this program to do is, read the value of ADC 100 times, takes the average and enables LCD_Update Task.
But it gives error that the following part needs declaration:
Code: |
if (count==100)
{
RTOS_Enable(LCD_Update);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue May 22, 2012 1:23 am |
|
|
You would....
A function needs to be declared _before_ you can reference it. You need to create a function prototype for the LCD_Update function _before_ you try to reference it.
Separately, is the section 'if (count==100)' a function?. It has to be to be used by #task, and isn't as you show it.
Best Wishes |
|
|
waheed
Joined: 19 May 2012 Posts: 26 Location: Pakistan
|
|
Posted: Tue May 22, 2012 3:15 am |
|
|
I have tried doing it this way. But it still gives error:
"Undefined identifier LCD_Update"
Code: |
#task (rate=100us, max= 50us)
void LCD_Enable()
{
if (count==1024)
{
rtos_enable(LCD_update);
}
}
void LCD_update()
{
count=0;
Volts_add=0;
dt_lcd_gotoxy(0,4);
printf(dt_lcd_printchar"Voltage = %fmV ",Volts); /* send to LCD*/
dt_lcd_gotoxy(0,3);
printf(dt_lcd_printchar,"Bits = %Lu ",bits);
sq_value=(sqrt(Volts)/100)*2.235;
dt_lcd_gotoxy(0,5);
printf(dt_lcd_printchar,"Sq. Vol. = %f ",sq_value);
dt_lcd_gotoxy(0,6);
printf(dt_lcd_printchar,"PWM1 = %f%% ",PWM1_duty);
dt_lcd_gotoxy(0,7);
printf(dt_lcd_printchar,"PWM2 = %f%% ",PWM2_duty);
}
void LCD_Disable()
{
rtos_disable(LCD_update);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue May 22, 2012 6:41 am |
|
|
_As I said_.
"A function needs to be declared _before_ you can reference it. You need to create a function prototype for the LCD_Update function _before_ you try to reference it."
Still applies. |
|
|
waheed
Joined: 19 May 2012 Posts: 26 Location: Pakistan
|
|
Posted: Tue May 22, 2012 10:29 pm |
|
|
Dear Ttelmah,
Will you please elaborate a little.
Should LCD_Update function be outside the task?
All Function are Enabled by default. Is there any way to Disable a specific task before the execution of rtos?
If I write LCD_Update before the LCD_Enable function, then LCD_Update task will run first and then how can I control it _ provided that all task are enabled by default... |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 23, 2012 3:04 am |
|
|
I think you've got a 'C' issue NOT an RTOS one.
Have you got a line like this, near the the top of your code, with all the other prototypes in ALL relevant files?
I know CCS is not entirely consistent about this, and often the compiler accepts code without prototypes.
Why not include count test as part of the LCD_update() task?
Code: |
void LCD_update()
{
if (count==100)
{
.... rest of LCD_update code as is
}
}
|
Simply do the test, ignore the bulk of the code if the test fails.
That way you don't have to worry about enabling and disabling the task.
You've got three different values that you're testing count for; 100, 1000, 1024 sprinkled through your code snippets. I'm assuming that you know what you're final intention is.
Mike |
|
|
waheed
Joined: 19 May 2012 Posts: 26 Location: Pakistan
|
|
Posted: Wed May 23, 2012 4:15 am |
|
|
Quote: |
Have you got a line like this, near the the top of your code, with all the other prototypes in ALL relevant files?
|
No, I haven't written a line like this. Should I include this?
Quote: |
Why not include count test as part of the LCD_update() task?
Code: |
void LCD_update()
{
if (count==100)
{
.... rest of LCD_update code as is
}
}
|
|
I have already tried this. This works fine, just trying to figure out how the RTOS _Enable() & RTOS_Disable() function work.
Quote: |
You've got three different values that you're testing count for; 100, 1000, 1024 sprinkled through your code snippets. I'm assuming that you know what you're final intention is.
|
I was just testing the code and didn't care for the values. By the way, the value, I am going to use is 1024. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed May 23, 2012 5:24 am |
|
|
waheed wrote: | Quote: |
Have you got a line like this, near the the top of your code, with all the other prototypes in ALL relevant files?
|
No, I haven't written a line like this. Should I include this? | Get back to your C study book and look up the difference between a 'function definition' and a 'function declaration'.
It is a little bit confusing but this is such basic C knowledge that I'm not going to repeat what is in every textbook. Just remember that before you can use a function the compiler has to 'know' what the input and output parameters are. So when processing your source files the compiler will go through it top to bottom and when it comes to an unknown function it will stop instead of being smart and scan through the rest of the file to find the unknown function. So it is up to you, as a programmer, to make sure the function name is known before it is being used. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed May 23, 2012 8:48 am |
|
|
Mike Walne wrote: |
I know CCS is not entirely consistent about this, and often the compiler accepts code without prototypes.
Mike |
Actually, I think this is one area, where CCS, sticks _exactly_ to the letter of C.
You must _either_ have a declaration, or a definition before the function is used. A definition, implicitly creates a declaration, if one does not already exist, so if you have the definition, before using the routine, a further declaration is not required. Types used in declarations and definitions must match (this is an ANSI requirement not in C originally).
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed May 23, 2012 11:10 am |
|
|
Thanks Ttelmah, you've cleared up a confusion in my mind.
I'm relatively new to 'C', and learn something every day.
So this code compiles OK without a prototype
Code: | // Prototypes
#include <18F452.h>
void test_function() {}
void main()
{
while (1) { test_function(); }
}
|
And this doesn't.
Code: | // Prototypes
#include <18F452.h>
void main()
{
while (1) { test_function(); }
}
void test_function() {}
|
I thought it was CCS not being strict.
Thanks again.
Mike |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|