time counter using pic16F84A (need help in checking my code)


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2007
    Posts
    3

    Smile time counter using pic16F84A (need help in checking my code)

    Hi,

    i am a colleage student and i am doing a pic project..

    my project's concept is connecting a normal switch to the pic and let pic to count the time (in seconds) when the switch is switched on and display the results on LED until switched off...

    EXAMPLE: when the switch is (switched on), pic starts to count the time from 0 second to xxx seconds. When the time is less than or equal to 5 seconds, it will blink the LED in portb.0. When the time is more than 5 seconds, it will blink the LED in portb.1. Then, when the swith is (switched off), the time counter will reset to 0 and wait for the next press on switch....

    THIS IS MY CODE:

    DEFINE osc 4 'using 4MHz oscillator

    second VAR BYTE

    TRISB.0= 0 ' define portb.0 as output
    TRISB.1= 0 'define portb.1 as output

    second= second + 1 'count from 0 second..1 second..2 seconds and so on..

    start:
    IF second <= 5 THEN
    GOSUB loopa
    ELSE
    GOSUB loopb
    PAUSE 500
    ENDIF

    loopa:
    HIGH PORTB.0 'blinking led in portb.0
    PAUSE 500
    LOW PORTB.0
    PAUSE 500
    GOSUB loopa

    loopb:
    HIGH PORTB.1 'blinking led in portb.1
    PAUSE 500
    LOW PORTB.1
    PAUSE 500
    GOSUB loopb

    RETURN 'return to start and count continuously until switch is switched off
    END



    But...my circuit cannot work..
    i had read through picbasic Pro data sheet but i still dunno which part i did wrongly..
    So..can anyone give me guidelines or tell me the errors in my project?
    I will appreciate it...
    Thanks a lot...

    Eric 17/8/07
    Last edited by ck0210; - 17th August 2007 at 09:35.

  2. #2
    Join Date
    Aug 2007
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    hmm...
    i have few more questions..

    --- is the define of "TRISB.0= 0" and "TRISB.1= 0" is useless...
    --- should i delete the "RETURN"...
    --- is it suitable to use "IF..THEN...".....how about "FOR...TO...NEXT"
    --- is the define of "OSC 4" is useless...

    thanks for helps..^^

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    1. When you use GOSUB to reach a sub-routine, there should always be a RETURN to get back.

    2. TRIS is not necessary when using the HIGH and LOW commands.

    3. GOTO start at the end of start ensures program execution will loop in start, and not fall through to the routines below it.

    See how this works.
    Code:
    DEFINE osc 4 'using 4MHz oscillator
    
    second VAR BYTE
    'TRISB is not necessary when using the HIGH and LOW commands
    'TRISB.0= 0 ' define portb.0 as output
    'TRISB.1= 0 'define portb.1 as output
    
    start:
    second= second + 1 'count from 0 second..1 second..2 seconds and so on..
    IF second <= 5 THEN
    GOSUB loopa 
    ELSE
    GOSUB loopb
    PAUSE 500
    ENDIF
    GOTO Start
    
    loopa:
    HIGH PORTB.0 'blinking led in portb.0
    PAUSE 500
    LOW PORTB.0
    PAUSE 500
    'GOSUB loopa ' replaced with RETURN
    RETURN
    
    loopb: 
    HIGH PORTB.1 'blinking led in portb.1
    PAUSE 500
    LOW PORTB.1
    PAUSE 500
    'GOSUB loopb ' replaced with RETURN
    RETURN 'return to start and count continuously until switch is switched off
    END
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Aug 2007
    Posts
    3


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    thanks for your help...i very appreciate it..
    i will program it in my school laboratary later..
    hope that i can get the results..


    Eric...

Similar Threads

  1. Help with Pic Delay code mod please
    By g7jiq in forum General
    Replies: 1
    Last Post: - 26th March 2009, 00:06
  2. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  3. Code snippet: time execution for each line: help please
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 6th April 2008, 15:46
  4. I don't understand this code!
    By Russ Kincaid in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 13th February 2008, 02:55
  5. Time diference in microseconds
    By leonel in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 14th March 2005, 08:25

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