Continue to Site

# Cosine Wave generation in ARM microcontroller

Status
Not open for further replies.

#### anne rachel

##### Junior Member level 2
Hi,

Can anyone suggest me an algorithm to generate a cosine wave using ARM microcontroller assembly language??

Sorry, I don't have any code to offer - I suspect it would be kind of tricky because of all the math required to generate a sine (or cosine) in assembly language.

Could I offer a couple of other solutions?

Assuming you have a digital to analog converter, you might be able to put a table into your program that has the correct values to output to generate the sine wave.

Another way would be to fall back on analog electronics. If you output a square wave, you can filter it to get a sine wave. A square wave is made up of the odd harmonics of a sine wave (http://resources.teachnet.ie/amhiggins/squaresaw.html). A 100 hz square wave is made of of 100 hz, 300 hz, 500 hz, 700 hz (etc) sine waves. If you take the 100 square wave and put an analog filter on it that will cut out most everything past 100 hz, you will get a sine wave.

Hope this helps...

anne,

In order to avoid exorbitant ALU calculation, I performed this task in an 8-bit PIC microcontroller just accessing a look-up table, and loading this value into a PWM duty-cicle module.

Code:
const unsigned int8 Duty0  [] = {7,15,23,31,38,46,53,61,68,75,82,88,95,101,107,113,118,123,128,133,137,141,144,147,150,153,155,156,158,159,159,160};
const unsigned int8 Duty1  [] = {7,15,23,30,38,45,52,60,67,74,80,87,93,99,105,111,116,121,126,130,134,138,142,145,148,150,152,154,155,156,157,157};
const unsigned int8 Duty2  [] = {7,15,22,30,37,44,52,59,66,72,79,85,92,98,103,109,114,119,124,128,132,136,139,142,145,147,149,151,152,153,154,154};
const unsigned int8 Duty3  [] = {7,14,22,29,36,44,51,58,65,71,78,84,90,96,102,107,112,117,122,126,130,134,137,140,143,145,147,149,150,151,151,152};
const unsigned int8 Duty4  [] = {7,14,21,29,36,43,50,57,63,70,76,83,89,94,100,105,110,115,120,124,128,131,135,138,140,143,145,146,147,148,149,149};
const unsigned int8 Duty5  [] = {7,14,21,28,35,42,49,56,62,69,75,81,87,93,98,104,109,113,118,122,126,129,133,136,138,140,142,144,145,146,147,147};
const unsigned int8 Duty6  [] = {7,14,21,28,35,42,48,55,61,68,74,80,86,91,97,102,107,112,116,120,124,127,130,133,136,138,140,142,143,144,144,144};
const unsigned int8 Duty7  [] = {7,13,20,27,34,41,48,54,60,67,73,79,84,90,95,100,105,110,114,118,122,125,128,131,134,136,138,139,141,141,142,142};
const unsigned int8 Duty8  [] = {6,13,20,27,34,40,47,53,60,66,72,78,83,89,94,99,104,108,112,116,120,123,126,129,132,134,136,137,138,139,140,140};
const unsigned int8 Duty9  [] = {6,13,20,26,33,40,46,52,59,65,71,76,82,87,92,97,102,106,111,115,118,122,125,127,130,132,134,135,136,137,138,138};
const unsigned int8 Duty10 [] = {6,13,20,26,33,39,45,52,58,64,70,75,81,86,91,96,101,105,109,113,116,120,123,125,128,130,132,133,134,135,136,136};
const unsigned int8 Duty11 [] = {6,13,19,26,32,38,45,51,57,63,69,74,80,85,90,94,99,103,107,111,115,118,121,124,126,128,130,131,132,133,134,134};
const unsigned int8 Duty12 [] = {6,12,19,25,32,38,44,50,56,62,68,73,78,84,88,93,98,102,106,110,113,116,119,122,124,126,128,129,130,131,132,132};
const unsigned int8 Duty13 [] = {6,12,19,25,31,37,43,49,55,61,67,72,77,82,87,92,96,100,104,108,111,115,118,120,122,124,126,128,129,129,130,130};
const unsigned int8 Duty14 [] = {6,12,18,25,31,37,43,49,55,60,66,71,76,81,86,91,95,99,103,107,110,113,116,118,121,123,124,126,127,128,128,128};
const unsigned int8 Duty15 [] = {6,12,18,24,30,36,42,48,54,59,65,70,75,80,85,89,94,98,101,105,108,111,114,117,119,121,123,124,125,126,126,126};
const unsigned int8 Duty16 [] = {6,12,18,24,30,36,42,47,53,59,64,69,74,79,84,88,92,96,100,104,107,110,113,115,117,119,121,122,123,124,125,125};
const unsigned int8 Duty17 [] = {6,12,18,24,30,35,41,47,52,58,63,68,73,78,82,87,91,95,99,102,105,108,111,114,116,118,119,121,122,122,123,123};
const unsigned int8 Duty18 [] = {5,11,17,23,29,35,41,46,52,57,62,67,72,77,81,86,90,94,97,101,104,107,110,112,114,116,118,119,120,121,121,121};
const unsigned int8 Duty19 [] = {5,11,17,23,29,34,40,46,51,56,61,66,71,76,80,85,89,93,96,100,103,106,108,111,113,115,116,118,119,119,120,120};
const unsigned int8 Duty20 [] = {5,11,17,23,28,34,40,45,50,55,61,65,70,75,79,83,87,91,95,98,101,104,107,109,111,113,115,116,117,118,118,118};
const unsigned int8 Duty21 [] = {5,11,17,22,28,34,39,44,50,55,60,65,69,74,78,82,86,90,94,97,100,103,105,108,110,112,113,114,115,116,117,117};
const unsigned int8 Duty22 [] = {5,11,16,22,28,33,39,44,49,54,59,64,68,73,77,81,85,89,92,96,99,102,104,106,109,110,112,113,114,115,115,115};
const unsigned int8 Duty23 [] = {5,11,16,22,27,33,38,43,48,53,58,63,68,72,76,80,84,88,91,95,98,100,103,105,107,109,110,112,113,113,114,114};
const unsigned int8 Duty24 [] = {5,11,16,22,27,32,38,43,48,53,58,62,67,71,75,79,83,87,90,93,96,99,102,104,106,108,109,110,111,112,112,112};
const unsigned int8 Duty25 [] = {5,10,16,21,27,32,37,42,47,52,57,61,66,70,74,78,82,86,89,92,95,98,100,103,105,106,108,109,110,111,111,111};
const unsigned int8 Duty26 [] = {5,10,16,21,26,32,37,42,47,51,56,61,65,69,74,77,81,85,88,91,94,97,99,101,103,105,106,108,109,109,110,110};
const unsigned int8 Duty27 [] = {5,10,15,21,26,31,36,41,46,51,56,60,64,69,73,77,80,84,87,90,93,96,98,100,102,104,105,106,107,108,108,108};
const unsigned int8 Duty28 [] = {5,10,15,21,26,31,36,41,46,50,55,59,64,68,72,76,79,83,86,89,92,94,97,99,101,103,104,105,106,107,107,107};
const unsigned int8 Duty29 [] = {5,10,15,20,25,30,35,40,45,50,54,59,63,67,71,75,78,82,85,88,91,93,96,98,100,101,103,104,105,105,106,106};
const unsigned int8 Duty30 [] = {5,10,15,20,25,30,35,40,44,49,54,58,62,66,70,74,77,81,84,87,90,92,95,97,99,100,102,103,104,104,105,105};
const unsigned int8 Duty31 [] = {5,10,15,20,25,30,35,39,44,49,53,57,61,65,69,73,77,80,83,86,89,91,94,96,97,99,100,102,102,103,103,104};
const unsigned int8 Duty32 [] = {5,10,15,20,24,29,34,39,43,48,52,57,61,65,69,72,76,79,82,85,88,90,92,95,96,98,99,100,101,102,102,102};

+++

Last edited:

I didn't get your solution.Can u tell why you have taken 33 duty arrays?? Can you give the logic behind your solution??

anne,

In order to generate cosine output on uC, you can do in 2 ways :
--> Performing a direct calculation by function of math library : PWM_DUTY = sin ( 2*PI*t/f )
--> Geting values from a look-up table : PWM_DUTY = Table[t]

The first approach, consumes a lot of core processing, even at a float point microcontroler, but allow a more flexible result.
The second approach, saves core processing, however is not a flexible implementation. Each value represent a sine of an angle.

The above example I show, have a lot of vectores, which is sometimes usefull for fine calibration.
You coud select just one vector to use once.
I calculated to 32 points, but you can select any other precision.

+++

Sorry..
I am actually new to ARM UC .I still didn't get it properly. What do you mean by assign those values to PWM_DUTY module?
Do you mean f=fpwm/N
where fpwm=fs/255
here f=cosine wave frequency
fs=timer clock
N=no of samples
i got this info from
https://abov.co.kr/data/appnote/an114.pdf
How do I display the sampled values on the UC??

Sorry, I didn´t realize you are newbie in uC.
I assumed you have some knowledge on that arquetetures.

+++

Status
Not open for further replies.