Motor dan PWM

TUTORIAL JEMBATAN H-BRIDGE

1. Buatlah rangkaian seperti dibawah ini :






2. kemudian masukan program berikut :
#include <mega32.h>;
#include <delay.h>;

void motor(intdir, unsigned char pwm) {
if (pwm==0) {  // AUTO BREAK PROGRAM !!
PORTD.0=0;
OCR1A=0;
}
else {
if(dir==0) {
PORTD.0=0;
OCR1A=pwm;  // YANG DIATUR LEBAR PULSA POSITIF
}
else{
PORTD.0=1;
OCR1A=255-pwm; // YANG DIATUR LEBAR PULSA NEGATIF
}
}
}

void main(void)
{

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 15.625 kHz
// Mode: Ph. correct PWM top=0x00FF
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1;
TCCR1B=0x05;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

while (1)
{
// Place your code here
motor(0,255);
delay_ms(50);
motor(0,250);
delay_ms(50);
motor(1,250);
delay_ms(50);
motor(1,250);
delay_ms(50);
}
}


Komentar