Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <math.h>
  2. #ifndef SINLUT_H
  3. #define SINLUT_H
  4. int factorial(int n);
  5. int t2;
  6. double sineTaylor(double x,int k);
  7. boolean check_time(double t);
  8. double sine,k,p;
  9. unsigned long t;
  10. static unsigned long start_time;
  11. const int out1 = 2;
  12. const int out2 = 3;
  13. const int out3 = 4;
  14. const int out4 = 5;
  15. const int out5 = 6;
  16. const int out6 = 7;
  17.  
  18. const uint8_t tab[128] = {0,1,2,2,3,4,5,5,6,7,8,8,9,10,10,11,12,13,13,14,15,15,16,17,17,18,18,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,27,28,28,28,29,29,29,29,30,30,30,30,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,30,30,30,30,30,29,29,29,29,28,28,28,27,27,27,26,26,25,25,24,24,23,23,22,22,21,21,20,20,19,18,18,17,17,16,15,15,14,13,13,12,11,10,10,9,8,8,7,6,5,5,4,3,2,2,1};
  19.  
  20. //Return sin(x). x is an integer where -128 = -pi and +128 = pi
  21. int sinLUT(int x){
  22. int out;
  23. x = x & 0x00ff; //Truncate bits 8 and up - these just represent multiples of 2pi
  24. if (x & 0x80) //Is the angle in the range -pi to 0?
  25. out = -(int)tab[x&0x7f]; //Yes, lookup the answer and invert it
  26. else
  27. out = (int)tab[x]; //No, just lookup the answer
  28. return out;
  29. }
  30.  
  31. inline void fastWriteX(byte x){ //Pins 8-13 are PORTB bits 5:0 so just write them
  32. PORTB = x;
  33. }
  34.  
  35. inline void fastWriteY(byte x){ //Pins 7-2 are PORTD bits 7:2 so shift data left by two bits
  36. PORTD = x << 2;
  37. }
  38.  
  39. #endif
  40. void setup() {
  41. // put your setup code here, to run once:
  42. pinMode(out1, OUTPUT);
  43. pinMode(out2, OUTPUT);
  44. pinMode(out3, OUTPUT);
  45. pinMode(out4, OUTPUT);
  46. pinMode(out5, OUTPUT);
  47. pinMode(out6, OUTPUT);
  48. p = 3.141;
  49. t2 = -128;
  50. k = 3;
  51. t = (unsigned long)(1000000/(256*100));//last term is frequency
  52. start_time = micros();
  53. }
  54.  
  55. void loop() {
  56. // put your main code here, to run repeatedly:
  57. sine = sinLUT(t2);
  58. t2 += 1;
  59. if (t2>126) {
  60. t2 = -128;
  61. }
  62.  
  63. //scale for 6 digital out (+1 and * 1.5 to get 0-3 range)
  64. sine = 32+sine;
  65. while (!(check_time(micros()))) {
  66. continue;//delayMicroseconds(1000000);
  67. }
  68. output(sine);
  69. }
  70. boolean check_time(unsigned long f) {
  71. if (f-start_time >= t) {
  72. start_time += t;
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. double sineTaylor(double x,int k){
  79. double y=0;
  80. for(int i=0;i<k+1;i++){
  81. y=y+pow(-1,i)*pow(x,(2*i+1))/factorial(2*i+1);
  82. }
  83. return y;
  84. }
  85. int factorial(int n){
  86. int z=1;
  87. for(int i=1;i<=n;i++){
  88. z=z*i;
  89. }
  90. return z;
  91. }
  92. void output (int i) {
  93. fastWriteY(i);
  94. /*
  95. digitalWrite(out1, i&0x01);
  96. digitalWrite(out2, i&0x02);
  97. digitalWrite(out3, i&0x04);
  98. digitalWrite(out4, i&0x08);
  99. digitalWrite(out5, i&0x10);
  100. digitalWrite(out6, i&0x20);
  101. */
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement