PDA

View Full Version : beginer help needed



asifiqbal
- 19th March 2014, 14:06
hello
I am beginer and practicing small codes so here is a code and i need help about it
--------------------------------
define osc 4
Trisb =%01111111
do
high portb.7
pause 300
low portb.7
pause 300
loop until portb.2 = 0
---------------------------
when i run this code led on start blinking but when i press button and make portb.2 low then operation of led should stop but it doesent stop ,it keep blinking whether there is 1 or 0 at portb.0
what is wrong this code and how to correct it please guide me thanks

HenrikOlsson
- 19th March 2014, 16:11
Hi,
Which PIC are you using?
It probably has some analog function on PortB.2 so you need to set it to digital mode.

Another thing is that it might do exactly what you want it to do. But when the Until PortB.2=0 evaluates true you don't do ANYTHING so the program continues into empty space and then start over, try:

...
...
loop until portb.2 = 0
LOW PortB.7
END

Also, remember that define is case sensitive so it should be define OSC 4. In this case it doesn't really matter since 4MHz is the default anyway but try to remember that.

/Henrik.

EDIT: What exactly is the reason for posting the exact same question twice? You've already received an answer in the other thread....
Any moderator care to delete or join threads?

asifiqbal
- 20th March 2014, 10:14
thankyou sir ur reply was helpful, i have another question to ask, my device is pic 16f72 and port b is digital
when a programm run in a loop how can i intrrupt it for example
main:
high portb.7
pause 100
low portb.7
pause 100
goto main
------
now this loop keep repeating how can i intrrupt this loop ? if i write like this
main:
high portb.7
pause 100
low portb.7
pause 100
goto main
if portb.2=0 then
portb.7 = 0
endif
------------
intrrupt here doesnt work what is the way out to get out of the loop now

Dave
- 20th March 2014, 10:59
main:
high portb.7
pause 100
low portb.7
pause 100
if portb.2=0 then goto EXIT 'test for exit 1 time for each loop
goto main
EXIT:
portb.7 = 0
remaining code goes here...........


OR
main:
high portb.7
if portb.2=0 then goto EXIT 'test for exit after setting port pin high
pause 100
low portb.7
if portb.2=0 then goto EXIT 'test for exit after setting port pin low
pause 100
goto main
EXIT:
portb.7 = 0
remaining code goes here...........

HenrikOlsson
- 20th March 2014, 11:06
Hi,
There are several ways of doing it.
One way is to actually use a hardware interrupt, another way is to simply poll the input periodically as you go thru the loop. Yet another way is to move away from the loop structure and use a state machine aproach instead. Instead of using PAUSE, which "blocks" the processor, you can use hardware timers, either with interrupts or by simply polling the interrupt flag. It really depends on what you want to do and how fast you need to do it.

If all you want to do is blink the LED untill an input goes high (or low) then you basically had it in the first example you posted. The drawback there is that the input is only checked once per iteration thru the loop so if the PAUSE is long there will be a long "reaction time".

As you see there's not a single answer to your question, it all depends....

asifiqbal
- 21st March 2014, 05:03
Oh thanks a lot ,too many puzzeles have been solved in my mind with this answer of yours ,i m really feeling like i have jumped from beginner level to 5 steps ahead thanks again, i have an electronics desin of a ups circuit and i am only asking questions related to it ,now the next part of my question is that this pic16f72 generates an AC volt pulse at two of its pins at 50 hrtz , i want to know the command for doing this ,another thing that how can i make two output pins to opposite of one another ,for example if i take porb.0 and portb.1 , if one port is equal to 1 then other should automatically become equal to 0 and vice versa ,

HenrikOlsson
- 21st March 2014, 06:15
Hi,
Oh man, not again.....
What is with this inverter/VFD/UPS stuff, it seems to be a common "beginner project" around here, yet it's NOT (I repeat NOT) a simple project, I don't get it.
Is it possible that this is a school assignment at a certain class/school somewhere and whoever gets assigned the project "this year" always ends up here?

Anyway, as before, there are several ways of making the PIC output a frequency.
Generally speaking using a PIC with a CCP or ECCP or even PCPWM module is the way to go for these sort of things. The PWM feature of the these modules can often, but not always, be configured in half bridge mode. The 16F72 does not have this feature.