need led blink program


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2014
    Location
    Pakistan
    Posts
    19

    Default need led blink program

    hi everyone i am a beginner and learning basic language for pic microcontrolers ,kindly someone tell me about following program ,
    i know how to blink a led on portb.0 of pic16f72
    its like
    -----------
    clear
    define osc 4
    mainloop:
    high portb.0
    pause 500
    low portb.0
    pause 500
    goto mainloop
    end
    --------------
    now i want to run portb.0 led with 0.5 minute pause and portb.1 led with one minute pause and portb.2 led with two minute pause ,all at same time in one program ,how will i write the program ?
    kindly help me thanks

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: need led blink program

    Hi,
    First of all, welcome!

    There are many many ways of doing what you describe and selecting which approach to take depends on a lot of things. For example, what kind of accuracy you want and if the program is supposed to other things as well. Here's one possible approach which is straight forward but not the most accurrate:
    Code:
    LED1_Time CON 30	'30 seconds
    LED2_Time CON 60	'60 seconds
    LED3_Time CON 120	'120 seconds
    
    LED1_Count VAR BYTE	' Timekeeping variables
    LED2_Count VAR BYTE
    LED3_Count VAR BYTE
    
    LED1 VAR PortB.0	' Pin aliases for the LEDs
    LED2 VAR PortB.1
    LED3 VAR PortB.2
    
    Start:
        TRISB = %11111000   ' PortB.0-2 outputs.
        
        ' Preload timekeeping variables
    	LED1_Count = LED1_Time
    	LED2_Count = LED2_Time
    	LED3_Count = LED3_Time
    
    Main:
    	' Decrement counters
    	LED1_Count = LED1_Count - 1
    	LED2_Count = LED2_Count - 1
    	LED3_Count = LED3_Count - 1
    
    	' Time for LED1 to toggle?
    	IF LED1_Count = 0 THEN
    		TOGGLE LED1
    		LED1_Count = LED1_Time        ' Reload counter for next period.
    	ENDIF
    
    	'Time for LED2 to toggle?
    	IF LED2_Count = 0 THEN
    		TOGGLE LED2
    		LED2_Count = LED2_Time        ' Reload counter for next period.
    	ENDIF
    
    	' Time for LED3 to toggle
    	IF LED3_Count = 0 THEN
    		TOGGLE LED3
    		LED3_Count = LED3_Time        ' Reload counter for next period.
    	ENDIF
    
    	PAUSE 1000
    GOTO Main
    It does compile without errors and I think it should be fine but I have not tested it.
    As you can imagine the accuracy of this isn't the best but again, it might work perfectly fine for your needs. If better accuracy is needed you could change the PAUSE 1000 to use PAUSEUS in a loop and tweak the value. Using a timer interrupt is of course "the best" solution but it's much more complicated and may not be needed, again YMMV.

    Finally, define osc 4 won't work. It needs to be define OSC 4. 4MHz is the default so in this particular case it doesn't matter but had you done define osc 8 instead of define OSC 8 the timings would have been wrong.

    /Henrik.

  3. #3
    Join Date
    Mar 2014
    Location
    Pakistan
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: need led blink program

    hi,henric thanks for replying ,i tried ur programm but its not working ,i guess i could not describe my requirement clearly,let me explain again ,
    here i have this simple led blink programm and in this program led on portb.7 is blinking endlessly with the pause of 500ms
    now i want another led to blink on portb.6 with pause of 1000 ms ,along with the led which is already blinking with pause of 500ms on portb.7 , both led on portb.6 and portb.7 should blink endlessly but different speed.
    I dont need this programm in any project i am just trying to learn ,Thankyou ,i hope now i have made my requirement clear

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: need led blink program

    Hi,
    Really, is "It's not working" the best description you can come up with?
    What's "not working"? What DOES it do? What DOESN'T it do?

    In your original post you said PortB.0 and 30seconds, PortB.2 and 1 minute, PortB.3 and 2 minutes.....
    Now you're saying PortB.7 and 500ms, PortB.6, 1000ms.... So, no you probably didn't explain your requirements very well.

    I've now tried my previously posted code and it works just fine as per your original specifications. I then changed it to match the revised specification, tested it again at it works just fine here. I'll post it here again:
    Code:
    ' Specify delay time in units of 100ms. 10=1second, 25=2.5seconds etc
    LED1_Time CON 5	    ' 0.5 seconds
    LED2_Time CON 10	' 1 seconds
    LED3_Time CON 30	' 3 seconds
    
    LED1_Count VAR BYTE	' Timekeeping variables
    LED2_Count VAR BYTE
    LED3_Count VAR BYTE
    
    LED1 VAR PortB.7	' Pin aliases for the LEDs
    LED2 VAR PortB.6
    LED3 VAR PortB.5
    
    TRISB = %00011111
    
    Start:
       
        ' Preload timekeeping variables
    	LED1_Count = LED1_Time
    	LED2_Count = LED2_Time
    	LED3_Count = LED3_Time
    
    Main:
    	' Decrement counters
    	LED1_Count = LED1_Count - 1
    	LED2_Count = LED2_Count - 1
    	LED3_Count = LED3_Count - 1
    
    	' Time for LED1 to toggle?
    	IF LED1_Count = 0 THEN
    		TOGGLE LED1
    		LED1_Count = LED1_Time        ' Reload counter for next period.
    	ENDIF
    
    	'Time for LED2 to toggle?
    	IF LED2_Count = 0 THEN
    		TOGGLE LED2
    		LED2_Count = LED2_Time        ' Reload counter for next period.
    	ENDIF
    
    	' Time for LED3 to toggle
    	IF LED3_Count = 0 THEN
    		TOGGLE LED3
    		LED3_Count = LED3_Time        ' Reload counter for next period.
    	ENDIF
    
    	PAUSE 100
    GOTO Main
    Now, I've tested this on another device than what you're using so it's possible that there's something device specific that needs to be set. If you can't get this to work then please describe what it does or doesn't do and post your complete code.

    /Henrik.

  5. #5
    Join Date
    Mar 2014
    Location
    Pakistan
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: need led blink program

    hi
    sorry henrick ,i told u i am new and it is my very first post ,it was my fault i couldnt explain properly and i wrote minute but actually i wanted to write seconds dont mind i will learn rules fast ,
    now this second program of u is working exactly as i wanted it to work ,Thanks a lot you r very helpful ,soon i will approach u with another problem and i hope u will keep helping me
    Last edited by asifiqbal; - 4th March 2014 at 09:59.

  6. #6
    Join Date
    Mar 2014
    Location
    Pakistan
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: need led blink program

    actually i have another question to ask related to this post ,can i write this programme like following
    ------
    clear
    define osc 4
    loop1:
    high portb.7
    pause 500
    low portb.7
    pause 500
    goto loop1
    loop2:
    high portb.6
    pause 1000
    low portb.6
    pause 1000
    goto loop2
    loop3:
    high portb.5
    pause 2000
    low portb.5
    pause 2000
    goto loop3
    end
    ----------
    i tried this way but it didnt work please tell me what is wrong if i write like above

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: need led blink program

    Hi,
    You can write it like that and it will compile but it won't work the way you want it to.

    The program starts from the beginning and executes one instruction at the time.
    * Turns on the LED
    * Waits 500ms
    * Turns off the LED
    * Waits 500ms
    * Jump to Loop1 and starts over.

    The program will act as if the Loop2 and Loop3 routines wasn't even there.

    And again, make a habit of writing your defines in UPPER CASE otherwise they'll have no effect.
    Code:
    define OSC 4     ' <----- OSC must be uppercase or it will not have the desired effect!
    /Henrik.

Similar Threads

  1. 18F2550 and Blink Led
    By nuno106 in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 21st September 2022, 12:01
  2. Led blink and interrupt
    By critix in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th April 2012, 11:31
  3. Novice 16F83A BLINK LED program help
    By owali in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 25th July 2007, 05:02
  4. Blink.bas Program
    By merlinknight in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 7th March 2007, 17:42
  5. Can't get a 12F629 to blink a led
    By Sharky in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th June 2005, 11:28

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