'**************************************************************** '* Name : BetServo.BAS * '* Author : Bjoern Ludwar * '* Notice : Copyright (c) 2006 * '* : All Rights Reserved * '* Date : 12/12/2006 * '* Version : 1.2.1 * '* Notes : requires 4Mhz clock * '* : DAC added * '**************************************************************** @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF ' original config line in 16f690 INC file define OSC 4 Include "modedefs.bas" ' lower pot uses PORTA.0 -> end position ' upper pot uses PORTA.1 -> speed TASTE1 var PORTA.3 'red button TASTE2 var PORTA.4 'black button 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 N var byte 'used for speed regulation TASTEH VAR BYTE 'memorizes if key was pressed in 'continous' DACOUT var word 'holds word for DAC output init: out = 0 TRISA = %11111111 'make RA0,1,3, and 4 inputs TRISC = %11000011 'make RC2,3,4, and 5 outputs ANSEL = %00000011 'make all pins except RA0&1 digital ANSELH = %00000000 ADCON1.7 = 1 '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 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 'serout PORTB.5,N2400,["POTI1 ",#ENDPOS," POTI2 ",#SPEED,10] 'button pressed? IF TASTE1 = 0 THEN ramp IF TASTE2 = 0 THEN continous goto main '*** end of main program *** ramp: 'if red button was pressed ramp to set end point with set speed POS = 0 speed = speed/6 'SPEED = SPEED*SPEED 'scale speed to something 'SPEED = DIV32 255 'more usefull 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 'hold for a while for n=0 to 60 gosub setpos 'output pulse PAUSE 8 'wait a bit next GOTO main continous: 'if black button is pressed give a small pulse until pressed again repeat until taste2=1 'wait for release of black key repeat period.byte0 = 95 'move a bit GOSUB SETPOS PAUSE 8 for n=0 to 150 'set back to min position and wait period.byte0 = 82 GOSUB SETPOS PAUSE 8 next until taste2=0 'do until black key pressed again repeat until taste2=1 'wait for release of black key 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