PDA

View Full Version : time counter using pic16F84A (need help in checking my code)



ck0210
- 17th August 2007, 09:32
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

ck0210
- 17th August 2007, 09:43
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..^^

Bruce
- 17th August 2007, 13:26
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.


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

ck0210
- 17th August 2007, 20:39
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...