Recent

Author Topic: What to change ModePortA, to enable internal pull-ups  (Read 1230 times)

pascalbythree

  • Full Member
  • ***
  • Posts: 199
What to change ModePortA, to enable internal pull-ups
« on: February 24, 2023, 04:44:11 pm »
Hello for AVR forum responders,

Does anybody know what to change about this code to open a port in output mode, but then to enable the internal pull-ups ?

Code: Pascal  [Select][+][-]
  1. procedure ModePortA(Pin: byte; Value: Boolean);
  2.   begin
  3.     if Value then
  4.       begin
  5.         DDRA := DDRA or (1 shl pin);
  6.       end
  7.     else
  8.       begin
  9.         DDRA := DDRA and not (1 shl pin);
  10.       end;
  11.   end;

let say like this attachment, but then in Freepascal Syntax

Using FPC on the attiny26

Thank you and greets Wouter van Wegen
« Last Edit: February 24, 2023, 04:52:48 pm by pascalbythree »

pascalbythree

  • Full Member
  • ***
  • Posts: 199

pascalbythree

  • Full Member
  • ***
  • Posts: 199
Re: What to change ModePortA, to enable internal pull-ups
« Reply #2 on: February 24, 2023, 09:04:27 pm »
Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !Jay ! Jay !


It got to work!:


   DDRB := 0;

   PORTB := 255;


   DDRA := 0;

   PORTA := 255;


Thank you for your time.


Incase problems i will be back soon !

ccrause

  • Hero Member
  • *****
  • Posts: 767
Re: What to change ModePortA, to enable internal pull-ups
« Reply #3 on: February 25, 2023, 07:29:23 am »

pascalbythree

  • Full Member
  • ***
  • Posts: 199
Re: What to change ModePortA, to enable internal pull-ups
« Reply #4 on: February 27, 2023, 09:04:48 am »
Does anybody know how to enable internal pull-up resistors pin by pin ?  8-)

Check:

https://microchipdeveloper.com/8avr:ioports

I need to set PORTB3 and PORTB6 as input mode with internal pull ups enabled.

I need to set PORTA0 and PORTA1 and PORTA2 and PORTA3  as output mode with internal pull ups enabled.

I need to set PORTA7 and PORTA6 and PORTA5 and PORTA4  as output mode with internal pull ups disabled.

second question:

How to write 0xFF in FPC-AVR source code, so it compiles ? Do i need a character on the left ?

Greets, PascalByThree
« Last Edit: February 27, 2023, 09:42:40 am by pascalbythree »

ccrause

  • Hero Member
  • *****
  • Posts: 767
Re: What to change ModePortA, to enable internal pull-ups
« Reply #5 on: February 27, 2023, 12:22:25 pm »
Does anybody know how to enable internal pull-up resistors pin by pin ?  8-)
This is explained in the wiki.  Some further explanation: each pin in a port can be set individually to any valid combination of output, input with no pull-up, input with pullup, without affecting other pins (if you do it correctly of course).

Quote
I need to set PORTB3 and PORTB6 as input mode with internal pull ups enabled.
Code: Pascal  [Select][+][-]
  1.   // Configure pins 3 & 6 as output (and leave other pin modes unaffected)
  2.   DDRB := DDRB and not ((1 shl 3) or (1 shl 6));
  3.   // Configure pullup resistors on pins 3 & 6 (and leave other pin settings unchanged)
  4.   // This only activates the pullup resistors if the pin directions are configured as output
  5.   PORTB := PORTB or (1 shl 3) or (1 shl 6);
  6.  

Quote
I need to set PORTA0 and PORTA1 and PORTA2 and PORTA3  as output mode with internal pull ups enabled.

I need to set PORTA7 and PORTA6 and PORTA5 and PORTA4  as output mode with internal pull ups disabled.
When a pin is configured for output mode, the pullup resistor is irrelevant. If a pin is configured as output, it is driven directly by internal transistors to either Vcc or ground.  For this case the pullup resistor is effectively bypassed.  The atmega328p datasheet has this to say about configuring a pin (section 14.2.1 of the linked document):
"If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is activated. To
switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be configured as an output
pin. The port pins are tri-stated when reset condition becomes active, even if no clocks are running.
If PORTxn is written logic one when the pin is configured as an output pin, the port pin is driven high (one). If
PORTxn is written logic zero when the pin is configured as an output pin, the port pin is driven low (zero)."


Setting a pin to output mode:
Code: [Select]
  DDRA:= DDRA or (1 shl 0) or (1 shl 1) or (1 shl 3); // and so on...
  // Or you can specify the bit pattern as binary:
  DDRA := DDRA or %11111111;  // set all pins as output
  // or in hexadecimal
  DDRA := DDRA or $FF;  // set all pins as output

Quote
How to write 0xFF in FPC-AVR source code, so it compiles ? Do i need a character on the left ?
0xFF is a C language construct that indicates a hexadecimal value of 255 in decimal.  The equivalent Pascal construct to indicate a hexadecimal value is to use a dollar sign: $FF.

pascalbythree

  • Full Member
  • ***
  • Posts: 199
