Code:
'PIC 16F917
'----------------------------------------------------------------------------
'Clock setup
Define OSC 20
'Interrupt setups
INTCON = %11000000 'Enable: Global interrupts, Peripheral interrupts
PIE1 = %01100000 'Enable: ADC interrupt, Receive interrupt
'ADC setups
ADCON0 = %10100001 'Rt justified, VRef + pin, Select ch.0, enable ADC
ADCON1 = %00100000 'Fosc / 32
'Pin setups
ANSEL = %00000001 'Port A.0 = analog input
TRISA = %11101001 'PortA: 1,2,4 = output, 0,3,5,6,7 = input
TRISB = %11000000 'PortB: 0-5 = LCD output, 6,7 = ICSP input
TRISC = %11111111 'PortC: 0-7 = input (defaults)
TRISD = %11111111 'PortD: 0-7 = input (defaults)
TRISE = %11111111 'PortE: 0-2 = input (defaults), 3-7 = N/A
'Usart setups
SPBRG = %00011001 'Set 9600 baud w/20MHz clock
TXSTA = %00100110 'Enable transmit, set BRGH high
RCSTA = %10010000 'Enable Usart, enable receiver
'----------------------------------------------------------------------------
'LCD setup (16 x 4 lines)
DEFINE LCD_LINES 4
Define LCD_BITS 4 '4 data interface lines
Define LCD_DREG PORTD 'LCD data port
Define LCD_DBIT 4 'LCD data bits, pic starting pin 0 or 4
Define LCD_RSREG PORTD 'LCD register select port
Define LCD_RSBIT 2 'LCD register select bit
Define LCD_EREG PORTD 'LCD enable port
Define LCD_EBIT 3 'LCD enable bit
'----------------------------------------------------------------------------
'Variable setups
RxFlag VAR PIR1.5
RxInt var PIE1.5
ADCFlag var PIR1.6
ADCInt var PIE1.6
ADCGo var ADCON0.2
SerInStr var byte(25)
X VAR BYTE ' Holds "$" start position
Y VAR BYTE ' Holds "=" mid position
Z VAR BYTE ' Holds "*" end position
Index VAR BYTE ' Index pointer
B0 VAR WORD
B1 VAR WORD
TorchLockVal var word
CNT VAR BYTE
CNT2 VAR BYTE
TorchHystVal var byte
FirstRun var bit
TorchVNow var byte
ControlIsTracking var PORTA.1
TorchUp var PORTA.2
TorchDn var PORTA.4
EnableIn var PORTA.5
InMotion var PORTE.1
'Initial variable values
TorchLockVal = 120
TorchHystVal = 0
firstrun = 1
pause 1000
LCDout $fe, 1
LCDout $fe, $03, "WELCOME TO THE"
Lcdout $fe, $42, "DOUBLE D ENT."
Lcdout $fe, $14, "TORCH HEIGHT CONTROL"
Lcdout $fe, $54, "LCD WARMING UP"
FOR CNT2 = 1 TO 5
FOR CNT = 0 TO 4
Lcdout $fe, $63 + CNT, "."
PAUSE 400
Lcdout $fe, $63 + CNT, $20
NEXT CNT
NEXT CNT2
Start:
'Enter here if no Toolpath is running AND THC is not enabled
IF (EnableIn = 0) and (InMotion = 0) then
RxInt = 1 'Enable receive interrupt
ADCInt = 0 'Disable ADC interrupt
'***** START MSG1 *****
LCDout $fe, 1
LCDout $fe, $03, "CONTROL IS IN"
Lcdout $fe, $43, "STANDBY MODE."
LCDout $fe, $15, "READY TO RECEIVE"
Lcdout $fe, $55, "A COMMAND"
Lcdout $fe, $66
LCDOUT $fe, $0F
'***** END MSG1 *****
firstrun = 1
'Enter here if Toolpath is running OR THC is enabled OR both
ELSE
RxInt = 0 'Disable receive interrupt
ADCInt = 1 'Enable ADC interrupt
ADCGo = 1 'Start ADC conversion
if ADCFlag = 1 THEN GOTO NewADC 'New ADC interrupt flag
if firstrun = 1 then 'Write the entire message
LCDout $fe, 1
Lcdout $fe, $01, "PLASMA VOLTAGE=", TorchVNow
Lcdout $fe, $40, "--------------------"
Lcdout $fe, $14, "TORCH LOCK VALUE=", TorchLockVal
LCDout $fe, $55, "HYSTERESIS VALUE=", TorchhystVal
FIRSTRUN = 0
ELSE 'Write just the changed values
Lcdout $fe, $10, TorchVNow
ENDIF
select case TorchVNow
case TorchVNow > TorchLockVal + TorchhysTval 'Torch is too high, tell CNC to lower it
high torchdn
low torchup
case TorchVNow < TorchLockVal - TorchhysTval 'Torch is too low, tell CNC to raise it
high torchup
low torchdn
case else 'Torch is locked on, we're good
low torchup
low torchdn
end select
endif
GOTO Start
'ADC interrupt routine-------------------------------------------------------
NewADC:
TorchVNow = ADRESL 'Read ADC value into VAR
ADCFlag = 0 'Clear ADC interrupt flag
Return
'----------------------------------------------------------------------------
'Serial interrupt routine----------------------------------------------------
SerialInt:
'Command structure ex:
' $CommandWord=###*
RxInt = 0 'Disable receive interrupt
SerInStr = 0 'Clear the buffer
hserin[str SerInStr\25\42] 'Fill the buffer. Move on when "*" is found
'Thanks to Bruce: www.rentron.com for the parsing routine
Get_Start: 'X will hold the start "$" position
FOR X = 0 TO 24 'Iterate through the entire buffer
IF SerInStr[X] = "$" THEN Get_Mid 'We found it, move on to next find
NEXT X
GOTO Start 'Buffer is empty or improper Command string
Get_Mid: 'Y will hold the end "=" position
FOR Y = X+1 TO 24 'Iterate through the buffer from last found to end
IF SerInStr[Y] = "=" THEN Get_End 'We found it, move on to next find
NEXT Y
GOTO Start 'Buffer is empty or improper Command string
Get_End: 'Z will hold the end "*" position
FOR Z = Y+1 TO 24 'Iterate through the buffer from last found to end
IF SerInStr[Z] = "*" THEN ParseCommand 'We found it, now let's parse it
NEXT Z
GOTO Start 'Buffer is empty or improper Command string
Continued below... over 10000 characters... sorry
Bookmarks