ADC as timer
Moderator: Benj
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
ADC as timer
Hi Guys
I have used the ADC to control a time delay for one of my metal detectors and it works on the simulator but I have a problem regarding what pre-set value that I must use on the real thing. At the moment I have it running in milliseconds to be able to see if it's doing what I want it to but I will be changing that to microseconds. It could very well be that I'm barking up the wrong tree as I have never used this way to control a detectors pulse. I just want to add that I need to have these delays for the configuration of the detector and for the final timing of the pulse and the sampling time. If I could find a schematic diagram as to how to go about it all it would be great but I hunted the net and found nothing that I could relate to.
All the best
Xavier
I have used the ADC to control a time delay for one of my metal detectors and it works on the simulator but I have a problem regarding what pre-set value that I must use on the real thing. At the moment I have it running in milliseconds to be able to see if it's doing what I want it to but I will be changing that to microseconds. It could very well be that I'm barking up the wrong tree as I have never used this way to control a detectors pulse. I just want to add that I need to have these delays for the configuration of the detector and for the final timing of the pulse and the sampling time. If I could find a schematic diagram as to how to go about it all it would be great but I hunted the net and found nothing that I could relate to.
All the best
Xavier
- Attachments
-
- Pulse_Pin-Pointer.fcf
- (20 KiB) Downloaded 369 times
- 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: ADC as timer
Hello,
The delay functions can only handle a byte so if the value your passing can go above 255 then you will need to create a simple mechanism to ensure that you do not pass any values over 255 to the delay function.
This can be done inside a macro like this.
while (delay > 255)
{
delay_us (255)
delay = delay - 255
}
delay_us(delay)
Hope this helps.
The delay functions can only handle a byte so if the value your passing can go above 255 then you will need to create a simple mechanism to ensure that you do not pass any values over 255 to the delay function.
This can be done inside a macro like this.
while (delay > 255)
{
delay_us (255)
delay = delay - 255
}
delay_us(delay)
Hope this helps.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Although that is true with V4, I thought this was revamped with V5 so it now works with integers.Benj wrote:The delay functions can only handle a byte so if the value your passing can go above 255 then you will need to create a simple mechanism to ensure that you do not pass any values over 255 to the delay function.
I Could be wrong of course

Time for some experimenting. I will post my findings
Martin
Edit. This was the post I remember looking at:
http://www.matrixmultimedia.com/mmforum ... 83&#p32183
Martin
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
I can confirm:
Martin.
As I just tried it myself.JonnyW wrote:you can use one call in v5. The delays support values up to 65536 of whatever unit you are delaying by. Inserting as an immediate constant allows up-to (and including) 2000, so you could delay by, say, 1.5 seconds (1500 ms).
Martin.
Martin
- 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: ADC as timer
Great thanks for that Martin, Note to self, v5 is excellent 

Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Your welcome Ben.
Martin
Now that I do agree with that 10000000%Benj wrote: v5 is excellent

Martin
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Thanks for the reply guys
I have it set at 65536 and it works great on V5 but not on the PIC, any idea where I could be going wrong? Also how do I know what POT to use? 1K, 10K, 100K?
is there a way to work that out? and do I have to use Vref or can I use VDD?
All the best
Xavier

