Need Help on Arduino UNO Rev3

,,,

Please look at #7 (you can edit your post and do this) http://forum.arduino.cc/index.php/topic,148850.0.html

void delay(unsigned int duration)
{
while(duration--!=0);
}

The compiler will realize this doesn't do anything useful and optimize it all away.

(my previous code was written in C).

Heh. Don't you wish "your" previous code had more comments, now?

sbit RS   = P3 ^ 5; 
sbit RW   = P3 ^ 6;
sbit E    = P3 ^ 7;
sbit DSP_SW = P1^0;

Arduino doesn't have "sbit" - I assume that "P3 ^ 5" means bit 5 of P3 on the 8051? OTOH, Arduino has a different mechanism for abstracting individual output bits; you will probably end up with something like:

static const RS = 3;  // digital pin 3
static const E = 4;  // digital pin 4

and etc.

void delay(unsigned int duration)
{
    while(duration--!=0);
}

On arduino, the C compiler will optimize this away, since it doesn't actually do anything, and no delay will occur. You can probably do something like:

   while (duration--!=0) delayMicroseconds(4);

It's hard to say how long your old code actually delays for. But an 8051 is about 1 us per instruction (12 cycles at ~11MHz), so a loop with a few instructions is probably taking "a few" microseconds.

E=0;

will need to be replaced by something like "digitalWrite(E, 0)" (assuming you made the previous change in the definition of E.)

You can consider replacing all your low-level LCD code with the existing LCD library support, and all the timer stuff with millis()

[quote author=Nick Gammon link=topic=202890.msg1495032#msg1495032 date=1386223194]

void delay(unsigned int duration)
{
while(duration--!=0);
}

The compiler will realise this doesn't do anything useful and optimise it all away. [/quote]

There is an interesting theme going around at present regarding smart-arse compiler "optimisations" in the GCC which have for some time without warning, removed critical security code from common - and not so so common software.

One of these - but by no means the only one - is sections of cryptographic code which are purposely designed to deliberately slow the process to prevent brute force attacks. It is now necessary to correct the C compilers - and re-compile many important applications.

Don't crosspost!

Mark

Don't crosspost!

Mark

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Paul__B: It is now necessary to correct the C compilers - and re-compile many important applications.

The compiler is not doing anything wrong if you compile for optimization and it optimizes code away. You could work around it in this case by making the variables volatile.