Hi, a simple loop should generate 11 Pulses of 22µs with pause of 4µs.
But even I omitt the wait 4µs I get 15µs low pulses.
Why? I stripped the Code to the minimum
I used in pricip this from Help: http://gcbasic.sourceforge.net/help/_generate_accurate_pulses.html
The Hi Pulse is 22µs ,not 23
the LOW is 15µs
The delay is a result of overhead related to the instructions required by the loop and jumping to a sub and returning, These all take time. With your chip 8MHz, 1 us resolution is not likely, Increase the chip MHz to 16 or 32 and it will improve.
That being said there are much better ways to generate accurate pulses.
Let me make you something that is a bit more accurate. About 2 hrs
Last edit: William Roth 2017-11-26
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
William, thanx in advance. The 8 MHz is set, if I must go higher then I have to use external OSC. The Target PIC is an PIC16F818. Unfotunatley the PCB is already produced. :-(
This 8 MHz 25k22 is just a test if it is possible to manage the 36.7KHz Pulse Bursts
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
For your specific application the included code should work better as there is much less instruction time on the "off time" side.
You can tweak the period by adjusting the value of "PR2" the timer2 Match register. The value of 158 is a corrected value that considers the instructions required to handle the interrupt routine. I have no no magic formula. I just got it close and then tweaked it until it was correct. The chip needs to to operate at 32Mhz to keep the instruction cycle time as low as possible.
I get a period of 27.2us and a frequency of 36.77KHz. Pretty close. Please try this and see what your scope tells you. Let me know.
#chip18f25K22,32#optionExplicit#defineLEDD1PORTB.1DirLEDD1Out';----SetupPulseparameters#definePulsePinPORTB.5DirPulsePinOutSetPulsePinoffDimCountasbytePR2=158'// This is the timer2 Period or Match register'// When TMR2 increments and "matches" PR2'// the TMR2 interrupt flag is set.'// This register sets the pulse on time.InitTimer2(2,PS2_4,0)Starttimer2OninterruptTimer2MatchCallPulseCounterCount=0Do'WaitingforInterruptLoopSubPulseCounterSetPulsePinOFFCount++Ifcount>10thenWait200msCount=0EndifWait3us'// actually 4us due to some overheadSetPulsePinONTMR2=0'// Clear timer2 to zeroEndSub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I just read your message. Let me see what I can do with 8 Mhz on a 16F818 or something close.
Sometimes simple is better. This works acceptably well on 16F1829 and should be the same on 16F818. Period is 27.2 us. Frequency is ~36.76KHz
NOP adds a short delay to fine tune delays
Use Osctune register to nail it dow to exactly 36.7Khz
#chip16f1829,8
#optionExplicit
#defineLEDD1PORTB.1DirLEDD1Out'; ---- Setup Pulse parameters#define PulsePin PORTB.5Dir PulsePin OutSet PulsePin offDim Count as byte'// Trim with OSCTUNE to get a period of exactly 27.2 us OSCTUNE=b'00111001'DoRepeat11SetportB.5OnWait22usNOPsetportB.5offNOP:NOPEndRepeatWait200msLoop
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello William, thank you for taking the time to analyze my problem and to finish it right on time.
I did the finetuning with assembler 30 years ago. (that was for a heating controller with 68HC11, at that time it was about the duty cycle of a SMT-160 temperature sensor).
Wonderful how easy it is to mix in Great Cow BASIC!
What I did not know was the Basic Loop REPEAT. I read the same in the Help System .. wonderful.
Thanks again for your support!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This evening I do not have enough time to test, but a short Test of Repeat -- instead of for ..next
Result: from 15µs to 13.8µs.
Not very much, but for the last percent it is worth to use it as a standard way, at least if the counter value isn't needed anyway.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, a simple loop should generate 11 Pulses of 22µs with pause of 4µs.
But even I omitt the wait 4µs I get 15µs low pulses.
Why? I stripped the Code to the minimum
I used in pricip this from Help:
http://gcbasic.sourceforge.net/help/_generate_accurate_pulses.html
The Hi Pulse is 22µs ,not 23
the LOW is 15µs
I must misst something, but can't think of it.
Last edit: bed 2017-11-26
The delay is a result of overhead related to the instructions required by the loop and jumping to a sub and returning, These all take time. With your chip 8MHz, 1 us resolution is not likely, Increase the chip MHz to 16 or 32 and it will improve.
That being said there are much better ways to generate accurate pulses.
Let me make you something that is a bit more accurate. About 2 hrs
Last edit: William Roth 2017-11-26
William, thanx in advance. The 8 MHz is set, if I must go higher then I have to use external OSC. The Target PIC is an PIC16F818. Unfotunatley the PCB is already produced. :-(
This 8 MHz 25k22 is just a test if it is possible to manage the 36.7KHz Pulse Bursts
@Bed
For your specific application the included code should work better as there is much less instruction time on the "off time" side.
You can tweak the period by adjusting the value of "PR2" the timer2 Match register. The value of 158 is a corrected value that considers the instructions required to handle the interrupt routine. I have no no magic formula. I just got it close and then tweaked it until it was correct. The chip needs to to operate at 32Mhz to keep the instruction cycle time as low as possible.
I get a period of 27.2us and a frequency of 36.77KHz. Pretty close. Please try this and see what your scope tells you. Let me know.
I just read your message. Let me see what I can do with 8 Mhz on a 16F818 or something close.
Sometimes simple is better. This works acceptably well on 16F1829 and should be the same on 16F818. Period is 27.2 us. Frequency is ~36.76KHz
NOP adds a short delay to fine tune delays
Use Osctune register to nail it dow to exactly 36.7Khz
Hello William, thank you for taking the time to analyze my problem and to finish it right on time.
I did the finetuning with assembler 30 years ago. (that was for a heating controller with 68HC11, at that time it was about the duty cycle of a SMT-160 temperature sensor).
Wonderful how easy it is to mix in Great Cow BASIC!
What I did not know was the Basic Loop REPEAT. I read the same in the Help System .. wonderful.
Thanks again for your support!
This evening I do not have enough time to test, but a short Test of Repeat -- instead of for ..next
Result: from 15µs to 13.8µs.
Not very much, but for the last percent it is worth to use it as a standard way, at least if the counter value isn't needed anyway.
You can also try my favorite:
or the old fashioned
Both may shave time, but I am not sure how much if any.
Cheers
Chris