Created
September 12, 2017 19:40
-
-
Save dirkjanfaber/23ad3de5d2a16690b5587aabdc2526e9 to your computer and use it in GitHub Desktop.
In order to generate sine tabs for stepper motors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Compile: gcc -o sine sine.c -lm -Wall | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <math.h> | |
#define PI 3.14159265 | |
int main () | |
{ | |
uint16_t i = 256; // number of steps in the sin_tab. Use 4, 8, 16, 32, 64, 128 or 256 | |
uint8_t max = 255; // amplitude (in case of tmc2130, set to 248) | |
// no need to change anything from here | |
uint16_t angle = 0; | |
int16_t curA, curB; | |
uint32_t sin_tab[2*i]; | |
uint16_t j=0; | |
angle=0; | |
while ( angle < 360 ) { | |
printf("Angle = %3u: ", angle); | |
curA = (int16_t)(max*sin(angle*(PI/180))); | |
curB = (int16_t)(max*cos(angle*(PI/180))); | |
printf("cA= %4d, cB = %4d", curA, curB); | |
if ( curA < 0 ) { curA = ( curA ^ 0xff00 ) | 0x0100 ; } | |
if ( curB < 0 ) { curB = ( curB ^ 0xff00 ) | 0x0100 ; } | |
angle += ( 360 / i ); | |
sin_tab[j] = ( curA << 16 ) | curB; | |
printf(" 0x%08x\n", sin_tab[j]); | |
j++; | |
} | |
printf("const uint32_t sin_tab%d[%d] = {\n", i, i); | |
for ( j=0; j<i; j++ ) { | |
printf(" 0x%08x", sin_tab[j]); | |
( j < ( i -1 ) ) ? printf(", \n") : printf("\n};\n"); | |
}; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment