Device = 18F1320
Clock = 4 // 4MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Dim NOT_RBPU As INTCON2.7
Dim Lit As Byte
Dim Keys As Byte
OSCCON = $62 // select 4MHz internal clock
ADCON1 = $7f //all digital
NOT_RBPU=0 //WPUs on port B
While true
DelayMS(10)
If(PORTB.0=0) Then //is button 1 pressed
TRISA.7 = 1
High(PORTA.0)
Low (PORTA.6)
EndIf
If(PORTB.2=0) Then
TRISA.7 = 1
Low (PORTA.0)
High(PORTA.6)
EndIf
If(PORTB.5=0) Then
TRISA.0 = 1
High(PORTA.6)
Low (PORTA.7)
EndIf
Wend
End
It will work but put a current limiting resistor from your 12 volt pos to the Vin of the 7805 a 24 Ohms 1 watt would work and a fuse. That way if your 7805 go's out it will save you a lot trouble.The simple 7805 might work
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Dim NOT_RBPU As INTCON2.7
Dim TMR2IE As PIE1.1
Dim TMR2IF As PIR1.1
//global variables
Dim Lit As Byte
Dim IntFlag As Bit
//interrupt routine gets called 500 times per second
//and multiplexes the LEDs Aslo sets a flag after 0.5S.
Interrupt MyInt()
Dim Count As Byte
Dim Row As Byte
TMR2IF=0
Row=Row+1
If Row=3 Then
Row=0
EndIf
TRISA = TRISA Or %11000001 ;turn all LEDs off
PORTA = PORTA And %00111110
Select Row
Case 0
TRISA.0=0
PORTA.0=1
If Lit.0=1 Then
TRISA.6=0
EndIf
If Lit.5=1 Then
TRISA.7=0
EndIf
Case 1
TRISA.6=0
PORTA.6=1
If Lit.1=1 Then
TRISA.0=0
EndIf
If Lit.2=1 Then
TRISA.7=0
EndIf
Case 2
TRISA.7=0
PORTA.7=1
If Lit.4=1 Then
TRISA.0=0
EndIf
If Lit.3=1 Then
TRISA.6=0
EndIf
EndSelect
Count=Count+1
If Count=250 Then
Count=0
IntFlag=1
EndIf
End Interrupt
//main code starts here
OSCCON = $72 // select 8MHz internal clock
ADCON1 = $7f //all digital
NOT_RBPU=0 //WPUs on port B
Lit=0 //turn all LEDs off
T2CON = %01001110 //pre=16 post=10
PR2 = 25 //=8000000/4/16/10/25 = 500Hz
TMR2IE=1 //enable timer 2 interrupts
Enable(MyInt) //set interrupt going
While true
If(PORTB.0=0) Then //is button 1 pressed
Lit.0=1 //yes so turn on LED 1
Else
Lit.0=0 //no so turn off
EndIf
If(PORTB.2=0) Then
Lit.2=1
Else
Lit.2=0
EndIf
If(PORTB.5=0) Then
Lit.4=1
Else
Lit.4=0
EndIf
If IntFlag=1 Then //if 0.5S passed then flash other LEDs
Lit=Lit Xor %00101010
IntFlag=0
EndIf
Wend
End
TRISA = TRISA Or %11000001 ;turn all LEDs off
hey thanks Pommie! I really appreciate it! Slight problem though, it won't compile. It says "end expected" and it highlighted this line
Code:TRISA = TRISA Or %11000001 ;turn all LEDs off
TRISA = TRISA Or %11000001 //turn all LEDs off
If(PORTB.0=0) Then //is button 1 pressed
TRISA.7 = 1
High(PORTA.0)
Low (PORTA.6)
EndIf
//main code starts here
Dim Keys As Byte
Dim Previous As Byte
Dim NewKeys As Byte
OSCCON = $72 // select 8MHz internal clock
ADCON1 = $7f //all digital
NOT_RBPU=0 //WPUs on port B
Lit=0 //turn all LEDs off
T2CON = %01001110 //pre=16 post=10
PR2 = 25 //=8000000/4/16/10/25 = 500Hz
TMR2IE=1 //enable timer 2 interrupts
Enable(MyInt) //set interrupt going
Lit=1
While true
DelayMS(10) //reqired for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
If(NewKeys.0=1) Then //is button 1 pressed
Lit=Lit*2 //shift it left
If(Lit=64) Then //got to end
Lit=1 //yes so wrap around
EndIf
EndIf
If(NewKeys.2=1) Then //is button 2 pressed
Lit=Lit/2 //shift it right
If(Lit=0) Then //got to end
Lit=32 //yes so wrap around
EndIf
EndIf
Wend
This is why we always try to tell newb programmers to maybe scale back the (usually) advanced projects they always seem to want to do right away, and try to nudge them toward learning the basics first. Either way they always end up having to learn the basics anyway.I gotta tell ya, this code writing is turning out to be considerably more difficult than I anticipated. I feel like I need to know all the basics for half of this stuff to make any sense.
Comments won't teach you the basics, but they can really help make someone else's code more understandable.The blue notes after each line help, but I still have so many questions.
Sounds like you need to learn to debounce your switch. Try this: when the switch is detected, go immediately to a loop that keeps checking the switch until it is released. Then go to the action you wanted that button press to perform. Simple, easy debouncing.I can't for the life of me get the same button to do one thing, then something else when pressed again. It just keeps jumping right to the second thing I want it to do.
Time and lots of simple programming projects, gradually getting more advanced, will help.Where can I learn the basics so this all makes more sense?
Bundles of it! Got a wheelbarrow?OR should I just have someone write all the code I need to make my project work? Is that going to cost me money?
I can't for the life of me get the same button to do one thing, then something else when pressed again. It just keeps jumping right to the second thing I want it to do.
Lit=1
While true
DelayMS(10) //reqired for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
If(NewKeys.0=1 and Lit.0=1) Then //is button 1 pressed
Lit.0=0
Lit.1=1
EndIf
If(NewKeys.0=1 and Lit.1=1) Then
Lit.0=1
Lit.1=0
EndIf
Wend
Device = 18F1320
Clock = 8 // 8MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF
Dim NOT_RBPU As INTCON2.7
Dim TMR2IE As PIE1.1
Dim TMR2IF As PIR1.1
//global variables
Dim Lit As Byte
Dim IntFlag As Bit
//interrupt routine gets called 500 times per second
//and multiplexes the LEDs Aslo sets a flag after 0.5S.
Interrupt MyInt()
Dim Count As Byte
Dim Row As Byte
TMR2IF=0
Row=Row+1
If Row=3 Then
Row=0
EndIf
TRISA = TRISA Or %11000001 //turn all LEDs off
PORTA = PORTA And %00111110
Select Row
Case 0
TRISA.0=0
PORTA.0=1
If Lit.0=1 Then
TRISA.6=0
EndIf
If Lit.5=1 Then
TRISA.7=0
EndIf
Case 1
TRISA.6=0
PORTA.6=1
If Lit.1=1 Then
TRISA.0=0
EndIf
If Lit.2=1 Then
TRISA.7=0
EndIf
Case 2
TRISA.7=0
PORTA.7=1
If Lit.4=1 Then
TRISA.0=0
EndIf
If Lit.3=1 Then
TRISA.6=0
EndIf
EndSelect
Count=Count+1
If Count=250 Then
Count=0
IntFlag=1
EndIf
End Interrupt
//main code starts here
Dim Keys As Byte
Dim Previous As Byte
Dim NewKeys As Byte
OSCCON = $72 // select 8MHz internal clock
ADCON1 = $7f //all digital
NOT_RBPU=0 //WPUs on port B
Lit=0 //turn all LEDs off
T2CON = %01001110 //pre=16 post=10
PR2 = 25 //=8000000/4/16/10/25 = 500Hz
TMR2IE=1 //enable timer 2 interrupts
Enable(MyInt) //set interrupt going
Lit=1
While true
DelayMS(10) //reqired for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
If(NewKeys.0=1 And Lit.0=1) Then //is button 1 pressed
Lit.0=0
Lit.1=1
EndIf
If(NewKeys.0=1 And Lit.1=1) Then
Lit.0=1
Lit.1=0
EndIf
Wend
If(NewKeys.0=1 And Lit.0=1) Then //is button 1 pressed
Lit.0=0
Lit.1=1
elseIf(NewKeys.0=1 And Lit.1=1) Then
Lit.0=1
Lit.1=0
EndIf
State=0
while true
DelayMS(10) //required for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
if Keys.0=0 then //if key 0 is released then reset
State=0
endif
Select State
case 0
Lit=0 //if key1 pressed then light first LED
if NewKeys.0=1 then
State=1
endif
case 1
Lit.0=1
if NewKeys.2=1 then
State=2
endif
case 2
Lit.1=1
if NewKeys.2=1 then
State=3
endif
case 3
Lit.2=1
end select
wend
Dim Keys As Byte
Dim Previous As Byte
Dim NewKeys As Byte
Dim state as byte
OSCCON = $72 // select 8MHz internal clock
ADCON1 = $7f //all digital
NOT_RBPU=0 //WPUs on port B
Lit=0 //turn all LEDs off
T2CON = %01001110 //pre=16 post=10
PR2 = 25 //=8000000/4/16/10/25 = 500Hz
TMR2IE=1 //enable timer 2 interrupts
Enable(MyInt) //set interrupt going
State=0
while true
DelayMS(10) //required for debounce
Previous=Keys //keep copy of previous key state
Keys=PORTB Xor $ff //get new keys and invert
NewKeys=(Keys Xor Previous) And Keys //keep only new presses
if Keys.0=0 then //if key 0 is released then reset
State=0
endif
Select State
case 0
Lit=0
if NewKeys.2=1 then //if key1 pressed then light first LED
State=1
endif
case 1
Lit.0=1
if NewKeys.2=1 then
State=2
endif
case 2
Lit.1=1
if NewKeys.2=1 then
State=3
endif
case 3
Lit.2=1
if NewKeys.2=1 Then
state=0
endif
end select
wend
case 3
Lit.2=1
DelayMS(5000)
Lit.2=0
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?