0% found this document useful (0 votes)
44 views5 pages

Lampiran: Script Progam

This document contains code for an Arduino program that controls pulse width modulation (PWM) output. It includes functions for setting the PWM frequency, reading voltage from a sensor, and outputting PWM signals at a specified duty cycle. The program allows adjusting the PWM frequency from 62.5 kHz to 16 MHz and setting the duty cycle from 1-255. It also contains datasheets for a transistor and MOSFET component.

Uploaded by

Bang Allen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Lampiran: Script Progam

This document contains code for an Arduino program that controls pulse width modulation (PWM) output. It includes functions for setting the PWM frequency, reading voltage from a sensor, and outputting PWM signals at a specified duty cycle. The program allows adjusting the PWM frequency from 62.5 kHz to 16 MHz and setting the duty cycle from 1-255. It also contains datasheets for a transistor and MOSFET component.

Uploaded by

Bang Allen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lampiran

Script Progam

Lampiran
Data Sheet Transistor
BD139

Lampiran
Data Sheet Mosfet
IRF540N

1. Script Progam
//file luar (library)
#include <util/delay.h>
#define PWMOUT 5
#define ACS A0
#define VSENSOR A1
void setup()
{
//==========================Ganti Frekuensi=====================
//pilihan frekuensi
//5,1 > 16Mhz
//5,8 > 2 Mhz
//5,64 > 250Khz
//5,256 > 62.5Khz
//5,1024 > 15625 Hz
setFreq(5,8);
}
int v;
int npwm;
void loop()
{
//===================Ganti PWM range 1 - 255======================
npwm=150;
//=============================================================
===========
pwm(npwm);
}
int vRead()
{
return analogRead(VSENSOR)/1023.0*5.0/0.176;
//4K7 22K
//26700
}
void pwm(int nPwm)
{
analogWrite(PWMOUT, nPwm);
}
void setFreq(int pin, int div)
{
//Base frequency 3,9,10,11 = 31250 Hz

//Base frequency 5,6 = 62500 Hz


//Divisor 5,6,9,10 = 1,8,64,256,1024
//Divisor 3,11 = 1,8,32,64,128,256,1024
//Pair (5,6),(9,10),(3,11)
//delay and millis uses 3,5,6,11
//servo uses 9,10
byte mode;
if(pin==5||pin==6||pin==9||pin==10){
switch(div){
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin==5||pin==6){
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin==3||pin==1){
switch(div){
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x07; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000| mode;
}
}

You might also like