![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| 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 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 by blueroomelectronics; 3rd January 2008 at 03:35 AM. | |
| |
| | (permalink) |
| .... Not | |
| |
| | (permalink) |
| 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 | |
| |
| | (permalink) |
| 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;
} 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;
} 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. | |
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) |
| 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 by blueroomelectronics; 3rd January 2008 at 06:01 AM. | |
| |
| | (permalink) | |
| Quote:
| ||
| |
| | (permalink) | ||
| Quote:
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. Quote:
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. 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! | |||
| |
| | (permalink) |
| 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. http://www.parallax.com/tabid/181/Li...me,ProductName 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 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 by blueroomelectronics; 3rd January 2008 at 07:14 AM. | |
| |
| | (permalink) | ||
| Quote:
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. Quote:
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 by nickelflippr; 3rd January 2008 at 07:57 AM. | |||
| |
| | (permalink) | |
| Quote:
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!). | ||
| |
| | (permalink) |
| 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:
} 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);
}
} 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 by 3v0; 3rd January 2008 at 03:59 PM. | |
| |
| | (permalink) |
| 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'd also like to avoid CCS as it is different than C18 and will only serve to confuse. Last edited by blueroomelectronics; 3rd January 2008 at 04:12 PM. | |
| |
| | (permalink) |
| Excellent -- I read another few chapters of the C18 User Manual this morning -- it's all starting to make sense now! | |
| |
| | (permalink) |
| 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. | |
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| Title | Starter | Forum | Replies | Latest |
| PIC Basic code error can you help, probably very simple | lompa | Micro Controllers | 3 | 2nd December 2004 05:28 AM |
| im stupid cos i can do this in a 'C program but not in basic | Ricardoco | Micro Controllers | 33 | 21st November 2004 09:15 PM |
| A Simple program for MAX238CNG | ice-egoz | Micro Controllers | 6 | 4th August 2004 05:00 AM |
| Simple Visual Basic question ( headache) | cubdh23 | General Electronics Chat | 1 | 14th June 2004 03:36 AM |
| Pic Basic Question | Tako Kichi | Micro Controllers | 2 | 13th November 2003 08:54 PM |