# 3 Phase Sine Wave Generator Based on Arduino Due

12,882

17

4

the purpose of this share is to help someone who's trying to utilize Due's greater performance + lack of reference + non-helpful datasheet.

this project is able to generate up to 3 phase sine wave @ 256 samples / cycle at low freq (<1kHz) and 16 samples/cycle @ high freq (up to 20kHz), which is good enough to be smoothed by simple LPFs and the output is almost perfect.

the attached file was not my final version for I added some additional feature but the core is same to that. Note the samples/cycle was set lower than above statement.

since the CPU capacity is maximized through the approach shown in the attached file, I used an Arduino Uno as control unit, who utilize Arduino Due's external interrupt to pass frequency value to Arduino Due. In addition to frequency control, the Arduino Uno also controls amplitude (through digital potential-meter + OpAmp) as well as I/O---there will be a lot of room to play with.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Generate Sine Data Array

Since real-time calculation is CPU demanding, a sine data array is required for better performance

uint32_t sin768[] PROGMEM= ....
while x=[0:5375]; y = 127+127*(sin(2*pi/5376/*or some # you prefer depends on requirement*/))

## Step 2: Enabling Parallel Output

Unlike Uno, Due have limited reference. However in order to generate 3 phase sine wave based on Arduino Uno, 1st of all, performance is not applausable due to its low MCLK (16MHz while Due is 84MHz), 2nd, it's limited GPIO can produce max 2 phase output and you need additional analogue circuit to produce the 3rd phase (C=-A-B).

Following GPIO enabling was mostly based on try and trial+not helpful datasheet of SAM3X

PIOC->PIO_PER = 0xFFFFFFFE; //PIO controller PIO Enable register (refer to p656 of ATMEL SAM3X datasheet) and http://arduino.cc/en/Hacking/PinMappingSAM3X, Arduino Due pin 33-41 and 44-51 were enabled

PIOC->PIO_OER = 0xFFFFFFFE; //PIO controller output enable register, refer to p657 of ATMEL SAM3X datasheet PIOC->PIO_OSR = 0xFFFFFFFE; //PIO controller output status register, refer to p658 of ATMEL SAM3X datasheet

PIOC->PIO_OWER = 0xFFFFFFFE; //PIO output write enable register, refer to p670 of ATMEL SAM3X datasheet

//PIOA->PIO_PDR = 0x30000000; //optional as insurance, does not seem to affect performance, digital pin 10 connect to both PC29 and PA28, digital pin 4 connect to both PC29 and PA28, here to disable disable PIOA #28 & 29

## Step 3: Enabling Interrupt

To maximize its performance, CPU load should be as low as possible. However due to the non-1to1 correspondence between the CPU pin and the Due pin, bit operation is necessary.

You can further optimize the algorithm but the room is very limited.

void TC7_Handler(void)
{ TC_GetStatus(TC2,1);

t = t%samples; //use t%samples instead of 'if' to avoid overflow of t

phaseAInc = (preset*t)%5376; //use %5376 to avoid array index overflow

phaseBInc = (phaseAInc+1792)%5376;

phaseCInc = (phaseAInc+3584)%5376;

p_A = sin768[phaseAInc]<<1; //refer to PIOC: PC1 to PC8, corresponding Arduino Due pin: pin 33-40, hence shift left for 1 digit

p_B = sin768[phaseBInc]<<12; //refer to PIOC: PC12 to PC19, corresponding Arduino Due pin: pin 51-44, hence shift left 12 digit

p_C = sin768[phaseCInc]; //phase C output employe PIOC: PC21, PC22, PC23, PC24, PC25,PC26, PC28 and PC29, corresponding Arduino Due pin: digital pin: 9,8,7,6,5,4,3,10, respectively

p_C2 = (p_C&B11000000)<<22; //this generates PC28 and PC29

p_C3 = (p_C&B00111111)<<21; //this generates PC21-PC26

p_C = p_C2|p_C3; //this generates parallel output of phase C

p_A = p_A|p_B|p_C; //32 bit output = phase A (8bit)|phase B|phase C

PIOC->PIO_ODSR = p_A; //output register =p_A

t++; }

## Step 4: R/2R DAC

build 3x8bit R/2R DAC, loads of ref on google.

## Step 5: Full Code

#define _BV(x) (1<<(x));
uint32_t sin768[] PROGMEM= /* x=[0:5375]; y = 127+127*(sin(2*pi/5376)) */

uint32_t p_A,p_B,p_C,p_C2,p_C3; //phase A phase B phase C value--though output are 8bits only, p_A and p_B value will be operated to generate a new 32 bit value in order to cop with 32bit PIOC output

uint16_t phaseAInc, phaseBInc,phaseCInc,freq, freqNew; uint32_t interval; uint16_t samples,preset; uint32_t t = 0;

