Sei sulla pagina 1di 6

7/12/2013

ADC value sent on USB - Country Laboratory

Search this site

Home

News

AVR Projects

PIC Projects

Electronics

Links

Sitemap

PIC Projects > Jal projects >

Translation
Romanian pages

ADC value sent on USB


Few words about ADC pin selection and voltage measurement. There are two main groups of microcontrollers regarding to analog pin selection (Sebastien Lelong wrote a nice tutorial here):

Main Pages
Software File archive Windmill Blog Contact

1. Analog dependent pin selection


The first group (an older one) have dependant analog pin selection. What mean that? If you select, by example, AN3 as analog input, all analog pins bellow it, become analog pins. Let's presume that we have only 4 pins at our disposal and all are mixed with analog channels: P i nn a m e -R A 0 / A N 0 -R A 1 / A N 1 -R A 2 / A N 2 -R A 3 / A N 3 One of them we need to use it as analog input and the other three as digital input to count events or as key handlers. If we select AN3 as analog input, then all lower pins which are mixed with analog channels become analog inputs making impossible to use the other three pins as digital input. So, in this case, you are forced to use RA0/AN0 as analog input and you can use RA1-RA3 pins as digital inputs. Note: As an observation, a pin selected to be analog input can be also digital output without any setup. Unfortunately, 18F2550/18F4550 microcontrollers are members of this group so, we have to take care when we design our electronics. This is an image from the datasheet about dependent analog pin selection:

Recent site activity


File archive
attachment removed by Vasile Guta Ciucur attachment from Vasile Guta Ciucur

001 EvB 4.3 with Arduino Language


edited by Vasile Guta Ciucur

Windmill controller with PICAXE 08M File archive

edited by Vasile Guta Ciucur attachment from Vasile Guta Ciucur

003 AVR Eclipse, ATMEL Toolchain and ATmegaCLib


edited by Vasile Guta Ciucur

View All Analog Microwave Channels


www.m wradio.com Motorola R ock we ll C ollins Grange r Le nk urt Harris Farinon

Traductor de Google

https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb

1/6

7/12/2013

ADC value sent on USB - Country Laboratory

AN5, AN6 and AN7 pins are available only on 18F4550 microcontrollers. On FreeJALduino boards digital pins are noted as Dx and analog channels are noted as Ax, where x is the pin number regarding to Arduino notation.

2. Analog independent pin selection


Ok, the secong group permit independent analog input pin selection. This mean that we can select AN3 as analog input without affecting the status of the pins bellow him (exactly, this mean that from AN0 to AN2 we can use them as digital inputs - and this is impossible when you have a microcontroller from old series, with dependent analog pins). Microchip has started modernizing his microcontrollers, offering new variants for any old model . I'm expecting the same thing for 18F2550/18F4550 microcontrollers. I will not insist on this, the Sebastien's tutorial covering very well the both cases.

3. Measuring volts with microcontrollers using 1024 bit resolution


Analog pins of 18F2550/18F4550 microcontrollers accept a maximum of 5Vcc as input voltage. For higher voltage we need to use a voltage divider. Also, the maximum impendance on analog inputs is 2.5 Kohms.

3.1. The voltage divider


We will use only a simple divider, based on this schematic:

The general formula is

but for a complete reference, go to Wikipedia. Anyway, if you are not good at math or tired of math calculations, try this online calculator, is simple to use it. To calculate the impendance, use this formula: Z= R1*R2/(R1+ R2)

4. A practical example
It is the same example used when we forced USB-CDC driver installation on this page (see the chapter 6 from that page). So, we will continue with JAL program description. But first, a short resume. We planned to have a maximum of 55Vcc voltage to

https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb

2/6

7/12/2013

we will continue with JAL program description. But first, a short resume. We planned to have a maximum of 55Vcc voltage to measure which must correspond to 5Vcc on ADC channel and we made calculations to obtain the values for R1 resistor (R1=10K) - use the online calculator for the three known values, Vin=55Vcc, Vout=5Vcc and R2=1K (1000 ohm).

ADC value sent on USB - Country Laboratory

is set on external power and the power can be between 9Vcc - 12Vcc.

So, the left resistor is 10K and the right resistor is 1K. The board

4.1. The JAL program description


