'**************************************************************** '* Name : BetServo.BAS * '* Author : Bjoern Ludwar * '* Notice : Copyright (c) 2006 * '* : All Rights Reserved * '* Date : 26/03/2007 * '* Version : 1.4 * '* Notes : requires 4Mhz clock * '* : DAC added * '* : trigger in instead of second button * '* : Display added * '**************************************************************** @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF define OSC 4 'oscillator at 4 MHz Define LCD_DREG PORTB Define LCD_DBIT 4 Define LCD_RSREG PORTC Define LCD_RSBIT 7 Define LCD_EREG PORTC Define LCD_EBIT 6 ' lower pot uses PORTA.0 -> end position ' upper pot uses PORTA.1 -> speed ' PORTTB.4, 5, 6, 7 and RC6, 7 used for LCD TASTE1 var PORTA.3 'red button TRIG var PORTA.4 'trigger input OUT var PORTC.5 'PWM output CS var PORTC.2 'select ADC SCK VAR PORTC.3 'clock for ADC SDI VAR PORTC.4 'data for ADC POS var byte ENDPOS VAR BYTE PERIOD var word SPEED var WORD PTRIG VAR BYTE 'holds old trigger status DACOUT var word 'holds word for DAC output N var word 'for loops init: out = 0 TRISA = %11111111 'make RA0,1,3, and 4 inputs TRISB = %00000000 'make RB4,5,6,7 outputs TRISC = %00000000 'make RC2,3,4,5,6,7 outputs ANSEL = %00000011 'make all pins except RA0&1 digital ANSELH = %00000000 ADCON0 = %00000001 'analog results right justified ADCON1 = %00010000 'conversion with Fosc/8 DACOUT.byte1 = %00010000 'write to DACa, unbuffered, Vx1 DACOUT.Byte0 = %00000000 '(not used) Cs=1 : sck=0 : sdi=0 'init lines for ADC PTRIG = 0 'init trigger memory pause 500 LCDOUT $FE,1,"Betservo" LCDOUT $FE,$C0,"v. 1.4" Pause 1500 LCDOUT $FE,1 main: 'output pulse to set servo to min position (10*82ys=700ys pulse length) period.byte0 = 82 GOSUB SETPOS 'read potis ADCIN 0,endpos ADCIN 1,speed 'display values on LCD LCDOUT $FE,$02,"vel: ",DEC3 SPEED LCDOUT $FE,$C0,"pos: ",dec3 ENDPOS 'reset trigger memory (trigger has to return to low before active again) if trig=0 then ptrig=0 'button pressed? IF TASTE1 = 0 THEN ramp IF TRIG = 1 and PTRIG = 0 then ramp goto main '*** end of main program *** ramp: 'if red button was pressed ramp to set end point with set speed PTRIg = 1 'set trigger memory POS = 0 speed = speed/6 'scale speed IF SPEED = 0 then SPEED=1 'and make sure it is >0 'ramp up repeat pos = pos + speed if pos > endpos then pos = endpos 'calculate pulse length 82 to 218 times 10ys is the full range period = 18*pos 'same as period = 0.53*pos+82 period = period / 100 'makes a 0..255 value into period = period + 82 '82 to 127 times 10ys GOSUB SETPOS 'output pulse PAUSE 8 'wait a bit to slow down movement until pos >= endpos period.byte0 = 82 for n=1 to 512 gosub setpos next TASTE1 = 1 GOTO main SETPOS: 'subroutine that sets servo and sends position to ADC pulsout out,period.byte0 'set PWM signal cs=0 'and write to DAC SHIFTOUT sdi,sck,5,[dacout.byte1,period.byte0] cs=1 return