Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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: 14
"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: 3
Last edited:
"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 = 1 or PORTx = 0 is not used in the Pic18 series, LATx = 1 or LATx = 0 is used (the pins must be configured as outputs first).
PORTx is used in the Pic18 series as in input or pin reading (x.0 = PORTx.0 or x = PORTx) and the pins must be configured as inputs.
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 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).
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).
 

Latest threads

Back
Top