I have it set at 65536 and it works great on V5 but not on the PIC, any idea where I could be going wrong? Also how do I know what POT to use? 1K, 10K, 100K?
is there a way to work that out? and do I have to use Vref or can I use VDD?
All the best
Xavier
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
65535 does seem to work ok.
Would you like to post your flowchart, I can take a look at it for you.
As for pot I would go for a value between 4K7 and 10K.
The higher the value the more ADC value could be affected by electrically noisy environment.
The lower the resistance the more current will flow and power will be consumed.
As for connections. Two outer pins of pot can be connected to +5V & 0V. The centre pin is connected to the ADC pin.
When the pot is adjusted to supply the ADC with a voltage depending on the setting the the relationship between ADC and Voltage for byte is:
ADC=255/5*Vin
So if Vin =2V then ADC = 255/5*2=102
vin = 5/255*ADC
If using interger then replace 255 with 1023
Martin
Would you like to post your flowchart, I can take a look at it for you.
As for pot I would go for a value between 4K7 and 10K.
The higher the value the more ADC value could be affected by electrically noisy environment.
The lower the resistance the more current will flow and power will be consumed.
As for connections. Two outer pins of pot can be connected to +5V & 0V. The centre pin is connected to the ADC pin.
When the pot is adjusted to supply the ADC with a voltage depending on the setting the the relationship between ADC and Voltage for byte is:
ADC=255/5*Vin
So if Vin =2V then ADC = 255/5*2=102
vin = 5/255*ADC
If using interger then replace 255 with 1023
Martin
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Hi Martin
I posted the flow chart in my first post. I have been using 100k pots then I used 5k pots but it looks as though it is either running at a very high speed (much higher than the simulation) or there is something that I did wrong. I will use seconds instead of milliseconds to see if that will resolve my problem if it does then I will use the scope to see what is happening when I have it in milliseconds.
Thanks for your time and your help
Best regards Xavier
I posted the flow chart in my first post. I have been using 100k pots then I used 5k pots but it looks as though it is either running at a very high speed (much higher than the simulation) or there is something that I did wrong. I will use seconds instead of milliseconds to see if that will resolve my problem if it does then I will use the scope to see what is happening when I have it in milliseconds.
Thanks for your time and your help
Best regards Xavier
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Sorry though you was going to change it, my mistake.
OSC is set to 19.66MHz, but you have got it set to internal osc.
There is not a 19.66MHz internal osc.
So your timing will all be out as your experiencing.
Best advice when first creating a flowchart is create a 1Hz flasher.
e.g :
Loop: Led on: delay 500ms: Led off delay 500ms: Goto Loop
Make sure LED is flashing at correct frequency.
What frequency do you want to run the oscillator at?
Are you using the internal osc, or an external crystal?
Martin
It looks like a problem with your configuration settings.Xavier wrote: or there is something that I did wrong.
OSC is set to 19.66MHz, but you have got it set to internal osc.
There is not a 19.66MHz internal osc.
So your timing will all be out as your experiencing.
Best advice when first creating a flowchart is create a 1Hz flasher.
e.g :
Loop: Led on: delay 500ms: Led off delay 500ms: Goto Loop
Make sure LED is flashing at correct frequency.
What frequency do you want to run the oscillator at?
Are you using the internal osc, or an external crystal?
Martin
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Hi Martin
I wanted to run it on the internal timer for the moment but later on change it to external. As I'm not yet sure at what frequency the detector will be running at, but it will not be over 300hz and probably more in the region of 120hz to prevent over heating and to allow enough decay time for the coil. PS this is for a pulse induction detector (Pin pointer)
All the best
Xavier, Please remember that I'm very green in the digital world
I wanted to run it on the internal timer for the moment but later on change it to external. As I'm not yet sure at what frequency the detector will be running at, but it will not be over 300hz and probably more in the region of 120hz to prevent over heating and to allow enough decay time for the coil. PS this is for a pulse induction detector (Pin pointer)
All the best
Xavier, Please remember that I'm very green in the digital world

-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Hi Xavier,
I have set the internal oscillator for 8MHz. For this a C code block with osccon=0x70; is required.
So that means If your not using EB006, then less components are needed.
If you are using EB006 then you can switch external osc off by moving jumpers (J18 & J19 i think?) from OSC to A6&7 and SW2 from XTAL to RC
You also had Watchdog timer left on, so your microcontroller probably kept restarting hence giving you short time delays.
:
http://www.matrixmultimedia.com/lc_microcontroller.php
Martin
I have set the internal oscillator for 8MHz. For this a C code block with osccon=0x70; is required.
So that means If your not using EB006, then less components are needed.
If you are using EB006 then you can switch external osc off by moving jumpers (J18 & J19 i think?) from OSC to A6&7 and SW2 from XTAL to RC
You also had Watchdog timer left on, so your microcontroller probably kept restarting hence giving you short time delays.
Take a look here for a great FREE!!!! 'An introduction to microcontroller programming' courseXavier wrote:Please remember that I'm very green in the digital world

