AVR Pullups

For general Flowcode discussion that does not belong in the other sections.
Post Reply
mnfisher
Valued Contributor
Posts: 938
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

AVR Pullups

Post by mnfisher »

Pins on AVR MCUs - so ATTinies to Mega2560 have pullup resistors that can be enabled. Note this code is specific to AVR MCUs such as ATTiny85, ATMEGA328 and 2560 (so Arduinos etc) (It should work wherever the DDRx and PORTx registers are laid out in the same fashion)

This can make using things like buttons simpler because it removes the need for a pull-up on the circuit.

I wondered if it was possible to make a simple program to turn on the pullup resistor for a pin - and came up with this.
Pullup.fcfx
(7.15 KiB) Downloaded 168 times
Calling Pullup with a port ('B', 'C' etc depending on MCU) and a pin (0..7 - again MCU dependent) - calculates a bit mask and the address of the relevant DDR and PORT register (the ports handily separated by 3, starting with port B) Then applies the bit mask to the registers.
Test with an LED between the pin and ground (no need for a resistor)

This seems to work AOK..

But - is there a better way to do it? I can use a property and get port and pin from a 'single digital pin' using Property.GetValue - but I'm uncertain how I could 'write' the necessary 'C' code in (and at what point in the program?)

ie - generate :

Code: Select all

mask = 1 << pin;
DDRx &= ~mask;
PORTx |= mask;
Also - are there any pitfalls.. For example could it be used to enable pullups for i2c without breaking anything??

Martin

Post Reply