13. Interfacing Motor DC 
Dalam pengendalian motor dc teknik yang digunakan adalah dengan 
menggunakan PWM agar motor dc dapat bergerak cepat atau lambat. Sementara 
untuk mengubah arah putaran motor dc adalah dengan membalikkan polaritas 
tegangan yang digunakan 
1. Pelaksanaan 
a. Hubungkan modul motor dc dengan minimum sistem pada PORTC 
b. Ubah register PORTC sebagai output 
c. Sesuaikan timer0 pada CodeWizard seperti berikut
d. Gunakan program berikut 
#include <mega8535.h> 
#include <delay.h> 
// Alphanumeric LCD functions 
#include <alcd.h> 
#include <stdio.h> 
unsigned char lcd[16]; 
unsigned char data,pwm,a; 
// Timer 0 overflow interrupt service routine 
interrupt [TIM0_OVF] void timer0_ovf_isr(void) 
{ 
// Place your code here 
a++; 
if(a<=pwm){PORTC.1=0;} 
else {PORTC.1=1;} 
} 
#define ADC_VREF_TYPE 0x20 
// Read the 8 most significant bits 
// of the AD conversion result 
unsigned char read_adc(unsigned char adc_input) 
{ 
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); 
// Delay needed for the stabilization of the ADC input voltage 
delay_us(10); 
// Start the AD conversion 
ADCSRA|=0x40; 
// Wait for the AD conversion to complete 
while ((ADCSRA & 0x10)==0); 
ADCSRA|=0x10; 
return ADCH; 
}
Void main() 
{ 
PORTC=0x03; 
DDRC=0x03; 
while (1) 
{ 
// Place your code here 
data=read_adc(0); 
if(data>=138) {pwm=(data-128)*2+1;PORTC.0=0;} 
else 
if(data<=118) {pwm=255-(data*2);PORTC.0=1;} 
else {pwm=0;} 
sprintf(lcd,"PWM = %3d",pwm); 
lcd_gotoxy(0,0); 
lcd_puts(lcd); 
sprintf(lcd,"ADC = %3d",data); 
lcd_gotoxy(0,1); 
lcd_puts(lcd); 
} 
}

13. interfacing motor dc

  • 1.
    13. Interfacing MotorDC Dalam pengendalian motor dc teknik yang digunakan adalah dengan menggunakan PWM agar motor dc dapat bergerak cepat atau lambat. Sementara untuk mengubah arah putaran motor dc adalah dengan membalikkan polaritas tegangan yang digunakan 1. Pelaksanaan a. Hubungkan modul motor dc dengan minimum sistem pada PORTC b. Ubah register PORTC sebagai output c. Sesuaikan timer0 pada CodeWizard seperti berikut
  • 2.
    d. Gunakan programberikut #include <mega8535.h> #include <delay.h> // Alphanumeric LCD functions #include <alcd.h> #include <stdio.h> unsigned char lcd[16]; unsigned char data,pwm,a; // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { // Place your code here a++; if(a<=pwm){PORTC.1=0;} else {PORTC.1=1;} } #define ADC_VREF_TYPE 0x20 // Read the 8 most significant bits // of the AD conversion result unsigned char read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCH; }
  • 3.
    Void main() { PORTC=0x03; DDRC=0x03; while (1) { // Place your code here data=read_adc(0); if(data>=138) {pwm=(data-128)*2+1;PORTC.0=0;} else if(data<=118) {pwm=255-(data*2);PORTC.0=1;} else {pwm=0;} sprintf(lcd,"PWM = %3d",pwm); lcd_gotoxy(0,0); lcd_puts(lcd); sprintf(lcd,"ADC = %3d",data); lcd_gotoxy(0,1); lcd_puts(lcd); } }