Low frequency output


Closed Thread
Results 1 to 6 of 6
  1. #1
    barkerben's Avatar
    barkerben Guest

    Default Low frequency output

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by mister_e; - 15th November 2004 at 20:45.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default

    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

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    barkerben's Avatar
    barkerben Guest


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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.

    Code:
    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
    Last edited by mister_e; - 16th November 2004 at 15:39.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  2. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  3. Serious Serial Situation Setbacks...
    By Dansdog in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th February 2007, 03:46
  4. inaccurate frequency using TMR1 PI18F452
    By nkarpovich in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 14th October 2006, 16:22
  5. 4-line LCD Help - using PortA instead of B
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 31st March 2005, 03:14

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts