I am wanting to read/measure the duty cycle on an input pin using the PIC16F1823. Is this possible? I cannot seem to find anything in the documentation. Do you have an example codes to help me set this up ?
Thanks,
Phil
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've done this for a particle detector (PM2.5). The basic procedure is to use the gate on TIMER1 connected to the signal so that TIMER1 will only run if the gate is high (or low - can be selected).
One of the other timers needs to run continuously (with suitable pre and post scale selected).
When this second timer overflows the values in TMR1H:TMR1L give the amount of time the gate has been on (or off).
This is something of a niche activity so will probably have to be done by writing to the registers rather than using GCB commands. Below is my example it is on a 16F18326 so I have more 16-bit timers available. In the example time1 is running continuously (from an external clock crystal) and timer3 and timer5 are gated (from the particle detector).
The processor is normally in SLEEP and wakes up on a TIMER1 overflow (every 8 seconds) when timer3 and timer5 can be read.
Note the processor you have has only one 16-bit counter (timer1) and the register names may be different, but I hope it gives you some ideas.
I wrote a program that measures the frequency of the mains electricity supply which uses a modified version of that method outlined in the article @evanvennn linked to above. This uses the pulse width measured value which is then converted to a frequency. I took my inspiration from a post by @williamroth.
The ideal code/method /PIC depends upon how quickly and how often you need to measure the duty cycle.
Many PIC chips include a Signal Measurement Timer (SMT) peripheral that simplifies measuring frequency, pulse width, duty cycle, et al. It is very fast and very accurate.
If speed a accuracy are important would use a PIC 16F18426 with SMT.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Measuring a pulse width alone will not provide enough information to allow calculation of duty cycle unless the frequency is fixed. If the frequency varies then both the off time and on time will need to be measured. In some circles these are referred to as mark and space. The mark time and space time are summed to give Cycle time/Period then the period is divided by the mark time to give duty cycle in percentage.
duty cycle % = 100 / ( (Mark + Space) / Mark )
The SMT Timer peripheral does this automagically
The same general method applies to any chip.
1. Measure the on time
2. Measure the off time
3. Do the math
Skip # 2 if the frequency/period is fixed
Last edit: William Roth 2021-01-02
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Due to the way the mains waveform is sampled in my design the timing of the "on" and "off" pulses are not symmetrical, I do indeed add the two timings together in order to get an accurate (or probably, less inaccurate) measurement of the period.
I had not been aware of the SMT peripheral, thanks for advising me of that, I'll look into it should I be needing genuine accuracy.
In the Grid Frequency Meter, I can resolve the frequency of the 50Hz (nominally) supply to within a few mHz. Once you get into that level of resolution it becomes difficult to gauge the accuracy to any useful degree. Compared to the original Elektor design that is crystal controlled, I can calibrate mine to within a few mHz, after calibration it remains fairly consistent. I can live with that.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You could use the Zero Cross Module in combination with the SMT Module (in Mode2) to make a nice Grid Freq Meter. Use a good external crystal for the PIC OSC and it should be quite accurate.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi GCB team,
Happy New Year!!!
I am wanting to read/measure the duty cycle on an input pin using the PIC16F1823. Is this possible? I cannot seem to find anything in the documentation. Do you have an example codes to help me set this up ?
Thanks,
Phil
I've done this for a particle detector (PM2.5). The basic procedure is to use the gate on TIMER1 connected to the signal so that TIMER1 will only run if the gate is high (or low - can be selected).
One of the other timers needs to run continuously (with suitable pre and post scale selected).
When this second timer overflows the values in TMR1H:TMR1L give the amount of time the gate has been on (or off).
This is something of a niche activity so will probably have to be done by writing to the registers rather than using GCB commands. Below is my example it is on a 16F18326 so I have more 16-bit timers available. In the example time1 is running continuously (from an external clock crystal) and timer3 and timer5 are gated (from the particle detector).
The processor is normally in SLEEP and wakes up on a TIMER1 overflow (every 8 seconds) when timer3 and timer5 can be read.
Note the processor you have has only one 16-bit counter (timer1) and the register names may be different, but I hope it gives you some ideas.
Also,. see the Help: http://gcbasic.sourceforge.net/help/_measuring_a_pulse_width.html
I wrote a program that measures the frequency of the mains electricity supply which uses a modified version of that method outlined in the article @evanvennn linked to above. This uses the pulse width measured value which is then converted to a frequency. I took my inspiration from a post by @williamroth.
His post is here:
https://sourceforge.net/p/gcbasic/discussion/629990/thread/a02288e0/#2238
The program was written for the 16F1825 which I think is a close relation to the 1823?
You may possibly be able to reuse some of the code for your application?
My post here: https://sourceforge.net/p/gcbasic/discussion/projects%26guides/thread/47ef37a6cc/
The ideal code/method /PIC depends upon how quickly and how often you need to measure the duty cycle.
Many PIC chips include a Signal Measurement Timer (SMT) peripheral that simplifies measuring frequency, pulse width, duty cycle, et al. It is very fast and very accurate.
If speed a accuracy are important would use a PIC 16F18426 with SMT.
Measuring a pulse width alone will not provide enough information to allow calculation of duty cycle unless the frequency is fixed. If the frequency varies then both the off time and on time will need to be measured. In some circles these are referred to as mark and space. The mark time and space time are summed to give Cycle time/Period then the period is divided by the mark time to give duty cycle in percentage.
duty cycle % = 100 / ( (Mark + Space) / Mark )
The SMT Timer peripheral does this automagically
The same general method applies to any chip.
1. Measure the on time
2. Measure the off time
3. Do the math
Skip # 2 if the frequency/period is fixed
Last edit: William Roth 2021-01-02
Due to the way the mains waveform is sampled in my design the timing of the "on" and "off" pulses are not symmetrical, I do indeed add the two timings together in order to get an accurate (or probably, less inaccurate) measurement of the period.
I had not been aware of the SMT peripheral, thanks for advising me of that, I'll look into it should I be needing genuine accuracy.
In the Grid Frequency Meter, I can resolve the frequency of the 50Hz (nominally) supply to within a few mHz. Once you get into that level of resolution it becomes difficult to gauge the accuracy to any useful degree. Compared to the original Elektor design that is crystal controlled, I can calibrate mine to within a few mHz, after calibration it remains fairly consistent. I can live with that.
There is an entry in the GCB help for the SMT timer here:
http://gcbasic.sourceforge.net/help/_smt_timers.html
You could use the Zero Cross Module in combination with the SMT Module (in Mode2) to make a nice Grid Freq Meter. Use a good external crystal for the PIC OSC and it should be quite accurate.