Top Topics

74*245 Motor Driver

H-Bridge Driver

Simple PWM Gen.

Handy Method Measuring RPM

Measuring RPM via Photo reflector

Introduction to Robotic

DC, Stepper,and Servo Motor

Related Link

Microcontroller Tutorial

Computer Interface
Tutorial

.............more links

 

 

BACK

Source Code

// Özkal Özsoy Aug 2005
// PWMbug Robot Bug with PWM
// (Pulse Width Modulation)
// DC motor Speed control


#include <16F627.h>
#use delay(clock=4000000,RESTART_WDT)
#fuses INTRC_IO,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NOWDT


#define TONE_PIN PIN_B0
#define bekl 500

int pwm ;
int pwmval ;

void beep(period){
while((period--) != 0)
{
output_high(TONE_PIN);
delay_ms(1);
output_low(TONE_PIN);
delay_ms(1);
}
}

void bekle(period){
while(period > 0)
{
period--;
delay_ms(1);
}
}


void dur(period){
output_low(PIN_A3);
output_low(PIN_A2);
output_low(PIN_A1);
output_low(PIN_A0);
delay_ms(period);
}

#int_rtcc // This function is called every time
clock_isr() { // the RTCC (timer0) overflows (255->0).
//if(pwmval<100) pwmval++;
//else pwmval=1;
pwmval++;
}

void sola(period){
output_high(PIN_A3);
output_low (PIN_A2);
output_low (PIN_A1);
output_high(PIN_A0);
delay_ms(period);
dur(1);
}

void saga(period){
output_low (PIN_A3);
output_high(PIN_A2);
output_high(PIN_A1);
output_low (PIN_A0);
delay_ms(period);
dur(1);
}

void ileri(period){
output_low (PIN_A3);
output_high(PIN_A2);
output_low (PIN_A1);
output_high(PIN_A0);
delay_ms(period);
}

void geri(period){
output_high(PIN_A3);
output_low (PIN_A2);
output_high(PIN_A1);
output_low (PIN_A0);
delay_ms(period);
dur(1);
}


main() {

byte start;

set_rtcc(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

dur(0);
beep(100);

pwm=0;

while(1){


if(pwmval>=pwm) dur(0);
else ileri(1);

if(!input(PIN_B2)) {
dur(500);
beep(10);
geri(bekl);
saga(bekl);
}

if(!input(PIN_B1)) {
dur(500);
beep(10);
geri(bekl);
sola(bekl);
}

if((!input(PIN_B4)) && (pwm>0)) { // geri Tuþu = Hýz azaltma
beep(5);
pwm--;
}

if((!input(PIN_B3)) && (pwm<255)) { // Ýleri Tuþu = Hýz arttýrma
beep(5);
pwm++;
}

}

}

 

BACK