http://www.matrixmultimedia.com/lc_microcontroller.php
Martin
- Attachments
-
- Pulse_Pin-Pointer2.fcf
- (20 KiB) Downloaded 375 times
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Hi Martin
Thanks for that link I'm busy downloading it. So the watchdog needed to be disabled and then there was the osccon=0x70 I suppose that there must be a list that would go with all the different frequencies? But tell me up to what frequency can I go to using the internal oscillator? I programmed my PIC with your modified code
it's doing exactly what I want it to. This is great thank you so much.
All the best
Xavier
Thanks for that link I'm busy downloading it. So the watchdog needed to be disabled and then there was the osccon=0x70 I suppose that there must be a list that would go with all the different frequencies? But tell me up to what frequency can I go to using the internal oscillator? I programmed my PIC with your modified code

All the best
Xavier
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Hi Xavier,
your welcome. Just glad that your flowchart works the way you intended it too.
Then I look for the internal osc values: I posted some information on how I derive the osccon value to use. see:
http://www.matrixmultimedia.com/mmforum ... 29&#p28829
your welcome. Just glad that your flowchart works the way you intended it too.

What I do is search on Google for the data sheet, then on the data sheet search for osccon.Xavier wrote: I suppose that there must be a list that would go with all the different frequencies? But tell me up to what frequency can I go to using the internal oscillator?
Xavier
Then I look for the internal osc values: I posted some information on how I derive the osccon value to use. see:
http://www.matrixmultimedia.com/mmforum ... 29&#p28829
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Hi Martin
Firstly I want to thank you for the info on the OSCCON very interesting stuff now I have to find a calculator to do the job but that should not be an issue. I managed to blow my 16f690 (very stupid of me) but I happened to have a few 16f877a so I thought why not put them to use. The first thing that I found was that there is no internal oscillator so I used a 4Mhz crystal and two 22p caps I also have 4 LED's that are independently turned on. These LED's are there only to show that the program is working and will be removed. I also have 4 x 10K multi turn pots connected to the ADC you will notice that there are 6 in the flow chart but I'm only using ADC0 to ADC3 for the moment. Now here is the problem that I have come across, my program runs for about 15 seconds then the thing stops I have tried many things but non are the right solution and as I cant see it being a hardware issue (I have replaced the chip) it can only be software but I really can't see where I'm going wrong. By the way the first program worked but the detection was only 5mm so I did a few mods that should make it a lot better.
All the best
Xavier
Firstly I want to thank you for the info on the OSCCON very interesting stuff now I have to find a calculator to do the job but that should not be an issue. I managed to blow my 16f690 (very stupid of me) but I happened to have a few 16f877a so I thought why not put them to use. The first thing that I found was that there is no internal oscillator so I used a 4Mhz crystal and two 22p caps I also have 4 LED's that are independently turned on. These LED's are there only to show that the program is working and will be removed. I also have 4 x 10K multi turn pots connected to the ADC you will notice that there are 6 in the flow chart but I'm only using ADC0 to ADC3 for the moment. Now here is the problem that I have come across, my program runs for about 15 seconds then the thing stops I have tried many things but non are the right solution and as I cant see it being a hardware issue (I have replaced the chip) it can only be software but I really can't see where I'm going wrong. By the way the first program worked but the detection was only 5mm so I did a few mods that should make it a lot better.
All the best
Xavier
- Attachments
-
- Pulse_Pin-Pointer_4.fcf
- (27.26 KiB) Downloaded 327 times
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Ho I forgot I'm getting this when I compile the flow chart to hex
Warning: function never returns: 'FCM_Start'
Building CASM file
Serious Warning: Possible sw stack corruption, function 'delay_ms' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)
Warning: function never returns: 'FCM_Start'
Building CASM file
Serious Warning: Possible sw stack corruption, function 'delay_ms' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Hi Xavier,
Your welcome. The calculator is shipped free with windows 7. Should be in Start, accessories folder.
Or you can click on the windows key and in the search box enter cal and you should see calculator appear.
If you have not got windows 7, then calculator can be downloaded from here:
http://www.softpedia.com/progDownload/W ... 22549.html
This site is reputable, and calculator is not shareware, but freeware, so is legal to use.
Just click on one the 'Softpedia Secure Downloads'
As for your Flowchart, there are serious issues which would defiantly cause your microcontroller to lock up.
What will be causing this is you have delays in timer0 interrupt and delays in your macros.
You should avoid at all costs delays, and components that use delays within your interrupts. Eg. LCD, RS232.
You only need to enable an interrupt once, (unless you disable it within your software).
You have placed the interrupt within a loop in main. Definitely not. You should place interrupt enable at the start of main, before the loop.
Also you have got the timer0 interrupt calling another macro, which calls another macro.
That will also cause corruption. It is always best to keep interrupts as short as possible, with no calling macros or as stated earlier no delays.
Martin
Your welcome. The calculator is shipped free with windows 7. Should be in Start, accessories folder.
Or you can click on the windows key and in the search box enter cal and you should see calculator appear.
If you have not got windows 7, then calculator can be downloaded from here:
http://www.softpedia.com/progDownload/W ... 22549.html
This site is reputable, and calculator is not shareware, but freeware, so is legal to use.
Just click on one the 'Softpedia Secure Downloads'
As for your Flowchart, there are serious issues which would defiantly cause your microcontroller to lock up.
Any warnings like this can't be ignored and will cause your hardware to freeze. It may take a little while but it will probably happenXavier wrote: Serious Warning: Possible sw stack corruption, function 'delay_ms' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)