USB_ADC.jal example can be found in C:\jal\project\FreeJALduino\FreeJALduino5\examples\ or in this page at chapter 6.b. So, let's get started: The application will measure voltage on A0 (our board definition for RA0/AN0) pin and will show on Windows terminal two values using USB Serial connection: the ADC digital value (which can have values from 0 to 1023) and the calculated Voltage value. i n c l u d ef r e e j a l d u i n o 4-F r e e J A L d u i n o 4p i n o u tl a y e rf o rt h el a s tb o a r dm o d e l -i n c l u d el i b r a r i e s i n c l u d eu s b _ s e r i a l i n c l u d ep r i n t i n c l u d ef o r m a t i n c l u d ed e l a y The necessary libraries for our application: pinout layer definitions which also have processor, speed and pragma bootloader definitions, USB Serial library, print and format libraries for printing strings and formated numbers on terminal , and the one required for delays.

v a rd w o r dV a l u e v a rw o r dA D _ R E S U L T v a rw o r dV o l t s -v a rb y t eM i l l i v o l t s c o n s tb y t es t r 1 [ ]="V o l t s \ n \ r " Variables and constant definitions. Value is a double word variable container which will store our primary voltage calculation (32bit integer math calculation - I really wanted to have this, although a 16bit math is possible and is doing faster). Volts variable will be used to display a formated value on Terminal . AD_RESULT store the ADC digital value. https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb

3/6

7/12/2013

ADC value sent on USB - Country Laboratory

variable will be used to display a formated value on Terminal . AD_RESULT store the ADC digital value.

e n a b l e _ d i g i t a l _ i o ( )-f i r s t ,a l lp i n ss e tt od i g i t a l This function is used to set all pins to digital and is a good starting point on every program you want to write. This is required because as default, all microcontroller pins which have analog channels assigned are set as analog inputs.

c o n s tb i tA D C _ H I G H _ R E S O L U T I O N=t r u e-1 0 b i tr e s o l u t i o n c o n s tb y t eA D C _ N V R E F=0-n ov o l t a g er e f e r e n c e c o n s tb y t eA D C _ N C H A N N E L=1-h o wm a n yA D Cc h a n n e l sw ea r eu s i n g , -a l w a y ss t a r t i n gt h ec o u n tf r o mA 0( R A 0 / A N 0 ) -T h i sw i l la u t o m a t i c a l l ys e tA 0a sa n a l o gi n p u t c o n s tw o r dA D C _ R S O U R C E =9 0 9-i m p e n d a n c ei no h m s( R 1 * R 2 / ( R 1 + R 2 ) ) These constants are required by ADC library and define resolution, voltage reference, the number of analog inputs used and the impendance of the voltage divider, required by library to calculate the exact time for ADC aquisition (see Jallib API documentation). The constants must be defined before including the ADC library. Read the comments form the code.

