Skip to content

Instantly share code, notes, and snippets.

@monsonite
Created October 3, 2015 14:58
Show Gist options
  • Save monsonite/bf787fb4fa620330ae54 to your computer and use it in GitHub Desktop.
Save monsonite/bf787fb4fa620330ae54 to your computer and use it in GitHub Desktop.
A 50Hz sinusoid generator for the Open Inverter Project
/*
50Hz 8-bit sinusoid pwm driver for Open Inverter Project
Complimentary pwm available on Digital 3 and Digital 11
Ken Boak & Trysatn Lea #bothyHack - September 2015
*/
int PWMval = 128;
// Use XL to calculate a sinusoidal look up table - stored in this array
int Sin_table[255] = {
128,
131,
134,
137,
140,
143,
146,
149,
153,
156,
159,
162,
165,
168,
171,
174,
177,
180,
182,
185,
188,
191,
194,
196,
199,
201,
204,
207,
209,
211,
214,
216,
218,
220,
223,
225,
227,
229,
231,
232,
234,
236,
238,
239,
241,
242,
243,
245,
246,
247,
248,
249,
250,
251,
252,
253,
253,
254,
254,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
254,
254,
253,
253,
252,
251,
251,
250,
249,
248,
247,
245,
244,
243,
241,
240,
238,
237,
235,
233,
232,
230,
228,
226,
224,
222,
219,
217,
215,
213,
210,
208,
205,
203,
200,
198,
195,
192,
189,
187,
184,
181,
178,
175,
172,
169,
166,
163,
160,
157,
154,
151,
148,
145,
142,
139,
135,
132,
129,
126,
123,
120,
116,
113,
110,
107,
104,
101,
98,
95,
92,
89,
86,
83,
80,
77,
74,
71,
68,
66,
63,
60,
57,
55,
52,
50,
47,
45,
42,
40,
38,
36,
33,
31,
29,
27,
25,
23,
22,
20,
18,
17,
15,
14,
12,
11,
10,
8,
7,
6,
5,
4,
4,
3,
2,
2,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
2,
2,
3,
4,
5,
6,
7,
8,
9,
10,
12,
13,
14,
16,
17,
19,
21,
23,
24,
26,
28,
30,
32,
35,
37,
39,
41,
44,
46,
48,
51,
54,
56,
59,
61,
64,
67,
70,
73,
75,
78,
81,
84,
87,
90,
93,
96,
99,
102,
106,
109,
112,
115,
118,
121,
124,
};
void setup() {
// declare following pins for PWM output:
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(3,LOW); // Set 3 and 11 to initial states
digitalWrite(11,HIGH);
TCCR2A = 0xE3; // Normal pwm on digital 11, inverted pwm on digital 3
TCCR2B = 0x01; // 7.8kHz pwm
}
void loop() {
int k = 255;
while (k--)
{
PWMval = Sin_table[k];
OCR2A = PWMval;
OCR2B = PWMval;
delayMicroseconds(77); // Delaying by 77uS will give 50Hz waveform
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment