Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Juan Esteban Paz
Published © CC BY

Joystick Controlled DC Motor w/H-Bridge

A very simple example of Hbridge DC motor control, Joystick ADC reading and offset calibration.

BeginnerProtip30 minutes3,222
Joystick Controlled DC Motor w/H-Bridge

Things used in this project

Story

Read more

Schematics

PSOC Workspace

Code

Main.c

C/C++
mainc
#include <project.h>
//ADC param
#define CHA  (0u)

int adcCountsA=0,dutyc;
double mVoltsA,mot,joystickzero;
//Motor param
#define CCW()ControlReg_Write(0x01);
#define CW() ControlReg_Write(0x02);

#define PWMmaxcomp 254 //Period 255

void motoroutput();
CY_ISR(ADCEOC){
    adcCountsA = ADC_GetResult16(CHA);
    mVoltsA = ADC_CountsTo_Volts(CHA,adcCountsA); 
    dutyc=(mVoltsA-joystickzero)/joystickzero*PWMmaxcomp;
    mot=dutyc;
    motoroutput();
}   
void motoroutput(){
	//CMD -4000 : 4000  PWM 0-65535
 	if((mot>=0)&&(mot<PWMmaxcomp)){
        PWMSpd_WriteCompare(mot); 
        CCW();       
	}
	else if ((mot<0)&&(mot>-PWMmaxcomp)){        
		PWMSpd_WriteCompare(-mot); 
        CW();
	}	    
}

int main(){
    PWMSpd_Start();   
    CyGlobalIntEnable; 
    ISRADCEOConv_StartEx(ADCEOC);
    ADC_Start();
    ADC_StartConvert();
    CyDelay(50);
    joystickzero=mVoltsA;
    
    CyGlobalIntEnable;/* Enable global interrupts */
    for(;;)
    {
    
    }
}

Credits

Juan Esteban Paz

Juan Esteban Paz

13 projects • 27 followers
Mechanical and Electrical Engineer enjoy working with PSOC

Comments