28.12.2014 Views

Nuts & Volts

Nuts & Volts

Nuts & Volts

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

THE DESIGN CYCLE<br />

converter. Although there were multiple<br />

choices for analog-to-digital converter<br />

resolution offered, all of them were<br />

disabled with the exception of one. I<br />

was forced to choose 10 bits of analogto-digital<br />

converter resolution and I was<br />

offered only two choices for conversion<br />

time, which were 18.446 µS and 27.669<br />

µS. I chose the 27.669 µS conversion<br />

time as its error percentage was<br />

specified at 0.000979% versus 2.479%<br />

for the 18.446 µS conversion time.<br />

Within the Methods area of the<br />

ADC Bean, I decided to allow the full<br />

use of the 10 bits of resolution and<br />

nixed any code generation that<br />

truncated the analog-to-digital converter’s<br />

output to eight bits. After<br />

compiling the MR16_ADC project for<br />

the first time to establish the project’s<br />

supporting file system and the Bean<br />

code for the ANALOG.c file, I copied<br />

the MR16_RS232 project’s LED blinker<br />

code — the endless loop structure<br />

— and related includes and variable<br />

definitions into the ADC project’s<br />

MR16_ADC.c application file.<br />

As we discovered earlier, I again<br />

had to change the PLL value from<br />

0x1E to 0x66 in the MR16_ADC<br />

project’s Cpu.c file so that the PLL<br />

could lock and provide an accurate<br />

clock signal. At this point, I should<br />

have a simple LED blinker. Just to<br />

check things out, I compiled the<br />

project again and loaded it into the<br />

MC68HC908MR16. The LED blinked<br />

as designed. Things are good.<br />

From experience, we know that the<br />

Processor Expert has generated all of<br />

the necessary analog-to-digital converter<br />

initialization files, which it placed in<br />

a file named after the ADC Bean<br />

(ANALOG.c). Before we start to add any<br />

analog-to-digital converter functions<br />

and macros to our project, let’s examine<br />

the initialization actions that were<br />

taken against the analog-to-digital<br />

converter configuration registers.<br />

Access the package of<br />

MC68HC908MR16 projects you’ve<br />

downloaded from the <strong>Nuts</strong> & <strong>Volts</strong> or<br />

EDTP websites and pull up your copy<br />

of ANALOG.c from within the<br />

MR16_ANALOG project. ADCLK is<br />

the first analog-to-digital converter<br />

register that is altered in the analogto-digital<br />

converter initialization<br />

function ANALOG_Init. The macro<br />

setReg8 is used to load the ADCLK register<br />

with 0x64. The MC68HC908MR16<br />

datasheet tells us that the three most<br />

significant bits of the most significant<br />

nibble (0b011x) determines the ADC<br />

Clock Divide Ratio, which in our case<br />

divides the ADC clock by eight. The<br />

least significant bit of the most significant<br />

nibble (0bxxx0) has a value of<br />

zero, which tells us that the analog-todigital<br />

converter is getting its clock<br />

from the external clock (CGMXCLK).<br />

Recall that the frequency of<br />

CGMXCLK is equal to the frequency of<br />

the crystal, which is, for us, 4.9152<br />

MHz. The MC68HC908MR16 datasheet<br />

goes on to say that the analog-to-digital<br />

converter internal clock must lie<br />

between 500 kHz and 1.048 MHz. This<br />

internal ADC clock frequency range is<br />

called f ADIC . We know that our analogto-digital<br />

converter clock divisor is 8<br />

and our CGMXCLK is 4.9152 MHz. We<br />

can calculate our f ADIC as follows:<br />

f ADIC<br />

Now that we’ve calculated f ADIC ,<br />

which is also our ADC frequency, and<br />

found it to be within specifications; we<br />

can apply some simple math and justify<br />

that conversion time figure of 27.669<br />

µS that the Bean Inspector forced me<br />

to choose. Analog-to-digital converter<br />

conversion time is defined as follows:<br />

I’ll save us some time and tell you<br />

that the Bean Inspector does not use 16<br />

cycles to compute the analog-to-digital<br />

converter conversion time. So, substituting<br />

the known values we’ve previously<br />

dug up and using 17 ADC cycles:<br />

Okay, another mystery solved.<br />

Now, let’s finish up deciphering the<br />

ADCLK value. I specified right<br />

justified analog-to-digital converter<br />

results in the ANALOG Bean configuration.<br />

That is reflected by the two<br />

most significant bits of the least<br />

significant nibble of the ADCLK value<br />

(0bxxxx01xx). Bits 0 and 1 of the<br />

ADCLK register are always zero.<br />

The ADSCR (ADC Status and<br />

Control Register) is the next register<br />

acted upon by the ANALOG_Init function.<br />

Again, a setReg8 macro loads<br />

the ADSCR register. This time the<br />

value loaded is 0x1F, which powers<br />

off the analog-to-digital converter<br />

module. Hopefully, we’ll see some<br />

code later that enables the analog-todigital<br />

converter and looks for input<br />

from our designated analog input pin.<br />

With that, let’s put some analog-todigital<br />

converter code together.<br />

Listing 3 is a code snippet that is<br />

taken from the MR16_ADC.c file. Let’s<br />

talk about it from top to bottom.<br />

The variable ANALOG_OutV is<br />

defined in the ANALOG.c file. To use it<br />

here, we must declare it as an external<br />

variable. The rest of the variable declarations<br />

aren’t new. However, I did throw<br />

out the “a” and substituted “result” to<br />

make things a bit more readable in the<br />

source code. The variable declarations<br />

are followed by a call to initialize the<br />

= CGMXCLK / ADC Clock Divide Ratio = 4.9152MHz/8 = 614.4kHz<br />

Conversion Time = 16 to 17 ADC Cycles / ADC Frequency<br />

Conversion Time = 17 / 614.4kHz = 27.669uS<br />

MC68HC908MR16 and the peripherals<br />

within it we have chosen to use.<br />

The ball starts to roll when we initiate<br />

the analog-to-digital conversion<br />

using the ADC Bean’s ANALOG_<br />

Measure function. I have chosen to<br />

only take and process one ADC value<br />

at a time. The CodeWarrior<br />

Development Studio for HC08 v5.0 C<br />

compiler does not include printf functionality<br />

automatically in the libraries.<br />

So, instead of implementing the printf<br />

workarounds presented in the help<br />

text, I resorted to caveman tactics to<br />

convert the hexadecimal analog<br />

voltage readings into something<br />

a human can read.<br />

In true caveman style, I allocated<br />

four bytes (ones, tens, hundreds,<br />

thousands) to represent a number in<br />

the format of x.xxx decimal. I also<br />

provided a +5.12V power supply for<br />

the MC68HC908MR16 hardware to<br />

provide for 0.005-volt steps for all<br />

of 1024 (0 through 1023) steps of the<br />

10-bit analog-to-digital converter<br />

(+5.12V / 1024 = 0.005V per step).<br />

March 2006 29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!