Using a Variable for an Interrupt Rate?
I have the following code that is supposed to output a variable frequency square wave from 1 to 1000 Hz by 1 Hz as accurate as possible.· I am using the SX28 50Mhz just for this purpose and I am sending it a WORD that represents the frequency.· I want to use the Interrupt function if possible but I found that the it wont accept a variable for the "rate" parameter.
The code looks for RA.1 pin to be high meaing a new frequency is available from the host. It should read in the new frequency word and set the interrupt rate to it.
How can I make this work?
Thanks
Steve
The code looks for RA.1 pin to be high meaing a new frequency is available from the host. It should read in the new frequency word and set the interrupt rate to it.
How can I make this work?
Thanks
Steve
DEVICE SX28, OSChs1, TURBO, STACKX, OPTIONX FREQ 50_000_000 OutPin PIN RA.3 OUTPUT 'Square Wave output Pin Sio VAR RA.0 ' pull-up via 4.7K SioReady VAR RA.1 ' Pulled High if data ready from extrnal source sData VAR Word ' Serial Input must be 1-1000 Baud CON "T2400" INTERRUPT sData ' 1000 = 1000 uSec 'Variable not working with Interrupt 'Typical Ranges needed 'Frequency = 1Hz Period = 1 Seconds 'Frequency = 10Hz Period = 0.1 Seconds 'Frequency = 100Hz Period = 0.01 Seconds 'Frequency = 500Hz Period = 0.02 Seconds 'Frequency = 1000Hz Period = 0.001 Seconds outpin = NOT Outpin 'Toggle the output 50% Duty RETURNINT Program Start Start: If SioReady = 1 then SERIN Sio, Baud, sData_LSB, 1000, No_Char ' wait for byte SERIN Sio, Baud, sData_MSB, 1000, No_Char ' wait for byte endif No_Char: If sData=0 then Start 'If no data received then keep looking. sData = 1000 ' Use for testing purposes only while not connect to host.
Comments
The program that's attached is from my July Nuts & Volts article; it shows how to do PWM control (for motors) using a serial command. What's important is that I'm using the ISR to receive the serial command and do the PWM. It will take some work on your part but you could modify this program to get to get to your goal.
The reason I would use and first check a toggle flag in the ISR is to provide consistent timing for the actual output switching without needing to worry about maintaining consistent timing in my logic subroutine. If you perform your logic and then toggle your output during the same interrupt, your logic timing must be consistent. If you set a flag and toggle your output first thing on the next interrupt, you get consistent timing without a lot of programming hassle.
That is how I would approach it.
- Sparks
I finally got my head around how the interrupts work. I used some examples to get to this point:
But I am having problems with truncation introducing error.
I read http://forums.parallax.com/attachment.php?attachmentid=47571
and saw what he did to get the accuracy up but he was limited to 382 hz on the low end.
I am getting the recirpical of the frequency and using that for the counter word. But the problem is like this.
If I want 962 Hz then I take 1/(2*962*0.00001) toget the counter of 51.98 but it is truncated to 51 which is actually 980.4 hz. This is a -1.88% error.
So I was working on how to get the remainder of 0.98 into a do nothing loop to make the frequency closer.
I was going to use shiftin to get the new frequency because of the serin interrupt issues.
I orderd the SX48 proto board so I can play with the timer1 pwm function. But there again I will still have the same error.
Steve
Correction: After looking at the code after a break I found that the PWM VP was adding an extra cycle to each phase -- that's been fixed.
Post Edited (JonnyMac) : 8/8/2007 10:10:40 PM GMT
I tried sending it a Frequency value of 1 and I get back ~ 5Hz.
I have not tried the upper limit yet.
Can you confirm that?
Note that I changed the ISR rate to allow for faster, more accurate baud rates -- this changes the phase timing resolution from 12.5 uS to 9.766 uS; this makes the pwm frequency range from 0.78 Hz to 51.2K Hz. You'll get the best control by sending the phase values directly as is allowed in the protocol.
Post Edited (JonnyMac) : 8/10/2007 3:53:58 PM GMT