Re: What to change ModePortA, to enable internal pull-ups
« Reply #6 on: February 28, 2023, 11:12:48 am »
Code: Pascal  [Select][+][-]
  1.    // Configure pins 3 & 6 as input PORTB
  2.   DDRB := DDRB and not ((1 shl 3) or (1 shl 6));
  3.   // active pull up resistors on 3 & 6 PORTB
  4.   PORTB := PORTB or (1 shl 3) or (1 shl 6);
  5.    
  6. // set all pins as output for PORTA    
  7.   DDRA := DDRA or $FF;  
  8. // active pull up resistors on 0 & 1 & 2 & 3 PORTA
  9.   PORTA := PORTA or (1 shl 0) or (1 shl 1) or (1 shl 2) or (1 shl 3);

So this is the right code for my pins?

PS: I am not that bad at coding as it seems like, just want to make sure.
« Last Edit: February 28, 2023, 11:52:01 am by pascalbythree »

ccrause

  • Hero Member
  • *****
  • Posts: 767
Re: What to change ModePortA, to enable internal pull-ups
« Reply #7 on: February 28, 2023, 12:26:53 pm »
Code: Pascal  [Select][+][-]
  1. // set all pins as output for PORTA    
  2.   DDRA := DDRA or $FF;

Your code will work.  Since you are completely rewriting all the bits in DDRA to 1, you can reduce the code to:
Code: Pascal  [Select][+][-]
  1.   DDRA := $FF;

Quote
Code: Pascal  [Select][+][-]
  1. // active pull up resistors on 0 & 1 & 2 & 3 PORTA
  2.   PORTA := PORTA or (1 shl 0) or (1 shl 1) or (1 shl 2) or (1 shl 3);

So this is the right code for my pins?
This code will drive pins 0..3 into high state (Vcc) with a strong drive, in other words it is not the same as a pullup resistor which has a high resistance (for AVR a typical pullup resistor value is ~ 20 kOhm). 

Looking at your circuit, it seems that pins PA0..PA3 drive pin 7 (Vss/ground) of the HEF4066 chips?  Do you want to disable the chips by pulling the ground pins to a high state?  Do you have a diagram of this circuit, it will be easier for me to follow that than the board layout.

pascalbythree

  • Full Member
  • ***
  • Posts: 199
Default value when opening port mode.
« Reply #8 on: June 08, 2023, 01:40:17 pm »
Code: Pascal  [Select][+][-]
  1. ModePortD(7,True);   // WVW MODE RELAIS 12V
  2. WritePortD(7,False); // WVW RELAIS 12V OFF
  3. ModePortB(0,True);   // WVW MODE RELAIS 15V
  4. WritePortB(0,False); // WVW RELAIS 15V OFF

Hey FPC responders, Does anybody have a small line of example code to open a port in freepascal for AVR and immediately write a default value ?

With the code above, my relais flippers once when i put the power on the board.

Before i remove the pull-up resistors, i want to try to solve it by code.

Anybody help? Greets, Wouter van Wegen


Code: Pascal  [Select][+][-]
  1.  procedure ModePortB(Pin: byte; Value: Boolean);
  2.   begin
  3.     if Value then
  4.       begin
  5.         DDRB := DDRB or (1 shl pin);
  6.       end
  7.     else
  8.       begin
  9.         DDRB := DDRB and not (1 shl pin);
  10.       end;
  11.   end;
  12.  
  13. procedure WritePortB(Pin: byte; Value: Boolean);
  14.   begin
  15.     if Value then
  16.       begin
  17.         PORTB := PORTB or (1 shl pin);
  18.       end
  19.     else
  20.       begin
  21.         PORTB := PORTB and not (1 shl pin);
  22.     end;
  23.   end;
  24.  
  25. procedure ModePortD(Pin: byte; Value: Boolean);
  26.   begin
  27.     if Value then
  28.       begin
  29.         DDRD := DDRD or (1 shl pin);
  30.       end
  31.     else
  32.       begin
  33.         DDRD := DDRD and not (1 shl pin);
  34.       end;
  35.   end;
  36.  
  37. procedure WritePortD(Pin: byte; Value: Boolean);
  38.   begin
  39.     if Value then
  40.       begin
  41.         PORTD := PORTD or (1 shl pin);
  42.       end
  43.     else
  44.       begin
  45.         PORTD := PORTD and not (1 shl pin);
  46.     end;
  47.   end;
  48.  

ccrause

  • Hero Member
  • *****
  • Posts: 767
Re: Default value when opening port mode.
« Reply #9 on: June 08, 2023, 10:17:09 pm »
Hey FPC responders, Does anybody have a small line of example code to open a port in freepascal for AVR and immediately write a default value ?

With the code above, my relais flippers once when i put the power on the board.

Before i remove the pull-up resistors, i want to try to solve it by code.
How do you trigger the relays, with a logical low or high? Both signals can be used, it just depends on how the external relay circuit is configured.

The startup state for the port and direction registers are all 0, so basically the controller should start with all pins in a high impedance input state, which can not drive an external circuit.  To change from this to an output state, you can either set DDR first, in which case the pin would sink current, or set PORT, in which case the pullup resistor would be enabled (ignoring the PUD bit for now...).  This is explained in the controller's documentation, for example in the atmega328p datasheet there is a section Switching from input to Output which explains all the possible combinations. You have to pick a sequence that does not give you an intermediate state that may trigger the relay circuit.

In your code example you first set DDRx then PORTx, so if the PORTx value is 0 to start with, it should sink current.  This seem logical based on your comments, perhaps you have other code that changes the PORTx value before the code you've shown get executed?

Since you mentioned pullup resistors: after reset the controller I/O pins will be in a high impedance state, a pullup resistor will then be able to pull the pin high until the code switches the pin to output low state.

 

TinyPortal © 2005-2018