Order of Port Initlisation ( __low_level_init() )

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using the IAR function __low_level_init() but I'm sure this is applicable to any compiler/IDE.

This section of code is inherited from a colleague (perhaps from IAR directly) and has never been changed (apart from actual Data Values) as it works.

However Is there any theory or logic behind the order in which DDRn and PORTn are initialised?

E.g. in my code I have:

int __low_level_init(void)
{
  DDRB  = 0xF7;
  PORTB = 0x49;
}

Now, I've just eaten a lovely cheery and Vanilla Danish, but I'm sure I can still think straight. So....

Apart from application specific reasons, there's nothing preventing me from swapping the order of DDRn and PORTn. Is there?

I.e.

int __low_level_init(void)
{
  PORTB = 0x49;
  DDRB  = 0xF7;
}

Ben
-Using IAR (& ocasionally CodeVision)
0.7734
1101111011000000110111101101

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Apart from application specific reasons, there's nothing preventing me from swapping the order of DDRn and PORTn. Is there?

No.
You need to consider what will happen to your application in both cases.
When powering up your device those registers are all 0x00.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Take a look at what happens to bit 6.

In the first case, as DDRB is set bit 6 is made output and it's corresponding bit in PORTB (default=0x00) at that moment is 0. Then in the next statement the PORT bit is set to 1. So bit 6 goes through the sequence highZ-0-1

In the second case that bit 6 goes highZ-pulled_up_to_1-1

The momentary 0 state in the first may not be what you want.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the comments.

I'm just trying to satisfy in my mind that my only concern needs to be what happens to the application (i.e. the hardware connected to the port).

Ben
-Using IAR (& ocasionally CodeVision)
0.7734
1101111011000000110111101101

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should always set the default state prior the port direction.

Otherwise the load may be switched on for a short time.
E.g. if you have a low active LED, you will see a visible flicker on every reset.

Peter