void setup() {

//parallel output PIOC setup: Arduino Due pin33-40 are employed as phase A output while pin 44-51 work for phase B output

PIOC->PIO_PER = 0xFFFFFFFE; //PIO controller PIO Enable register (refer to p656 of ATMEL SAM3X datasheet) and http://arduino.cc/en/Hacking/PinMappingSAM3X, Arduino Due pin 33-41 and 44-51 were enabled

PIOC->PIO_OER = 0xFFFFFFFE; //PIO controller output enable register, refer to p657 of ATMEL SAM3X datasheet

PIOC->PIO_OSR = 0xFFFFFFFE; //PIO controller output status register, refer to p658 of ATMEL SAM3X datasheet

PIOC->PIO_OWER = 0xFFFFFFFE; //PIO output write enable register, refer to p670 of ATMEL SAM3X datasheet

//PIOA->PIO_PDR = 0x30000000; //optional as insurance, does not seem to affect performance, digital pin 10 connect to both PC29 and PA28, digital pin 4 connect to both PC29 and PA28, here to disable disable PIOA #28 & 29 //timer setup, refer to http://arduino.cc/en/Hacking/PinMappingSAM3X,

pmc_set_writeprotect(false); // disable write protection of Power Management Control registers

pmc_enable_periph_clk(ID_TC7); // enable peripheral clock time counter 7

TC_Configure(/* clock */TC2,/* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1); //TC clock 42MHz (clock, channel, compare mode setting) TC_SetRC(TC2, 1, interval); TC_Start(TC2, 1);

// enable timer interrupts on the timer TC2->TC_CHANNEL.TC_IER=TC_IER_CPCS; // IER = interrupt enable register TC2->TC_CHANNEL.TC_IDR=~TC_IER_CPCS; // IDR = interrupt disable register

NVIC_EnableIRQ(TC7_IRQn); // Enable the interrupt in the nested vector interrupt controller freq = 60; //initialize frequency as 60Hz preset = 21; //array index increase by 21 samples = 256; //output samples 256/cycle interval = 42000000/(freq*samples); //interrupt counts TC_SetRC(TC2, 1, interval); //start TC Serial.begin(9600); //for test purpose }

void checkFreq()

{ freqNew = 20000 ;

if (freq == freqNew) {} else

{ freq = freqNew;

if (freq>20000) {freq = 20000; /*max frequency 20kHz*/};

if (freq<1) {freq = 1; /*min frequency 1Hz*/};

if (freq>999) {preset = 384; samples = 14;} //for frequency >=1kHz, 14 samples for each cycle

else if (freq>499) {preset = 84; samples = 64;} //for 500<=frequency<1000Hz, 64 samples for each cycle else if (freq>99) {preset = 42; samples = 128;} //for 100Hz<=frequency<500Hz, 128 samples/cycle

else {preset = 21; samples = 256;}; //for frequency<100hz, 256 samples for each cycle

interval = 42000000/(freq*samples); t = 0; TC_SetRC(TC2, 1, interval); } }

void loop() {

checkFreq(); delay(100); }

void TC7_Handler(void)

{ TC_GetStatus(TC2,1);

t = t%samples; //use t%samples to avoild overflow of t phaseAInc = (preset*t)%5376; //use %5376 to avoid array index overflow

phaseBInc = (phaseAInc+1792)%5376;

phaseCInc = (phaseAInc+3584)%5376;

p_A = sin768[phaseAInc]<<1; //refer to PIOC: PC1 to PC8, corresponding Arduino Due pin: pin 33-40, hence shift left for 1 digit

p_B = sin768[phaseBInc]<<12; //refer to PIOC: PC12 to PC19, corresponding Arduino Due pin: pin 51-44, hence shift left 12 digit

p_C = sin768[phaseCInc]; //phase C output employe PIOC: PC21, PC22, PC23, PC24, PC25,PC26, PC28 and PC29, corresponding Arduino Due pin: digital pin: 9,8,7,6,5,4,3,10, respectively

p_C2 = (p_C&B11000000)<<22; //this generates PC28 and PC29

p_C3 = (p_C&B00111111)<<21; //this generates PC21-PC26 //Serial.println(p_C3,BIN); p_C = p_C2|p_C3; //this generates parallel output of phase C

p_A = p_A|p_B|p_C; //32 bit output = phase A (8bit)|phase B|phase C //Serial.println(p_A>>21,BIN); //PIOC->PIO_ODSR = 0x37E00000;

PIOC->PIO_ODSR = p_A; //output register =p_A t++; }

## 4 Discussions

This is Definitely Helpful. Thanks for sharing.

However, I have one doubt though, i.e. instead of writing " while x=[0:5375]; y = 127+127*(sin(2*pi/5376/*) "

will it be " while x=[0:5375]; y = 127+127*(sin(2*pi*x)) " so that we will get an array.

And also in " uint32_t sin768[] " what is the significance of 768, does it mean there are 768 samples in the array corresponding to one complete cycle of sine wave.