Shop OBEX P1 Docs P2 Docs Learn Events
Getting timing right on the SX — Parallax Forums

Getting timing right on the SX

edited 2010-03-13 05:56 in General Discussion
Let's say that I want the ability to do any kind of square wave, either ½, ¾, ¼ duty cycle, of any frequency I want. It could be for doing a RS232 kind of protocol, or PWM for motor control or whatever.

Could someone on the forum please explain this a bit? What are the equations I should use?

The way I understand it right now... You take the Oscillator frequency, say 4_000_000 oscillator cycles/second. Then, since the SX runs at 4 oscillator cycles/ machine cycles, you end up with 1_000_000 machine_cycles/second.

Then, let's say you want a square wave beep at 4_000 audio_cycles/second, so you then must divide the 1_000_000 machine_cycles/second by the 4_000 audio_cycles/second to get 250 machine_cycles/audio_cycle.

Assuming a duty cycle of 20%, I must then multiply the 250 by .2 to get 50 for my on_time, and then to get my off time, multiply 250 by .8, and get 200.

So, would the pseudo-code look something like this?
Start

Load 50 into register A
Load 200 into register B

turn on pin

On_Loop
    decsz register A
    goto On_Loop

turn off pin

Off_Loop
     decsz register B
     goto Off_Loop

     goto Start





Please tell me if I'm on the right track with this. This is a major part of understanding the SX, is it not?

Formula in text:
A=(((Freq Osc)/(Machine_Cycles/Freq_Osc))/(Audio_freq))*(duty_cycle)

Comments

  • PJMontyPJMonty Posts: 983
    edited 2010-03-12 23:40
    I know that in a previous post you mentioned you were working on a Space Invaders style game. Is this question related to that project? I ask because if it is, you don't probably don't need to be able to generate any duty cycle square wave at any frequency. You probably need some specific frequencies with specific duty cycles. I mention this because people often try and engineer a solution to cover a huge problem, when in fact they are trying to solve a much smaller problem and should concentrate on that.

    Having said that, let's look at your question. The most important piece of information you left out is what are the highest and lowest frequencies you want (need) to generate? Without knowing that super critical bit of information, it's impossible to give you the best answer. It's like saying you want to make a car. Fine, but without knowing the speed range, you could be building a Formula 1 race car or a Honda Civic.

    So, what is your frequency range?

    Thanks,
    PeterM
  • edited 2010-03-13 05:56
    Thanks for your response. There are a lot of great posters here, and I appreciate any and all help.

    This question came up, because I'm trying to explain the SX to my 13 y.o. son. The freqs for the game would be in the audio range, for the beeps and bloops, so I'd say about 100 Hz up to 3000 Hz. Your thought on "engineering to meet your specifications" is right on. On a separate project I'm working on, it's easy to keep coming up with new features. The hard part is implementing them. Like Thomas Edison said, invention is 1% inspiration, and 99% perspiration. (That's a separate project btw, a remote controlled Lego car using SX as the steering module). This timing problem applies to it too, because I'll use PWM for driving the geared steering motor, which is mechanically coupled to an encoder pulled out of a ball style mouse. Anyway, 100 Hz up to 3000 Hz with little concern for accuracy.
Sign In or Register to comment.