SOLVED How to generate SINE PWM SOLVED

@Polymporph.

I am wondering if eddy current losses will be enough of an issue to go to the trouble of filtering.

I dont care about the hardware at the moment...

The range of my sine wave should be between 30 KHz and 80 Khz

This would most likely be the switching frequency of the PWM. There would be a hidden, "modulated" sine wave that would appear after appropriate filtering. The sine frequency would be much lower ... i.e. 50 to 60 Hz and would depend on the update rate of the PWM duty cycle.

Its is possible with the due but I just don't know the technique I can make it with

You're correct that its possible with the Due, but at this stage you're only at the tip of the iceberg.

Note that the Due has 2 types of PWM outputs. The type you are using has a single pin for each PWM and can be controlled using Arduino commands.

For your project, I'm quite sure you'll need to use the complementary PWM outputs, which are driven in pairs (PWMH0,PWML0), (PWMH1,PWML1), (PWMH2,PWML2), etc. These cannot be controlled using the existing Arduino commands. A library would need to be developed that involves controlling the required SAM3X registers and providing new functions. Also, a main consideration would be the shoot-through or dead-time specification for the H-bridge module or circuit. Another consideration would be understanding the power losses involved and filter requirements.

I noticed now that my programm is wrong. Without the sine look up table every thing works and i can generate 3 pwms with variable duty cycle and frequency... but when I add the sine look up table it dosent work anymore. I believe its cause my for loops just work all the time...I thinkg that my sine wave and my pwm ar not cuppeld together some how can anyonhelp me ?

#include "pwm01.h"



int sine[] = {2048,2305,2557,2802,3034,3251,3449,3625,3777,3901,3995,4059,4092,4092,4060,3996,3902,3778,3628,3452,3254,3037,2805,2560,2308,2051,1795,1542,1297,1065,847,649,473,321,197,102,37,4,4,35,99,193,316,466,642,839,1056,1288,1533,1785,2041};

static int k = sizeof(sine)/sizeof(int)/3; //length of the sine table =51 
static int j= k*2; //(2/3)*50 length of the sine table
//static int test= sizeof(sine)/sizeof(int) ;



void setup() 
{
   // uint32_t  pwm_duty =0.5* 65535;//2^16=65536
    uint32_t  pwm_freq2 = 30000;

    // Set PWM Resolution
    pwm_set_resolution(16);  

    // Setup PWM Once (Up to two unique frequencies allowed
    //-----------------------------------------------------    

    pwm_setup( 7, pwm_freq2, 2);  // Pin 7 freq set to "pwm_freq2" on clock B
    pwm_setup( 8, pwm_freq2, 2);  // Pin 8 freq set to "pwm_freq2" on clock B
    pwm_setup( 9, pwm_freq2, 2);  // Pin 9 freq set to "pwm_freq2" on clock B
      
    // Write PWM Duty Cycle Anytime After PWM Setup
    //-----------------------------------------------------    
   // pwm_write_duty( 7, pwm_duty );  // 75% duty cycle on Pin 7


   // delay(30000);  // 30sec Delay; PWM signal will still stream
        
    // Force PWM Stop On All Pins
    //-----------------------------    
 
 /* pwm_stop( 6 );
    pwm_stop( 7 );
    pwm_stop( 8 );
    pwm_stop( 9 );*/
}

void loop() 
{  
  while(1){
   for(int i = 0; i<50;i++){
    pwm_write_duty( 7, sine[i]*16 );  // 16 is a factor to improve the resoultion of the sine wave
    pwm_write_duty( 8, sine[(i+k)%51]*16);  // 75% duty cycle on Pin 8
    pwm_write_duty( 9, sine[(i+j)%51]*16 );  // 75% duty cycle on Pin 9
    
    delay(5); // The sine wave frequency is determinded in this way Sine index = 51*10 milli seconds delay= 510, Frequncy=1/510
   
     } 
     }
  
}

The range of my sine wave should be between 30 KHz and 80 Khz

