Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Arduino code

Status
Not open for further replies.

4bdi3l

Newbie
Joined
Mar 5, 2022
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
19
Hi everyone, I am new in this forum, nice to meet you. I'd like to ask if this code of arduino it's right. I don't want to use the default functions in arduino as digitalWrite, delay...etc. instead I'd like to use the micros(); function and the port register control.

This is the code that I want to change:

C:
     delayMicroseconds(7000-valor);     
      digitalWrite(3,HIGH);
      delayMicroseconds(10);
      digitalWrite(3, LOW);

and I believe that it should be something like that:

C:
       unsigned long currentMillis = micros();

      if (currentMillis - previousMillis >= 7000UL -valor) {
          PORTD |= B00001000;

        unsigned long currentMillis2 = micros();

        if (currentMillis2 - previousMillis2 >= 10UL) {
          PORTD &= !B00001000;
          previousMillis2 = currentMillis2;
        }
        previousMillis = currentMillis;

        }

Also this code:

C:
pinMode(2,INPUT)
if(!digitalRead(2)){
//
}

And I believe that it should be:

C:
DDRD &= B11111011;
if (!(PIND >> 2 & B00000100 >> 2)){
//
        }

I'm new programing in this way. I'd really appreciate if someone can corroborate if it is right,

Thanks and regards.
 

Hi,

I´m quite new to Arduino, too.
But I think you misunderstood "Arduino". Arduino is an IDE and maybe you can call it a "philosopy".

When I read your post you think that "arduino" = "AVR microcontoller". But this is not the case.
With the arduino IDE you may be able to program many different microcontrollers: AVR, ESPxx, ARM,....

So different microcontrollers have different access to their pins, for example. And to avoid that you need to learn all the access modes for each microcontroller individually they built high level functions.

This code: "digitalWrite(3,HIGH);" you can use and it will work with every supported microcontroller.
But this code "PORTD |= B00001000;" just works on one type of microcontroller.

****
For sure this all has benefits and drawbacks.
Arduino is simple to use, but also a bit awkward and not that code effective and processing time effective than it could be.

Good thing is you can use an AVR C compiler and don´t need arduino IDE at all.
You may use other assmbler.
You may use Ardunio, just do what you prefer.

**
Using Arduino while using very hardware oriented programming style is counter productive in my eyes.

Klaus
 
Example 1 is not quite equivalent for timing. It needs to be reorganized so that the test to take pin 3 back low is not within the if statement to take it high. It assumes that the time between taking pin 3 high is longer than 10 microseconds. The example code below assumes all the variables have been declared and pin 3 set to output.

Code:
void loop()
{
      currentMicros = micros();                     // check the current time

      if((currentMicros - previousMicros) >= 7000UL -valor)   //  if time (7000UL - valor) has passed
        { 
        previousMicros = currentMicros;     // start timing the the period to the next high pulses here
        previousMicros2 = currentMicros;   // start timing the length of the high pulse here
        PORTD |= B00001000;                         // bring pin 3 high
        }     
      currentMicros2 = micros();                  // check the current  time . 
                            
                                    //  During the time waiting for pin 3
                                    // to go high again, the if statement below will continually be setting 
                                    //  pin 3 low each time through to loop. if this is not OK then test to see                                                                             // if it is high before setting it low again.

      if((currentMicros2 - previousMicros2) >= 10UL)  // if 10 microseconds has elapsed since pin 3 high
        {
        PORTD &= !B00001000;                     // set pin 3 low
        }  
}

Example 2 looks to be OK. It may not need the shifts.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top