Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Sin table in PIC....

Status
Not open for further replies.
Hello there Brian,

It is customary to only compute the values for sin(a) for 'a' from 0 to pi/4 because EVERY other value in the table from 0 to 2pi can be easily computed from those values and it is much faster to compute just the 0 to 90 degree values using a sine algorithm and then simply repeat them for 90 to 180 and then flip the sign bit for 180 to 360 degrees. It's also normal to just store the values from 0 to 90 and then do the rest of the simple math on the fly as needed.

Say hello to Meg and Stewie for me :)
 
Hello there Brian,

It is customary to only compute the values for sin(a) for 'a' from 0 to pi/4 because EVERY other value in the table from 0 to 2pi can be easily computed from those values and it is much faster to compute just the 0 to 90 degree values using a sine algorithm and then simply repeat them for 90 to 180 and then flip the sign bit for 180 to 360 degrees. It's also normal to just store the values from 0 to 90 and then do the rest of the simple math on the fly as needed.

Hello,

Yes, I agree on that too. The flipping and mirroring takes extra work (some more cycles are used) but use less memory (only 1/4 of 256). Parallax Propeller (a microcontroller with 8-cores) has 1/4 of the sine 'burned' inside the chip, and to generate the rest of it, some manipulation like what you said can be easily done.

For begineers, a full period is written first to start seeing a sine wave in the scope, and then as an exercise to students, he/she must only use 1/4 of the sine wave and some extra code is written.

Thanks for the wishes too. :)
 
It's weird how this thread started as how to use the sine function and when that question was answered it turned into how to make sine table. I'm sorry but the more I read this the more I think the OP is a troll. Notice how he uses perfect English in some replies and then broken in others. He also NEVER replies to anything I post but happily replies to replies to my post!!

Mike.
 
Hi again,

Ian:
Oh ok, so you think he doesnt understand the making of the table? I thought i talked to him about more advanced topics but i could be wrong.

Pommie:
Interesting, so then what would his purpose here be then?
 
Pommie:
Interesting, so then what would his purpose here be then?

He's playing with people. He's a troll.

Do you really believe that someone could spend a year without learning anything?

Mike.
 
Casual banter aside, a good simple sine table can be found from the book "PIC Microcontroller and Embedded Systems" by Mazidi. On page 520. It is very obvious, too.

If he could find this book in the library, it'll answer most of his questions.
 
Casual banter aside, a good simple sine table can be found from the book "PIC Microcontroller and Embedded Systems" by Mazidi. On page 520. It is very obvious, too.

If he could find this book in the library, it'll answer most of his questions.

I'll bet he doesn't.

Mike.
 
Hi,

why it is giving error??

HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
Copyright (C) 2011 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Internal error - no stack allocated to function

********** Build failed! **********


Code:
#include <htc.h>
#include <math.h>
#include <stdio.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000


void main (void) {
float pi = 3.14;
float sine[10];
while(1){
for(int i=0;i<=10;i++) {

 sine[i]= sin(pi/180 * i);
}
}
}
 
Are you still using the pic16f877a??? I' not sure how many words are required for the math library... If its too much, then there won't be enough room for the stack... This is why I make my own fixed point math library... ( I know floats are returned, but no variables are needed ).
 
Hi,

why it is giving error??

HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
Copyright (C) 2011 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Internal error - no stack allocated to function

********** Build failed! **********


Code:
#include <htc.h>
#include <math.h>
#include <stdio.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000


void main (void) {
float pi = 3.14;
float sine[10];
while(1){
for(int i=0;i<=10;i++) {

 sine[i]= sin(pi/180 * i);
}
}
}

Floating-point mathematics are tedious and takes up a lot of memory, especially on the RAM. You will have to spend 40 bytes (one float = four bytes) for only 10 samples in the sine waves. And that PIC16F677A has only around 300 bytes of RAM. And even that, you cannot directly output a float value to a PORTx register, since the sizes are different.

From the previous posts, you may need to search google for "sine table for microcontrollers".
 
Is this fine?


Code:
 Left side is 0 to 255. Right side is 0 to +127 to 0 to -127 to 0.
Graph right side!
0 0.0 0.0000 0.0000 0
1 1.4 0.0246 0.0246 3
2 2.8 0.0493 0.0493 6
3 4.2 0.0739 0.0739 9
4 5.6 0.0986 0.0984 12
5 7.1 0.1232 0.1229 16
6 8.5 0.1478 0.1473 19
7 9.9 0.1725 0.1716 22
8 11.3 0.1971 0.1958 25
9 12.7 0.2218 0.2199 28
10 14.1 0.2464 0.2439 31
11 15.5 0.2710 0.2677 34
12 16.9 0.2957 0.2914 37
13 18.4 0.3203 0.3149 40
14 19.8 0.3450 0.3382 43
15 21.2 0.3696 0.3612 46
16 22.6 0.3942 0.3841 49
17 24.0 0.4189 0.4067 52
18 25.4 0.4435 0.4291 54
19 26.8 0.4682 0.4512 57
20 28.2 0.4928 0.4731 60
21 29.6 0.5174 0.4947 63
22 31.1 0.5421 0.5159 66
23 32.5 0.5667 0.5369 68
24 33.9 0.5914 0.5575 71
25 35.3 0.6160 0.5778 73
26 36.7 0.6406 0.5977 76
27 38.1 0.6653 0.6173 78
28 39.5 0.6899 0.6365 81
29 40.9 0.7146 0.6553 83
30 42.4 0.7392 0.6737 86
31 43.8 0.7638 0.6917 88
32 45.2 0.7885 0.7093 90
33 46.6 0.8131 0.7264 92
34 48.0 0.8378 0.7431 94
35 49.4 0.8624 0.7594 96
36 50.8 0.8870 0.7752 98
37 52.2 0.9117 0.7905 100
38 53.6 0.9363 0.8054 102
39 55.1 0.9610 0.8197 104
40 56.5 0.9856 0.8336 106
41 57.9 1.0102 0.8470 108
42 59.3 1.0349 0.8598 109
43 60.7 1.0595 0.8721 111
44 62.1 1.0842 0.8839 112
45 63.5 1.1088 0.8952 114
46 64.9 1.1334 0.9059 115
47 66.4 1.1581 0.9160 116
48 67.8 1.1827 0.9256 118
49 69.2 1.2074 0.9347 119
50 70.6 1.2320 0.9432 120
51 72.0 1.2566 0.9511 121
52 73.4 1.2813 0.9584 122
53 74.8 1.3059 0.9651 123
54 76.2 1.3306 0.9713 123
55 77.6 1.3552 0.9768 124
56 79.1 1.3798 0.9818 125
57 80.5 1.4045 0.9862 125
58 81.9 1.4291 0.9900 126
59 83.3 1.4538 0.9932 126
60 84.7 1.4784 0.9957 126
61 86.1 1.5030 0.9977 127
62 87.5 1.5277 0.9991 127
63 88.9 1.5523 0.9998 127
64 90.4 1.5770 1.0000 127
65 91.8 1.6016 0.9995 127
66 93.2 1.6262 0.9985 127
67 94.6 1.6509 0.9968 127
68 96.0 1.6755 0.9945 126
69 97.4 1.7002 0.9916 126
70 98.8 1.7248 0.9882 125
71 100.2 1.7494 0.9841 125
72 101.6 1.7741 0.9794 124
73 103.1 1.7987 0.9741 124
74 104.5 1.8234 0.9683 123
75 105.9 1.8480 0.9618 122
76 107.3 1.8726 0.9548 121
77 108.7 1.8973 0.9472 120
78 110.1 1.9219 0.9390 119
79 111.5 1.9466 0.9302 118
80 112.9 1.9712 0.9209 117
81 114.4 1.9958 0.9110 116
82 115.8 2.0205 0.9006 114
83 117.2 2.0451 0.8896 113
84 118.6 2.0698 0.8781 112
85 120.0 2.0944 0.8660 110
86 121.4 2.1190 0.8534 108
87 122.8 2.1437 0.8403 107
88 124.2 2.1683 0.8267 105
89 125.6 2.1930 0.8126 103
90 127.1 2.2176 0.7980 101
91 128.5 2.2422 0.7829 99
92 129.9 2.2669 0.7674 97
93 131.3 2.2915 0.7513 95
94 132.7 2.3162 0.7348 93
95 134.1 2.3408 0.7179 91
96 135.5 2.3654 0.7005 89
97 136.9 2.3901 0.6827 87
98 138.4 2.4147 0.6645 84
99 139.8 2.4394 0.6459 82
100 141.2 2.4640 0.6269 80
101 142.6 2.4886 0.6075 77
102 144.0 2.5133 0.5878 75
103 145.4 2.5379 0.5677 72
104 146.8 2.5626 0.5472 69
105 148.2 2.5872 0.5264 67
106 149.6 2.6118 0.5053 64
107 151.1 2.6365 0.4839 61
108 152.5 2.6611 0.4622 59
109 153.9 2.6858 0.4402 56
110 155.3 2.7104 0.4180 53
111 156.7 2.7350 0.3955 50
112 158.1 2.7597 0.3727 47
113 159.5 2.7843 0.3497 44
114 160.9 2.8090 0.3265 41
115 162.4 2.8336 0.3032 39
116 163.8 2.8582 0.2796 36
117 165.2 2.8829 0.2558 32
118 166.6 2.9075 0.2319 29
119 168.0 2.9322 0.2079 26
120 169.4 2.9568 0.1837 23
121 170.8 2.9814 0.1595 20
122 172.2 3.0061 0.1351 17
123 173.6 3.0307 0.1107 14
124 175.1 3.0554 0.0861 11
125 176.5 3.0800 0.0616 8
126 177.9 3.1046 0.0370 5
127 179.3 3.1293 0.0123 2
128 180.7 3.1539 -0.0123 -2
129 182.1 3.1786 -0.0370 -5
130 183.5 3.2032 -0.0616 -8
131 184.9 3.2278 -0.0861 -11
132 186.4 3.2525 -0.1107 -14
133 187.8 3.2771 -0.1351 -17
134 189.2 3.3018 -0.1595 -20
135 190.6 3.3264 -0.1837 -23
136 192.0 3.3510 -0.2079 -26
137 193.4 3.3757 -0.2319 -29
138 194.8 3.4003 -0.2558 -32
139 196.2 3.4250 -0.2796 -36
140 197.6 3.4496 -0.3032 -39
141 199.1 3.4742 -0.3265 -41
142 200.5 3.4989 -0.3497 -44
143 201.9 3.5235 -0.3727 -47
144 203.3 3.5482 -0.3955 -50
145 204.7 3.5728 -0.4180 -53
146 206.1 3.5974 -0.4402 -56
147 207.5 3.6221 -0.4622 -59
148 208.9 3.6467 -0.4839 -61
149 210.4 3.6714 -0.5053 -64
150 211.8 3.6960 -0.5264 -67
151 213.2 3.7206 -0.5472 -69
152 214.6 3.7453 -0.5677 -72
153 216.0 3.7699 -0.5878 -75
154 217.4 3.7946 -0.6075 -77
155 218.8 3.8192 -0.6269 -80
156 220.2 3.8438 -0.6459 -82
157 221.6 3.8685 -0.6645 -84
158 223.1 3.8931 -0.6827 -87
159 224.5 3.9178 -0.7005 -89
160 225.9 3.9424 -0.7179 -91
161 227.3 3.9670 -0.7348 -93
162 228.7 3.9917 -0.7513 -95
163 230.1 4.0163 -0.7674 -97
164 231.5 4.0410 -0.7829 -99
165 232.9 4.0656 -0.7980 -101
166 234.4 4.0902 -0.8126 -103
167 235.8 4.1149 -0.8267 -105
168 237.2 4.1395 -0.8403 -107
169 238.6 4.1642 -0.8534 -108
170 240.0 4.1888 -0.8660 -110
171 241.4 4.2134 -0.8781 -112
172 242.8 4.2381 -0.8896 -113
173 244.2 4.2627 -0.9006 -114
174 245.6 4.2873 -0.9110 -116
175 247.1 4.3120 -0.9209 -117
176 248.5 4.3366 -0.9302 -118
177 249.9 4.3613 -0.9390 -119
178 251.3 4.3859 -0.9472 -120
179 252.7 4.4105 -0.9548 -121
180 254.1 4.4352 -0.9618 -122
181 255.5 4.4598 -0.9683 -123
182 256.9 4.4845 -0.9741 -124
183 258.4 4.5091 -0.9794 -124
184 259.8 4.5337 -0.9841 -125
185 261.2 4.5584 -0.9882 -125
186 262.6 4.5830 -0.9916 -126
187 264.0 4.6077 -0.9945 -126
188 265.4 4.6323 -0.9968 -127
189 266.8 4.6569 -0.9985 -127
190 268.2 4.6816 -0.9995 -127
191 269.6 4.7062 -1.0000 -127
192 271.1 4.7309 -0.9998 -127
193 272.5 4.7555 -0.9991 -127
194 273.9 4.7801 -0.9977 -127
195 275.3 4.8048 -0.9957 -126
196 276.7 4.8294 -0.9932 -126
197 278.1 4.8541 -0.9900 -126
198 279.5 4.8787 -0.9862 -125
199 280.9 4.9033 -0.9818 -125
200 282.4 4.9280 -0.9768 -124
201 283.8 4.9526 -0.9713 -123
202 285.2 4.9773 -0.9651 -123
203 286.6 5.0019 -0.9584 -122
204 288.0 5.0265 -0.9511 -121
205 289.4 5.0512 -0.9432 -120
206 290.8 5.0758 -0.9347 -119
207 292.2 5.1005 -0.9256 -118
208 293.6 5.1251 -0.9160 -116
209 295.1 5.1497 -0.9059 -115
210 296.5 5.1744 -0.8952 -114
211 297.9 5.1990 -0.8839 -112
212 299.3 5.2237 -0.8721 -111
213 300.7 5.2483 -0.8598 -109
214 302.1 5.2729 -0.8470 -108
215 303.5 5.2976 -0.8336 -106
216 304.9 5.3222 -0.8197 -104
217 306.4 5.3469 -0.8054 -102
218 307.8 5.3715 -0.7905 -100
219 309.2 5.3961 -0.7752 -98
220 310.6 5.4208 -0.7594 -96
221 312.0 5.4454 -0.7431 -94
222 313.4 5.4701 -0.7264 -92
223 314.8 5.4947 -0.7093 -90
224 316.2 5.5193 -0.6917 -88
225 317.6 5.5440 -0.6737 -86
226 319.1 5.5686 -0.6553 -83
227 320.5 5.5933 -0.6365 -81
228 321.9 5.6179 -0.6173 -78
229 323.3 5.6425 -0.5977 -76
230 324.7 5.6672 -0.5778 -73
231 326.1 5.6918 -0.5575 -71
232 327.5 5.7165 -0.5369 -68
233 328.9 5.7411 -0.5159 -66
234 330.4 5.7657 -0.4947 -63
235 331.8 5.7904 -0.4731 -60
236 333.2 5.8150 -0.4512 -57
237 334.6 5.8397 -0.4291 -54
238 336.0 5.8643 -0.4067 -52
239 337.4 5.8889 -0.3841 -49
240 338.8 5.9136 -0.3612 -46
241 340.2 5.9382 -0.3382 -43
242 341.6 5.9629 -0.3149 -40
243 343.1 5.9875 -0.2914 -37
244 344.5 6.0121 -0.2677 -34
245 345.9 6.0368 -0.2439 -31
246 347.3 6.0614 -0.2199 -28
247 348.7 6.0861 -0.1958 -25
248 350.1 6.1107 -0.1716 -22
249 351.5 6.1353 -0.1473 -19
250 352.9 6.1600 -0.1229 -16
251 354.4 6.1846 -0.0984 -12
252 355.8 6.2093 -0.0739 -9
253 357.2 6.2339 -0.0493 -6
254 358.6 6.2585 -0.0246 -3
255 360.0 6.2832 0.0000 0

Code:
; Set sine "table" in an array
sineval var byte[72]
sineval[1] = 128 ;0 degree
sineval[2] = 137 ;5 degree
sineval[3] = 146
sineval[4] = 154
sineval[5] = 163
sineval[6] = 171
sineval[7] = 179
sineval[8] = 187
sineval[9] = 194
sineval[10] = 200
sineval[11] = 206
sineval[12] = 212
sineval[13] = 216
sineval[14] = 220
sineval[15] = 224
sineval[16] = 227
sineval[17] = 228
sineval[18] = 229
sineval[19] = 230 ;90 degree
sineval[20] = 229
sineval[21] = 228
sineval[22] = 227
sineval[23] = 224
sineval[24] = 220
sineval[25] = 216
sineval[26] = 212
sineval[27] = 206
sineval[28] = 200
sineval[29] = 194
sineval[30] = 187
sineval[31] = 179
sineval[32] = 171
sineval[33] = 163
sineval[34] = 154
sineval[35] = 146
sineval[36] = 137
sineval[37] = 128 ;180 degree
 
Pretty good. It's the first 180 degrees. Now you can draw the remaining for the next 180 degrees and you are set to generate a sine wave with a certain frequency. :)
 
I second Brian's comments. Spending a few minutes with google or a book, would allow anyone with a working knowledge of C and GCSE level mathematics to get a simple sine table running. In the slim probability you are not a troll Ritesh, I would suggest getting some books on C to build up a working knowledge such that you can tackle such projects on your own. Sams Teach Yourself C in 21 Days was a book I read when I was younger.
 
Status
Not open for further replies.

Latest threads

Back
Top