Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tinary Nano 4808 analogRead() returns 255 from all analog pins #151

Closed
Telmac1802 opened this issue Jun 12, 2022 · 1 comment
Closed

Tinary Nano 4808 analogRead() returns 255 from all analog pins #151

Telmac1802 opened this issue Jun 12, 2022 · 1 comment

Comments

@Telmac1802
Copy link

Hello

I have a few solar cells and lead acid battery. I tryed to measure a battery voltage using analogRead() and a resistor divider
in Tinary Nano 4808 and MegaCoreX v1.0.10. AnalogRead() returns reading 255 from every analog pin (A0-A11).
I could not get a real useful reading using analogRead().
The issue resembles: #128, but is not the same.

On the file https://github.com/MCUdude/MegaCoreX/blob/master/megaavr/variants/nano-4808/pins_arduino.h
on line 77 there is a calculation if a pin is an analog pin or not:
#define digitalPinToAnalogInput(p) (((p) < 8) ? (p) : ((p) >= 14 && (p) >= 25) ? (p) : NOT_A_PIN) .

I found a working analogRead()-solution of all pins A0-A11 using following definition on line 77:
#define digitalPinToAnalogInput(p) ((((p) < 8) && (p)>3) ? ((p)+8) : ((p) >= 14 && (p) <= 17) ? ((p)-14) : ((p) >= 18 && (p) <= 21) ? ((p)-6) : (((p) >= 22) && ((p) <= 25)) ? ((p)-18) : NOT_A_PIN)

I hope it will be usefull for other Nano 4808 owners also.

@MCUdude
Copy link
Owner

MCUdude commented Jun 12, 2022

Thank you for submitting this issue! You're absolutely right, the macro is not right at all.
I've modified your macro a little to accept both 0-11 and A0-A11 as input parameters.
The fix will be present in the next boards manager release.

#define digitalPinToAnalogInput(p)     (((p) <= 3) ? (p) : \
                                       ((p) <= 7)  ? ((p) + 8) : \
                                       ((p) <= 11) ? ((p) - 4) : \
                                       ((p) >= 14  && (p) <= 17) ? ((p) - 14) : \
                                       ((p) >= 22  && (p) <= 25) ? ((p) - 18) : \
                                       ((p) >= 18  && (p) <= 21) ? ((p) - 6)  : NOT_A_PIN)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants