1

I'm a new programmer,my program is about getting LED on,maybe the simplest in C language,but it is always having _'Delay10ms':function contains unnamed parameter,hoping someone help me to solve it. here are my words:

#include<reg51.h>
#include<intrins.h>
#define GPIO_LED P2

void Delay10ms(unsigned char time)
{
    unsigned char i,j;
    for(i=1;i<110*time;i++)
       for(j=1;j<110;j++);
}

void main()
{
    unsigned char n,i,j;
    GPIO_LED=0X01;
    while(1)
    {
        for(n=0;n<7;n++)
        {
            GPIO_LED=_crol_(GPIO_LED,1);
            Delay10ms(50);
        }

        for(n=0;n<7;n++)
        {
            GPIO_LED=_cror_(GPIO_LED,1);
            Delay10ms(50);
        }
    }
}
LPs
  • 16,045
  • 8
  • 30
  • 61

2 Answers2

0

Either <reg51.h> or <intrins.h> probably have standard library includes. One of these library includes contains the standard header function time() that returns the current system time. Because your function parameter's name is time the IDE probably can't figure out what you meant with it and thus it gives you that error/warning. Try renaming your variable.

Also, the return type of main() has never been void (C++), use int.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • 1
    C standard allows the return type of main to be void. It certainly is correct in this context. – 2501 Oct 17 '16 at 08:32
  • @2501 Right, I answered this question as a C++ question since it has the C++ tag. – Hatted Rooster Oct 17 '16 at 08:33
  • 1
    `time` as parameter and as used in the function body is completely disambiguated from `time()`. The `time` parameter takes precedence over the `time()` function (the `time()` function cannot be used anymore in the function body). - C language. – Paul Ogilvie Oct 17 '16 at 09:00
  • @2501 In the current, C11, version of the C standard `main` **shall** (my emphasis) have return type `int`. http://stackoverflow.com/a/18721336/646887 – Klas Lindbäck Oct 17 '16 at 09:06
  • @Paulogilvie Yes, the compiler should be able to do that. The IDE might not be able to. We're not sure if this is a warning coming from the IDE or an error coming from the compiler yet. – Hatted Rooster Oct 17 '16 at 09:07
  • Cheng should rename the parameter to see if the error goes away. – Paul Ogilvie Oct 17 '16 at 09:22
  • *Nods silently* – Hatted Rooster Oct 17 '16 at 09:23
  • @2501: Can you quote chapter and verse for the C standard permitting `void main`? http://stackoverflow.com/a/207992/771073 suggests otherwise, and that fits with my recollection. (I suppose it might have been added in a later standard - but I would be slightly surprised.) – Martin Bonner supports Monica Oct 17 '16 at 09:23
  • `main` must return a value that will be used as the exit value of the program. Hence `main` must be `int`. – Paul Ogilvie Oct 17 '16 at 09:24
  • @KlasLindbäck : Oops. Sorry, missed your comment. Pretty much closes that one down :-) – Martin Bonner supports Monica Oct 17 '16 at 09:24
  • 1
    @MartinBonner Of course. The current C standard, 5.1.2.2.1 §1 *or in some other implementation-defined manner.*. Using void main is common in microcontrollers: https://electronics.stackexchange.com/questions/55767/who-receives-the-value-returned-by-main This covers both theoretical and practical part of your question. – 2501 Oct 17 '16 at 21:34
  • @KlasLindbäck You're interpreting the standard incorrectly. The "shall" also refers to the last sentence which starts with "or". See my comment above. – 2501 Oct 17 '16 at 21:49
  • @2501 - Yes. You are quite right. I had misinterpreted your original comment (I thought you meant that it allowed a programmer to use `void main` *anywhere*, rather than "if the implementation chooses to support it"). – Martin Bonner supports Monica Oct 18 '16 at 05:15
-1

It has been a Long time gone since I played c, but your code Looks o.k., maybe there is a name conflict ("time")

But there are some more questions: - Why do you use char (unsigned char = 0..255) instead of classic int? This would be make code more readable. - What do you expect how relyable your timer could be on a multitasking operating System?

I would implement such a function (if it not exists) as

void MyDelay (unsigned int delay_time)
{    
    start = MyOs.GiveMeTheTime()
    while (MyOs.GiveMeTheTime() - start < delay_time)
    {};
}

MyOs and GiveMeTheTime are names chosen by me. You must find out, which functions are available on your System, I am sure, they are.

I suppose, you try to program anything like a raspberry pi. Maybe this link could help:

Example Which Provides Accurate uS Timing

am2
  • 380
  • 5
  • 21
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/14005447) – Hayt Oct 17 '16 at 13:49
  • @Hayt: Sorry, but why should the answer not fit to the question? The probably mistake is a name conflict. The way would not work relyable on multitasking systems and there is an example, how to avoid this. WHAT doesn't match to the Problem? – am2 Oct 17 '16 at 19:02
  • @am2 This is an answer, but likely a bad (and not useful) one - The mistake you point out is merely a guess (there are probably no `time` in `reg51.h` or `intrins.h` (you can find them on google if you want to check)), and the workaround you propose is probably not appropriate here since OP's code is targeting micro-controller, likely without OS, on which "looping" is the "standard" way of delaying (you cannot rely on OS-based functions if there are no OS). – Holt Oct 17 '16 at 21:54
  • @Holt: a guess, of course, you only can guess, if there is that much Information? The Syntax of the code is correct, isn't it? I do not know, what kinds of microcontroller you use, but all I know have at least one timer and librarys for this, f.e. arduino http://playground.arduino.cc/Code/ElapsedMillis . And any of them have an operating System, f.e. raspberry, odroid ... It is in my mind the completely wrong way to do this by loops. Do, whatever you want, but it would be nice, if other people may have their own experiences. – am2 Oct 18 '16 at 07:10
  • @am2 The question lacks information, that's for sure, but if this is the case, you should post a comment asking for more details, not a "guess" answer. I never say micro-controller with timer library did not exist - But ones without also do. You cannot assume that because RPi / Odroid / Arduino are bundle with a lots of stuff (hardware AND software), every micro-controllers also has these. Just take STM32 based on Cortex-M3, these are vastly used (In industry), and until last year, the only way to dealy was looping, and even now, you'd have to rely on timer and not on a simple system clock. – Holt Oct 18 '16 at 08:12
  • @Holt: all your arguments for timers are o.k., but it is guessing too to suppose, he doesn't have timer, system clock or anything like that. (until last year? until 1937 we even did not have Computers, thaught, we talk about today). He told, he is a beginner, do you really think, he works with industrial uControllers? I do not have a Problem with other Points of view, but to be called an useless idiot sucks me. – am2 Oct 18 '16 at 08:22
  • @am2 *"Last year"* is not that far for industrial micro-controller, and as I said, even now, the way to handle delay might be far from what you propose (relying on timer rather than a system clock), and I would not be surprise if OP was a student working with industrial controllers. I did no called you *"an useless idiot"* (I would not allow myself) - Your answer contains good information, but is likely out of context here, and thus "useless" in the SO-sense... But this is not really your fault, this question is lacking to much information to be answered in a meaningful way. – Holt Oct 18 '16 at 08:44