RELATION OF VOLTAGES

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

RELATION OF VOLTAGES

Post by miguelmauricior »

Hi,
Can somebody tell what is the best method to implement this?

In the micro a signal inputs with one value but at the output the value must be other for example:

INPUT VOLTAGEvdc--------OUTPUT VOLTAGEvdc
1 ---------------------------------------0,412
2 ---------------------------- ----------0,312
3 ---------------------------------------0,212
4 ---------------------------- -----------0,112
5 ----------------------------------------0,100
What is the best method? A lookup table is ok or is something better to write it down on it decimal values?

Thanks.....
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

There are several ways to achieve desired results.
It depends what you want the results to be If reading 1.4V for example
The easiest way would be use the Switch component macro to give desired results from ADC input
e.g if ADC is reading 1V then byte value = 255/vcc*1 =255/5*1 = 51
If ADC is reading 3V then byte value = 255/vcc*3 =255/5*3 = 153 etc.
So if you only want specific voltages to give a reading e.g 1V but not 1.2V
then use switch component to only assign a integer variable with 412, then the display can show the string 0 then followed
by int value of 412.
Only problem with that method is ADC is waiting for exactly 1.00V (depending on tolerance of supply voltage)
so if voltage 1.05 or 0.93 then display will show 0 (How attached flowchart works)
It's a bit different if you want 1- 1.49V = 0.412
and
1.5V - 2V = 0.312
If that's the case you can use if >= then display will show a valve other than 0 for most of the voltage i/p unlike attached flowchart.
I think the best way if you don't want 1- 1.49V = 0.412, would be to allow a tolerance e.g 0.95 - 1.05 = 0.412. but since its your project, and I don't know the application it's your choice.
If you can let be know, then can help further.
Attachments
Voltage_Relation_16F88.fcf
(8.5 KiB) Downloaded 471 times
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi MEDELEC,

I was reading and thanks for your directions. Been more exactly about the process I want to manage is like this:

INPUT VOLTAGE_________________________________________OUTPUT VOLTAGE mV

0_________________________________________________________448
0,1_______________________________________________________439
0,2_______________________________________________________430
0,3_______________________________________________________421
0,4_______________________________________________________412
0,5_______________________________________________________403
0,6_______________________________________________________394
0,7_______________________________________________________385
0,8_______________________________________________________376
0,9_______________________________________________________367
1________________________________________________________ 358

1,1__________.................................................................

This is a tiny (0-1V) sample of the variable behavior, I took 50 samples . The inpunt voltage is in the range of 0-5V , so 5/50=0,1 in

the right column you will see the expected voltage value according to the input voltage, if you see carefully there is a decrement of 9mV

So I heard all suggestions to solve this. Let know your point of view.

Thanks.....
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

Hiya miguelmauricior.
I have devised this flowchart based on what you said.
It appears to work OK, but I'm the 1st to admit my maths skills are not the best.
Can you let me know if its more along the lines of what you are after please.
I have left the ADC int value on the LCD for reference. You can of course remove that if required.
There will be a slight tolerance error due to nature of pic maths, rounding down to nearest whole number, and if supply voltage is not exactly 5V
Attachments
Voltage_Relation_16F88.fcf
(8.5 KiB) Downloaded 432 times
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

Thanks for your reply and effort. I revised quickly and it seems to work OK, but I will make a more detailed proof and then I will let you know.
MMRZ

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Mecdelec,

I have a question relatad with the FORMULA. Can you explain me why is (448-(500/102*Read_ADC_Int/100*9)) according with the clues I gave you I want to know youyr point of view to make this formula if you do not mind or course.

Thanks.....
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

