Anda di halaman 1dari 4

SPWM fungsional tanpa tabel-sinusiodal dengan arduino – Semesin https://www.project.semesin.com/2020/06/10/spwm-fungsional-tanpa-tab...

1 dari 4 16/03/2024 21:30


SPWM fungsional tanpa tabel-sinusiodal dengan arduino – Semesin https://www.project.semesin.com/2020/06/10/spwm-fungsional-tanpa-tab...

1 #define pinSPWM_A 9
2 #define pinSPWM_B 10
3
4 #define frekuensiOutput 50
5 #define frekuensiSPWM 1000

2 dari 4 16/03/2024 21:30


SPWM fungsional tanpa tabel-sinusiodal dengan arduino – Semesin https://www.project.semesin.com/2020/06/10/spwm-fungsional-tanpa-tab...

6
7 volatile byte indexSPWM;
8 volatile float output;
9 int limitOutput;
10
11 void setup() {
12 pinMode(pinSPWM_A, OUTPUT);
13 pinMode(pinSPWM_B, OUTPUT);
14
15 Serial.begin(9600);
16 Serial.println(F("SPWM fungsional dengan a
17 Serial.println(F("https://www.project.seme
18 Serial.println();
19
20 TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (
21
22 long cycles = (F_CPU / frekuensiSPWM / 2);
23 uint8_t clockSelectBits;
24
25 if (cycles < 0xFF) clockSelect
26 else if ((cycles >>= 3) < 0xFF) clockSelec
27 else if ((cycles >>= 3) < 0xFF) clockSelec
28 else if ((cycles >>= 2) < 0xFF) clockSelec
29 else if ((cycles >>= 2) < 0xFF) clockSelec
30 else cycles = 0xFF ‐ 1, clockSelect
31
32 TCCR1B = (1 << WGM13) | clockSelectBits;
33 ICR1 = cycles;
34 TIMSK1 = _BV(TOIE1);
35 OCR1A = 0;
36 OCR1B = 0;
37
38 limitOutput = cycles;
39 }
40
41 void loop() {
42
43 output = limitOutput / 2;
44
45 }
46
47 ISR(TIMER1_OVF_vect)
48 {
49 int duty = 1.0 * sin(2.0 * PI * indexSPWM
50
51 if (duty >= 0)
52 {
53 OCR1AL = duty;
54 OCR1BL = limitOutput + 1;
55 }
56 else
57 {
58 OCR1AL = limitOutput + 1;
59 OCR1BL = 0 ‐ duty;

3 dari 4 16/03/2024 21:30


SPWM fungsional tanpa tabel-sinusiodal dengan arduino – Semesin https://www.project.semesin.com/2020/06/10/spwm-fungsional-tanpa-tab...

60 }
61
62 indexSPWM++;
63 indexSPWM %= frekuensiSPWM / frekuensiOutp
64 }


4 dari 4 16/03/2024 21:30

Anda mungkin juga menyukai