"I have noticed that even those who assert that everything is predestined and that
we can change nothing about it still look both ways before they cross the street"
-Stephen Hawking
this for next loop takes 45us to execute when PERIOD_COUNT is a word var even without the pause, it severly limits resolutionCode: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
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
Last edited by richard; - 19th November 2016 at 02:37.
Warning I'm not a teacher
I thank you Sir. MUCH better resolution, like you said. While I have used DT's Instant Interrupts a little,
I have not used them a lot. I'm starting to get a handle on how you did that. In fact, I may port this over
to an 18F2550 so I can use debug to watch everything to help my understanding.
Thanks again,
Andy
"I have noticed that even those who assert that everything is predestined and that
we can change nothing about it still look both ways before they cross the street"
-Stephen Hawking
it should be pretty straight forward to get it to work on a pic18.I may port this over
to an 18F2550 so I can use debug to watch everything to help my understanding.
trying to debug/watch isr's in action is an art form in itself especially when they need to run at full speed to function realistically or even at all.
try this version if you want to regulate the speed the servo
ps the osctune setting is for my particular chip others may/will need a different value ,see data sheet
I chose a value that gives me an accurate 2mS pulse for a servo_t value of 2000 / trial and error
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 * '* : now with speed control and osc tuned for accuracy * '**************************************************************** #CONFIG __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _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/mclr USED FOR TRIGGER INPUT 'GP4 vacant '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 OSCTUNE=3 ;trim best way closed con 2000 open con 500 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.3 LED VAR GPIO.5 'VARIABLES servo_t VAR word 'pulse TIME in us tick var byte mvflg var bit servo_t=open LED = !TRIGGER @ int_enable TMR0_INT @ int_enable TMR1_INT MAIN: IF TRIGGER = 1 THEN 'CLOSE THE DAMPER LED = 1 'TURN ON LED while servo_t < closed servo_t = servo_t + 20 mvflg=0 while !mvflg :wend wend servo_t = closed ELSE 'OPEN THE DAMPER LED = 0 'TURN ON LED while servo_t > open servo_t = servo_t - 20 mvflg=0 while !mvflg :wend wend servo_t = open ENDIF goto main END serv: tmr0=tmr0+97 Timer1 = ~servo_t;+50 alternative trim method pir1.0=0 t1con=$11 SERVO =1 tick=tick+1 if tick.0 then mvflg=1 @ int_return st: t1con=0 servo=0 @ int_return
Last edited by richard; - 20th November 2016 at 05:18.
Warning I'm not a teacher
Bookmarks