USING THIS CODE BELOW, Write the C code for the CalcSineWave function. The funct
ID: 3590771 • Letter: U
Question
USING THIS CODE BELOW, Write the C code for the CalcSineWave function. The function should output the next value in the table each time it is called and loop back to the beginning when it reaches the end of the table. THIS CODE SHOULD GIVE YOU A SINE WAVE.
#include <stdint.h>
uint16_t SineWave;
//Sine table table
const uint16_t SineWaveTable[32] = {
2048, 2447, 2831, 3185, 3496, 3750, 3940, 4056,
4095, 4056, 3940, 3750, 3496, 3185, 2831, 2447,
2048, 1648, 1264, 910, 599, 345, 155, 39,
0, 39, 155, 345, 599, 910, 1264, 1648};
uint16_t CalcSineWave(void)
{
//your code
}
int main(void)
{
while(1)
{
SineWave = CalcSineWave();
}
}
Explanation / Answer
#include <stdint.h>
#include<stdio.h>
uint16_t SineWave;
//Sine table table
const uint16_t SineWaveTable[32] = {
2048, 2447, 2831, 3185, 3496, 3750, 3940, 4056,
4095, 4056, 3940, 3750, 3496, 3185, 2831, 2447,
2048, 1648, 1264, 910, 599, 345, 155, 39,
0, 39, 155, 345, 599, 910, 1264, 1648};
uint16_t CalcSineWave(void)
{
static int i = -1; //I value will be initialized only once
i++;
if(i==32){ //Comparing and rolling back to beginning
i=0;
}
if(i<32){
return SineWaveTable[i]; //Every time return the next value in the table
}
}
int main(void)
{
while(1)
{
SineWave = CalcSineWave();
printf("%u ",SineWave);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.