unsigned int const sinetable[512] = {
1023,1022,1022,1022,1022,1022,1022,1022,1022,1022,1022,1022,1022,1022,1022,1021,
1021,1021,1021,1021,1021,1020,1020,1020,1020,1019,1019,1019,1019,1018,1018,1018,
1018,1017,1017,1017,1016,1016,1016,1015,1015,1014,1014,1014,1013,1013,1012,1012,
1011,1011,1010,1010,1009,1009,1008,1008,1007,1007,1006,1006,1005,1005,1004,1003,
1003,1002,1002,1001,1000,1000,999,998,998,997,996,996,995,994,993,993,
992,991,990,989,989,988,987,986,985,985,984,983,982,981,980,979,
978,977,977,976,975,974,973,972,971,970,969,968,967,966,965,964,
963,962,961,959,958,957,956,955,954,953,952,950,949,948,947,946,
945,943,942,941,940,938,937,936,935,933,932,931,929,928,927,926,
924,923,921,920,919,917,916,915,913,912,910,909,907,906,905,903,
902,900,899,897,896,894,893,891,889,888,886,885,883,882,880,878,
877,875,874,872,870,869,867,865,864,862,860,859,857,855,853,852,
850,848,846,845,843,841,839,838,836,834,832,830,828,827,825,823,
821,819,817,815,813,812,810,808,806,804,802,800,798,796,794,792,
790,788,786,784,782,780,778,776,774,772,770,768,766,764,761,759,
757,755,753,751,749,747,744,742,740,738,736,734,731,729,727,725,
723,720,718,716,714,711,709,707,705,702,700,698,695,693,691,689,
686,684,682,679,677,674,672,670,667,665,663,660,658,655,653,651,
648,646,643,641,638,636,633,631,628,626,624,621,619,616,614,611,
608,606,603,601,598,596,593,591,588,586,583,580,578,575,573,570,
567,565,562,560,557,554,552,549,546,544,541,538,536,533,530,528,
525,522,520,517,514,511,509,506,503,500,498,495,492,490,487,484,
481,478,476,473,470,467,465,462,459,456,453,450,448,445,442,439,
436,433,431,428,425,422,419,416,413,411,408,405,402,399,396,393,
390,387,385,382,379,376,373,370,367,364,361,358,355,352,349,346,
343,341,338,335,332,329,326,323,320,317,314,311,308,305,302,299,
296,293,290,287,284,281,278,275,272,269,266,263,259,256,253,250,
247,244,241,238,235,232,229,226,223,220,217,214,211,208,204,201,
198,195,192,189,186,183,180,177,174,170,167,164,161,158,155,152,
149,146,143,139,136,133,130,127,124,121,118,114,111,108,105,102,
99,96,93,89,86,83,80,77,74,71,68,64,61,58,55,52,
49,46,42,39,36,33,30,27,24,20,17,14,11,8,5,2
};
#include <stdio.h>
#include "sinetable.h"
#include <stdint.h>
int16_t get_sine(uint16_t position) {
// @ inputs: position in wave 0 - 2047
if(position < 1024) {
if(position < 512) {
// Q1
//return(position);
return(sinetable[position]);
} else {
// Q2
position = 511 + (512 - position);
//return(position);
return( -1 * sinetable[position]);
}
} else {
if(position < 1024 + 512) {
// Q3
position = -1 * (1024 - position);
//return(position);
return( -1 * sinetable[position]);
} else {
// Q4
position = 1023 + 1024 - position;
//return(position);
return(sinetable[position]);
}
}
}
uint16_t sine_t_pos = 0; // sinetable position
uint16_t sine_s_ctr = 0; // sine speed counter
uint16_t get_scaled_sine(uint16_t offset, uint16_t speed, uint16_t modulation) {
// @ input offset: 0 - 1023 /10bit/
// @ input speed: 0 - 1023 /10bit/
// @ input modulation: 0 - 1023 /10bit/
// @ function output: 0 - 1023 /10bit/
int32_t sine;
if(sine_s_ctr < speed) {
sine_s_ctr += 1;
} else {
sine_s_ctr = 0;
if(sine_t_pos < 2047) {
sine_t_pos++;
} else {
sine_t_pos = 0;
}
}
sine = get_sine(sine_t_pos);
sine = sine * (1 + modulation);
sine = sine >> 11;
sine = sine + offset;
if(sine < 1024) {
if(sine < 0) {
return((uint16_t) 0);
} else {
return((uint16_t) sine);
}
} else {
return((uint16_t) 1023);
}
}
main(void) {
uint32_t c, d;
uint16_t sine;
for(c=0; c<65535<<4; c++) {
sine = get_scaled_sine(512, 1, 512);
printf("sine: %d\n\r", sine);
}
}
[ 2 comments ] ( 11 views ) | [ 0 trackbacks ] | permalink