Mega4808 ADC questions (AVR IoT WG)

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

Hello once again.

 

I'm now reading and learning about the ADC device of this Mega4808.

 

In those Start guides posted by Moe123 (and pointed by El Tangas) in the other thread, and I have this first question.

 

They say:

 

The ADC input pin needs to have the digital input buffer and the pull-up resistor disable....

 

So, they have this code that comes I don't know from where (they didn't mentioned the specific registers/properties we needed to change for this):

 

/* Disable digital input buffer */
 PORTD.PIN6CTRL &= ~PORT_ISC_gm;
 PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
 /* Disable pull-up resistor */
 PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;

 

Because they say "disable digital input buffer and pull-up resistor". So when I read the 2nd and 3rd lines of code, I assume that second line is for disabling digital input buffer and the 3rd line is to disable pull-up resistor.

But when I read the 1st line of code, I ask myself what is this for?

Last Edited: Fri. Mar 1, 2019 - 09:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the register definition from the manual:

 

 

So I will add some comments to the code:

 

/* Disable digital input buffer */
 PORTD.PIN6CTRL &= ~PORT_ISC_gm;                /* Clear ISC field */
 PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;   /* Set new ISC value */
 /* Disable pull-up resistor */
 PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;           /* Clear pullup bit */

 

Of course this code is pretty horrible, probably automatically generated. All this just to preserve the INVEN bit, which is not relevant here, anyway.

 

 

This code could be optimized in a number of ways. At the very least:

 

/* Disable digital input buffer and pull-up resistor */
 PORTD.PIN6CTRL &= ~(PORT_ISC_gm | PORT_PULLUPEN_bm);       /* Clear ISC field and pullup bit at the same time */
 PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;               /* Set new ISC value */

 

 

But since the compiler is prevented from optimizing reads and writes on volatile variables like PORTD.PIN6CTRL, this code will actually generate a few less instructions if you put it in a single line. A bit ugly, though:

 

/* Disable digital input buffer and pull-up resistor */
  PORTD.PIN6CTRL = ( PORTD.PIN6CTRL & ~(PORT_ISC_gm | PORT_PULLUPEN_bm) ) | PORT_ISC_INPUT_DISABLE_gc;               

 

Finally, since INVEN is irrelevant for a pin used for analog data, you might as well just set it to zero, along with the pullup enable bit:

/* Disable digital input buffer, pull-up resistor and digital inverter */
  PORTD.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc;       /* Set ISC field, all other bits are set to zero */

 

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

So, the 3 initial lines of code can be replaced by that single on? Is that it after all?

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

Basically, yes.

BTW, there is an ADC tutorial, not for the mega4808 exactly, but for another chip of this series: https://www.avrfreaks.net/forum/...

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

El Tangas wrote:

Basically, yes.

BTW, there is an ADC tutorial, not for the mega4808 exactly, but for another chip of this series: https://www.avrfreaks.net/forum/...

 

The tutorials from Microchip are kind of easier than just go through the datasheet and user manual. I'll check that one too.

 

My next step is to find a way of being able to see the data from the ADC somewhere. Or by sending the data to my laptop and see in in terminal or by sending it to some basic site not sure how yet. Because to send it just like the Sensor Node, looks like a ton of code to write!

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

You just need UART and there's another of the Microchip examples for that.

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

clawson wrote:
You just need UART and there's another of the Microchip examples for that.

 

That's what I'm working on now!

I'm trying to check why I can't see anything with minicom but I think I can see the correct signal in scope. I tried both in USART0 and USART1 but can't see anything showing up in minicom!

 

For instance, I sent an "A\n" string to USART0 and placed a logic probe (scope Logic Analyser) in PA0 which is the output pin of Tx for USART0 and I see this:

 

 

The baud rate seems to be correct as 1/104us = ~9615 which matches the 9600 I set in the code.

An 'A' is 65 or 0100 0001

A 'LF' is 10 or 0000 1010.

 

So we have the 4 bits of both chars plus the stop bits and that looks like to match the string but I can't catch anything in minicom/cutecom.

Last Edited: Sun. Mar 3, 2019 - 03:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Anyway this needs to go to another thread realted to USART I guess!

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

Actually, this is how I interpret your data:

 

 

Looks like LF instead of CR? Does your scope have a logic analyser function that can decode serial data?

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

PsySc0rpi0n wrote:
I'm trying to check why I can't see anything with minicom ...
the following is not Un*x (it's on Windows)

https://youtu.be/WK4ljyKDMIQ?t=245 for 1m in Getting Started With Your AVR-IoT WG Development Board - YouTube

 

"Dare to be naïve." - Buckminster Fuller