OK lets look at that for a moment.
80KHz is a wave every 12.5uS. Then you have 50 samples in your wave so a sample is needed every 0.25uS.
If you have 12 bit samples that that is 4096 divisions of time in the PWM.
That means each division of time will be 0.25 uS / 4096 = 61.03 pS per division of time.
This translates into a PWM clock of around 16 GHz which for an 80MHz processor is a bit outside what it can be done.

Its is possible with the due but I just dont know the technique I can make it with

Do you watch the Simpsons? As Nelson Muntz would say Har Har

What ever 'it' is it is not PWM.

Why are you looping for 50 iterations when the sine table is length 51?

What ever 'it' is it is not PWM.

The OP has already seen and copied code from reply#8 here. This example is only the initialization part of some of the Complementary PWM outputs available on the Due. I'm interfacing the Due with this module. The example is part of an incomplete project that is a programmable AC power source, but the module can be used in many applications, including motor control.

Anyhow, I've successfully used Complementary PWM on the Due with PWM frequency of 15KHz and duty cycle resolution of 5600. Using a sine table of length 9000 (one quadrant), I am able to control the phase from 0 to 360 deg with 0.001 deg resolution. The dead time was matched to the module specification of 2 us.

Note: For the Due, if the Complementary PWM frequency is changed to 30 KHz, the maximum duty cycle resolution would become 2800. Pwm01.h is not used as it does not configure Complementary PWM.

30 to 80kHz sine wave? How fast is this turning? How many poles?

Did you really mean 30 to 80kHz clock for the PWM?

Anyhow, I've successfully used Complementary PWM on the Due with PWM frequency of 15KHz and duty cycle resolution of 5600.

@dlloyd
This is like DC compared to what he is after.

30 to 80kHz sine wave? How fast is this turning?

At one cycle per revolution its - 4,800,000 RPM

Did you really mean 30 to 80kHz clock for the PWM?

No he meant it for the final waveform so the PWM is much faster than that.

This is like DC compared to what he is after.

Yeah, but what about the Due's embedded 480 MHz PLL?? (kidding).

For the longest time I was trying to figure out the term "SINE PWM" ... the best I could come up with is the process of taking a sine wave, then chopping it using PWM (with constant duty cycle), then filtering it to get a smaller sine wave. But as it turns out, it's kind of like the term "Amps AC" (Amps Alternating Current).

@so3ody, It really seems that you're trying to develop a VFD (variable-frequency drive). If so, this really is one huge project. You've mentioned that you're not concerned with hardware now, but the hardware (type of motor and drive AC-AC, DC-AC, etc) will greatly affect the software required to control it. Take a look here.

@
dlloyd

I just want to get 3 PWM SINE waves with variable frequency and duty cycle... this is almot a standar part of any ac motor driver..

What frequency is the drive - not the PWM, the drive frequency. The final resulting sine wave frequency.

Hi thanks for your reply. I actually wanted a variable frequency between 30 and 80 KHz but after discussions here in this forum I read that its not possible so I still dont know

what is actually the possible frequncy rang with arduino due ? sorry im really new dealing with arduino due and with microcontroller

Thanks in advance

The Due's clock is 84Mhz.

For 30 KHz PWM clock, you will have 2800 possible values for duty cycle (30K x 2800 = 84 MHz).
For 80 KHz PWM clock, you will have 1050 possible values for duty cycle (80K x 1050 = 84 MHz).

There's more than enough resolution to generate a sine wave, but one of the problems in your code its using 16-bit duty cycle resolution which is out of range of what's possible.

For 80 KHz PWM clock, the maximum value you should use for pwm_write_duty is 1050.

@
dlloyd

Thanks for your reply. I will try to change the resoultion to get it to 32 bit.

But I still have the problem that I cant vary the pwm filtern output frequency when I add the sine wave array to it. It just doesnt work anymore

Its cause my 3 loops which are for the 3 waves..the compiler reply them all the time ..can I make it in a nother way but without delays ?

Not 32 bit (4,294,967,296) ... try 10 bit (1024). Really, 1050 should be the max value that's written to pwm_write_duty.

Go back to basics ... just generate 1 waveform ... use delayMicroseconds() for better frequency control.

