You are on page 1of 2

DC Motor interfacing with Microcontrollers tutorial: Programming Microcontroller

Assembly programming
CODE:

L293D_A L293D_B L293D_E Main:

equ P2.0 equ P2.1 equ P2.2 org 0H acall acall acall acall acall acall acall acall sjmp rotate_f delay break delay rotate_b delay break delay Main L293D_A L293D_B L293D_E

;L293D A - Positive of Motor ;L293D B - Negative of Motor ;L293D E - Enable pin of IC

;Rotate motor forward ;Let the motor rotate ;Stop the motor ;Wait for some time ;Rotate motor backward ;Let the motor rotate ;Stop the motor ;Wait for some time ;Do this in loop ;Make Positive of motor 1 ;Make negative of motor 0 ;Enable to run the motor ;Return from routine ;Make positive of motor 0 ;Make negative of motor 1 ;Enable to run the motor ;Return from routine ;Make Positive of motor 0 ;Make negative of motor 0 ;Disable the o/p ;Return from routine ;Some Delay

rotate_f: setb clr setb ret rotate_b: clr setb setb ret break: clr clr clr ret delay: back: back1: here: mov mov mov djnz djnz djnz ret r7,#20H r6,#FFH r5,#FFH r5, here r6, back1 r7, back L293D_A L293D_B L293D_E L293D_A L293D_B L293D_E

C programming
CODE:

#include <AT89X51.H>#define L293D_A P2_0 //Positive of motor #define L293D_B P2_1 //Negative of motor #define L293D_E P2_2 //Enable of L293D // Function Prototypes void rotate_f(void); void rotate_b(void); void breaks(void); void delay(void); void main(){ while(1){ rotate_f(); delay(); breaks(); delay(); rotate_b(); delay(); breaks(); delay(); } } void rotate_f(){ L293D_A = 1; L293D_B = 0; L293D_E = 1; } void rotate_b(){ L293D_A = 0; L293D_B = 1; L293D_E = 1; } void breaks(){ L293D_A = 0; L293D_B = 0; L293D_E = 0; } //Make positive of motor 1 //Make negative of motor 0 //Enable L293D //Forward run funtion //Backward run function //Motor stop function //Some delay //Our main function //Infinite loop //Run forward //Some delay //Stop //Some delay //Run Backwards //Some delay //Stop //Some delay //Do this infinitely

//Make positive of motor 0 //Make negative of motor 1 //Enable L293D

//Make positive of motor 0 //Make negative of motor 0 //Disable L293D

void delay(){ //Some delay... unsigned char i,j,k; for(i=0;i<0x20;i++) for(j=0;j<255;j++) for(k=0;k<255;k++); }

You might also like