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.

Very simple program in Swordfish BASIC SE (free version)

Status
Not open for further replies.

blueroomelectronics

Well-Known Member
Here's a simple program for the 18F1320.
It's simply here to demostrate the simplicity and readability of BASIC
Code:
// Simply turns on an LED PortA.7 (pin 16)
Device = 18F1320        
Config OSC = INTIO2, WDT = OFF, LVP = OFF 
High (PORTA.7)          
End
Now BASIC assumes a lot for you and the compiler makes decisions based on what it thinks you're trying to do.
BASIC is good for getting your feet wet till you're ready to tackle C or ASM.

Remember: almost all early home computers came with BASIC in ROM.
 
Last edited:
The most complex part of the whole program is the config statement.
CONFIG is mandatory for PICs no matter which compiler / assembler you use.

What is CONFIG?
Config is not a program, it simply blows or clears fuse bits (it's an old term) and all they do is tell the microcontroller how it should be Configured on initial power up.
Now the default was chosen by manufacturer as a safe way to powerup.
Example1:
Code:
 Config OSC = INTIO2
By default the 18F1320 (and every other PIC) expects to see an external oscillator (crystal, resonator), but I wanted it to use its built in internal oscillator on powerup so I told the compiler to blow the "INTIO2" fuse (all fuses are set (1) when the chip is new or erased. You only need to clear or blow (0) the fuses you need.
 
Code:
// Simply turns on an LED PortA.7 (pin 16)
#include <p18cxxx.h>
#pragma config WDT = OFF, OSC = INTIO2, PWRT = ON, MCLRE = ON

void main()
{
    TRISA = 0x00;  // make all of port A inputs
    LATAbits.LATA7 = 1;
}

Other then showing the user that his code is in the main procedure you
specify the direction of the port bits. TRISA = 0x00

The CCS compiler has a directive/pragma that will set port direction for you as needed.

If you would like to further hide the hardware use this
Code:
// Simply turns on an LED PortA.7 (pin 16)
#include <p18cxxx.h>
#pragma config WDT = OFF, OSC = INTIO2, PWRT = ON, MCLRE = ON

#define OFF 0
#define ON  1
#define LED7 LATAbits.LATA7

void main()
{
    TRISA = 0x00;  // make all of port A inputs
    LED7 = ON;
}

I have a preteen girl that can write for and while loops. Convert one to the other and then write the equivlent code using goto.

Sorry about that. I have seen too many people learn basic first and never progress past it. For that reason I do not like it as a first language. I wish Pascal was still popular. It was a very good first language.
 
GCBasic can make lots of assumptions also.

#chip 18f4620,20
Dir PortA.7 Out
Set PortA.7 On

I don't understand why anyone has to learn a particular language if their stated purpose is hobby use. The ultimate goal for many is to build interesting, sometimes useful, projects and having fun doing it.
 
Easy enough, what is the linker file with an "i"
eg: 18F1320i.lkr

Yes many people would never go beyond BASIC, and for many that's all they ever needed. So many people shun C and ASM because they are strict and not the easiest languages to get a grip with.
BASIC is simple and easy for almost anyone to grasp. True it hides much of what's going on inside the PIC and generates fairly bloated code, but we all crawl before we walk.
I've programmed in plenty of BASICs and I have no problem with MPASM. Swordfish is not Stamp BASIC or PICBASIC, it's a very modular and highly structured language. Its editor is teriffic and easy to use.

PS I too wish Pascal was one of the more popular languages too. I still hate all those curly brackets in C, and really really hate == why couldn't they use := like Pascal? ++ -- puddles o fun.

Edit:

PPS why does C use this command instead of...
LATAbits.LATA7 = 1;

wouldn't this be more logical?

LATA.7 = 1;

MPLAB compiled the program, it claims 109bytes program and 76bytes RAM? That seems a little high. In assembly it would be about 4 bytes long and zero RAM.
 
Last edited:
nickelflippr said:
GCBasic can make lots of assumptions also.

#chip 18f4620,20
Dir PortA.7 Out
Set PortA.7 On

I don't understand why anyone has to learn a particular language if their stated purpose is hobby use. The ultimate goal for many is to build interesting, sometimes useful, projects and having fun doing it.

I've looked at GCBasic and even chatted with the developer about a version for the Mongoose. I looked at Swordfish SE and it's really a nice compiler/IDE. The limitation of 256 bytes RAM is generous too for the free SE edition.
 
nickelflippr said:
I don't understand why anyone has to learn a particular language if their stated purpose is hobby use. The ultimate goal for many is to build interesting, sometimes useful, projects and having fun doing it.

You do not.

However if there is any chance that the hobby could turn into a profession, then it matters.

When people are told "learn Basic, it is easy". They do find it easy at first. In time they run into the complexities that exist in any usable language and say to themselves "Gosh, it is a good thing I only tried Basic. As tough as this easy language is I would have no chance of learning a hard language like C". Then they go off to work at McDonald's.

http://www.euclideanspace.com/software/language/basic/index.htm said:
However Basic does have some bad points, it is not very scalable, it can encourage bad programming practices and interpreted programs can run slower. There are modern dialects of Basic which get round these problems by making the language more like a modern structured language but then you loose the simplicity.

It is a trade-off. You either get a BASIC with the flaws. Or fix the flaws and end up with a language so much like any other procedural language that it should not be called basic.

For the most part I do not care what language people use. To each his own. Good code can be written in most any language by a good programmer.

If I were in it purely for fun I would use, teach, and preach forth. Forth is a very extensible language. In forth you can do things like write code to generate code and execute it. :D Write a basic interpreter in forth. Forth allows for in-line assembly, but the assembler is also written in forth. blather...

However the interest of the student comes first, and to that end I need to use a language that will be recognised, maybe even helpful, when they hit collage. At the uC level that is currently C.

I doubt anyone reads this far!
 
Parallax sold 90,000 BOE-Bots and countless BASIC Stamps to schools, they have several books designed for classrooms aimed at the high-school level. Sure not everyone will go beyond BASIC, but not everyone is a rocket scientist either. Youth is an impatient lot and if you can get their attention and hold onto it you're on the right track.
**broken link removed**
There appears to be plenty of young students around kumlinks age here that want to see results right away, else they lose interest.

Lets try lesson 2: in C18 and Swordfish BASIC *you'll need to provide the C18 code 3V0
Code:
// Lesson 2 
// blinks an LED PortA.7 (pin 16)
Device = 18F1320        
Config OSC = INTIO2, WDT = OFF, LVP = OFF 
Main:
    Toggle(PORTA.7)
    DelayMS(1000)
GoTo Main         
End

Same code without GoTo
Code:
// Lesson 2 
// blinks an LED PortA.7 (pin 16)
Device = 18F1320        
Config OSC = INTIO2, WDT = OFF, LVP = OFF 
While True
    Toggle(PORTA.7)
    DelayMS(1000)
Wend         
End
 
Last edited:
Swordfish is not Stamp BASIC or PICBASIC, it's a very modular and highly structured language. Its editor is teriffic and easy to use.

I should take a look at Swordfish, seems to get good reviews from the forum. But its like a document/editor, CAD, browser, etc. once you are used to one and all their built defeciencies, you hate to switch. But sometimes you are compelled to switch.

My background in the microcontroller area is really skinny. Started out on an OOPic (interpreted language) which lets you use a C or Basic type of syntax, if you could call it that. From what little I know, it looks more like Java with the command.Dot.method.Dot.blah.Dot.blah. What was I tryin to do here? I forget already. I was compelled to switch to a bare Pic because the OOPic was not fast enough for Dallas one wire communication.

The basic type syntax just seems more readable to me personally.

I doubt anyone reads this far!

No, I read this far, as other peoples deeper experience and different perspectives help me understand the debates that pop up, which micro?, or that language?

When I visited my old college during a senior projects day, I was encouraged that real world type problems were being addressed. Also that the main robot platform was an Atmel 128 with programming done in C.

So yes, by all means C if your future is dependent on it.

BooHoo... no toggle command in GCBasic
Code:
#chip 18f4620,20
Dir PortA.7 Out
Main:
Set PortA.7 On
wait 1 sec
Set PortA.7 Off
wait 1 sec
goto Main
 
Last edited:
3v0 said:
I doubt anyone reads this far!

I did! :D

I read a lot and take in a lot of information -- and would like to say thanks for the advise you give out -- in this thread, in the "C Microcrontroller tutorial" thread and every other thread that I have been finding on the topic of C lately -- it is not going unnoticed (and I can't give you rep until I "spread some around" -- sorry!).
 
Lesson 2: C18 code
Code:
// Flash LED on PortA.7 (pin 16)
#include <p18cxxx.h>
#pragma config WDT = OFF, OSC = INTIO2, PWRT = ON, MCLRE = ON

void main()
{
    TRISA = 0x00;  // make all of port A inputs
    Loop:
        LATAbits.LATA7  = !LATAbits.LATA7;
        delay_ms(1000);
    goto Loop:
}
The not operator (!) is not as obvious as toggle.

Code:
// Flash LED on PortA.7 (pin 16)
#include <p18cxxx.h>
#pragma config WDT = OFF, OSC = INTIO2, PWRT = ON, MCLRE = ON

#define FOREVER 1
void main()
{
    TRISA = 0x00;  // make all of port A inputs
    while(FOREVER)
    {
        LATAbits.LATA7 = !LATAbits.LATA7;
        delay_ms(1000);
    }
}

I could have said while(1) and saved a line.
All the examples show the use of an uninitialized value. It does not matter here but it is poor form esp given the context.

The difference is trival. But if you want a C compiler that will really hold your hand use the CCS products. They do this to such a point that some C programmers complain about it. They also do a good job of generating good code on PIC16 processors which is not an easy thing to do.

EDIT: I failed to point out that delay_ms is dependant on the processor speed.
CCS adjusts it using
#pragma use delay(clock=8000000)
With C18 you need to do that adjustment manualy by adding delays in terms of clock cycles to the delay_ms function. A good tutorial or example will either give you the code for this or show you how to do it. All languages and compiles need to deal with this issue.
 
Last edited:
Ahh you're right, I stand corrected. You do need the CLOCK directive.
Code:
 // Lesson 2.1 (corrected thanks 3V0) 
// blinks an LED PortA.7 (pin 16)
Device = 18F1320
Clock = 4 
Config OSC = INTIO2, WDT = OFF, LVP = OFF
OSCCON = $62         // sets 4MHz clock default is 32KHz 
While True
   Toggle(PORTA.7)
   DelayMS(1000)
Wend 
End

I have no doubt C in the long run is more powerful than BASIC. Point is you have to start somewhere. I would like to put all this in the book.
I'd also like to avoid CCS as it is different than C18 and will only serve to confuse.
 
Last edited:
Excellent -- I read another few chapters of the C18 User Manual this morning -- it's all starting to make sense now! ;)
 
It is not that it is impossible to use basic as a first language with good results. But to do so one needs to be careful about how one goes about it. Examples in any language need to be well formed and follow best programming practices.


Bill, I tried Skype but got no answser. I need to get to school as I am already somewhat late.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top