Or, as a test, you could set your PWM frequency to 15000 and the resolution to 12 bit, then use the sine waveform (table) of values from here: http://arduino.cc/en/Tutorial/DueSimpleWaveformGenerator#.Uy3IjmRzboY

Hi so I added the new sine wave table from the example. But I still have the same problem as I mentioed before

when I change the PWM frequency it desent change the frequency of my filterd sine wave..they dosent affect each other

#include "pwm01.h"



int sine[] = { 0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
    0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
    0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
    0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
    0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
    0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
    0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
    0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
    0x112, 0xdf, 0xb1, 0x87, 0x64, 0x45, 0x2c, 0x19, 0xb, 0x2,
    0x0, 0x2, 0xb, 0x19, 0x2c, 0x45, 0x64, 0x87, 0xb1, 0xdf,
    0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
    0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794};

static int k = sizeof(sine)/sizeof(int)/3; //length of the sine table =51 
static int j= k*2; //for the third wave length of the sine table
//static int test= sizeof(sine)/sizeof(int) ;



void setup() 
{
   // uint32_t  pwm_duty =0.5* 65535;//2^16=65536
    uint32_t  pwm_freq2 = 30000; // Set a unique frequency to the three output signals

    // Set PWM Resolution
    pwm_set_resolution(16);  //

    // Setup PWM Once (Up to two unique frequencies allowed
    //-----------------------------------------------------    

    pwm_setup( 7, pwm_freq2, 2);  // Pin 7 freq set to "pwm_freq2" on clock B
    pwm_setup( 8, pwm_freq2, 2);  // Pin 8 freq set to "pwm_freq2" on clock B
    pwm_setup( 9, pwm_freq2, 2);  // Pin 9 freq set to "pwm_freq2" on clock B
      
    // Write PWM Duty Cycle Anytime After PWM Setup
    //-----------------------------------------------------    
   // pwm_write_duty( 7, pwm_duty );  // 75% duty cycle on Pin 7


   // delay(30000);  // 30sec Delay; PWM signal will still stream
        
    // Force PWM Stop On All Pins
    //-----------------------------    
 
 /* pwm_stop( 6 );
    pwm_stop( 7 );
    pwm_stop( 8 );
    pwm_stop( 9 );*/
}

void loop() 
{  
  while(1){
   for(int i = 0; i<50;i++){
    pwm_write_duty( 7, sine[i]*16 );  // 16 is a factor to improve the resoultion of the sine wave
    pwm_write_duty( 8, sine[(i+k)%51]*16); 
    
    delay(5); // The sine wave frequency is determinded in this way Sine index = 51*10 milli seconds delay= 510, Frequncy=1/510
   
     } 
     }
  
}

Hi so I added the new sine wave table from the example. But I still have the same problem as I mentioed before when I change the PWM frequency it desent change the frequency of my filterd sine wave..they dosent affect each other

You're right! It never will. The PWM frequency is only the "carrier" frequency for the "modulated" sine wave. One way you can change the sine wave frequency is to update the duty cycle at a faster or slower rate.

If you're controlling a motor, another way to control speed is to scale the values (x 0.01 to 1.00) for 1 to 100% coarse speed control (this changes the sine amplitude). You could also vary the supply voltage with another PWM output for fine resolution speed control (by changing power supply amplitude). Note that some VFDs have a sine output frequency range of around 0.1 to 600 Hz.

In your code below, there is some minor changes marked with ########.
Try to determine why those changes were made.

If you set delayMicroseconds to 8000, you will get about 1 Hz sine wave on all 3 outputs.
If you set delayMicroseconds to 8, you will get about 670 Hz sine wave on all 3 outputs.

You're still at the tip of this iceberg and I'm slipping off (I need to return to my iceberg) ... good luck!

#include "pwm01.h"

int sine[] = { 0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
    0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
    0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
    0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
    0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
    0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
    0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
    0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
    0x112, 0xdf, 0xb1, 0x87, 0x64, 0x45, 0x2c, 0x19, 0xb, 0x2,
    0x0, 0x2, 0xb, 0x19, 0x2c, 0x45, 0x64, 0x87, 0xb1, 0xdf,
    0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
    0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794};

