In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Indisputable Attitude wrote: 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? [code] 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 [/code] 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) ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=435308 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2010 (http://www.dotNetBB.com)