This is my line of thinking....
Since the lowest voltage (0V) you want 448 so you start off with that figure.
for every 1V difference ,your result will change by 90.
So you have Result= 448-(90*i/p_voltage)
Therefore if i/p Voltage = 0.5V then Result = 448-(90*0.5)= 403
Since value of i/p_voltage is a integer for better resolution = 0 - 1023,
in the calculation you need to replace i/p_voltage with formula that uses equivalent int.
e.g. 0.5V = 1023/5*0.5 = 204*0.5= 102. so the calculation has to convert 102 to 0.5.
Which is 5/1023*102 = 0.498
However there is one snag because not dealing with floats (floats take up loads of RAM)
we need to work with whole numbers, not fractions of numbers.
So instead of 448-(90*5/1023*i/p_int_Reading)
= 448-(90*5/1023*102)
= 403

the fraction part is /1023 so you have 90*5/1023 = 0.439. in PIC maths = 0
so unless you increase the numerator or decrease dominator or both you will always have 0*int_reading = 0
Hence the increasing numerator to 500 and decreasing denominator to 102.
Then I just divided result by 100 to reduce size back down then moved the 90 to the end but it required /10 = 9.

Like I said my maths skills are not great, so same result could probably be achieved by a simper method.

Hope this answers your question.
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

Thanks for your explanation, is clear for me , I will probably bother you If I need more details are you agree?
MMRZ

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

I have contact you more faster than I could think. I have a doubt. Forget you need to show the reading on a display and lets focus in doing the conversion of voltage according you told me. Asume you want to do this real. What I must do to get the signal I need? How I must make the conecction of PIC?
If I use the input signal as INT variable Could I get the equivalent voltage in the output? or I must changhe the type of variable at the input?

Thanks.....
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

miguelmauricior wrote:
How I must make the conecction of PIC?
If you right click pot in the control panel , then select connections, a separate box will appear.
In this box you will see 'Connect to: ADC: then a drop down box with ADC1 showing.
That is the pin connection used for i/p voltage.
If you can't see the diagram showing the chip pins: From View menu, make sure Chip is ticked.
Then you should see AN1 (note ADC is the same as AN) is pin 18. so you either use that as the i/p pin, of if you want a different pin for input change the ADC1 in the drop down box.
miguelmauricior wrote:
f I use the input signal as INT variable Could I get the equivalent voltage in the output?
If I am understanding correctly, This is much more difficult to achieve, since o/p is either logic 0 (0V) or logic 1 (+5V if using a 5V supply)
If you are wanting the o/p to be an equivalent to i/p then you need to create a digital to analogue converter.
You can buy them but it complicates things as you need to use at least 8 bits to have a range of 0 - 255.
What I would do is use a PWM at say 2KHz - 5KHz place a resistor in series with o/p and one side of a capacitor. then connect the other side of cap to 0V. The higher the duty the higher the voltage appearing across the cap so long as correct values are chosen. You will need to plot duty against o/p voltage then derive a look up table of formula (if linear enough or your very cleaver at maths and can do second order equitations etc) to give correct duty.
Like I said it is not easy, and I don't believe there is a straight forward answer for the equivalent voltage o/p. (unless there is a feature of a PIC I as missed)
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

Thanks again for your reply. Respect the first question I have no doubts, but in the second I am afraid it must be something to do. It supose the chip has an analogue to digital converter so it can recieve the analog signal and the program makes the conversion as the table indicates.
Why is not posible? somehow I am trying to atenuate a signal like an operarional amplifier does. The question is how I must manage the INT variable at the input to get the expected voltage at the output?
MMRZ

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RELATION OF VOLTAGES

Post by Benj »

Hello,

The best way of creating an analogue voltage would be to use an I2C or SPI DAC. You will be able to pick them up for a low cost from Rapid or Farnell etc.

Otherwise as Medelec suggested a PWM output with a current limiting resistor and a smoothing capacitor will work well for generating a analogue voltage. Care should be taken when calculating the capacitor being used. You should use a cap that will respond in around 3 cycles of the PWM signal.
http://ww1.microchip.com/downloads/en/A ... 00538c.pdf

A third way would be to create a R2R network using resistors and an 8-bit port as shwn here.
http://www.avr-asm-tutorial.net/avr_en/AVR_DAC.html

Some PIC devices do have onboard DAC hardware but these seem rare and only in the much larger SMD devices. Flowcode does not currently support these.

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hello Benj,

