Code:
FOR PERIOD_COUNT = 1 TO 187 '20mS PERIOD
IF PERIOD_COUNT > PULSE THEN 'MAKE LINE LOW FOR REST OF 20mS PERIOD
SERVO = 0 'MAKE SURE SERVO SIGNAL IS NOW LOW
ELSE
SERVO = 1 'SERVO SIGNAL LINE HIGH FOR 1.0 OR 2.0mS
ENDIF
NEXT PERIOD_COUNT
this for next loop takes 45us to execute when PERIOD_COUNT is a word var even without the pause, it severly limits resolution
if you do it this way the resolution is 1 uS or thereabouts
Code:
'****************************************************************
'* Name : hobby_servo.BAS *
'* Author : richard *
'* Notice : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 11/19/2016 *
'* Version : 1.0 *
'* Notes : 12f683 *
'* : *
'****************************************************************
#CONFIG
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _CP_OFF
#ENDCONFIG
DEFINE OSC 8 'LETS PBP KNOW WE WILL BE RUNNING AT 8MHZ
include "dt_ints-14.bas"
include "REENTERPBP.bas"
'PIN DEFENITIONS
'GP2 USED TO DRIVE SERVO (SIGNAL LINE)
'GP3 USED FOR MCLR AND ICSP PROGRAMMING
'GP4 USED FOR TRIGGER INPUT
'GP5 USED FOR LED
'SET UP THE REGISTERS
OSCCON = %01110001 '8MHZ INTERNAL CLOCK USED
CMCON0 = %00000111 'CIN PINS ARE I/O, COUT PIN IS I/O
OPTION_REG=%01010111 ;set prescaler and assign it to tmr0
define NO_WDT 1
TRISIO = %00011011 'GP5 AND GP2 ARE OUTPUTS, THE REST ARE INPUTS
ANSEL = 0 'NO ANALOG PORTS - ALL DIGITAL
WPU = %00010000 'GP4 WEAK PULL UP ENABLED. GP3 WEAK PULL UP AUTOMATICALLY ENABLED
'AS THE PIN IS DEFINED AS MCLR
@Timer1 = TMR1L ; map timer1 registers to a word variable
Timer1 VAR WORD EXT
t1con=0
wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
;wsave VAR BYTE $70 SYSTEM ' alternate save location for W
' if using $70, comment wsave1-3
' --- IF any of these three lines cause an error ?? ------------------------
' Comment them out to fix the problem ----
' -- Which variables are needed, depends on the Chip you are using --
wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
'Wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3
' --------------------------------------------------------------------------
asm
INT_LIST macro
INT_Handler TMR0_INT, _serv, PBP, yes
INT_Handler TMR1_INT, _st, PBP, yes
endm
INT_CREATE
ENDASM
'PIN ALIASES
SERVO VAR GPIO.2
TRIGGER VAR GPIO.4
LED VAR GPIO.5
'VARIABLES
servo_t VAR word 'pulse TIME in us
SERVO = 0 'MAKE SURE NO SIGNAL
LED = 0 'MAKE SURE LED IS OFF
@ int_enable TMR0_INT
@ int_enable TMR1_INT
MAIN:
IF TRIGGER = 1 THEN 'CLOSE THE DAMPER
servo_t = 2000 '2.0mS PULSE TO SERVO
LED = 1 'TURN ON LED
ELSE 'OPEN THE DAMPER
servo_t = 1000'1.0mS PULSE TO SERVO
LED = 0 'TURN OFF LED
ENDIF
goto main
END
serv:
tmr0=tmr0+100
Timer1 = -servo_t
pir1.0=0
t1con=$11
SERVO =1
@ int_return
st:
t1con=0
servo=0
@ int_return
Bookmarks