static int k = sizeof(sine)/sizeof(int)/3; //length of the sine table =120 #########################
static int j= k*2; //for the third wave length of the sine table
//static int test= sizeof(sine)/sizeof(int) ;



void setup() 
{
   // uint32_t  pwm_duty =0.5* 65535;//2^16=65536
    uint32_t  pwm_freq2 = 15000; // Set a unique frequency to the three output signals #########################

    // Set PWM Resolution
    pwm_set_resolution(12);  // #########################

    // Setup PWM Once (Up to two unique frequencies allowed
    //-----------------------------------------------------    

    pwm_setup( 7, pwm_freq2, 2);  // Pin 7 freq set to "pwm_freq2" on clock B
    pwm_setup( 8, pwm_freq2, 2);  // Pin 8 freq set to "pwm_freq2" on clock B
    pwm_setup( 9, pwm_freq2, 2);  // Pin 9 freq set to "pwm_freq2" on clock B
      
    // Write PWM Duty Cycle Anytime After PWM Setup
    //-----------------------------------------------------    
   // pwm_write_duty( 7, pwm_duty );  // 75% duty cycle on Pin 7


   // delay(30000);  // 30sec Delay; PWM signal will still stream
        
    // Force PWM Stop On All Pins
    //-----------------------------    
 
 /* pwm_stop( 6 );
    pwm_stop( 7 );
    pwm_stop( 8 );
    pwm_stop( 9 );*/
}

void loop() 
{  
  while(1){
   for(int i = 0; i<120;i++){ //#########################
    pwm_write_duty( 7, sine[i] );  //#########################
    pwm_write_duty( 8, sine[(i+k)%120]); //#########################
    pwm_write_duty( 9, sine[(i+j)%120]); //#########################
    
    delayMicroseconds(8000); // The sine wave frequency is determinded in this way Sine index = 51*10 milli seconds delay= 510, Frequncy=1/510 #########################
   
     } 
     }
  
}

Thanks alot man for your help :slight_smile:

I know that im still on the beginning but am I on the right way devolping a Varible frequency drive ?

Hi there

My code genrates 3 sine waves throught varying the duty cycle of 3 pwm signals. I have a fixed pwm frequency = 15 khz
and I change the sine wave frequency in the void loop throught delay micro seconds

Can I chnage it in a better way ? Im sorry if my question sounds stupid but im new in programming uc

int sine[] = { 0x7ff, 0x86a, 0x8d5, 0x93f, 0x9a9, 0xa11, 0xa78, 0xadd, 0xb40, 0xba1,
	0xbff, 0xc5a, 0xcb2, 0xd08, 0xd59, 0xda7, 0xdf1, 0xe36, 0xe77, 0xeb4,
	0xeec, 0xf1f, 0xf4d, 0xf77, 0xf9a, 0xfb9, 0xfd2, 0xfe5, 0xff3, 0xffc,
	0xfff, 0xffc, 0xff3, 0xfe5, 0xfd2, 0xfb9, 0xf9a, 0xf77, 0xf4d, 0xf1f,
	0xeec, 0xeb4, 0xe77, 0xe36, 0xdf1, 0xda7, 0xd59, 0xd08, 0xcb2, 0xc5a,
	0xbff, 0xba1, 0xb40, 0xadd, 0xa78, 0xa11, 0x9a9, 0x93f, 0x8d5, 0x86a,
	0x7ff, 0x794, 0x729, 0x6bf, 0x655, 0x5ed, 0x586, 0x521, 0x4be, 0x45d,
	0x3ff, 0x3a4, 0x34c, 0x2f6, 0x2a5, 0x257, 0x20d, 0x1c8, 0x187, 0x14a,
	0x112, 0xdf, 0xb1, 0x87, 0x64, 0x45, 0x2c, 0x19, 0xb, 0x2,
	0x0, 0x2, 0xb, 0x19, 0x2c, 0x45, 0x64, 0x87, 0xb1, 0xdf,
	0x112, 0x14a, 0x187, 0x1c8, 0x20d, 0x257, 0x2a5, 0x2f6, 0x34c, 0x3a4,
0x3ff, 0x45d, 0x4be, 0x521, 0x586, 0x5ed, 0x655, 0x6bf, 0x729, 0x794};



#include "pwm01.h"
#include <math.h>
#include <Arduino.h>
#include <stdio.h>
#include "CalculateDelayValueForSinusFrequency.h"



		// Calculate the delay value for the sine wave frequency
		
 long double CalculateDelayValueForSinusFrequency ( int p_sinewave_frequency_Hz) 
 {
	long double DelayForSinFreq_us;
	DelayForSinFreq_us=1000000/ (p_sinewave_frequency_Hz*120); // f=1/T
	return DelayForSinFreq_us;

}


/***		Programm Variablen		***/
int PhaseU= 7; 
int PhaseV= 8;
int PhaseW= 9;
int SineFrequency =50 ; // desierd sine wave frequency in Hz
int p_SinusAmplitudenTeilerFaktor = 1; //  default = 1 means 3.3volt =1 = 3.3 volt for the sine wave signal

static int k = sizeof(sine)/sizeof(int)/3; //length of the sine table =120
static int j= k*2; //for the third wave length of the sine table






void setup()
{.
	// uint32_t  pwm_duty =0.5* 65535;//2^16=65536
	uint32_t  pwm_freq2 = 15000; // Set a unique frequency to the three output signals #########################

	// Set PWM Resolution
	pwm_set_resolution(12);  // #########################

	// Setup PWM Once (Up to two unique frequencies allowed
	//-----------------------------------------------------

	pwm_setup( PhaseU, pwm_freq2, 2);  // Pin 7 freq set to "pwm_freq2" on clock B
	pwm_setup( PhaseV, pwm_freq2, 2);  // Pin 8 freq set to "pwm_freq2" on clock B
	pwm_setup( PhaseW, pwm_freq2, 2);  // Pin 9 freq set to "pwm_freq2" on clock B
	
	// Write PWM Duty Cycle Anytime After PWM Setup
	//-----------------------------------------------------
	// pwm_write_duty( 7, pwm_duty );  //


	// delay(30000);  // 30sec Delay; PWM signal will still stream
	
	// Force PWM Stop On All Pins
	//-----------------------------
	
	// pwm_stop( 7 );
	// pwm_stop( 8 );
	//pwm_stop( 9 );
	
	
	}
	


	void loop()
	{
		
		while(1){
			
			for(int i = 0; i<120;i++){ 
				pwm_write_duty( 7, sine[i]/p_SinusAmplitudenTeilerFaktor);  
				pwm_write_duty( 8, sine[(i+k)%120/p_SinusAmplitudenTeilerFaktor]); 
				pwm_write_duty( 9, sine[(i+j)%120/p_SinusAmplitudenTeilerFaktor]); 
				
				
				
		
		
				
			
	    
				delayMicroseconds(CalculateDelayValueForSinusFrequency(SineFrequency));
				
			}
		}
		
	}

This is my header file to calculate the delay value for the sine wave frequency

/*
 
#ifndef CalculateDelayValueForSinusFrequency_H
#define CalculateDelayValueForSinusFrequency_H

#include "Arduino.h"

long double CalculateDelayValueForSinusFrequency ( int sinewave_frequency);
	
	#endif

the pwm01.h header

/*  

Library:     pwm01.h (version 01)
Date:        2/11/2013
Written By:  randomvibe  

Purpose:     
   Setup one or two unique PWM frequenices directly in sketch program,
   set PWM duty cycle, and stop PWM function.

User Functions:     
   pwm_set_resolution(int res) ~  setup PWM resolution; up to 16 bit
   pwm_setup( uint32_t pwm_pin, uint32_t pwm_freq, int iclock) ~ Setup PWM on clock-A (iclock=1) or clock-B (iclock-2)
   pwm_write_duty( uint32_t pwm_pin, uint32_t pwm_duty) ~ Write PWM duty cycle
   pwm_stop( uint32_t pwm_pin) ~ Force PWM stop

Notes:
   - Applies to Arduino-Due board, PWM pins 6, 7, 8 & 9.
   - Libary Does not operate on the TIO pins.
   - Unique frequencies set via PWM Clock-A ("CLKA") and Clock-B ("CLKB")
     Therefore, up to two unique frequencies allowed.
   - Set max duty cycle counts (pwm_max_duty_Ncount) equal to 255
     per Arduino approach.  This value is best SUITED for low frequency
     applications (2hz to 40,000hz) such as PWM motor drivers, 
     38khz infrared transmitters, etc.
   - Future library versions will address high frequency applications.
   - Arduino's "wiring_analog.c" function was very helpful in this effort.
      
*/

