XMega ADC is confusing me :(

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

Hi everyone!

 

So I recently started using the Xmega16C4 and as a basic first project I wanted to measure an analog voltage and display it on a cheap OLED using SH1106 driver communicating over I2C.

 

The problems I’m having is that I’m used to the suppleness of the Arduino IDE (You must hear this a lot) and it’s just recently that I left Arduino altogether. I have mostly done simpler projects on the ATMega series and programmed it through the terminal on my RPI.

 

The thing that is confusing me is the documentation, I’ve read through the Xmega16C4, Xmega-C, AN2535 app note/datasheets and I get that I want to run the ADC in an "Unsigned Mode" to get the full 12-bit resolution and "Single Ended" but I have no idea of how to implement it code. There is also a lot of info on the web that says that the ADC is a mess and that you have to do a bunch of extra things to get it to work, but state that ATMEL are going to re spin the chips and fix it. Are they fixed?

 

In the bottom of the AN2535 appnote there states "Get source code from Atmel start" but when I go there aren’t any examples for the Xmega ADC.

 

I’m using Atmel Studio 7 if anybody is wondering.

 

Could someone please give me a hand in this and post an explanation of the Xmega ADC that is up to date and some example code that is stripped down to the essentials so only code for the ADC. I want to learn how it works, rather than just settling with completed code so any comments on how and why the example would do what it does would be helpful!

 

 

Thanks in advanced for any help!

Hi everyone!

 

So I recently started using the Xmega16C4 and as a basic first project I wanted to measure an analog voltage and display it on a cheap OLED using SH1106 driver communicating over I2C.

 

The problems I’m having is that I’m used to the suppleness of the Arduino IDE (You must hear this a lot) and it’s just recently that I left Arduino altogether. I have mostly done simpler projects on the ATMega series and programmed it through the terminal on my RPI.

 

The thing that is confusing me is the documentation, I’ve read through the Xmega16C4, Xmega-C, AN2535 app note/datasheets and I get that I want to run the ADC in an "Unsigned Mode" to get the full 12-bit resolution and "Single Ended" but I have no idea of how to implement it code. There is also a lot of info on the web that says that the ADC is a mess and that you have to do a bunch of extra things to get it to work, but state that ATMEL are going to re spin the chips and fix it. Are they fixed?

 

In the bottom of the AN2535 appnote there states "Get source code from Atmel start" but when I go there aren’t any examples for the Xmega ADC.

 

I’m using Atmel Studio 7 if anybody is wondering.

 

Could someone please give me a hand in this and post an explanation of the Xmega ADC that is up to date and some example code that is stripped down to the essentials so only code for the ADC. I want to learn how it works, rather than just settling with completed code so any comments on how and why the example would do what it does would be helpful!

 

 

Thanks in advanced for any help!

Last Edited: Thu. Aug 23, 2018 - 07:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There seems to be an echo in here!

 

Watch_Your_Dog_Timer wrote:
it’s just recently that I left Arduino altogether

Why?

 

It would seem that the Arduino IDE is ideal for you - for the very reasons you are now finding!

 

programmed it through the terminal on my RPI

That seems a hugely retrograde step!

 

surprise 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

There seems to be an echo in here!

 

Watch_Your_Dog_Timer wrote:
it’s just recently that I left Arduino altogether

Why?

 

It would seem that the Arduino IDE is ideal for you - for the very reasons you are now finding!

 

programmed it through the terminal on my RPI

That seems a hugely retrograde step!

 

surprise 

 

The reason why I left the Arduino was because of it did not have support for the µC that I wanted to use. It also felt unnecessary since all the boards that I use are custom made/ custom design. I did want to advance in µC programming and I felt like having C++ wrapped in Arduino was not the way to go if I want to do this professionally in the future. I’m also going off to collage in a year from now so it’s kind of a head start.

 

The reason why I used the standalone AVR-GCC toolchain was because I didn’t like the Atmel studio that much, its felt clunky and I didn’t use half of the features (Its really nice having it auto complete things). I used a Raspberry Pi because it was much easier to install the Toolchain on it and just SSH into it from my Windows computer, rather than installing the Toolchain on my windows computer. I did try WinAVR but it did not work on Windows 10 :(

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

Watch_Your_Dog_Timer wrote:
The reason why I left the Arduino was because of it did not have support for the µC that I wanted to use.

So what was the particular reason for wanting to use XMega?

 

As noted here it's never been one of the popular models - so there is always going to be a (relative) lack of supporting material ...

 

EDIT 

 

Watch_Your_Dog_Timer wrote:
 as a basic first project

I think using the ADC, OLED, and  I2C would come some time after "basic first projects" ?

 

Basic First Projects: https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Aug 23, 2018 - 08:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Watch_Your_Dog_Timer wrote:
The reason why I left the Arduino was because of it did not have support for the µC that I wanted to use.

So what was the particular reason for wanting to use XMega?

 

As noted here it's never been one of the popular models - so there is always going to be a (relative) lack of supporting material ...

 

EDIT 

 

Watch_Your_Dog_Timer wrote:
 as a basic first project

I think using the ADC, OLED, and  I2C would come some time after "basic first projects" ?

 

Basic First Projects: https://www.avrfreaks.net/commen...

 

When switching from the Arduino IDE to the "Normal IDE" I did it because of an ATMega chip that had more memory (I might be wrong here, it was a while ago I did this). The reason why I decided to use the XMega16C4 was because it was cheaper (~1$ at DigiKey ) then most other µC in the AVR lineup that fit within specs and was a modular design so I could easily switch between different Xmega chips if I needed to in the future. 

 

I’ve already got LED's to blink on the Xmega but haven’t done anything with the UART, maybe I should, it’s just so rear that I use the UART for anything. I’ve already written the driver/code for the SH1106 communication (Haven’t written it all from scratch, it’s based on existing drivers for the SSD1306 but is written to better suit my needs), it should work but I haven’t tested it yet, still waiting for the PCB's to arrive. 

 

 

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

Watch_Your_Dog_Timer wrote:
haven’t done anything with the UART, maybe I should

You certainly should!

 

even if you don't use it for your application as such, it is invaluable as a diagnostic/debug facility.

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Aug 23, 2018 - 08:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The xMega ADC is a little hard to figure out. See http://barefootelectronics.com/x...

 

277,232,917 -1 The largest known Mersenne Prime

Measure twice, cry, go back to the hardware store

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

Watch_Your_Dog_Timer wrote:

There is also a lot of info on the web that says that the ADC is a mess and that you have to do a bunch of extra things to get it to work, but state that ATMEL are going to re spin the chips and fix it. Are they fixed?

The XMEGA A series, not to be confused with the XMEGA AU series, were a mess on the analog side of things.  I learned as much - if not more - about working with the peripherals from the ASF source.  You generally have to tumble down a rabbit hole or two (or three or four or more) before you see what is going into the actual peripheral registers.  I never would have got the EBI working with my own code on the XMEGA-A1 XPLD if all I had was the documentation.  You can download the beast here.  There are also links on that page for ASF 3 and ASF4/START.

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

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

Another approach : find an "xmega core" for Arduino and see how analogRead() is implemented within that.
.
EDIT And thus proving the power of Google... https://github.com/Xmegaduino/Xmegaduino/blob/xmegaduino/hardware/arduino/xmega/cores/xmega/wiring_analog.c

Last Edited: Fri. Aug 24, 2018 - 09:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@clawson wrote:

Another approach : find an "xmega core" for Arduino and see how analogRead() is implemented within that.

Not that I've ever looked at Arduino source, but that's probably easier than making sense of ASF!

 

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

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

Thanks for all the help!

 

I haven’t had much time lately but I found this example for a dev board I had lying around (XmegaB1 Explained)

 

It worked for the Xplained dev board so ill rewrites the code to work on my board. Hopefully it won’t be too much of a hassle. Will look into this during the weekend!

 

https://www.digikey.com/eewiki/d...

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

You are one of millions being confused by the Xmega's ADC, apart from the big ADC learning curve to use it it's an absolute wonderful chip.

 

And of course ASF and other Atmel example does nothing but confuse you even more.

 

The simplest thing is to use the ADC in 11 bit mode, I have a couple of thousand boards using the Xmega32E5 in the field in the past 2 years.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:

You are one of millions being confused by the Xmega's ADC, apart from the big ADC learning curve to use it it's an absolute wonderful chip.

 

And of course ASF and other Atmel example does nothing but confuse you even more.

 

The simplest thing is to use the ADC in 11 bit mode, I have a couple of thousand boards using the Xmega32E5 in the field in the past 2 years.

 

Yeah the ASF isn't much help...

So using the ADC in 11 bit mode is the way to go then, could you point me in the right direction? An example or part of a app note would be great!

Also is there anything else i should consider when using the Xmega ADC? 

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

That's easier said than done laugh a search on Xmega ADC should find several threads including mine of about 3 years ago.

 

My project is in ASM and for the Xmega32E5, I can share some code snippets for the init and reading the ADC if you like.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

js wrote:

That's easier said than done laugh a search on Xmega ADC should find several threads including mine of about 3 years ago.

 

My project is in ASM and for the Xmega32E5, I can share some code snippets for the init and reading the ADC if you like.

 

Il will search some more on the Xmega ADC, would be great if you posted the ADC part, could always learn something from it!

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

Hope this makes some sense, I'm also attaching my macros file, had to renamed it from .inc to .txt for upload

 

; Setup 12bit ADC
; Read and set Calibration Values
	ldi	temp, NVM_PROD_SIGNATURES_ADCACAL0_offset
	call	Read_calibration_byte
	STORE	ADCA_CALL, temp
	ldi	temp, NVM_PROD_SIGNATURES_ADCACAL1_offset
	call	Read_calibration_byte
	STORE	ADCA_CALH, temp

	ldi	temp, ADC_FLUSH_bm;				; cancel any pending conversions, disable ADC
	STORE	ADCA_CTRLA, temp
	ldi	temp, ((1<<ADC_CONMODE_bp) | ADC_RESOLUTION_12BIT_gc)	; Set resolution for 12bit, signed mode
	STORE	ADCA_CTRLB, temp
	ldi	temp, ADC_REFSEL_AREFA_gc		; Use voltage reference VCC on PA0 
	STORE	ADCA_REFCTRL, temp
	ldi	temp, ADC_PRESCALER_DIV32_gc	; Divide clock by 16 (4MHz/32 = 125KHz) 
	STORE	ADCA_PRESCALER, temp 
	ldi	temp, ADC_CH_INPUTMODE_SINGLEENDED_gc	; Single ended input mode
	STORE	ADCA_CH0_CTRL, temp
	clr	temp
	STORE	ADCA_CH0_INTCTRL, temp			; Disable interrupts 
	SETB	ADCA_CTRLA, ADC_ENABLE_bp, temp2
	rcall	WAIT1ms

//More init follows for other stuff	you could put a ret here if you like

; On entry temp must contain the address of the calibration byte to read.
; Calibration byte returned in temp.
Read_calibration_byte:
	mov	ZL, temp				; Point Z to required signature byte
	clr	ZH
	ldi	temp, NVM_CMD_READ_CALIB_ROW_gc
	STORE	NVM_CMD, temp				; Load command into NVM Command register.
	lpm	temp, Z
	rjmp	NVM_CMD_NO_OP				; Reset NVM and return

//NVM_CMD_NO_OP is used by other reoutines, you can drop the above line if you like
;Resets NVM to no operation and return
NVM_CMD_NO_OP:
	ldi		temp1, NVM_CMD_NO_OPERATION_gc
	STORE	NVM_CMD, temp1
	ret

; Returns converted value in reg temp, temp1 for 12 bit
; ADCA_CH0_MUXCTRL must already be set up on entry
Read_ADC:
	SETB	ADCA_CH0_INTFLAGS, ADC_CH0IF_bm, temp2	; Clear any conversion flag if set
	SETB	ADCA_CH0_CTRL, ADC_CH_START_bp, temp2	; Start conversion
Wait_for_ADC:
	SKBS	ADCA_CH0_INTFLAGS, ADC_CH0IF_bp, temp2	; Wait for conversion to finish 
	rjmp	Wait_for_ADC
	LOADW	temp, temp1, ADCA_CH0RES
	tst	temp1					; Check for negative numbers
	brpl	not_negative_adc
	LDIW	temp, temp1, 0			; If negative values then zero result
not_negative_adc:
	ret

//Reading the ADC	
Check_Walk:
	wdr					;Reset watchdog
	ldi	temp, (ADC_CH_MUXPOS_PIN5_gc)	; Select PORTA5 Crossing signal input
	STORE	ADCA_CH0_MUXCTRL, temp
	rcall	Read_ADC			; Throw away first reading as the channel has changed
	rcall	Read_ADC
	

 

Attachment(s): 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly