Trying to make code smaller


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2007
    Posts
    42

    Default Trying to make code smaller

    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?

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Code:
    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.
    Last edited by aratti; - 2nd January 2009 at 14:18.
    All progress began with an idea

  3. #3
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by aratti View Post
    Code:
    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

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    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.
    Last edited by aratti; - 2nd January 2009 at 15:06.
    All progress began with an idea

  5. #5


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by peterdeco1; - 2nd January 2009 at 14:53. Reason: GOOFED ON A LINE

  6. #6
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by aratti View Post
    Have you declared the variable A0 (A0 var byte)?

    Al.

    yes, in the initialization

  7. #7
    Join Date
    Nov 2007
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by peterdeco1 View Post
    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

  8. #8


    Did you find this post helpful? Yes | No

    Default

    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

  9. #9
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    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;
    Code:
    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.
    All progress began with an idea

  10. #10


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. Code doesn't work on 16F648A
    By Mr_Joe in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 1st September 2018, 22:09
  2. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  3. Can someone help me make a code for this:
    By joseph in forum mel PIC BASIC
    Replies: 0
    Last Post: - 29th April 2009, 18:58
  4. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  5. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts