Sign In

Trouble In Controlling Servo Motor With Adxl337 Accelerometer

Ibrahim Maassarani ibrahim.maassarani@gmail.com Lebanon

Hardware: Pic16f877a Wired On Board Software: Assembly, Mp-lab

there is something wrong with the calibrations .. using it is supposed to turn towards 90 degrees more and more as the voltage increases above 1.65v .. turn towards -90 degrees more and more as the voltage decreases and is less then 1.65v .. and when it is 1.65v it should turn to 0 degree

i have wired a 1.65 DC voltage source to the pic and saved the value after i converted it to digital in a file .. and started comparing the input voltage from the accelerometer with the saved value but there is something wrong

here is the code below

Images

trouble in controlling servo motor with adxl337 accelerometer

Program

#INCLUDE 	"P16F877A.INC" 

__CONFIG 	_XT_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF

	CBLOCK	0X20
	A1, A2, A3
	B1, VREF
	ENDC 
	
	ORG		0X00
	GOTO	MAIN 

MAIN
	CALL	SETUP
	CALL	SETUP_PWM1

REP
	CALL	A2D_CHANNEL_1
	MOVWF	VREF
	CALL	A2D_CHANNEL_0
	SUBWF	VREF

	BTFSS	STATUS, C
	GOTO	$+7
	BTFSS	STATUS, Z
	GOTO	$+3
	CALL	MOTOR_STOP
	GOTO	REP
	CALL	MOTOR_LEFT
	GOTO	REP
	CALL	MOTOR_RIGHT
	GOTO	REP

SETUP_PWM1
	BSF		STATUS, RP0
	MOVLW	D'255'
	MOVWF	PR2
	BCF		TRISC, 2
	BCF		STATUS, RP0
	MOVLW	B'00000110'
	MOVWF	T2CON
	MOVLW	B'00001100'
	MOVWF	CCP1CON
	RETURN

A2D_CHANNEL_0
	MOVLW	B'01000001'
	MOVWF	ADCON0
	CALL	A2D_DELAY
	BSF		ADCON0, GO
	BTFSC	ADCON0, GO
	GOTO	$-1
	MOVF	ADRESH, W
	RETURN
	
A2D_CHANNEL_1
	MOVLW	B'01001001'
	MOVWF	ADCON0
	CALL	A2D_DELAY
	BSF		ADCON0, GO
	BTFSC	ADCON0, GO
	GOTO	$-1
	MOVF	ADRESH, W
	RETURN

A2D_DELAY
	MOVLW	D'5'
	MOVWF	B1
	DECFSZ	B1, F
	GOTO	$-1
	RETURN

MOTOR_STOP
	CALL	A2D_CHANNEL_0
	MOVWF	CCPR1L
	RETURN

MOTOR_LEFT
	CALL	A2D_CHANNEL_0	
	MOVWF	CCPR1L
	RETURN

MOTOR_RIGHT
	CALL	A2D_CHANNEL_0
	MOVWF	CCPR1L	
	RETURN

SETUP
	CLRF	PORTA
	CLRF	PORTB
	CLRF	PORTC
	CLRF	PORTD
	CLRF	PORTE
	BSF 	STATUS, RP0
	MOVLW	B'00000000'
	MOVWF	ADCON1
	MOVLW	B'00000111'
	MOVWF	CMCON
	MOVLW	B'00000011'
	MOVWF	TRISA
	CLRF	TRISC
	BCF 	STATUS, RP0
	RETURN

END

Replies

R Avinash
2016-05-24 14:38:54
So what exactly is happening when you are running this whole setup? Is the program and hardware not at all working?
R Ibrahim Maassarani
2016-05-26 07:39:06
at 1.6 v .. it is not rotating to 0 degree .. it rotate to -27 degrees
R Avinash Gupta
2016-05-26 11:31:22
Ok. Are you talking about the real servo or proteus simulation?


If real servo then specify its model number and manufacturer's name.

If you are talking about the simulated servo then you need to change its settings by right clicking on it and going to appropriate menu item.
R Ibrahim Maassarani
2016-05-27 09:33:27
ok .. thanks .. i will try that
i haven't got to the hardware part yet
R Ibrahim Maassarani
2016-05-27 22:31:57
pleeeaseeee ... does anyone even have a C code for this..

control a servo with the adxl335 using pic16f877a
R Avinash Gupta
2016-05-28 02:30:43
At what frequency you pic is running? I mean what crystal oscillators you have used?
R Ibrahim Maassarani
2016-05-28 20:20:18
4 mhz ... i have added two capacitors in parallel .. a 400 nF and a 7uF and it worked .. but there is still something wrong with my program because it is not turning correctly based on the reference voltage - if it is above, below or equal to 1.6V

Post a reply to Ibrahim Maassarani

Think you can help Ibrahim Maassarani ? Then post your thoughts that might help Ibrahim Maassarani. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.