9 years, 2 months ago.

K22F: accessing ADC registers directly

I'm migrating from Teensy3.1 to K22F, since I need a FPU. The mbed demos are working. I need to write a custom ADC routine, since I need to be able to start a conversion, do something else meanwhile, and get the result in the next timeframe.

I've included "mbed.h" in my naked testprogram, and try to write to the registers directly. But this first line makes the program hang, I cant find what is wrong..

ADC0_CFG1 = 0x08 ; simple 10 bit conversion THIS LINE STOPS EXECUTION

ADC0_SC2 = 0x00; default reference never reached

ADC0_SC1A = 1; start conversion never reached

I'm using the reference manual, Chapter "34.5.1.2 Pseudo-code example" http://cache.freescale.com/files/microcontrollers/doc/ref_manual/K22P121M120SF7RM.pdf?fasp=1

Greetings and thanks in advance! Paul

2 Answers

9 years, 2 months ago.

Example you can use (or maybe it fullfills all your requirements, even though it is not exactly what you try here): http://developer.mbed.org/users/Sissors/code/FastAnalogIn/file/afc3b84dbbd6/FastAnalogIn_KLXX_K20D50M.cpp

Note this one is not tested for K22F, but if they kept same ADC peripheral it should still work (minus that I don't think bus_frequency() function is still available for K22F). The reason it stops for you is that it is lacking SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK; This enables clock to the ADC (if that does not work, check if it might have moved to different SCG register). If the clock is not enabled to a peripheral, Freescale MCUs will simply lock up when trying to access them.

Also take a look at line 34 - 35, if other code using the same Port does not already do so, you also need to enable the clock to the relevant port. (Although actually there it was not needed since the pinmap_pinout function also does it iirc).

Accepted Answer

Thanks Erik for your lightning fast reply! The clock enabling does the trick :) I need to switch from ADC channel every timeframe, my timeframe is 20uS (that makes 50k samplerate) So that's why I'm using custom code, its for my guitar hex-pitch to midi convertor that is allready running on Teensy. Now I can get the thing running on K22F, you made my day! -Paul

posted by Paul Driessen 25 Feb 2015
9 years, 2 months ago.

Hi, Paul-san,

Probably you need to enable clock gating for ADC.
The bit is ADCO (bit 27) of SIM_SCGC6 in the manual
12.2.10 System Clock Gating Control Register 6 (SIM_SCGC6)

please try
SIM_SCGC6 |= 0x08000000 ;

moto

Oops, I was reading KL25Z manual.
Erik-san's correct answer was already posted anyway.
moto

posted by Motoo Tanaka 25 Feb 2015