Page 1 of 1

Loop keeps repeating

Posted: Mon Aug 19, 2013 10:54 pm
by Dutch
Hello,

While looking on the net for how PIC microprocessors work just out of interest and maybe how to program these for my hobby I found this great program Flowcode and a free downgraded trail version perfect for testing if it would suit my needs…

Because I have programmers at my disposal from my work like Pickit3 and a Galep-5 it’s fun to try and explore… I have read and played some already and found a goal to make.. I stumbled upon all kind of small issues but found most answers on this forum.. (watchdog timer, spaces in the filename (sigh … Grrr  ))

I now have something I cant get sorted and tried a lot. I made a program with two leds. One led (2) is lit when no input is given. When the input is switched ‘on’ the second led (1) pulses, together with led 2 on/off for 3 times and then both remain solid 'on' until the switch is released again.

When I run the program in demo it does exactly what I want (have found two ways to do that actually). But when I load the program in the PIC it keeps flashing like it does not see the ‘second part’ of the line where two leds need to stay solid ‘on’ until the switch is released. It ‘skips’ a part and starts it’s line from top again writing the line again resulting in a repeating flash. when I replace the last both leds solid on by a output and a delay it works but does not react on releasing the switch obviously.
Both loops separately programmed does work fine.
....... Help.

Re: Loop keeps repeating

Posted: Mon Aug 19, 2013 11:40 pm
by kersing
I think the watchdog timer could be an issue. If you do not reset the watchdog periodically it will reset the controller. Try setting it to 'off'.
In the Build menu, choose 'Project Options', Push 'Configure chip' and set 'Watchdog timer' to 'Off'.

Re: Loop keeps repeating

Posted: Tue Aug 20, 2013 12:02 am
by medelec35
Hi Dutch, I was also going to mention Watchdog timer, but kersing beat me to it. That causes a fair amount of issues if left enable and not using clear watch dog commands like kersing has stated.
Having Auto clear watch dog enabled will only clear WDT when a delay is reached, or a delay within a component. EG LCD or RS232
So if you have a loop with no delays, watchdog will time out and reset microcontroller.

The only other pearls of wisdom I can give are:
If programming via ICSP using MCLR then disable Low Voltage programming.
otherwise your lose the use of RB4. That will not affect the program you posted, but may affect programs developed in the future that will rely on RB4.
Since you have Master clear enabled, make sure MCLR is tied to +5V (via say 33K resistor is using ICSP)

Martin

Re: Loop keeps repeating

Posted: Tue Aug 20, 2013 8:34 am
by Dutch
Thanks guys. I'll try that tonight or maybe this afternoon at work when it lets me :wink: .
I remember from my first try's the PIC did not function right when i had WDT off so i left it on.

Maybe i need to read up about the WDT... :roll:

Re: Loop keeps repeating

Posted: Tue Aug 20, 2013 4:15 pm
by Spanish_dude
The watchdog timer is a timer, that when it reaches it's max value (or something like that) it resets the microcontroller.
This is used to prevent the microcontroller of getting stuck inside a loop (or something similar).

Let's say you have a program with a main loop and you are using the watchdog timer as a means to reset your microcontroller in case something goes wrong.
When everything works fine, you'll have to reset the watchdog timer from time to time (let's say at the end of your main loop).
If you don't do that then it's going to reset the microcontroller even though there's nothing wrong.

Think of it as some kind of a timeout, like it's used when sending/receiving data to/from RS232 and other.

- Nicolas

Re: Loop keeps repeating

Posted: Tue Aug 20, 2013 4:28 pm
by Dutch
Ah, thanks. That clarifies the dog thing. Sounds logical
Hope it solves my issue... W'll see tonight in a few

Re: Loop keeps repeating

Posted: Tue Aug 20, 2013 8:35 pm
by Dutch
Oke..Turned off the WDT. now only led 1 goes on without any flash when I press the switch, nothing further happens...So it reads the switch.. and gives an action but does not perform the loopcount..Hmm
what command am I forgetting here?

Re: Loop keeps repeating

Posted: Wed Aug 21, 2013 11:57 am
by dazz
Hi
try disabling the decision branch in your loop that counts, up load to chip, is that closer to what you need ?

Regards
Dazz

Re: Loop keeps repeating

Posted: Wed Aug 21, 2013 12:06 pm
by Dutch
I'll try that tonight. (or already did, donno anymore :wink: )
I'm guessing the programm dont even reach the decision i put in that line but i'll take part's off untill things change

strange is that when i take the second loop (while switch = 1) out of the line and replace it by portA=3 and a 60 second delay (both leds on for 60 seconds) it runs. but that is not my goal.

Thanks for thinking with me here Dazz...! let you know.

Nice that something simple as this can be hard and kinda hairpulling if you dont know where to look anymore :lol:
On the other hand i learn from it.. just my other projects are at a standstill. this gotto work!

Re: Loop keeps repeating

Posted: Wed Aug 21, 2013 10:58 pm
by Dutch
Oke, it works :D .
combination of setting the WDT in off position at two places and a good check of the osc circuit.

I'm not ready yet with my idea's but lets search the max of the demo version...

Thanks for looking guys.