Page 1 of 4
code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 8:08 am
by daveb0360
Hi Guys,
I'm hoping someone can help with this one pretty quickly as I've got a customer waiting.
I have built a simple thermostat..hardware done but now I need some code to run it.
The unit needs to run on a 12 or 24 volt system so the PIC (12F675) needs to check that the incoming supply is between 2 well defined 'windows' of voltage. It has a resistive divider on AN2, scaling the incoming supply line to fall into 5v upper limit of ADC with a maximum 30V rail........this is simple.
However, I need the chip to deactivate the temperature measurement routine and disconnect an output (set GP0 - 0) if ever this rail measures outside 2 windows that are (unscaled, window 1 will be 14V to 16V and window 2 will be 27V to 30V). Outside of these two 'windows' of good voltage, the device will lock out - go into defined state but recover to normal operation at any time the voltage comes right.
In summary, voltage below 14V - Switch off GP0
Voltage above 14V but below 16V - normal operation
voltage above 16V but below 27V - Switch off GP0
Voltage above 27V but below 30V - normal operation
Voltage above 30V - lock out operation and indicate with flashing LED.
There needs to be some form of verification loop to ensure that noise doesn't trigger when nearing thresholds or hysteresis.
Is there simple code that can do the checks on an2.
Many thanks in advance
Dave B
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 10:12 am
by medelec35
Hi Dave,
This should get you started:
http://www.matrixmultimedia.com/mmforum ... 71&#p26475
I'm at work now, but when I get home this evening, If you have not got a flowchart doing what you would like to, then I will create one or you.
Martin
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:01 pm
by Enamul
Hi,
Here is a sample program to fulfil your requirement...I have made that for 4 MHZ crystal as clock but if you want to use internal clock please let me know..I will modify the code..
Hope this will help.
Enamul
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:09 pm
by daveb0360
Hi Enamul,
Thanks so much for this, yes I am running internal clock at 4mhz. This simulates ALMOST as needed. The only issue is that when the led flashes on overrange, it should stop flashing when back in range......I may be able to figure this out myself but you have given me fuel for going forwards.
Much appreciated.
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:15 pm
by daveb0360
Hi Enamul,
If I rewrite the macro 'normal' to contain the code for the temperature measurement and control routine, my thinking is that this will work as a thermostat that only works when the voltages measured are within the required windows.........Am I right?
dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:22 pm
by Enamul
Hi,
I have changed to solve the issue of blinking..in normal condition...change to internal clock..
If I rewrite the macro 'normal' to contain the code for the temperature measurement and control routine, my thinking is that this will work as a thermostat that only works when the voltages measured are within the required windows.........Am I right?
Yes..it should..you can add the code you have added in my latest posted program so that I can have a look..
Enamul
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:30 pm
by medelec35
Hi dave,
I had a chance during lunch to create attached flowchart.
You mentioned about spikes not activating over voltage, so I have set flowchart for 100 samples before detection takes place.
Flash rate is approx 3Hz
With this and Enamul's version you have got 2 to work from....better than none
Martin
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:35 pm
by daveb0360
Hi Enamul,
You are a genius sir.........I bow to you...
Now I have to do my bit and produce the code to measure temperature, hysteresis and indication.
When I've done this, I'll forward you the code if you don't mind for an expert analysis.......thanks so much again.
I hope this talent you have means you have a good job!
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 12:37 pm
by daveb0360
Hi Martin,
Can't thank you enough, now all I have to do is write the code for the temperature control and allocate it to the correct part of the code......genius you guys are..
Thanks so much......I reckon I can roll from this...
Cheeeeeerss
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 1:10 pm
by medelec35
No problem Dave,
Glad with combination of flowcharts you have enough to allow your project to be completed.
With my flowchart I'm assuming with potential dividing resistors there is a division of 6.
So for 27V you have 27/6*10 =4.5*10 = 45.
If that's not the case then values representing voltages within decision branch will need to be adjusted or yo can remove formula and just manually calculate requiired ADC values.
Martin
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 1:30 pm
by daveb0360
FYI, This is the front end of the circuit.....
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 1:36 pm
by daveb0360
Hi Martin,
Yes, division is approximately 6.....depending on ADC input loading, this is where I need to 'play' with conversions and calcs when I have the finished hardware on the bench......this I am ok with thanks.....or I hope I will be....lol.
Am waiting for PCB's to be delivered tomorrow, then I can build a couple and play with absolutes.
Many thanks again ....both of you guys.
I dare say this won't be the last bit of help needed on this.....I'm bound to get stuck at some point in the 'refining' process but groundwork is there. For example, I need to detect and indicate if sensor goes faulty (open/short).... I also guess I'm gonna need to start figuring out the difference between integers, bytes, readasvoltage etc. in order to use the best suited to the application.
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 1:40 pm
by daveb0360
One question.......do you think it would be ok to replace the 100K resistor, R8 with a 10k....saves me buying another value in......10K is used elsewhere in the circuit and would reduce cost/complexity...
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 2:21 pm
by kersing
daveb0360 wrote:One question.......do you think it would be ok to replace the 100K resistor, R8 with a 10k....saves me buying another value in......10K is used elsewhere in the circuit and would reduce cost/complexity...
Dave
It depends on the programmer you are using, but normally that should not be a problem at all. In the
PICkit 3 documentation Microchip even suggests 4.7 - 10 K typical.
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 2:43 pm
by daveb0360
Thanks, am using Pickit 2....it's been a great little programmer.
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 3:10 pm
by medelec35
I agree with kersing,
10 K will be suitable as you can even go lower than this.
As for thermistors, best way would be to produce a look-up-table (LUT)
Which gives the value of temperature determined by value of ADC connected to Thermistor
If you have a datasheet for thermistor (hopefully including beta value), I can create a flowchart that assign’s a variable with a value of degrees C measured by thermistor.
Martin.
Re: code for dual voltage 'window' on ADC input
Posted: Mon Aug 06, 2012 3:11 pm
by Enamul
Hi,
I never used 100K in my circuit.. always 10K..so you can change that to 10K easily
One question.......do you think it would be ok to replace the 100K resistor, R8 with a 10k....saves me buying another value in......10K is used elsewhere in the circuit and would reduce cost/complexity...
Enamul
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 12:24 am
by medelec35
Hi David,
I have integrated room temperature measurement.
Until you can post details of thermistor,I can make assumptions.
So I'm assuming the following:
Beta is 3500 @25 to 50C
Room temperature req is 21 deg C
Thermostat will switch boiler off at 20C (I can change that if required) since there could be residual heat still heating up room after Thermostat has shut off.
For external controls, I am curious to how your going determine set point (if it's adjustable), and how your going disable Thermostat if room is not in use?
Note Look-up data for LUT can be found in Supplementary code (view, project options, Click on Supplementary code)
If you would like to learn how LUT work then try here:
http://www.matrixmultimedia.com/mmforum ... 26&t=10066
Martin
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 9:02 am
by daveb0360
Wow.....the help you provided is unbelievable.......thanks so much.
FYI, the heater is actually a water heater and has a maximum preset temp of 45C +/- 2c.
The thermistor is
http://uk.farnell.com/vishay-bc-compone ... dp/1761147
2.2K @25c and beta of 3520k
Boards are coming today so will be building one up and uploading the code for trials over the next day or so.
Indicator LED needs to be slow flash whilst power to unit is on......probably 0.25sec every 2 secs or so. Like a car stereo or alarm indicator.
LED needs to rapid flash if over temp by more than 20 degc to warn of possible scald risk or empty tank. at the same time, this condition MUST ensure heater output is set OFF.....like an emergency override to prevent damage or scalding.
LED needs to be constant on when water is at temperature setpoint with slow flash off whilst heating......long on.......short off.
LED needs to go very rapid if sensor is open or short as well as preventing heater from starting whilst in this fault condition.
Bit of a tall order I suspect
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 9:36 am
by daveb0360
Hi Guys,
Is there a limit to the number of macros that can be called from within the main loop?
I am thinking I could write a macro for each of the led flash functions and call the relevant one from within the main loop.....done in the correct order and cancelling the current one would seem logical is this feasible?
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 10:26 am
by Enamul
Hi,
As you are a FC v4 user, there is no known limitation in the number of macros...
Is there a limit to the number of macros that can be called from within the main loop?
Enamul
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 10:37 am
by daveb0360
Thanks Enamul,
I was more concerned as to whether there was any limitations within the chip........but it's good know there are no program limitations known....
Cheers again.
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 10:40 am
by daveb0360
Enamul wrote:Hi,
I never used 100K in my circuit.. always 10K..so you can change that to 10K easily
One question.......do you think it would be ok to replace the 100K resistor, R8 with a 10k....saves me buying another value in......10K is used elsewhere in the circuit and would reduce cost/complexity...
Enamul
Thanks, I ended up using 22k.......I have these elsewhere in the circuit
Dave
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 11:05 am
by Enamul
I was more concerned as to whether there was any limitations within the chip........but it's good know there are no program limitations known....
Actually normal macro is just a reusable code which is invoked when you call them..the limitation will come if you macros within INT macros in-depth upto 8...as the return address is saved in stack...in hardware PIC has 8-level deep stack..but we don't normally use that much..I am not quite sure MM FC custom macros and built-in macro uses similar hardware stack or software stack..
Enamul
Re: code for dual voltage 'window' on ADC input
Posted: Tue Aug 07, 2012 12:05 pm
by medelec35
Hi David,
Given safty requirements of your progect, I would suggest having watchdog enabled.
I also recommend using 12F617 instead of 12F675.
It has many advantages.
E.g twice as much memory (I have ran out of space with 12F675),
Cheaper,
can run at 4 or 8MHz,
Has 10bit PWM.
Also If I post a flowchart a bit later, it will be of your risk if you decide to use it.
Please only download and use if your going to accept full responsibily.
Martin