Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Thisused the same as thisCode:While true portb.0 = 1 Delayms(50) Portb.0 = 0 delayms(50) Wend
Code:While true DelayMS(50) toggle(portb.0) DelayMS(50) Wend
Now that was done with the full ver's of Swordfish Basic the free is not as good toggle uses more with it.
...As far as TOGGLE, only using for testing (indication) not going in the final code...
...Working on a different issue. Wondering if I really need the weak pullups on portB but it works BUT on portB.6 I show 1.46v while portB.4&5 show 3.75. (I have 10k pullups soldered in the circuit.)...
... but yes I wondered about too much in the INTERRUPT?...
Interrupt PORTB_Change()
Save(0) // Save the system variables
If RBIF = 1 Then // Check if the interrupt was a PORTB change
RBIF = 0
WREG = PORTB // Be sure to read the contents of PORTB to
// clear the mismatch
// PORTB 7:4 has changed, do what you want here...
PORTC = PORTB // In this case, I am making PORTC = PORTB
EndIf
Restore // Restore the system variables
End Interrupt
// PORTB 7:4 has changed, do what you want here...
PORTC = PORTB
EndIf
Restore // Restore the system variables
End Interrupt
Dim SavePort as byte
// PORTB 7:4 has changed, do what you want here...
SavePort = PORTB // In this case, I am saving to a variable to test in main
// so the chip can keep doing something useful and not a have a melt down becuse I got lost in
// a long interupt hander becuse you don't no that your code jumps to the wrong place.
EndIf
Restore // Restore the system variables
End Interrupt
main:
If saveport = XX then // I had a button press
// do this
High(portc.0)
else
Low(portc.0)
endif
Interrupt PORTB_Change()
Save(0) // Save the system variables
If RBIF = 1 Then // Check if the interrupt was a PORTB change
RBIF = 0
WREG = PORTB // Be sure to read the contents of PORTB to
// clear the mismatch
// PORTB 7:4 has changed, do what you want here...
PORTC = PORTB // In this case, I am making PORTC = PORTB
EndIf
Restore // Restore the system variables
End Interrupt
If WhatTheHellButtonWasPressed.bits(4) = 1 then
'the button connected to RB4 was pressed
End if
If WhatTheHellButtonWasPressed.bits(5) = 1 then
'the button connected to RB5 was pressed
End if
If WhatTheHellButtonWasPressed.bits(6) = 1 then
'the button connected to RB6 was pressed
End if
If WhatTheHellButtonWasPressed.bits(47) = 1 then
'the button connected to RB7 was pressed
End if
'xxxxxxxxxxxxxxxxxxxx
'interrupt
'xxxxxxxxxxxxxxxxxxxxx
Interrupt PORTB_Change()
Save(0) // Save the system variables
If RBIF = 1 Then // Check if the interrupt was a PORTB change
RBIF = 0
WREG = PORTB // Be sure to read the contents of PORTB to
// clear the mismatch
if portb.bits(6)=0 then led1 = 1
endif
if portb.bits(5)=0 then led2 = 1
endif
if portb.bits(4)=0 then led0 = 1
endif
// PORTB 7:4 has changed, do what you want here...
EndIf
Restore // Restore the system variables
End Interrupt
Sub PORTB_Interrupts(Control As Boolean) // Small routine to enable/disable PORTB Interrupts.
If Control = True Then
RBIE = 1 // interrupt enable bit
Enable(PORTB_Change)
Else
RBIE = 0 // port change interrupt enable bit
Disable(PORTB_Change) //
EndIf //
End Sub
Inline Sub Sleep()
Asm
Sleep
End Asm
End Sub
'xxxxxxxxxxxxxxxxxxxxxxxxxxx
// TURN DETECTION
'xxxxxxxxxxxxxxxxxxxxxxxxxx
' LEFT
'--------------------
If led1 = 1 Then
RBIF = 1
// LED1 = 1 // GREEN // NOTE I need to make ports LOW for transmission, HIGH for testing
led2 = 0
led0 = 0
TX_pwr = 1 // turn on transmitter
TX_Enable = 0 // enable encoder
DelayMS(500)
Toggle (LED1)
DelayMS(500)
LED1 = 0 // make sure its off
TX_pwr = 0
TX_Enable =1 // LOW for transmit
rbie = 0
End If
'-------------------------
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 1/2/2013 *
* Version : 1.0 *
* Notes : this one works 1/10/2013 *
* : *
*****************************************************************************
}
Device = 18F2420
Clock = 8
Config LVP = OFF, mclre = off
Include "InternalOscillator.bas"
Include "utils.bas"
Dim RBIF As INTCON.0 //port change interupt flag
Dim RBIE As INTCON.3 //port change interupt enable
Dim x As Byte
Dim TX_Enable As PORTC.0
Dim TX_pwr As PORTC.1
Dim GD_bat As PORTC.2
Dim led0 As PORTC.3 // blue
Dim LED1 As PORTC.4 //green
Dim led2 As PORTC.5 //red
Dim s1 As PORTB.6
Dim s2 As PORTB.5
Dim s3 As PORTB.4
Dim saveport As Byte
'xxxxxxxxxxxxxxxxxxxxxxxxxxx
// TURN DETECTION SUB ROUTINES
'xxxxxxxxxxxxxxxxxxxxxxxxxx
'-------------------------
sub RIGHT ()
'--------------------------
led2 = 1
RBIF = 1
led0 = 0
LED1 = 0
// led2 = 1 // RED
TX_pwr = 1 // turn on transmitter
TX_Enable = 0 // enable encoder
DelayMS(500) // just enough to see for testing
Toggle (led2)
DelayMS(500)
led2 = 0 // make sure its off
TX_pwr = 0
TX_Enable =1 // LOW for transmit
end sub
'-----------------------
Sub LEFT()
'--------------------
LED1 = 1
RBIF = 1
// LED1 = 1 // GREEN // NOTE I need to make ports LOW for transmission, HIGH for testing
led2 = 0
led0 = 0
TX_pwr = 1 // turn on transmitter
TX_Enable = 0 // enable encoder
DelayMS(500)
Toggle (LED1)
DelayMS(500)
LED1 = 0 // make sure its off
TX_pwr = 0
TX_Enable =1 // LOW for transmit
RBIE = 0 // reset enable bit
End Sub
' xxxxxxxxxxxxxxxxxxxxxxxxxx
sub BATTERY_TEST()
'xxxxxxxxxxxxxxxxxxxxxxxxxxx
' BAD BATTERY
If led0 = 1 And PIR2.bits(2)=1 Then // check hlvd flag
TX_pwr = 1 //turn on transmitter
led0 = 1 //blue led next to green BI color in final plan
DelayMS(1000)
Toggle(led0)
End If
PIR2.bits(2)=0
' GOOD BATTERY
If led0 = 0 And PIR2.bits(2)=0 Then
GD_bat = 1 // green led next to blue GD_bat = good battery
DelayMS(1000)
Toggle( GD_bat)
DelayMS(1000)
TX_pwr = 0 //turn off transmitter
EndIf
// PIR2.bits(2)=0 // reset interrupt flag DO I REALLY NEED TO RESET?
// RBIF = 0 // Port change interrupt flag bit
end sub
'xxxxxxxxxxxxxxxxxxxx
'interrupt
'xxxxxxxxxxxxxxxxxxxxx
Interrupt PORTB_Change()
Save(0) // Save the system variables
If RBIF = 1 Then // Check if the interrupt was a PORTB change
RBIF = 0
WREG = PORTB // Be sure to read the contents of PORTB to
// clear the mismatch
If PORTB.bits(6)=0 Then LEFT()
EndIf
If PORTB.bits(5)=0 Then RIGHT()
EndIf
If PORTB.bits(4)=0 Then BATTERY_TEST()
EndIf
// PORTB 7:4 has changed, do what you want here...
EndIf
Restore // Restore the system variables
End Interrupt
Sub PORTB_Interrupts(Control As Boolean) // Small routine to enable/disable PORTB Interrupts.
If Control = True Then
RBIE = 1 // interrupt enable bit
Enable(PORTB_Change)
Else
RBIE = 0 // port change interrupt enable bit
Disable(PORTB_Change) //
EndIf //
End Sub
Inline Sub Sleep()
Asm
Sleep
End Asm
End Sub
'
//sssssssssssssssssssssssssssssssssssssssssss
SetAllDigital
Input(s1)
Input(s2)
Input(s3)
Output(led0)
Output(LED1)
Output(led2)
Output(TX_Enable)
Output(TX_pwr)
Output(GD_bat)
HLVDCON = %00010111 // 3.12 volts
PIR2.bits(2)=0 // clear HLVD flag
PIE2.bits(2) = 1 //enable HLVD interrupt
// Start Of Program...
TRISB = %11110000 // Make PORTB7:4 all inputs
TRISC = %00000000 // Make PORTC all outputs
led0 = 0 // turn off all leds
LED1 = 0
led2 = 0
TX_Enable = 1
TX_pwr = 0
GD_bat=0
While True // Infinite loop
PORTB_Interrupts(True) // Enable PORTB interrupts
Sleep // Put the PIC to sleep and wait for a change
// on PORTB 7:4
Wend
nterrupt PORTB_Change()
Save(0) // Save the system variables
If RBIF = 1 Then // Check if the interrupt was a PORTB change
RBIF = 0
WREG = PORTB // Be sure to read the contents of PORTB to
// clear the mismatch
If s1=0 Then
//LEFT()
EndIf
Interrupt PORTB_Change()
Save(0) // Save the system variables
If RBIF = 1 Then // Check if the interrupt was a PORTB change
RBIF = 0
WREG = PORTB // Be sure to read the contents of PORTB to
// clear the mismatch
If s1=0 Then
led0 = 1 // TEST IF WORKING
delayms(500)
led0 = 0
//LEFT()
EndIf
If s2=0 Then
led0 = 1 // TEST IF WORKING
delayms(500)
toggle(led0)
//RIGHT()
EndIf
If s3=0 Then
led1=1 // TEST IF WORKING
delayms(500)
led1 = 0
// BATTERY_TEST()
EndIf
// PORTB 7:4 has changed, do what you want here...
EndIf
Restore // Restore the system variables
End Interrupt