Hi guys, I am currently building a projct which uses the analoge input from a 3 axis accelerometer and turns this into a proportional servo output.
I have now managed to get y chip to read ADC input from one input and to put out a servo ouput using a FOR loop.
Now the problem I am having is the FOR loop doesnt seem to be interuptable. So the servo will only respond to a new input once it has reached its current destination. I want to find a way to have the servo change direction before it reaches its end destination.
The second issue i have is the programe seems to freeze at times. The LCD does not update and the programe does not recognise any change in input for a while before suddenly snapping back into life.
Attached below is my code
Code:
'***********************************************************************
'*college project to control 2 servo motors from a 3 axis accelerometer*
'***********************************************************************
'*****27/02/09***********************************************************
' V 0.2
' For loop responding to input of ADC
' active ADC inputs displayed on line 2 of lcd
' servo position shown on line 1 of LCD
'***********************************************************************
'** Shows the Connections on the ports of the PIC **********************
'***********************************************************************
'
'A0 = X-AXIS
'A1 = Y-AXIS
'A2 = Z-AXIS
'A3 =
'A4 =
'A5 =
'
'BO = LCD INTERFACE
'B1 = LCD INTERFACE
'B2 = LCD INTERFACE
'B3 = LCD INTERFACE
'B4 = LCD INTERFACE
'B5 = LCD INTERFACE
'B6 = LCD INTERFACE
'B7 = LCD INTERFACE
'
'C0 = SERVO 1
'C1 = SERVO 2
'C2 =
'C3 =
'C4 =
'C5 =
'C6 =
'C7 =
'***********************************************************************
'****************** DEFINE ADCIN PARAMETERS*****************************
'***********************************************************************
DEFINE ADC_BITS 8 'SETS NUMBER OF BITS IN RESULTS
DEFINE ADC_CLOCK 3 'SET CLOCK SOURCE
DEFINE ADC_SAMPLUS 50 'SET SAMPLING TIME IN US
DEFINE LCD_DREG PORTB 'Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 4 'Define first pin of portb connected to LCD DB4
DEFINE LCD_RSREG PORTB 'Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 3 'Define Portb pin used for RS connection
DEFINE LCD_EREG PORTB 'Define PIC prot used for E line of LCD
DEFINE LCD_EBIT 0 'Define PortB pin used for E connection
DEFINE LCD_BITS 4 'Define the 4 bit communication mode to LCD
DEFINE LCD_LINES 2 'Define using a 2 line LCD
DEFINE LCD_COMMANDUS 2000 'Define delay between sending LCD commands
DEFINE LCD_DATAUS 50 'Define delay time between data sent.
SERVOY VAR PORTC.0
SERVOX VAR PORTC.1
YAXIS VAR WORD
XAXIS VAR WORD
ZAXIS VAR WORD
XPRESET VAR WORD
YPRESET VAR WORD
ZPRESET VAR WORD
B0 VAR WORD
B1 VAR WORD
B2 VAR WORD
B3 VAR WORD
TRISA = %00000011 'SETS INPUTS/OUTPUTS FOR BANK A
TRISB = %00000000 'SETS INPUTS/OUTPUTS FOR BANK B
TRISC = %00000000 'SETS INPUTS/OUTPUTS FOR BANK C
ADCON1 = %00000110 'SETS THE ANALOGUE/DIGITAL INPUTS TO BANK A
PORTA = %00000000 'SETS THE INITIAL VALUE OF BANK A
PORTB = %00000000 'SETS THE INITIAL VALUE OF BANK B
PORTC = %00000000 'SETS THE INITIAL VALUE OF BANK C
'**************************************************************************
' CENTER THE SERVO
' ****************
' Pause for one second to allow initalisations
' Clear the LCD display
' Center Servo 1 and then servo 2
'
'**************************************************************************
PAUSE 1000
LCDOUT "STARTING"
pause 1000
LCDOUT $FE, 1
CENTER: 'NEW SUBROUTINE FOR SETTING THE SERVO
FOR B0 = 1 TO 100 'MOVES THE SERVO FROM ITS 0 TO ITS 100 MARK
PULSOUT SERVOY, 150 '
PAUSE 20 '
NEXT
FOR B1 = 1 TO 100 'Pans the servo from 0 to a halfway point of 100
PULSOUT SERVOX, 150 '
PAUSE 20 '
NEXT
'**************************************************************************
' setup ADC
' *********
' reads the ADC ports and sets the initial values for calculations
'**************************************************************************
STARTUP:
ADCIN 0, B2 ' reads in the ADC on PORT A.0 and sends it to memory location B2
ADCIN 1, B3 ' reads in the ADC on PORT A.1 and sends it to memory location B3
gosub servomove
'**************************************************************************
' Servo calculations
' ******************
'
'
'**************************************************************************
MAIN:
ADCIN 0, B2 ' Reads in the PORT A.0 ADC AND STORES THE VALUE IN B2
ADCIN 1, B3 ' READS IN THE PORT a.1 ADC AND STORES THE VALUE IN B3
XPRESET = B2 + 50
If Xpreset > 92 then
goto positive
endif
if xpreset < 92 then
goto negative
endif
positive:
FOR B0 = B0 TO XPRESET
PULSOUT SERVOY, B0
PAUSE 20
gosub servomove
NEXT
goto main
negative:
FOR B0 = B0 TO xpreset STEP -1
PULSOUT SERVOY, B0
PAUSE 20
gosub servomove
NEXT
goto main
'
'
''*******************************************************************************************************************
' SERVOMOVEMENT CONTROL
' *********************
' MOVES BOTH SERVOS FOR BOTH X AND Y MOVEMENT
' CREATES THE NEW X AND Y PRESETS FOR THE CALCULATIONS
' UPDATES THE LCD DISPLAY
'*******************************************************************************************************************
SERVOMOVE:
'
' PULSOUT SERVOY, B1 ' SENDS THE NEW VALUE FOR SERVO Y
'
'
' PULSOUT SERVOX, B0 ' SENDS THE NEW VALUE FOR SERVO X
'
'
ADCIN 0, B2 ' Reads in the PORT A.0 ADC AND STORES THE VALUE IN B2
ADCIN 1, B3 ' READS IN THE PORT a.1 ADC AND STORES THE VALUE IN B3
LCDOUT $FE, 2 ' SENDS THE COMMAND TO THE LCD TO MOVE TO THE START OF THE FIRST LINE
LCDOUT "Servo Pos= ",DEC3 B0 ' SENDS THE DATA FOR THE X AXIS TO THE LCD ON THE FIRST LINE
LCDOUT $FE, $C0 ' MOVES THE CURSUR OF THE LCD TO THE START OF THE SECOND LINE
LCDOUT "Y = ",DEC3 B2, " X = ",DEC3 B3 ' SENDS THE DATA FOR THE Y AXIS TO THE LCD ON THE SECOND LINE
RETURN ' RETURNS TO THE PREVIOUS BLAAAAAAAAAAAAAAAAAAAAAAH
GOTO MAIN
END
Bookmarks