PDA

View Full Version : Low frequency output



barkerben
- 15th November 2004, 21:09
Hi,

I've used PICs before, but not for a while, and never using Basic. I'm trying to output a low frequency from a PIC to control a stepper motor - I need to be able to output a squre wave down to 1Hz and up to about 100Hz, with the frequency deendant on input to some of the other pins.

With the built in PWM I can only go down to about 250Hz with a 4Mhz XT - is there any way of getting reliable, low frequency outputs?

Cheers,

Ben

mister_e
- 15th November 2004, 21:32
depending of your skill level in programming, you can build your own frequency generator who runs in pseudo background with your own HIGH/LOW level duration or you may like to use internal PWM with an external clock divider wich is more simple but need more external components.

But you can do everything in code. you must calculate rough how many time every instruction take, insert variable delay and calculate the input variation.

you may use interrupt or not. Is your input will be in voltage or in frequency?

let us know the exact thing you know and you need. tell us wich PIC you want to use etc, etc

regards

barkerben
- 15th November 2004, 22:49
Hi,

I'm using the PIC 16F628. The idea is that the PIC outputs a pulse chain at a given frequency to trigger my stepper driver IC one one pin of one of the PIC ports. The other pins on that port specify direction, step mode etc. If one of the PIC's pins - my 'address' pin - on the other port is sent high by the paralell port on my computer, then the PIC interrupts and reads new instructions on motor direction, frequency etc from some other pins which are atached to the paralell port. It then alters the frequency output and other pins accordingly. If the 'address' pin is not high, then the other pins are ignored and the PIC goes on outputting its pulses. This interrupt on one pin gives the PIC an 'address' so I can run two together, but issue commands to one or the other - each will only call its interrupt routine when a specific pin is sent high.

Both PICs have 4Mhz external XTs. The actual output frequency is not actually that important, within reason. What is more importnant is the relative frequencies - I need to know that I am stepping one motor at x times the other, but the exact speed is not importnant. Thiis is because the motors are used to control a mount which must track in a given direction. Roughly knowing the speed of each motor is nice, but more important is relative speed of the motors, and thus the tracking direction.

I was looking through the picbasic manual, and came across pause comands thate were used in one example. Presumably I could use this function? Although there would be an error due to the time taken to actually process the looping commands etc, this error should be constant whatever my desired frequency, giving me a constant offsett...?


I chose a 4Mhz XT because it was as low as I could go, but
still only lets me get hardware PWM down to about 250Hz it seems ...


Cheers (and sorry for the long post)


Ben

mister_e
- 16th November 2004, 06:39
I was looking through the picbasic manual, and came across pause comands thate were used in one example. Presumably I could use this function? Although there would be an error due to the time taken to actually process the looping commands etc, this error should be constant whatever my desired frequency, giving me a constant offsett...?


well, what i suggest is to have an short fixed delay loop and to call here as long as you need it to acheive your pseudo PWM tasks and avoid latency to get changes from InputPins.

how it sounds to you?

regards

barkerben
- 16th November 2004, 09:36
That was what I was thinking. I could have an infinite loop containing a few pin high/pin low commands, and a pause command, where the pause variable is set in the interrupt routine.

As I understand it, although this would not give me an exact frequency due to the time taken to execute the commands, the offset from the desired frequency should be constant whatever the value of pause needed? I guess it depends how PICBasic implements the code in assembler ...?

Cheers,


Ben

mister_e
- 16th November 2004, 16:25
That was what I was thinking. I could have an infinite loop containing a few pin high/pin low commands, and a pause command, where the pause variable is set in the interrupt routine.


yep it could be this. You can use interrupt for port pin level change for that... surely. A little problem may happen... latency to answer to interrupt. In this case, you'll fix it really simple using a loop delay let' say with PAUSEUS 10... about 10Usec.

Here's a simple snippet to explain.




main:
......
Frequency=250 ;have 250Hz as frequency
Period_Delay_ms=1000 / frequency ;calculate period delay
Half_Period_ms=Period_Delay_ms/2 ;calculate half period delay
gosub PseudoPWM
.....

PseudoPWM:
For a=0 to Half_Period_ms
For b=0 to 100
HIGH PORTB.0
PAUSEUS 10
Next
next

For a=0 to Half_Period_ms
For b=0 to 100
LOW PORTB.0
PAUSEUS 10
Next
next
return


in the above, if any interrupt happen in the PseudoPWM, it will be answer in *about* 10 Usec

for frequency acurracy you'll have to measure the frequency out regarding the frequency you're suppose to have with your delay and play with PAUSE delay, NAP or @NOP to fine tune the error percentage. Once it's done everything will work just fine.

regards