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

Modify pin number for A10 to match hardware; add C6 definition for re… #36

Merged
merged 1 commit into from
Sep 20, 2020

Conversation

jayzakk
Copy link
Collaborator

@jayzakk jayzakk commented Aug 2, 2020

…set line

@jayzakk
Copy link
Collaborator Author

jayzakk commented Aug 2, 2020

A10/PE6 was defined for pin#26, not #25. Switched the two definitions, so that the analog functions can work with "A10" as constant.
I also added the missing C6 (reset) pin for further implementing the pinMode() function to include the C6 switch in PMX2.
I will add another PR for that, maybe as a special function "pinModeC6(GPIO|RESET)" - not in pinMode() for security reasons.

@jayzakk
Copy link
Collaborator Author

jayzakk commented Aug 7, 2020

@dbuezas , can you please merge this one, so the pinout sheet matches? :-)

(I see that I could merge that by myself now after the invite, but I rather would like to keep my fingers far away from the "merge" button. especially of my own commits ^^)

@dbuezas
Copy link
Owner

dbuezas commented Aug 11, 2020

Allright! I'm in vacations now, I'll merge, test and release when I'm back.
Awesome contributions btw! :)

@jayzakk
Copy link
Collaborator Author

jayzakk commented Aug 12, 2020

@dbuezas , enjoy your vacation ;)
Got some more to merge ^^

@dbuezas
Copy link
Owner

dbuezas commented Aug 18, 2020

Would you also add an example for this in the examples/lgt8f folder? 🙏

@seisfeld
Copy link
Contributor

seisfeld commented Aug 18, 2020

Hey @jayzakk, I added this patch to my core manually. Can you elaborate how A10 can be used then? The data sheet mentions PMX2 several times. Does PE6 default to GPIO or AVREF now? Similar doing bitSet(PMX2, E6EN); does not seem to have any effect when reading back with bitRead(PMX2, E6EN). Always shows 0.

What do we need to set to use it the same way as any other Ax pin?

@jayzakk
Copy link
Collaborator Author

jayzakk commented Aug 20, 2020

Just use the default Arduino functions for that, there isn't anything special in using the A10 line (after the patch, that is the reason I made a PR):

/*
 * Test the A10 line:
 * 
 * Connect D4 (PD4==DAC0) with REF (PE6==A10)
 * 
 */

void setup() {
  char info[100];

  Serial.begin(115200);
  delay(500);
  Serial.println("begin");

  // so, we want 12 bits resolution and A10:
  analogReadResolution(12);
  pinMode(A10, INPUT);

  // and we want our DAC0 as output
  pinMode(DAC0, ANALOG);

  // the analogReference is for both DAC and ADC!
  // DEFAULT = 5.0V
  analogReference(DEFAULT);

  // now ramp the DAC0 level from 0 to 255, and compare with the A10 reading:
  for (int i = 0; i < 256; i++) {
    // write out and wait a little while to settle
    analogWrite(DAC0, i);
    delay(5);

    // shift 4 bits from the read data for 12 to 8 bit normalization:
    int readValue = analogRead(A10) >> 4;

    // we define "ok" with +-2 reading, neither ADC nor DAC are very linear:
    boolean ok = readValue <= i + 2 && readValue >= i - 2;
    
    sprintf(info, "%3d -> DAC0 | A10=%3d | %s", i, readValue, ok ? "OK" : "missed");
    Serial.println(info);
  }

}
void loop() {
}

@seisfeld
Copy link
Contributor

Thanks @jayzakk. I patched my core manually and measured voltage with a voltage divider on A10, which seems to work as expected. 12Bit resolution seems to be default though, but I set it anyways as per your suggestion. I used the following little sketch:

const float reference = 2.048;
const float steps = 4064;

void setup() {
  pinMode(A10, INPUT);
  analogReference(INTERNAL2V048);
  analogReadResolution(12);
  Serial.begin(115200);
}

void loop() {
  int value = analogRead(A10);
  Serial.print(value);
  Serial.print(" - ");
  Serial.print((value / steps) * reference);
  Serial.println("V");
  delay(500);
}

@jayzakk
Copy link
Collaborator Author

jayzakk commented Aug 21, 2020

I did set the 12 bits, as I hope someone will fix #41 soon and I can rely on 10 bits default again ;)

So you can confirm "just using" the A10 (like any other analogue pin) does work as intended?

@seisfeld
Copy link
Contributor

Yes, it seems to work fine on my little test board. I can't tell if it has any side effects though given it's "normal" use is AREF...

@jayzakk
Copy link
Collaborator Author

jayzakk commented Aug 21, 2020

(Which makes me think: the pin has 2 output modes: AREF and PE6. How to switch between them? I would expect, pinMode() switches function to PE6, but then there would be no way back to AREF. Time to dig deeper into the code)

Edit:

My fault. I used pinMode(A10,ANALOG), but this is an OUTPUT mode. It should have been "INPUT" as in your sketch.

analogReference() function does some more magic:
if(analog_reference == EXTERNAL) -> disables E6EN bit in PMX2 -> get AREF back

and any kind of pinMode() on E6/A10 does:
else if(pin == E6) -> enables E6EN in PMX2 -> use GPIO/ADC.

So, my example worked, as the default PE6 mode is "input", and PE6 got enabled on the pin. That was luck. If PE6 would have been output, it may have killed the ADC0 ^^

@seisfeld
Copy link
Contributor

Regarding analogReference() there is also #27 open. So together with #41 there seems to be some odds and ends regarding ADCs in general.

@dbuezas
Copy link
Owner

dbuezas commented Sep 20, 2020

Merging now :)

@dbuezas dbuezas merged commit 6a52499 into dbuezas:master Sep 20, 2020
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

Successfully merging this pull request may close these issues.

3 participants