Programming with Basic
From Mechatronic Wiki
Contents |
Overview
Basic language is another example on how to program a microcontroller. Basic offer an easy to use language also a better understanding for programming and debugging.
Swordfish Compiler
Swordfish is a highly structured, modular PICĀ® BASIC compiler for the PIC18 family of PICĀ® microcontrollers. Swordfish is a true compiler that generates optimised, stand alone code which can be programmed directly into your microcontroller. Extensive library support is provided with full source code, some of which include LCD, GLCD, EEPROM, ADC, software and hardware SPI, software and hardware I2C, software UART, USART, string manipulation, USB and math libraries. Support for strings, arrays, structures, boolean, bit, unsigned and signed 8, 16 and 32 bit ordinal types and 32 bit floating point is also provided. Swordfish is supplied with a powerful and flexible Integrated Development Environment (IDE) which includes an advanced code explorer, full syntax highlighting, third party programmer integration, serial communicator and integrated boot loader application. Just a single mouse click, or key press, will compile, assemble and program your project into the target microcontroller.
General Information About Sordfish Compiler.
Below is the most important key compiler features.
1. Signed and unsigned ordinal, floating point, string and array constants Boolean, bit, 8, 16 and 32 bit signed and unsigned ordinal, floating point, char and string data types
2. Arrays and structures, including arrays of strings and structures
3. Alias and modifiers
4. Addressable EEPROM data constants
5. Conditional statements including if...then..elseif...else...endif and select...case
6. Repetitive statements including while...wend, repeat...until and for...next. Also supports the break and continue constructs
7. Short circuit Boolean expressions for faster code execution
8. Subroutines and Functions, including support for local constants, structures and variables.
9. Inline subroutines and functions
10. Overloaded subroutines and functions
11. Compound subroutines
12. Full support for passing by value and passing by reference. Functions can return all Boolean, bit, 8, 16 and 32 bit signed and unsigned ordinal, floating point, char and string types. Functions can also return structure types.
13.Powerful frame recycling algorithm ensures optimal RAM usage
14.All constants and declaration types can be declared as private or public, facilitating encapsulation or 'information hiding'
15. Typecasting support
16. Support for embedded assembler
17. Powerful hardware based interrupt support
18. Rich set of compiler directives including #ifdef, #constant, #variable, #define, #undefine, #ifdef...#else...#endif, #ifndef...#else...#endif, #if...#elseif...#else...#endif, #error, #warning and #option.
19. Predfined subroutines and functions, including AddressOf, BitOf, Bound, Dec, DelayMS, DelayUS,
20. High, Inc, Input, Low, Output, Terminate and Toggle
21. Extensive library support is provided with full source code, some of which include LCD, GLCD, EEPROM, ADC, software and hardware SPI, software and hardware I2C, software UART, USART, Secure Digital (SD), string manipulation, math, interrupt based RX and interrupt based timer libraries. Specific peripheral libraries are also included with full source. Some examples include the DS18B20, DS18S20 and DS2405.
22. Comprehensive set of relational, mathematical and logical operators
Sample Source Code for PWM:
// set clock and osc...
Device = 18F452
Clock = 20
// **************************************************************************
// * download PIC MultiCalc from http://mister-e.org/pages/utilitiespag.html
// * and then change to following constants to match...
// **************************************************************************
#option PRESCALER = 4 // prescale value [1, 4 or 16]
#option PR2_VALUE = 249 // prescale (PR2 register value)
#option MAX_DUTY = 1000 // max duty for this configuration
// bring options into the main program...
Const
#if PRESCALER = 1
PrescaleValue = $00,
#elseif PRESCALER = 4
PrescaleValue = $01,
#elseif PRESCALER = 16
PrescaleValue = $03,
#endif
PR2Value = PR2_VALUE,
MaxDutyValue = MAX_DUTY
// set duty routine...
Sub SetDuty(pDuty As Word)
CCP1CON.5 = pDuty.1
CCP1CON.4 = pDuty.0
CCPR1L = pDuty >> 2
End Sub
// local duty variable...
Dim Duty As Word
// program start...
Output(PORTC.2) // make PORTC.2 an output
CCP1CON = %00001100 // set CCP1 to PWM
T2CON = PrescaleValue Or $04 // set prescale, turn on TMR2 ($04)
PR2 = PR2Value // set PR2 to get 5KHz out @ 20MHz
// loop forever...
While true
Duty = 0
Repeat
SetDuty(Duty)
Inc(Duty, 10)
DelayMS(20)
Until Duty > MaxDutyValue
Wend
Basic Stamp Compiler
A BASIC Stamp is a single-board computer that runs the Parallax PBASIC language interpreter in its microcontroller. The developer's code is stored in an EEPROM, which can also be used for data storage. The PBASIC language has easy-to-use commands for basic I/O, like turning devices on or off, interfacing with sensors, etc. More advanced commands let the BASIC Stamp module interface with other integrated circuits, communicate with each other, and operate in networks. The BASIC Stamp microcontroller has prospered in hobby, lower-volume engineering projects and education due to ease of use and a wide support base of free application resources.
Sample Source Code:
again: 'beginning of the function
pulsout 0,150 '0, and 1 are the i/o pins on the
'stamp which the motors are on.
pulsout 1, 150 '150 is the ms pulse sent to the motors.
'adjust accordingly.
pause 15 'waits a tiny bit..
goto again 'begins the fuction again (creates a loop)

