Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Home
News
AVR Projects
PIC Projects
Electronics
Links
Sitemap
Translation
Romanian pages
Main Pages
Software File archive Windmill Blog Contact
Traductor de Google
https://sites.google.com/site/funlw65/tutorials/jal-projects/adc-value-sent-on-usb
1/6
7/12/2013
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.
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).
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
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
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.
-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.
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).
-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
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
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