I have a device, and it has digital i/o, analog i/o. I send to device below commands for communication.The device has gpio module. My device documantation is here
Write to digital input : gpio set/clear x
Read from digital output : gpio read x
Read from digital output : adc read x
(x : pin number)
How can I create sine/square wave and calculate amplitude? To create square wave :
- open device
- sleep
- write to device low mode(t0)
- sleep
- write to device high mode
- sleep
- write to device low mode(t1)
period = (t1 - t0)
Is this a square wave?
it seems your example is indeed square wave
if
write to device low mode(t0)
sets the output pin to low andwrite to device low mode(t1)
to high or reverse then period is the sum of the sleeps + some time for setting up GPIO state. Don't know why you have times inside the GPIO set lines and not in sleeps ... (may be something platform dependent?)To go to sin wave
use DAC or PWM + RC filter with some precomputed amplitude table in which the index is increasing periodically.
encoded:
128
is zero,255
is+1
and0
is-1
; now just add some index:and once in a while (on some timer perhaps) increment it and set the output to new value:
that and
31
just cause tho cycle the index from start again if end reached (sintab must be of size power of 2). The period is timer frequency/sintab size[notes]
You can modify this to your purpose for example make
sintab[][]
a 2D array where first index means amplitude and second is theix
as now. On older platforms (MCU's) you can encode the PWM sequence directly tosintab
end so on ...You can pre-compute the
sintab
values like this:or if your platform supports fast enough
sin
you can use above line directly without the actual array ...[edit1]
for
sinwave
you can use just0/1
states. If you need analog output and:You have DAC (digital to analog converter)
then send the actual amplitude to it like
dac write sintab[ix];
that will create the analog voltage on the output pin for you.You do not have any spare DAC's use PWM Pulse Width Modulation instead
it is an old school trick to avoid the need of DAC and still have analog output from digital pin. It works like this:
The output value is the cumulative energy/voltage per time chunk so you generate square-wave signal
The more time the output is H the bigger output value. This is still digital output but if you connect on it any nonlinear device like capacitor or coil then the
energy inertia
will cause to drop the H voltage to some level dependent on the square-wave ratio. Most common is the RC filter (R is serial and C is parallel to ground). If you want to drive some coil (motor) then you do not need the filter. This kind of use usually generate high pitch sound (the PWM frequency) often heard near machinery ...The PWM frequency has to be high enough (many times higher then the sinwave frequency)
Some code for PWM with amplitude and frequency setting: