Code:
' Name : Interface card.pbp
' Compiler : PICBASIC PRO Compiler 2.6A
' Assembler : MPASM
' Target PIC : 16F1947
' Hardware : PIMill Interface
' Oscillator : internal
' Keywords :
' Description : PICBASIC PRO program to interface parallel port
' to 4 stepper drives and limit switches
'
'
'
'
' Configure ports
'
'
ANSELA = %00000000 'port A all digital
ANSELE = %00000000 'port E all digital
ANSELF = %00000000 'port F all digital
ANSELG = %00000000 'port G all digital
CM1CON0 = %00000000 'No comparaters
CM1CON1 = %00000000
CM2CON0 = %00000000
CM2CON1 = %00000000
' preload latches before setting direction
LATB = %00000000
LATC = %00000000
LATD = %00010000 'LED's 0 for red, 1 FOR GREEN should start red
LATF = %00001111 ' low nibble is drive enable. 1 disables
LATG = %00000000
' I/O direction
TRISA = %11111111'step and dir inputs
TRISB = %11000001'7-0 icsp,icsp,h4o,h3o,h2o,h1o,estopO,enable In
TRISC = %00000000'step and dir outputs
TRISD = %00000000'led6,i2c,i2c,led5,led4,led3,led2,led1
TRISE = %11111111'ls4,lsh4.ls3,lsh3,ls2,lsh2,ls1,lsh1
TRISF = %00010000'spindle dir,servo out,dac out,flood input,4en,3en,2en,1en
TRISG = %11100111'x,x,icsp/mclr,rly2,rly1,Rx2,Tx2,DI
'REMOVE THIS LINE IF NOT SETTING THE CONFIGS IN CODE SPACE
@ __config _CONFIG1,_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
@ __config _CONFIG2,_WRT_OFF & _VCAPEN_OFF & _PLLEN_ON & _STVREN_OFF & _BORV_25 & _LVP_OFF
'Internal osc setup
OSCCON = %01110000 'clock is 8 meg. turn on PLL for 32 meg clock
DEFINE OSC 16 ' 32 meg not allowed according to book. be sure to double things that need this
'Variables
LED0 VAR LATD.0 ' Assign name "LED" to PORTD
LED1 VAR LATD.1
LED2 VAR LATD.2
LED3 VAR LATD.3
LED4 VAR LATD.4
LED5 VAR LATD.7
CNT VAR BYTE
ENALL VAR PORTB.0
BSRSTORE VAR BYTE
EN1 VAR LATF.0
EN2 VAR LATF.1
EN3 VAR LATF.2
EN4 VAR LATF.3
ASM
#DEFINE EN1A LATF,0 'stuff for testing
ENDASM
wsave VAR BYTE $70 SYSTEM
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
'INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR2_INT, _FiveMicroSec, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
'5.0 uSec OR 200,000 Hz INTERRUPTS
T2CON = %00000100 'Prescaler = 1:1, TMR2ON
'PRELOAD 223 "FROM MULTI-CALC"
PRELOAD VAR byte
PRELOAD = 239 ' FROM TESTING TO FIND THE RIGHT NUMBER
TMR2 = PRELOAD
@ INT_ENABLE TMR2_INT ; enable Timer 2 interrupts
Main: 'DO WHATEVER YOU WANT HERE
IF CNT=200 THEN
TOGGLE EN2
TOGGLE LED1
CNT=0
ENDIF
TOGGLE LED2
GOTO Main
'---[TMR2 - interrupt handler]--------------------------------------------------
FiveMicroSec:
TMR2 = PRELOAD 'THIS LINE
TOGGLE EN1 'AND THIS LINE
CNT =CNT+1 'WITH THIS CREATE A INT ROUTINE EVERY 4.99uS
' LATC = PORTA 'ADDING THIS LINE CHANGES THE TIME TO 5.245uS!!
' SO WHY DOES THIS CHANGE THE TIMING?@ INT_RETURN
Now It doesn't make sense to me. I am running a 32mHz clock. 5 uS interupt should takes 160 clock cycles, or 40 asm instructions. my interupt routine has 3 PBP instructions when working and 4 when I add the LATC=PORTA line. I can't imagine that would be 40 lines of ASM. I have tried to do this with a pure ASM handler, but I can only get it to work if I toggle LATA instead of LATF. I am sure this is a bank select issue. I can work through that if I must, but I would really like to use PBP instead.
Bookmarks