Help Quick Need to make code smaller
I need to srink this code from 1688 to 1024 or less for a 16F84.
This to make a car horn honk when the circuit gets power. It's adjustable from one to ten honks. This is the first code I have ever written so if you find any thing else wrong please tell me.
DEFINE OSC 4
TRISA.2 = 0
High PORTA.2
Pause 50
TRISB = %11111111
TRISA.0 = 1
TRISA.1 = 1
TRISA.3 = 1
TRISA.4 = 1
SWITCH:
Pause 250
IF PORTA.4 = 1 Then End
IF (PORTB.2 = 0) AND (PORTB.3 = 0) AND (PORTB.6 = 0) AND (PORTB.4 = 0) AND (PORTB.5 = 1) Then GoTo SW10
IF (PORTB.2 = 0) AND (PORTB.3 = 0) AND (PORTB.6 = 0) AND (PORTB.4 = 1) AND (PORTB.5 = 1) Then GoTo SW1
IF (PORTB.2 = 1) AND (PORTB.3 = 0) AND (PORTB.6 = 0) AND (PORTB.4 = 0) AND (PORTB.5 = 1) Then GoTo SW2
IF (PORTB.2 = 1) AND (PORTB.3 = 0) AND (PORTB.6 = 0) AND (PORTB.4 = 1) AND (PORTB.5 = 1) Then GoTo SW3
IF (PORTB.2 = 0) AND (PORTB.3 = 0) AND (PORTB.6 = 1) AND (PORTB.4 = 0) AND (PORTB.5 = 1) Then GoTo SW4
IF (PORTB.2 = 0) AND (PORTB.3 = 0) AND (PORTB.6 = 1) AND (PORTB.4 = 1) AND (PORTB.5 = 1) Then GoTo SW5
IF (PORTB.2 = 1) AND (PORTB.3 = 0) AND (PORTB.6 = 1) AND (PORTB.4 = 0) AND (PORTB.5 = 1) Then GoTo SW6
IF (PORTB.2 = 1) AND (PORTB.3 = 0) AND (PORTB.6 = 1) AND (PORTB.4 = 1) AND (PORTB.5 = 1) Then GoTo SW7
IF (PORTA.2 = 0) AND (PORTB.3 = 1) AND (PORTB.6 = 0) AND (PORTB.4 = 0) AND (PORTB.5 = 1) Then GoTo SW8
IF (PORTA.2 = 0) AND (PORTB.3 = 1) AND (PORTB.6 = 0) AND (PORTB.4 = 1) AND (PORTB.5 = 1) Then GoTo SW9
GoTo SWITCH
SW1:
PulsOut PORTA.2, 50000
End
SW2:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW3:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW4:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW5:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW6:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW7:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW8:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW9:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
SW10:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
End
Any help is appreciated.
Thanks
Re: Help Quick Need to make code smaller
Hi Programmednew,
Well, just at a glance you might try nested IF...THEN's instead of using the "AND"s (try to get rid of all of the AND's if you can).
The "PULSEOUT"s can be replaced with something like:
PortB.0 = 1 ; Make PortB.0 'high'
pause 1000 ; or whatever pause you need
PortB.0 = 0 ; Make portb.0 'low'
Try to get rid of all the PULSEOUT's too.
Using the "HIGH" command automatically makes the pin an output, so these two commands could be changed:
TRISA.2 = 0
High PORTA.2
To just this:
HIGH PORTA.2
Better yet, can all the "HIGHs and "LOW"s and just set your TRIS registers as needed then use:
PortA.2 = 1
Arch
Re: Help Quick Need to make code smaller
Couldn't edit my last post for some reason - so here's another idea to try:
Where you have some code like this:
SW10:
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
Pause 500
PulsOut PORTA.2, 50000
You could try this:
CounterA VAR BYTE
FOR CounterA = 0 TO 9
PulsOut PORTA.2, 50000
Pause 500
NEXT CounterA
Or get rid of the PULSEOUT as mentioned above.
Starting the FOR...NEXT loop at 0 instead of 1 saves a bit of space too.
>>>>
Also, you could set up the loop in a sub-routine, then just load a CounterStop value:
CounterStop VAR BYTE
; Your code sets a loop amount here.
CounterStop = 5 ; Or whatever amount of loops you want
GoSUB HornLoops
Horn_Loops:
FOR CounterA = 1 TO CounterStop
PulsOut PORTA.2, 50000
Pause 500
NEXT CounterA
RETURN
Arch