PDA

View Full Version : Programming/Nesting Order ???



gtvmarty
- 3rd July 2008, 12:21
Hi All,
I've been dabbling for a few years with PBPro, but still find certain programming methods a challenge.

My main question: Is there a 'preferred order' to layout programming code/loops/nesting within nests etc.

For example, i might have a 16x2 LCD, a few buttons (or keypad), a few leds, switches or rotary encoder and/or a few external lines to the outside world.

I wanted to know what is the best method to organise my code so everything is running smoothly.
Quite often when i press buttons or spin a rotary encoder, my LCD won't update, or doing one thing will delay the operation of another and so on....

I'd assume my buttons/keypad/rotary encoders should be HIGHer importance in looping, and the LCD, serout/control-lines out would be of lower importance?

Obviously my loops need to be sorted in way from MOST important to LEAST important with repspect to running in realtime or at leisure.

I know someone will say "use an interupt" but i haven't got that deep into using interupts yet...

Thanx in advance,
Marty.

tenaja
- 3rd July 2008, 13:47
Your descriptions sound pretty sound. :)

One thing I do is use flags to detect if an LCD needs updating or not. The setting and testing only take a few clock cycles, but save a lot. Another trick is to avoid updating the LCD too often. For instance, if your loop is going at 50Hz, there is no way you'll be able to read the display that fast. Anything much faster than about 10Hz is time wasted. You can set up a counter so you only update once every 5 loops or so.

Take note that the LCD commands are very slow, and made even worse by the built-in delay. (Which was put there to increase success rates, accommodating a variety of LCD screens.) You may want to play with the LCD Declare's to see if you can reduce the delay times to make your loop faster. The more characters you send, the more the delay effects your overall loop time. You might even want to set up a test loop just to time how long it takes to make your LCD update; the information should help you make decisions.

skimask
- 3rd July 2008, 13:50
My main question: Is there a 'preferred order' to layout programming code/loops/nesting within nests etc.
Yep...Your Order. You're the programmer. The order is up to you.


For example, i might have a 16x2 LCD, a few buttons (or keypad), a few leds, switches or rotary encoder and/or a few external lines to the outside world.
In my mind, the user comes first, everything else is secondary. You get stuck in a loop checking buttons and the LCD won't update and the user probably doesn't get any feedback that your button press has been recognized. Find something to make some feedback, blink the backlight on the LCD, blink an LED, add a small speaker and make it beep.


I know someone will say "use an interupt" but i haven't got that deep into using interupts yet...
It's the only way to go... Play with them. They're easier than you think...

gtvmarty
- 4th July 2008, 04:06
Thanx for the info so far guys....

I've since found (well hidden) in the back of the PB manual that i should be using for..next loops for delays instead of "pause 1000", otherwise i need to wait for the 1sec to elapse before my key's are read in etc (obviously missing keystrokes during that 1 second).
That's a MAJOR part of my problem solved.

Yep, i had been thru all the "flashing-LCD-by-writing-too-often-in-a-loop" problems ;-)


I'm still not keen on interupts ;-) but am finding (with lotsa fiddling) that i can nest loops within loops nicely now, and i do have priorities to loop more often for keys than LCD writes etc.

Thanx again,
Marty.