-t h e n ,l o a dt h eA D Cl i b r a r y i n c l u d ea d c -I n i t i a l i z eA D C a d c _ i n i t ( Finally, ADC library is included and initialization is made.

-i n i t i a l i z et h eU S Bs e r i a ll i b r a r y u s b _ s e r i a l _ i n i t ( ) This will initialize the USB Serial connection. From this moment, you must "push" continuously the USB at a maximum time of 10ms interval . otherwise, the USB connection will be lost and Windows will report that your USB device is not working properly. All you can do is to rewrite the application properly and re-upload it to the board.

-s t a r tm a i nl o o p f o r e v e rl o o p We start the main loop where our real program is acting.

u s b _ s e r i a l _ f l u s h ( ) This is how you keep USB connection alive when you don't have an USB activity (doing other operations than reading or writing on USB).

A D _ R E S U L T=a d c _ r e a d ( 0 ) Start analog reading on A0 pin.

p r i n t _ w o r d _ d e c ( u s b _ s e r i a l _ d a t a ,A D _ R E S U L T ) u s b _ s e r i a l _ d a t a="" Print the ADC digital value on Terminal via USB.

-V a l u e=5 3 7*A D _ R E S U L T Well , this is a disabled line but is here as reference. It was my first atempt on doing 32bit math but didn't worked because I didn't read the JAL documentation properly. There are two things to discuss about this disabled line: The first thing: What mean "537" value and how I obtained it? I used a desktop calculator (not a PC) to obtain the required values. Ok, we have a maximum of 55Vcc which correspond to 1024 "divisions" on ADC. 5 5/1 0 2 4=0 . 0 5 3 7 1 0 9 3 8 I removed the zero values and kept the first three non zero values, and those are 537 (because we can't use floating point math). So, for a maximum of 1024 ADC digital value, we have:

https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb

4/6

7/12/2013

ADC value sent on USB - Country Laboratory

5 3 7*1 0 2 4=5 4 9 8 8 8 We will divide this by 100 because we need a maximum of 4 digits starting from left to right

5 4 9 8 8 8/1 0 0=5 4 9 8 . 8 8 but we get only the integer part, 5498 which we display it as a formated string of 54.98 being our maimum calculated volt value. As you see, here is a little error but a convenient one. The second thing: Why I had 16bit math instead of the required 32bit one? Is because how JALv2 compiler is working. When a math operation is requested, the compiler will not look at the type of variable container and not even at size of the numbers. It will look only at operands types to figure out if it will use an 8bit(biggest operand being byte type), 16bit(biggest operand being word type) or 32bit(biggest operand being dword type) math calculations and will choose the bigger type from them. If you specify an operand in clear numbers, without a named type, then his type is considered as being UNIVERSAL which is signed 32bit. If your both operands are specified in clear numbers, then you will have the required 32bit math. If only one operand is specified in clear numbers (UNIVERSAL type) and the other operand is a named type, then the UNIVERSAL operand will get the type of the second operand which will be considered the biggest type. So, the response to my question is simple. My frist operand was UNIVERSAL (clear numbers) but was changed to the type of the second operand because this was a typed one (AD_RESULT is a word type variable) which was considered the biggest type operand. As a direct result, having a 16bit math operation instead of the required 32bit one. Then, the correct line is ...

V a l u e=d w o r d ( 5 3 7 )*A D _ R E S U L T .. the above one :-D . This time, the first operand is casted as dword type and because is the biggest type from the two operands, a 32bit math operation is triggered. Understood? Well , I didn't from first time... Ah, almost forgot: we have only integer math operations.

V o l t s=V a l u e/1 0 0 We shorten the value to a maximum of 4 digits which will be displayed as 99.99 formated number (maximum being 55.00 volts theoretically). To myself : Should I insert a protection against "Division by zero" error?

f o r m a t _ w o r d _ d e c ( u s b _ s e r i a l _ d a t a ,V o l t s ,4 ,2 ) p r i n t _ s t r i n g ( u s b _ s e r i a l _ d a t a ,s t r 1 ) We display the calculated Volt value as a formated string (as stated above).

f o r1 0 0l o o p-d e l a y1s e c o n d( al i t t l em o r et h a n ) u s b _ s e r i a l _ f l u s h ( ) d e l a y _ 1 m s ( 1 0 ) e n dl o o p I inserted one second delay just as an example on how to use such long delays without loosing the USB connection.

e n dl o o p

Closing the forever loop. Is also the end of the file but not the end of program which is executed forever, until no power or reset event.

5. Advices
Always use a multimeter to measure your resistors to know the exact output voltage for accurate results. Never choose tight voltage ranges for your application. By example, I will never have a 55Vcc as a maximum value. My application is ranging between 10Vcc and a maximum of 19Vcc. The 12Vcc battery can have a voltage in 10Vcc-14.4Vcc range. My solar panel have a maximum of 19Vcc voltage and I expect the same from my wind turbine.

https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb

5/6

7/12/2013

ADC value sent on USB - Country Laboratory

USB I/O Module


www.dajac.com/Expert+IO+1000 Easy PC Based Automation Digital, Analog, Motor, EEPROM

Comentarios

H o b b i tn a m e :T o g oT o a d f o o to fF r o g m o r t o n E l v i s hn a m e :M a b l u n gM i r i e l

C o n t e n tc o p y r i g h t( c )V a s i l eG u t aC i u c u r

Iniciar sesin | Informar de uso inadecuado | Imprimir pgina | Eliminar acceso | Con la tecnolog a de Google Sites

https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb

6/6

Potrebbero piacerti anche