If_Then_Else_Endif

bcz

Member
I want to control the program execution depending on a jumper setting in the hardware.
The program Voltmeter should execute only for RB0 = 0
What is wrong here?
---------------------------------------------------------------
PORTB.0 = 1 'Simulating Jumper Pin RB0 = 1
If PORTB.0 = 1 Then
Lcdcmdout LcdClear
WaitMs 100
Lcd_Out(1,1,"Mode: PWR-Meter ")
Lcd_Out(2,1,"Set to Voltmeter")
WaitMs 2000
Else 'Pin RB0 = 0 (Mode: Voltmeter)
-----------------------------------------------------------------
The complete source code is attached.
 

Attachments

  • Voltmeter v1.bas
    3.2 KB · Views: 16
"Goto" has not been used in high level languages for more than 30 or 40 years. By tradition in Basic language it is used only to establish the main loop or routine.
PORTx.x = 1 or PORTx.x = 0 is not used in the Pic18 series, LATx.x = 1, LATx.x = 0 or LATx = %01010101 is used (the pins must be configured as outputs first).
PORTx is used in the Pic18 series as in input or pin reading (y.0 = PORTx.0, y = PORTx or If PORTx.x = 1 Then ..... ) and the pins must be configured as inputs.

You can also do "Gosub GlobInitVolt" before entering the main routine or loop.
 

Attachments

  • Voltmeter v1.bas
    3.3 KB · Views: 5
Last edited:
Goto is still used in C. It is a clear and reliable way to exit nested loops. Some people may not like it for readability but it sure clears up a bunch of conditional breaks to exist the nested loops - an immediate jump out with no risk of a partial outer loop being executed along the way, no flags or other code to check on the way out. Simple and fast and actually adds clarity in some complex nested loops.
 
I'm not going to get into the discussion of "goto" yes or "goto" no. I'm just pointing out the general trend and I totally disagree with you. "goto" has more disadvantages than advantages. I program professionally in C and I don't use it and this is the first time I've heard of someone who uses it (in medium and high level languages).
 
It would help if we knew which processor it was for. I'm guessing an 8085 variant.

Mike.
Edit, just opened the attachment and see it's for an 18 series pic chip. You don't write to Triss anywhere or is this a "hidden" feature?
 
The source code (Voltmeter.bas) is for a PIC18F2550 MCU.
Triss - Do you mean TRISA, TRISB, etc.?
I am an "amateur" programmer and I do know that many peaple say, that any BASIC dialect and especially using "Goto" is a so called "Spaghetti Code".
For tens of years I have not used any BASIC, Turboscal or Assembler for that matter.
I came back to writing code to fix a PIC18F2550 application because I killed the MCU.
The program was written in mikroBasic by mikroElektronika, but the trial period has long expired.
 
I'm not here to argue the merits of goto, just pointing out that your claim...
"Goto" has not been used in high level languages for more than 30 or 40 years.
...is purely incorrect. It is part of a high level language and people do currently use it in specific instances. Kernels for operating systems and embedded systems. The Linux core makes extensive use of goto in C because it's a blazing fast solution to error handling in nested If or nested loops.

JavaScript developers realized the simplicity of using GOTO to break out of complex nested situations but the negative opinion of using GOTO resulted in a brilliant alternative - simply renaming GoTo to "break" with a label (known as a "labelled break" and nobody ever complained about spaghetti code or the GoTo, it's just known as a super convenient feature of JavaScript - and it doesn't have to be used as a break - it can just be used like a GoTo (which it is).
 
As Dog said... It has been used in basic for the "main" loop for ever!

The only reason people frown on goto's is because with high level language not all programmers understand the properties of a hard / software stack.

If you understand the stack then you can use them as much as needed..

To reword... goto's should be used cautiously
 

I also program 'professionally' in C (in that I design and program products for sale at work) - however, I come from a Pascal background, which is far more strictly typed than C, and 'goto' is strongly discouraged. This of course was why Pascal was long the language of choice for teaching at Universities - it forced good practice.

To the extent that when I had to use it (I was converting and rewriting a PIC BASIC Compiler that was to be built-in to 'PicProg BASIC' from the partially written Visual BASIC source code that someone sent me, and there were LOT'S of goto's) I had to look up how to use goto in Pascal/Delphi. I eliminated most of the goto's by altering the flow of the code, but a couple I couldn't overcome, without a major rewrite.

I don't recall ever using it in C either?, but with my Pascal background I never even think of using a goto.
 
In number #2 I left you your file with some modifications, it may still not do exactly what you want but the RB0 included in the conditional already works, you can continue from there.
 
From an Assembly language programmer of 40+ years, not sure if that qualifies as professional or not, "GOTO" and even "GOSUB" (aka CALL) are valid and used quite often. I program from a Flowchart based structure, where multiple flowcharts can interdigitate seamlessly within the program, giving better response and RTOS operations. For that to work each node in the flowchart must NOT be blocking. A Dispatcher is used in a binary tree configuration to allow each node to process quickly and independently. If you look at a flowchart as a state machine, each independent flowchart has it's own index referencing the active node within the flowchart. Each node is capable of reasigning an index value and ecah node returns to the dispatcher instead of the next node. I have programmed this way for years, and it makes it so much easier to come back to code weeks, months, even years later and be able to make adjustments. Also makes it easier going from one project to another and getting your head around the code in a timely manor. This programming structure also allows for other flowcharts to be added or removed. I have worked for a design company where we might have had 5 or 6 projects going at a time. Going from one project to the other, I would not have been able to do it without the active use of GOTO and the structure outlined above.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…