PDA

View Full Version : Trying to make code smaller



plyrathrt
- 2nd January 2009, 14:03
I am trying to shrink my code down but I am still "learning". Here is a example of what I am attempting to do.

Say I use

HIGH LED : PAUSEUS 50000 : LOW LED : PAUSEUS 50000

Now if I wanted to multiply that by 10 to make the LED flash 10 times, I don't not want to have to write that line 10 times.

Any help?

aratti
- 2nd January 2009, 14:15
Led var portX.x

A0 var Byte

Loop:
For A0 = 1 to 10
Toggle Led
Pause 50000
next A0
'comment the 3 lines hereunder for only ten flashes (leave the END)
Low Led
Pause 5000
goto Loop

end


This Example will flash led ten times. Led will turn off for 5 secs and flashing will repeat.
Hope it is what you need.

Happy new year

Al.

plyrathrt
- 2nd January 2009, 14:44
Led var portX.x

A0 var Byte

Loop:
For A0 = 1 to 10
Toggle Led
Pause 50000
next A0
'comment the 3 lines hereunder for only ten flashes (leave the END)
Low Led
Pause 5000
goto Loop

end


This Example will flash led ten times. Led will turn off for 5 secs and flashing will repeat.
Hope it is what you need.

Happy new year

Al.

when I write

main:
If button1 = 0 then test
goto main

test:
for a0 = 1 to 10
toggle led
pauseus 50000
next a0
low led
pauseus 50000
goto main

What happens is the led only flashes 4 times. I am using a 12F683 @ 8mhz OSC

aratti
- 2nd January 2009, 14:49
Have you declared the variable A0? (A0 var byte)
Have you defined the oscillator value? (DEFINE OSC 8)
Have you declared to which pin the name "Led" belongs? (Led var PortX.x)
Have you declared the pin to be an output? (Tris)
Have you declared to which pin the name "button1" belongs? (button1 var PortX.y)
Have you declared the pin to be an input? (Tris)
Al.

peterdeco1
- 2nd January 2009, 14:51
I've used this method. I used FINISHED instead of END just in case you want to add a line that begins the cycle over again with an input switch.


X VAR BYTE
CLEAR

START:
HIGH LED : PAUSEUS 50000 : LOW LED : PAUSEUS 50000 : LET X = (X + 1)
IF X < 10 THEN START

FINISHED:
'IF PORTA.0 = 1 THEN CLEAR : GOTO START
GOTO FINISHED

plyrathrt
- 2nd January 2009, 14:51
Have you declared the variable A0 (A0 var byte)?

Al.


yes, in the initialization

plyrathrt
- 2nd January 2009, 15:16
I've used this method. I used FINISHED instead of END just in case you want to add a line that begins the cycle over again with an input switch.


X VAR BYTE
CLEAR

START:
HIGH LED : PAUSEUS 50000 : LOW LED : PAUSEUS 50000 : LET X = (X + 1)
IF X < 10 THEN START

FINISHED:
'IF PORTA.0 = 1 THEN CLEAR : GOTO START
GOTO FINISHED

It only flashes once when the button is pressed. I want to get it so that when I press it the first time, it flashes the led once, press it again, and it flashes the LED twice, and so on.

My initalization is this
DEFINE OSC 8
OPTION_REG = %0000000
ANSEL = 0
ADCON0 = 0
CMCON0 = 7
TRISIO = %00111011
WPU = %00011010
A0 var byte
X var byte
button1 var gpio.1
led var gpio.2

peterdeco1
- 2nd January 2009, 16:06
I am not at my bench to test this but it should work. Also, your pauseus 50000 is very fast. Try using pause 1000 to space the flashes at 1 second intervals.

X var byte
y var byte
clear 'reset both to 0

start:
If button1 = 1 then start 'wait for button push
let x = (x + 1) 'count button presses

flashled:
High led : Pauseus 50000 : Low led : Pauseus 50000 : Let y = (y + 1) 'count led flashes
if y < x then flashled 'flash the led same number as button pushes
if x >= 10 then clear 'reset both counters after 10 button pushes & flashes
goto start

aratti
- 2nd January 2009, 16:08
I don't know if the setting are correct since I have never used this chip.

But assuming setting OK then this code will work as you want;


Loop:
pause 100
If button1=0 then
X=X+1
goto Start
endif
goto Loop

start:
For A0 = 0 to X
Toggle Led
Pause 50000
next A0
if X=255 then X=254
goto Loop

end

Flashing will increment by one at every press upto 255.

Now, you add a second button and relative code for decrementing the variable X.

Al.

Nicmus
- 5th January 2009, 17:20
When you are using Toggle command in an indexed loop you must double your index to get a certain number of blinks.
In your case the index variable A0 should be 20. You also need to know what the status of your LED output is for a good control over the number of blinks and add or eliminate unnecessary pauses.

Hope this helps.

Regards,

Nick