Change multiple IO pins mode (analog) at once on megaAVR® 0-series

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

Hi folks,

 

I have a question can I replace following code with something to do it on multiple pins at ones ? Something like is PINMASK on big atxmega ?

 

/* Disable digital input buffer */
     
     //PORTD
     PORTD.PIN6CTRL &= ~PORT_ISC_gm;
     PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;

     /* Disable pull-up resistor */
     PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;

Last Edited: Sun. Jan 5, 2020 - 05:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't think so, you can only access one PORTx.PINnCTRL register at a time.

But you could combine all the operations, this will save some bytes at the cost of legibility:

 

     //PORTD
     PORTD.PIN6CTRL = (PORTD.PIN6CTRL & ~(PORT_ISC_gm | PORT_PULLUPEN_bm)) | PORT_ISC_INPUT_DISABLE_gc;

 

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

You can create a loop. Early in startup code I set all pins to analog (power saving if ever needed)-

 

https://github.com/cv007/Avr0PlusPlus/blob/master/Port.hpp

(code at the bottom)

and the output (early code, easy to find)-

https://github.com/cv007/Avr0PlusPlus/blob/master/list.txt

 

so if doing a number of pins, the loop in this case is 21 instructions and makes sense for probably any 0/1 > 8 pins.

 

In this case, the reason its done in .init3, is .init2 sets the r1 register so no need to worry about that, and is before constructors which could init pins. The end result is any unused pins remain in an analog state, and there is no need to figure out which are unused.

 

I don't know what your goal may be, but I imagine a similar purpose.

 

 

 

 

 

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

Yes setting it in loop looks like reasonably good solution 

I was wondering if they keep this:
PORTCFG_MPCMASK = 0xFF;
PORTA_PIN6CTRL = (PORT_OPC_TOTEM_gc | PORT_ISC_BOTHEDGES_gc);

To set all port pins at once 
But as I look this feature is omitted 

So now if I want to change all PORTD and E Ill need 16 sets or a loop :/