Posted: 08:54am 01 Aug 2011 |
Copy link to clipboard |
Print this post |
|
Hi.
Here is some code that will generate a square wave at approximately 100Hz.
The mark/space ratio can be varied from 10% to 90% in 10% increments.
The code places the output on 2 pins, with one being the inverse of the other (with a very slight delay in the 2 pin's values changing as I can not set it with one statement). The code is written such that both pins will never be on at the same time.
The 100Hz frequency is about as high as you would want to go if using pure MMBasic code. Unfortunately, none of the I/O pins have a connection to the peripheral PWM module. However, if you can solder to one of the unused MCU pins then this capability could be provided.
Regardless, I will soon release a modified version of the firmware that will allow PWM output on any of the I/O pins (or maybe only pins 1 - 10). This will be implemented using interrupts so will not be as efficient as using the PWM module. Also, the frequency range will be less - but what values I will allow I have not yet decided.
Anyway, here is the code that I hope Sparkey is after:
100 ' initialise state and I/O pins
110 markpin = 1 'set the "normal" output to logical pin 1
120 spacepin = 2 'set the inverse output to logical pin 2
130 pin(markpin) = 1 'set initial state to on
140 pin(spacepin) = 0 'set initial state to off
150 setpin markpin,8 'set pin as digital output
160 setpin spacepin,8 'set pin as digital output
170 msr = 5 'set initial mark/space ratio at 50%
180 msrtimer = 0
190 settick 1,1000 ' set tick interrupt for every millisecond
200 cls
210 locate 0,0
220 ? "Mark/Space ratio is "; msr * 10;"%"
230 ? "Enter Mark/Space ratio (1 - 9 for 10% - 90%) or 'Q' to quit"
240 inpkey$ = ucase$(inkey$)
300 do while inpkey$ <> "Q"
310 key = asc(inpkey$) - asc("0")
320 if key < 1 or key > 9 then goto 380
330 msr = key
340 locate 0,0
350 ? "Mark/Space ratio is "; msr * 10;"%"
370 ? "Enter Mark/Space ratio (1 - 9 for 10% - 90%) or 'Q' to quit"
380 inpkey$ = ucase$(inkey$)
390 loop
400 end
999 ' settick interrupt routine
1000 msrtimer=msrtimer+1
1010 if msrtimer=msr then
1020 pin(markpin)=0:pin(spacepin)=1
1040 elseif msrtimer>=10 then
1050 msrtimer=0:pin(spacepin)=0:pin(markpin)=1
1080 endif
1090 ireturn
Regards
Gerard (vk3cg/vk3grs)Edited by seco61 2011-08-02
Regards
Gerard (vk3cg/vk3grs) |