Hi everyone ,I’m new in arduino and I’ve been looking for a way to generate SPWM using both carrier and sine wave. Could you tell me how to generate a high frecuency triangle signal in arduino mega?.
Below you can find my arduino code used to generate triangle, sine and PWM signal.
int hsc=0;
int PWM=0;
int thetax=0;
int mSignalx=0;
int pSignalx=0;
int pState=0;
float theta=0.0;
float ang=0.0;
float mSignal=0.0;
float pSignal=0.0;
float x=64;
void setup() {
}
void loop(){
while(1)
{
// funcion diente de sierra
if (hsc >= 256) {
hsc=0;
ang++;
if (ang>=32) {ang=0;}
}
else hsc++;
if (pState==0) {
pSignal++;
if (pSignal>=x) { pState=1; } }
else{
pSignal--;
if (pSignal<=-x) { pState=0; } }
// Sine Pulse Width Modulation
theta = (2*3.1416*ang)/32.0; // Angulo para la funcion sinusoidal
mSignal=x*sin(theta);
if (mSignal<pSignal) { PWM=0; }
else { PWM=1; }
// convert variables
thetax=round(theta);
mSignalx=round(mSignal);
pSignalx=round(pSignal);
}
}