I have not speak with you for months. Let me see if I understood correctly. If I want to use a PIC 12F675 I cannot directly process an analogue signal becuase the FC4 does not support this?

So I wonder the what it is the purpose of the option which requests for read as a voltage a variable(as a float) which you have just declared in the ADC MACRO?

Please correct me if I am wrong but I need to convert the input voltage in a digital signal with a discrete circuit and then I will get the analogue signal to the output of tghe PIC?

Thanks......
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

miguelmauricior wrote:
So I wonder the what it is the purpose of the option which requests for read as a voltage a variable(as a float) which you have just declared in the ADC MACRO?
the ADC macro is used for reading a voltage on analogue i/p pin and converting this input voltage into a numeric value which is stored in a variable.
E.g if reading ADC as a byte with a variable called Read_ADC_Voltage and there is 1.6V on the i/p pin, then value stored in a Read_ADC_Voltage is 255/5*1.6 = 81 not 82 or 81.6 since result = whole number rounded down.
if reading ADC as a In, then using same voltage. Value stored in Read_ADC_Voltage variable is 1023/5*1.6=327
So there is no voltage conversions, just variables assigned with a value.
Same with read as float that will just contain a higher precision number.
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RELATION OF VOLTAGES

Post by Benj »

Hello Miguel,
Let me see if I understood correctly. If I want to use a PIC 12F675 I cannot directly process an analogue signal becuase the FC4 does not support this?
This is not a Flowcode limitation. Rather that most microcontrollers can quite happily read in an analogue voltage but have no capbility for outputting an analogue voltage other then via the methods I have already suggested.

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec/Benj,

Ok I wil do that in order to get it. So thanks and any else I will let you Know.
MMRZ

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec/Benj,

I guess the R2R option is better becuse is more accurate than the PWM but I have another question. Cuold I have anotherkind of relation (voltage- frequncy).
In other words the output would not be and analog output instead of that the output would be a frequency according with the respective input voltage. Is this case the micro can directly manage the signal?

Let know thanks.....
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

miguelmauricior wrote:
I guess the R2R option is better becuse is more accurate than the PWM
Yes is much more accurate. But you need to be aware that the resolution is 19.5 mV with 8bits. but if thats accepable, then that would be my 1st choice as well.
miguelmauricior wrote:
Cuold I have anotherkind of relation (voltage- frequncy).
In other words the output would not be and analog output instead of that the output would be a frequency according with the respective input voltage. Is this case the micro can directly manage the signal?
Yes this should be fairly easy to set up.
You can use the PWM module. if you have not use PWM module before, the 1st PWM component has to be Enable, and ndlx(Byte) is the PWM channel (either 1 or 2 )There is an option to change period, but I not sure if you can get it to perform as well as you would like it to. I'm guessing it will not be accurate at all.

My 2nd choice although it does seem a bit of a waste is to use 16 pins of a microcontroller and use R2R DAC as you will then have 4.9mV resolution. However with resistor tolerances may not be as accurate as MAX5541, but I'm sure you will get good results.

I would probably use MAX5541 or equivalent which is Low-Cost, +5V, Serial-Input, Voltage-Output, 16-Bit DAC as my 3rd choice.
Use one pin of your microcontroller as a serial o/p, another for CS and one more for clock. Since is 16bit you would have the accuracy there.
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec/Benj,

I was reading about suggested methods and as I said before the best is with a DAC but I have two quesrtions related with this.

1. If I use ac DAC 0800 how is the right connection and in the code you suggested me I must declare an output with 8 bits?

2. If I use a DAC like MAX5541 how is the right connection and in the code you suggested me I must declare an output with 8 bits too?

Let know please.


Thanks.......
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

miguelmauricior wrote:
2. If I use a DAC like MAX5541 how is the right connection and in the code you suggested me I must declare an output with 8 bits too?

Let know please.


Thanks......
Have a look at:
http://datasheets.maxim-ic.com/en/ds/MAX5541.pdf
If it was me I woud 1st hold CS low, then transfer 16 bits (15 bits + a trailing 0) to Din (MSB 1st then LSB), but after each bit you need to clock it with an additional clock o/p form microcontroller.
After all 16 bits been transferred then CS needs to go high, then DAC is updated so you should get the equivalent analogue voltage out.
I have not personally done this, but it's just theory form looking at the datasheet. If I had one of these devices, I would test theories for you, but unfortunately I have not. sorry.
If you do decide to go down MAX5541 route, then I can help you to adjust your flowchart so it gives desired analogue o/p.
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

Thanks again for your reply. I could not reply before because I was doing field work. I red the reply and actually I do not have a serial DAC but I have a pararell one (DAC 0800) and I have a 16F876A for make some practices before and when I get the desired behavior I will use the MAX reference and the other PIC reference.
But I wonder if you have idea how to connect this DAC to this PIC or Is there some application note which gives me this reply?

Other question I have is related with an isolated proof of the PIC. It is supose the input must match to a digital output (for example:11111111 equivalent to 5VDC) having a +REF=5VDC and -REF=0VDC) Am I right? or not. So please can you tell me if I am doing this correctly or there something missing? I guess if I program the PIC and input 2,5V I must have the equvalent digital voltage at the output.

Thanks...
MMRZ

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

I did what you suggested me, I removed all LCD macros from the code but the behavior of the program differs according with the active outputs when the ADC input is sliding (comparing the percentage. The code you sent me and the code I modified). Anyway I have attached the modified one and can you tell me please what is wrong?

Thanks....
Attachments
CONVERSION DENSIDAD II.fcf
(6 KiB) Downloaded 346 times
MMRZ

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: RELATION OF VOLTAGES

Post by medelec35 »

miguelmauricior wrote: but I have a pararell one (DAC 0800)
if you have idea how to connect this DAC to this PIC or Is there some application note which gives me this reply?
The problem is you wanted 10 bit resolution, but DAC0800 is a 8bit DAC not 10bit.
So you will loose accuracy, and the formula needs revising for 8bit byte instead of 10bit integer.
You need to decide if you want to do that.
Martin

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

Hi Medelec,

Can you clear something for me? Yes in the 16F876A datasheet says it has an 10 bit ADC but physically I have just 8 pins per port. Yesyerday I did implent the cincuitry for a single ADC with an input (0-5V) refernced to 5V too and the output of ADC working fine according to theory.

Now I am trying to do the same thing but using the code I sent you unsing the port B but I am not getting the expectec behavior.

The pic is referenced to 5V because the input is (0-5V) too, the output goes to the DAC 0800 inputs and the DAC is referenced to 0.448mV, all are to the same GND.

The inpunt does not match with the output according to the table (remember). So I have many questions:
1.I would like to know for 8 bit. and after for 10 bit cases.
2.I guess the variable which goes to the pic output must be a BYTE , Am I right or not? what is the correct variable type?
3.Are correctly voltage refernced the PIC and the DAC or not
4.Why the circuitry is not doing the expected?

Please let know becuse I tried but unsuccesfully. Thanks.......
MMRZ

User avatar
miguelmauricior
Flowcode V4 User
Posts: 42
Joined: Tue May 05, 2009 12:13 pm
Location: COLOMBIA
Contact:

Re: RELATION OF VOLTAGES

Post by miguelmauricior »

[color=#0000FF][quote] Hi again Medelec,

Let me tell you something I tried. Probably is not right to interface the PIC with 5V reference with the DAC 0.448V reference. So I guees if I reduce the PIC inputting signal ten times (0-500mV) and its respective reference (500mV) too the signals between PIC and DAC will be more proportionated one to each other.

I tried this but there is somethig that must be corrected. The formula (Result = 448-(500/102)*(Read_ADC_Int/100*9)). So this what I guess.

Please can you tell me what I must do to achieve it and do not forget the previous post.

Thanks.....[/quote][/color]
MMRZ

Post Reply