What will be causing this is you have delays in timer0 interrupt and delays in your macros.
You should avoid at all costs delays, and components that use delays within your interrupts. Eg. LCD, RS232.
You only need to enable an interrupt once, (unless you disable it within your software).
You have placed the interrupt within a loop in main. Definitely not. You should place interrupt enable at the start of main, before the loop.
Also you have got the timer0 interrupt calling another macro, which calls another macro.
That will also cause corruption. It is always best to keep interrupts as short as possible, with no calling macros or as stated earlier no delays.
Martin
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Hi Martin
Well I removed all the macros and now just have the main macro but there is no way that I can do this without delays even with analogue I use delays. Now when I compile the flow chart to hex there are no more errors but it still hangs up. I'm starting to think that I should rather stick to analogue and leave the digital stuff to people like yourself.
I'll give it another go in the morning.
All the best PS I guess that it gets easier with time
Xavier
Well I removed all the macros and now just have the main macro but there is no way that I can do this without delays even with analogue I use delays. Now when I compile the flow chart to hex there are no more errors but it still hangs up. I'm starting to think that I should rather stick to analogue and leave the digital stuff to people like yourself.

All the best PS I guess that it gets easier with time

Xavier
- Attachments
-
- Pulse_Pin-Pointer_4b.fcf
- (21.24 KiB) Downloaded 353 times
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Well here are the latest happenings I made a blinking led on RB0 used a 4Mhz crystal and it hangs up. This can't be right something else must be wrong.
All the best
Xavier

All the best
Xavier
-
- Matrix Staff
- Posts: 9521
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
Re: ADC as timer
Hi Xavier
If you would like to post your flowchart we could eliminate that for you 1st.
Martin
If you would like to post your flowchart we could eliminate that for you 1st.
Martin
Martin
-
- Posts: 99
- Joined: Sat Mar 17, 2012 11:26 pm
- Location: South Africa
- Has thanked: 5 times
- Been thanked: 3 times
Re: ADC as timer
Hi Martin
Well I gave up on the 16f877a as there are far too many perimeters on that chip and I don't need such a big PIC ,so went to the shop and got a few 16f690. I have found that the way that I wanted to do thing dose not work very well so now my question is how cold I analyse a frequency change? I would imagine that I will still be using the ADC. As I did not find any thing that was related to my problem on the forum and I do not have a clue where to start, could you tell me where can I find an example or could you please tell me how I should go about doing this?
All the best
Xavier
Well I gave up on the 16f877a as there are far too many perimeters on that chip and I don't need such a big PIC ,so went to the shop and got a few 16f690. I have found that the way that I wanted to do thing dose not work very well so now my question is how cold I analyse a frequency change? I would imagine that I will still be using the ADC. As I did not find any thing that was related to my problem on the forum and I do not have a clue where to start, could you tell me where can I find an example or could you please tell me how I should go about doing this?
All the best
Xavier