#include "Arduino.h"


static int       pwm_resolution_nbit = 8;
static uint32_t  pwm_clockA_freq = 0;
static uint32_t  pwm_clockB_freq = 0;
static uint32_t  pwm_max_duty_Ncount = 255;


void pwm_set_resolution(int res)
{
	pwm_resolution_nbit = res;
}


void pwm_set_clockA_freq(uint32_t  val)
{
	pwm_clockA_freq = val;
}


void pwm_set_clockB_freq(uint32_t  val)
{
	pwm_clockB_freq = val;
}


static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to) 
{
    if (from == to)
        return value;
    if (from > to)
        return value >> (from-to);
    else
        return value << (to-from);
}



// MAIN PWM INITIALIZATION
//--------------------------------
void  pwm_setup( uint32_t  pwm_pin,  uint32_t  pwm_freq,  int iclock  ) 
{
    uint32_t  pwm_duty = 0;    
    uint32_t  chan = g_APinDescription[pwm_pin].ulPWMChannel; 
      
    // SET CLOCK FREQUENCY
    //------------------------
    if (iclock==1)  pwm_set_clockA_freq( pwm_max_duty_Ncount*pwm_freq ); 
    if (iclock==2)  pwm_set_clockB_freq( pwm_max_duty_Ncount*pwm_freq ); 


    if (pwm_pin>=6 && pwm_pin<=9)
    {    
        // SET PWM RESOLUTION
        //------------------------
        pwm_duty = mapResolution( pwm_duty, pwm_resolution_nbit, PWM_RESOLUTION);
    
        // PWM STARTUP AND SETUP CLOCK
        //-------------------------------
        pmc_enable_periph_clk( PWM_INTERFACE_ID );
        PWMC_ConfigureClocks( pwm_clockA_freq, pwm_clockB_freq, VARIANT_MCK );   
     
        // SETUP PWM FOR pwm_pin
        //------------------------
        PIO_Configure( g_APinDescription[pwm_pin].pPort,  g_APinDescription[pwm_pin].ulPinType,  g_APinDescription[pwm_pin].ulPin,  g_APinDescription[pwm_pin].ulPinConfiguration);
        if (iclock==1)  PWMC_ConfigureChannel(PWM_INTERFACE, chan, PWM_CMR_CPRE_CLKA, 0, 0);
        if (iclock==2)  PWMC_ConfigureChannel(PWM_INTERFACE, chan, PWM_CMR_CPRE_CLKB, 0, 0);
        PWMC_SetPeriod(PWM_INTERFACE, chan, pwm_max_duty_Ncount);
        PWMC_SetDutyCycle(PWM_INTERFACE, chan, pwm_duty);
        PWMC_EnableChannel(PWM_INTERFACE, chan);
    }

}



// WRITE DUTY CYCLE
//--------------------------------
void  pwm_write_duty( uint32_t  pwm_pin,  uint32_t  pwm_duty ) 
{
    if (pwm_pin>=6 && pwm_pin<=9)
    {    
        pwm_duty = mapResolution( pwm_duty, pwm_resolution_nbit, PWM_RESOLUTION);
        
        uint32_t  chan = g_APinDescription[pwm_pin].ulPWMChannel;
       
        PWMC_SetDutyCycle(PWM_INTERFACE, chan, pwm_duty);
    }
};



// FORCE PWM STOP
//--------------------------------
void  pwm_stop( uint32_t  pwm_pin ) 
{
    pinMode(pwm_pin, OUTPUT);      // sets the digital pin as output
    digitalWrite(pwm_pin, LOW);    // sets the LED off
};