efficient delay function

Thread Starter

Parth786

Joined Jun 19, 2017
642
What line is that compiler error for. I'm not aware of any compiler that doesn't give SOME indication of WHERE in the program it encountered the error.

Hint: What has to be true regarding the value of the variable 'wait' in order for the function code to reach the return statement?
I have read your this post many times but still I couldn't find out exact problem.

compiler show following errors

ram.c:4:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{
^
ram.c:10:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{
^
ram.c:13:4: error: expected '{' at end of input
}

after seeing these error, I can't understand where is exact mistake
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You are calling delayUs without a parameter.

printf("delayUs %d \n",delayUs());
If I pass parameters then this case,I get following errors

ram.c:4:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{

ram.c:10:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{

ram.c:13:4: error: expected '{' at end of input
}
 

WBahn

Joined Mar 31, 2012
32,893
If I pass parameters then this case,I get following errors

ram.c:4:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{

ram.c:10:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{

ram.c:13:4: error: expected '{' at end of input
}
When dealing with errors, always focus on the first error. The others may or may not be real since the first error often causes the compiler to misinterpret the code from that point on.

The error report is telling you exactly where in the file that it detected that an error exists. This is usually one space beyond where the error actually occurred, particularly if it's because something is missing, since the compiler had to read beyond the missing thing before seeing something that no longer made sense. Some compilers, knowing this, will return the position report as being at the prior location. The main thing to focus on, at least initially, is the line number and look at the code in that vicinity, keeping in mind that some errors are such that the compiler is able to go down a blind hole for a while before a syntax error makes itself known.

So "ram.c:4:1" means that it confirmed an error upon trying to make sense of what is on line 4 in column 1 in the file ram.c.

My guess is that you will discover that this right around this part of the code:

C:
void delayUs(unsigned int wait)
int main(void)
Which is why I said

If it is complaining about the line with your prototype, then what has to be at the end of a function prototype to terminate the statement?
Do you know what a function prototype is?

The first line in the quote above is a function prototype (also known as a function declaration).

The second line is the start of a function definition.

The two that you have look syntactically equivalent, which is the problem. The compiler has to be able to tell them apart.

Do you know how to write one?

A function prototype establishes a contract with the compiler. In this case it says that you are going to eventually define a function named "delayUs" and that, when you do, it is going to take exactly one argument and that argument is going to be of type 'unsigned int' and it is not going to return anything (is of type void).

Do you know how a statement consisting of a function prototype must be terminated?

To tell the compiler that you are only declaring the function, and not defining it, you must terminate a function prototype with a semi-colon.

If you had taken the time to learn and become proficient at the basics of C programming, you would have quickly seen that you are missing a semicolon after the first line in the above code snippet.

Then, when you call the function:

C:
printf("delayUs %d \n",delayUs());
You use it passing it nothing and expecting it to return an signed integer, when your prototype promised the compiler that you would pass it an unsigned int and that it would not return anything.

Then, why finally got around to defining the function:

C:
int delayUs(unsigned int wait)
You again violated the contract you made with the compiler because you promised it that this function would not return anything and now you are defining it as returning an 'int'.

These are all the kinds of mistakes that you should encounter and learn to identify and get past while you are still writing code to print out the numbers from 1 to 10 or calculate the average of three numbers. That's why introductory programs focus on simple tasks so that YOU can focus on the programming details, so that later when you are writing more complex programs, you can focus on the problem you are trying to solve instead of being hampered trying to deal with basic programming details.

Do you begin to see why myself and many others are always trying to tell you that you are jumping around and racing ahead and trying to tackle things that you are not ready for? You are trying to write real-time embedded code but are getting frustrated trying to deal with the fallout of not being willing to put forth the time and effort to become proficient at righting ANY code.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Do you know what a function prototype is?
Do you know how a statement consisting of a function prototype must be terminated?.
I know how we declare prototype function and terminate statement. I forgot to terminate statement. I was repeating same mistake. that's why I was getting error
Then, when you call the function: You use it passing it nothing and expecting it to return an signed integer, when your prototype promised the compiler that you would pass it an unsigned int and that it would not return anything.
I have re-written program. I am calling function and as well as printing the value of function
C:
#include <stdio.h>

int delayUs(unsigned int wait);

int main(void)
{
    printf("delayUs %d \n",delayUs(10));

    return 0;
}

int delayUs(unsigned int wait)
   {
      ++wait;
     return wait+2;
   }
delayUs 13

Do you begin to see why myself and many others are always trying to tell you that you are jumping around and racing ahead and trying to tackle things that you are not ready for? You are trying to write real-time embedded code but are getting frustrated trying to deal with the fallout of not being willing to put forth the time and effort to become proficient at righting ANY code.
I am focusing on basics of c programming and this is one example for practicing. I have read basics concept many times but some time I forget the things. I am thinking I am being good then past. and I need more practice to make perfect, That I am doing. I am writing my own program's and seeing the result. I think this is best way to learn programming. I am doing my best

I was trying to write embedded program at starting. then I felt I don't have enough skill's to write embedded programming, so I started to brush up my c programming skill's. while doing practice I do some silly mistakes. Next I will try to do, not much mistakes.
 
Last edited:

WBahn

Joined Mar 31, 2012
32,893
So how is your delayUs() function going to behave now?

Now matter WHAT value is passed to it by the caller, the function is going to do the same thing. Increment the value of wait and then return the value that is two greater than this.

What's the point?

Your code is functionally identical to the following:

C:
#include <stdio.h>

int main(void)
{
    printf("delayUs %d \n", 10 + 3);
    return 0;
}
You wanted the code to be more efficient....

In fact, some compilers might actually recognize that your function merely returns the value of the argument plus three and inline the function for you. I don't know how many compilers are actually smart enough to spot this, but some of them are scary-smart these days.

It's really hard to give you feedback on the logic of your program unless you adequately explain what it is that your program is SUPPOSED to do. That's particularly the case when you keep toying the goal in order to focus on different aspects of things, which is perfectly reasonable and fine, but we need to know what you are trying to accomplish with the current code that you are wanting feedback on.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
It's really hard to give you feedback on the logic of your program unless you adequately explain what it is that your program is SUPPOSED to do. That's particularly the case when you keep toying the goal in order to focus on different aspects of things, which is perfectly reasonable and fine, but we need to know what you are trying to accomplish with the current code that you are wanting feedback on.
I had not any special purpose about program. I just wanted to call function and as well as print the value of function. So I wrote a program that makes these two things.

Now I will try to write mining full program what's the program suppose to do and what's it shouldn't suppose to

Thanks for your feedback and response. I will back with fresh example.
 

Brian Griffin

Joined May 17, 2013
64
There is no way to tell how long that delay would do if you use a loop in C. Either it will be very inaccurate, or it would be optimized (removed) away by the compiler.

If you want precise loops, use assembly. Get a pen and paper too. Else, search for how to use the timers - they are much easier to handle.
 

spinnaker

Joined Oct 29, 2009
7,830

be80be

Joined Jul 5, 2008
2,395
LOL
I looked at parth code over on ETO and he was missing just a ; ; LOL

But Like i said you can't print the counter of a delay is would go from 0 to 65550 faster then you could see it LOL

I use the stop watch to time a delay and add some nop to tune it.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
It's really hard to give you feedback on the logic of your program unless you adequately explain what it is that your program is SUPPOSED to do. That's particularly the case when you keep toying the goal in order to focus on different aspects of things, which is perfectly reasonable and fine, but we need to know what you are trying to accomplish with the current code that you are wanting feedback on.
My problem is that I am not able to write a meaningful program yet. I know that I am still not enough good in c programming to write a embedded program. I have done a lot of work in the few day to improve my c programming skill's and still I am working on it. some time I do small mistake like I did in delay program. I didn't terminate prototype function while I knew it that we always terminate statement in c programming.

sometime it feel that after my many effort's I am not becoming good, so Am I worth to learning programming or not.
 

WBahn

Joined Mar 31, 2012
32,893
My problem is that I am not able to write a meaningful program yet. I know that I am still not enough good in c programming to write a embedded program. I have done a lot of work in the few day to improve my c programming skill's and still I am working on it. some time I do small mistake like I did in delay program. I didn't terminate prototype function while I knew it that we always terminate statement in c programming.

sometime it feel that after my many effort's I am not becoming good, so Am I worth to learning programming or not.
I can't really say one way or the other, at least not definitively. Different types of tasks require different types of thinking and every one is good at some ways and bad at others. Most people can learn to become at least passable at some of the ways they aren't good at, but I think everyone is going to have some ways of thinking that they simply can't become proficient at no matter how hard they try. But I don't know how to really tell when that's the case versus when it is a case of just not having tried hard enough, or of not having tried the right approach, or of just not having had that 'ahah' moment when something clicks and flood gates of understanding suddenly open wide
 

spinnaker

Joined Oct 29, 2009
7,830
I can't really say one way or the other, at least not definitively. Different types of tasks require different types of thinking and every one is good at some ways and bad at others. Most people can learn to become at least passable at some of the ways they aren't good at, but I think everyone is going to have some ways of thinking that they simply can't become proficient at no matter how hard they try. But I don't know how to really tell when that's the case versus when it is a case of just not having tried hard enough, or of not having tried the right approach, or of just not having had that 'ahah' moment when something clicks and flood gates of understanding suddenly open wide

And some people get occupied with trying to figure out complex issue vehicle tracking systems, automatic meter reading and cell phone remotes before they tackle the basics of something a simple as a for loop in C. The "flood gate" will never open for those people because they lack the discipline required to learn embedded programming.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
And some people get occupied with trying to figure out complex issue vehicle tracking systems, automatic meter reading and cell phone remotes before they tackle the basics of something a simple as a for loop in C.
sorry but I don't see anything wrong in asking about project. what I asked is part of the hardware design, not program code for project. I asked question about project after doing some research work.

I don't like to defend myself again and again. truth is that I know the basics of c programming and I am just trying to improve programming skill's
 

spinnaker

Joined Oct 29, 2009
7,830
sorry but I don't see anything wrong in asking about project. what I asked is part of the hardware design, not program code for project. I asked question about project after doing some research work.

I don't like to defend myself again and again. truth is that I know the basics of c programming and I am just trying to improve programming skill's

You need to concentrate on one thing right now or you will never learn anything. And no you don't know the basics. That is obvious.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
And no you don't know the basics. That is obvious.
Is it the basics of c programming. if yes then I understand it.
  • Printf and scanf
  • Main function
  • Data type’s (Int, char, flot, enum)
  • size of Data types ( signed unsigned , long short)
  • Variable (declaration and inilitiztion )
  • Operators
  • Control statement (if , else if , switch case )
  • Loop ( while , for)
  • Basic of array
  • Basic of pointer
  • Basic of Structure
  • Function & Type of function
    Function with no return type and no parameter
    Function with no return type and parameter
    Function with return type and no parameter
    Function with return type and parameter
  • Call by value and call by address
  • still solving exercise
Is this not sufficient to start embedded programming ? What is meaning of basic for you to start embedded programming?

I have already accepted many time that I am not good at c programming and still trying to improve programming skills. I am not expert but now I can write program at least.

Note : Please don't feel bad I am giving feedback of your question
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Is it the basics of c programming. if yes then I understand it.
  • Printf and scanf
  • Main function
  • Data type’s (Int, char, flot, enum)
  • size of Data types ( signed unsigned , long short)
  • Variable (declaration and inilitiztion )
  • Operators
  • Control statement (if , else if , switch case )
  • Loop ( while , for)
  • Basic of array
  • Basic of pointer
  • Basic of Structure
  • Function & Type of function
    Function with no return type and no parameter
    Function with no return type and parameter
    Function with return type and no parameter
    Function with return type and parameter
  • Call by value and call by address
  • still solving exercise
Is this not sufficient to start embedded programming ? What is meaning of basic for you to start embedded programming?

I have already accepted many time that I am not good at c programming and still trying to improve programming skills. I am not expert but now I can write program at least.

Note : Please don't feel bad I am giving feedback of your question

In your other thread you are just starting to understand the basics of pass by pointer vs pass by value and returning values from a function. That is one of the very first things you learn as a programmer. You might be able to list the various features of C but you still don't have an understanding. The vast majority of your posts show this. You need to sit down, take a good online course then put what you learn to use. Jumping all over the place with projects far beyond your skills is only going to keep you from improving your skills in C progamming.
 
Top