MikroElektronika User’s manual Development tools - Books - Compilers www.mikroelektronika.co.yu Basic Compiler for Microchip PIC microcontrollers mikroBASIC making it simple... Develop your applications quickly and easily with the world's most intuitive Basic compiler for PIC Microcontrollers (families PIC12, PIC16, and PIC18). Highly sophisticated IDE provides the power you need with the simplicity of a Windows based point-and-click environment.
mikroBASIC User’s manual Table of Contents CHAPTER 1 mikroBasic IDE Learn all about our advanced integrated environment. Create projects, debug your code, view statistics, and much more. CHAPTER 2 mikroBasic Reference BASIC language, syntax, and PIC specifics CHAPTER 3 Built-In and Library Routines Included routines allow you to create complex applications quickly and easily. Many useful routines for use with CAN, ADC, PDWM, CompactFlash, and other modules are at your disposal.
mikroBASIC making it simple...
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... To readers note DISCLAIMER: The mikroBASIC compiler and this manual are owned by MikroElektronika and is protected by copyright law and international copyright treaty. Therefore, you should treat this manual like any other copyrighted material (e.g., a book). The manual and the compiler may not be copied, partially or as a whole without the written consent from the MikroEelktronika.
C H A P T E R 1 mikroBasic IDE QUICK OVERVIEW mikroBasic is a Windows-based Integrated Development Environment, and is much more than just Basic compiler for PIC MCUs. With mikroBasic, you can: 1. 2. 3. 4. 5. 6. Create Basic source code using the built-in Code Editor Compile and link your source code Inspect program flow and debug executable logic with Debugger Monitor variables in Watch Window Get error reports Get detailed statistics (how compiled code utilizes PIC MCU memory, hex map, charts and more.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Watch Window Code Explorer Code Editor Breakpoints Dialog Error Window Code Assistant Code Editor features adjustable Syntax Highlighting, Code Assistant, Parameters Assistant, Auto Correct for common typos, and Code Templates. Code browser, Keyboard shortcut browser, and Quick Help browser are at your disposal for easier project management.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CODE EDITOR Basic Editor Features General code editing is same as working with any standard text-editor, including familiar Copy, Paste, and Undo actions, common for Windows environment.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Advanced Editor Features Code Assistant [CTRL+SPACE] If you type first few letter of a word and then press CTRL+SPACE, all valid identifiers matching the letters you typed will be prompted to you in a floating panel (see the image). Now you can keep typing to narrow the choice, or you can select one from the list using keyboard arrows and Enter.
mikroBASIC making it simple... Comment / Uncomment Icon. mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Also, Code Editor has feature to comment or uncomment selected block of code by simple click of a mouse, using icons and from Code Toolbar. Bookmarks Bookmarks make navigation through large code easier. CTRL+ : Goto bookmark CTRL+SHIFT+ : Set bookmark Goto Line Goto Line option makes navigation through large code easier.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CODE EXPLORER Find Declaration Icon. Collapse/Expand All Icon. Code Explorer is placed to the left of the main window by default, and gives clear view of every declared item in the source code. You can jump to declaration of any item by right clicking it, or by clicking the Find Declaration icon. To expand or collapse treeview in Code Explorer, use the Collapse/Expand All icon.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CREATING FIRST PROJECT Step 1 From a drop-down menu, select: Project > New Project, or click New Project icon New Project Icon. Step 2 Fill the New Project Wizard dialog with correct values to set up your new project. - Select a device for your project from the drop-down menu - Set configuration bits (Device Flags) by clicking Default push-button. - Select Device Clock by entering appropriate value in edit box.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... After you have set up your project, select OK push button in New Project Wizard dialog box. mikroBasic will create project for you and automatically open the program file in code editor. Now we can write the source code. Step 3 After you have successfully created an empty project with New Project Wizard, Code Editor will display an empty program file, named same as your project.
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Compile Icon. making it simple... Step 4 Before compiling, it is recommended to save the project (menu choice File>Save All). Now you can compile your code by selecting menu Run > Compile, or by clicking the Compile icon. mikroBasic has generated hex file which can be used to program PIC MCU. But before that, let's check our program with the Debugger. Also mikroBasic generates list and assembly files. Debug Icon.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers We will use the Step Over option (Run > Step Over or F8) to execute the current program line. Now, you can see the changes in variables, SFR registers, etc, in the Watch Window – items that have changed are marked red, as shown in the image below. We could have used Run/Pause (F6) option to execute all the instructions between the active line and the breakpoint (Run > Run/Pause Debugger).
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Step 6 Now we can use hex file and feed it to the device (PIC16F877 or other). In order to do so hex file must be loaded in programmer (PIC Flash by mikroElektronika or any other).
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers PROJECTS Each application, or project, consists of a single project file and one or more unit files. You can compile source files only if they are part of the project. First and essential step is creating a project. We will use New Project Wizard to create our new project.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Managing Source Files Source files created in mikroBasic have the extension ppas. By default, main unit file is named same as the project. Location of the main unit source file and other project information are stored in project file with extension ppp. Creating Main Unit File Main unit file is created simultaneously with the project and is named same as the project, with extension ppas.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Printing an Open File Make sure that window containing the file you want to print is the active window. Select File > Print from drop-down menu, or press CTRL+P, or click the Print icon. In the Print Preview Window, set the desired layout of the document and click the OK button. The file will be printed on the selected printer.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Compile and Link Source Code Compile Icon. When you have created the project and written the source code, you will want to compile it. Select Run > Compile from drop-down menu, or click Compiler Icon from Compiler Toolbar. Progress bar will appear to inform you about the status of compiling. If no errors are encountered, mikroBasic will produce hex file, assembly file, and list for the appropriate PIC MCU.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers DEBUGGER Source-level Debugger is integral component of mikroBasic development environment. It is designed to simulate operations of Microchip Technology's PIC MCU’s and to assist users in debugging Basic software written for these devices. Debug Icon. Debug Icon.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Run/Pause Debugger [F6] Run or pause Debugger. Run > View Breakpoints Invoke breakpoints window, with list of breakpoints. Double clicking item in window list locates breakpoint. Watch Window Watch Window allows you to monitor program items while running your program. It displays variables and special function registers of PIC MCU, their addresses and values. Values are updated as you go through the simulation.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ERROR WINDOW In case that errors were encountered during compiling, compiler will report them and won't generate a hex file. Error Window will be prompted at the bottom of the main window. Error Window is located under message tab, and displays location and type of errors compiler has encountered. Compiler also reports warnings, but these do not affect generating hex code.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... ASSEMBLY VIEW Assembly Icon. After compiling your program in mikroBasic, you can click toolbar Assembly icon or select Project > View Assembly from drop-down menu to review generated assembly code in a new tab window. Assembly is human readable with symbolic names. All physical addresses and other information can be found in Statistics or in list file.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers STATISTICS Statistics Icon. After successful compiling, you can review statistics on your code. Select Project > View Statistics from drop-down menu, or click the Statistics icon. There are five tab windows: Memory Usage Window Provides overview of RAM and ROM memory usage in form of histogram. RAM Window Summarizes all GPR and SFR registers and their addresses.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... ROM Window Lists op-codes and their addresses in form of a human readable hex code. Procedures (Graph) Window Displays procedures and functions in form of histogram, according to their memory allotment. page 22 Chapter 1.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Procedures (Details) Window Displays complete call tree, along with details for each procedure and function: size, start and end address, frequency in program, return type, etc.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... INTEGRATED TOOLS USART Terminal mikroBasic includes USART (Universal Synchronous Asynchronous Receiver Transmitter) communication terminal for RS232 communication. You can launch it from drop-down menu Tools > Terminal or by clicking the icon . ASCII Chart ASCII Chart is a handy tool, particularly useful when working with LCD display. You can launch it from drop-down menu Tools > ASCII chart.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers 7 Segment Display Decoder 7seg Display Decoder is a convenient visual panel which returns decimal/hex value for any viable combination you would like to display on 7seg. Click on the parts of 7 segment image to the left to get the desired value in the edit boxes. You can launch it from drop-down menu Tools > 7 Segment Display.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... KEYBOARD SHORTCUTS Complete list of keyboard shortcuts is available from Code Explorer window, tab Keyboard.
mikroBASIC making it simple...
C H A P T E R 2 mikroBasic Reference ‘Why Basic?’, you may wonder. Well, the answer is simple: it is legible, easy-tolearn, procedural programming language, with sufficient power and flexibility needed for programming microcontrollers. Whether you had any previous programming experience, you will find that writing programs in mikroBasic is very easy.This chapter will help you learn or recollect Basic syntax, along with the specifics of programming PIC Microcontrollers.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers IDENTIFIERS Identifiers are names used for referencing the stored values, such as variables and constants. Every program, procedure, and function must be identified (hence the term) by an identifier. Rules Valid identifier: 1. must begin with a letter of English alphabet or possibly the underscore (_) 2. can be followed by alphanumeric characters and the underscore (_) 3.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... KEYWORDS The following keywords (reserved words) cannot be redefined or used as identifiers.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers DATA TYPES Type determines the allowed range of values for variable, and which operations may be performed on it. It also determines the amount of memory used for one instance of that variable. Simple Type byte char* word short integer longint Size 8-bit 8-bit 16-bit 8-bit 16-bit 32-bit Range of values 0 .. 255 0 .. 255 0 .. 65535 -128 .. 127 -32768 .. 32767 -2147483648 ..
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Array Array is a set of data stored in consecutive memory locations. Defining an array and manipulating its elements is simple. Elements of array are always of same data type (any simple). dim dim dim days_of_the_week as byte[7] months as byte[12] AD_Conversion_result as word[10] First declaration above generates 7 variables of byte type.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers For example: dim size as longint[10] occupies 40 RAM locations (bytes). PIC MCU RAM m[0] m[0] m[0] m[1] m[0] m[0] m[2] m[0] m[3] Array is just a specified set of data in memory, stored in consequent locations After you have declared an array, for example: dim m as byte[5] you can easily access its elements m[0],m[1],m[2]....
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Strings String represents a sequence of characters. String type is similar to array, but can hold only characters. dim M_name as string[16] dim Start_message as string[6] For each string declaration, compiler will reserve the appropriate amount of memory locations. For example, string M_name will take 16+1 locations; additional memory location is reserved to contain the length of the string.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers NUMERALS AND CHARACTER STRINGS Numerals Numeric constants can be represented in decimal, binary, or hexadecimal number system. In decimal notation, they are represented as a sequence of digits, without commas or spaces, and can be prefixed with + or - operator to indicate the sign. Values default to positive (67258 is equivalent to +67258).
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CONSTANTS Constant is data whose value cannot be changed during the runtime. Every constant is declared under unique name which must be a valid identifier. It is a good practice to write constant names in uppercase. In mikroBasic, constants have to be of simple data type (no arrays or strings are allowed).
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... SYMBOLS Symbol makes possible to replace expression with a single identifier alias. Use of symbols increases the reusability and flexibility of code. BASIC syntax restricts you to single line expressions, allowing shortcuts for constants, simple statements, function calls, etc. Scope of symbol identifier is a whole source file in which it is declared.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers VARIABLES Variable is data whose value can be changed during the runtime. Every variable is declared under unique name which must be a valid identifier. This name is used for accessing the memory location occupied by the variable. Variable can be seen as a container for data and because it is typed, it instructs the compiler how to interpret the data it holds.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Note mikroBasic recycles local variable memory space - local variables declared in different functions and procedures share same memory space, if possible. Additional info Variable declaration has to be properly placed to have a correct meaning. Variables can be declared in a program block or implementation section of a module. Variable declaration must be placed ahead of the keyword begin.
mikroBASIC making it simple... Examples mikroBASIC - Basic Compiler for Microchip PIC microcontrollers program TRIAL include "other.pbas" ' You can declare variables in the program block dim dim dim dim dim dim tA tD tF tR tT tY as as as as as as integer integer integer word word word main: tA = tD and tF tR = STATUS and $03 TMR0 = 45 end. ... module other ' You can declare variables at the ' beginning of a module dim Sss as longint dim Ddd as longint ... end.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Any valid variable can be used after it has been declared: tA ' tC ' ' = 36 assign new value to the existing variable = tA + tB perform any kind of arithmetical or logical operation tE = pr_function(1,tA) ' pass variable to function or procedure, ' by value or address pr_procedure(1,2,tD,tE) ' use them in conditional and/or ' loop statements and more ...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers COMMENTS Comments are text that is added to the code for purpose of description or clarification, and are completely ignored by the compiler. ' Any text between an apostrophe and the end of the ' line constitutes a comment. May span one line only. It is a good practice to comment your code, so that you or anybody else can later re-use it.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... EXPRESSIONS Expression is a construction that returns a value. The simplest expressions are variables and constants, while more complex expressions are constructed from simpler ones using operators, function calls, indexes, and typecasts. Rules for creating legal expressions are presented in chapter Implicit Conversion and Legal Expressions.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers You cannot mix signed and unsigned data types in expressions with arithmetic or logical operators. You can use explicit conversion though. dim dim dim dim dim ... A = A = Sa as short A as byte Bb as word Sbb as integer Scccc as longint A + Sa A and Sa ' compiler will report an error ' compiler will report an error ' But you can freely mix byte with word.. Bb = Bb + (A * A) ' ..
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... DECLARATIONS AND STATEMENTS Aside from the include clause, program consists entirely of declarations and statements, which are organized into blocks. Declarations Names of variables, constants, types, procedures, functions, programs and units are called identifiers (numeric constant like 321 is not an identifier). Identifiers need to be declared before you can use them.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Statements Statements define algorithmic actions within a program. Simple statements - like assignments and procedure calls - can be combined to form loops, conditional statements, and other structured statements. Refer to Implicit Conversion and Assignment. Simple Statements Simple statement does not contain any other statements. Simple statements include assignments, and calls to procedures and functions.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers DIRECTIVES Directives are words of special significance for the mikroBasic, but unlike other reserved words, appear only in contexts where user-defined identifiers cannot occur. You cannot define an identifier that looks exactly like a directive.
mikroBASIC making it simple... Important mikroBASIC - Basic Compiler for Microchip PIC microcontrollers We recommend careful use of absolute directive, because you may overlap two variables by mistake.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... PROCEDURES AND FUNCTIONS Procedures and functions, collectively referred to as routines, are self-contained statement blocks that can be called from different locations in a program. Function is a routine that returns a value when it is executed. Procedure is a routine that does not return a value. Once these routines have been defined, you can call them once or multiple times.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers This means that the procedure call pr1_procedure(tA, tB, tC, tD) passes tA and tB by the value: it first creates par1 = tA and par2 = tB, then manipulates par1 and par2 so that tA and tB remain unchanged; passes tC and tD by the address: whatever changes are made upon vp1 and vp2 are also made upon tC and tD.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... In mikroBasic, use the keyword Result to assign the return value of a function. Example: sub function pr6_function(dim par1 as byte, dim par2 as word) as word dim locS as word locS = par1 + par2 Result = locS end sub Function Calls As functions return a value, function calls are technically expressions.
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Example of Stack Overflow making it simple... sub program Stack_overflow sub procedure interrupt nop end sub sub procedure proc0 nop end sub sub procedure proc1 proc0 end sub sub procedure proc2 proc1 end sub sub procedure proc3 proc2 end sub sub procedure proc4 proc3 end sub sub procedure proc5 proc4 end sub sub procedure proc6 proc5 end sub sub procedure proc7 proc6 end sub main: proc7 end.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers MODULES Each project consists of a single project file, and one or more module files. To build a project, the compiler needs either a source file or a compiled file for each module. Project file Module files Every project consist of single project file and one or more module files Each module is stored in its own file and compiled separately; compiled modules are linked to create an application.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Unit Influence on Scope (Visibility) mikroBasic variables defined at the beginning of the module are global hidden variables. When you declare an identifier at the beginning of a module, you cannot use it outside the unit, but you can use it in any routine defined within the module. Refer to chapter Scope (Variable Visibility) for more details.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Module files Project file mikroBasic 1. Once you have written your program, mikroBasic can compile each unit file and create mcl files Compiler mcl files Def file 2. Linker mikroBasic links mcl files and creates asm, list and hex files Library mcl files Output Generator Output Generator HEX file LST file ASM file 3.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... SCOPE (IDENTIFIER VISIBILITY) Scope, or identifier visibility, determines if identifier can be referenced in certain part of the program code. Location of identifier declaration in the code determines its scope. Identifiers with narrower scope - especially identifiers declared in functions and procedures - are sometimes called local, while identifiers with wider scope are called global.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers For example, in a function declaration: sub function Com(dim R as byte) as byte dim B as char dim K as byte ... end sub first line of the declaration is the function heading . B and K are local variables; their declarations apply only to the Com function block and override - in this routine only - any declarations of the same identifiers that may occur in the program module or at beginning of a module.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... PROGRAM ORGANIZATION Program elements (constants, variables and routines) need to be declared in their proper place in the code. Otherwise, compiler may not be able to comprehend the program correctly. Organization of the main unit should have the following form: program program_name include ... ' program name ' include other units symbol ... const ... dim ...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Organization of other modules should have the following form: module unit_name include ... ' unit name ' include other units symbol ... const ... dim ... ' symbols declaration ' constants declaration ' variables declaration sub procedure procedure_name ... end sub ' procedures declaration sub function function_name ... end sub ' functions declaration end.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... TYPE CONVERSION mikroBasic is capable of both implicit and explicit conversion of data types. Implicit conversion is the one automatically performed by compiler. On the other hand, explicit conversion is performed only on demand issued by user. This means that you can, obeying a few rules, combine simple data types with any operators to create legal expressions and statements.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Special case is conversion between signed and unsigned. It is important to understand that explicit conversion between signed and unsigned data does not change binary representation of data; it merely allows copying of source to destination. Example 1: dim tA as byte dim tB as byte if tA + tB > tC then tA = 0 end if This could be wrong, because there is an expression on the left.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Example 2: Explicit conversion can also be used when you are sure which type you want to convert expression to.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ASSIGNMENT AND IMPLICIT CONVERSION Overview mikroBasic provides automatic type conversion every time an assignment is performed. But it does not allow assigning signed data to unsigned and vice versa, because there is a significant risk of losing information.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... For signed types : dim dim ... A_ B_ B_ as integer A_ as short = -10 = A_ ' B becomes -10 In hex representation, this means that the higher byte is sign extended. C = expression Calculated value of the expression will be assigned to the destination. Part of the information may be lost if the destination cannot properly represent the result of the expression (i.e.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Lets see what happens when we add two bytes and assign result to byte byte3 First byte has value 241 and its binary representation is 11110001 = byte2 + byte1 1 1 1 1 0 0 0 1 + 1 0 0 0 0 0 0 0 Second byte has value 128 and its binary representation is 10000000 = 1 0 1 1 1 0 0 0 1 Result has 9 bits.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... As already stated, destination will store the correct value only if it can properly represent the result of the expression (that is, the result fits in destination range).
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers IMPLICIT CONVERSION AND LEGAL EXPRESSIONS Overview To create legal expressions, you can: 1. combine byte and word with any operators, 2. combine short, integer, and longint (note that longint does not employ *, div, mod) with any operators, 3. use Relation operators expression1 (relation operator) expression2 Expression1 and expression2 can be any legal expressions.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... - If one operand is byte and another is word, byte is converted in word. - If one operand is short and another is integer, short is converted to integer. - If one operand is short and another is longint, short is converted to longint. - If one operand is integer and another is longint, integer is converted to longint. 4.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers r = expression Refer to chapter Assignment and Implicit Conversion for details. Note When adding operands of the same type and assigning value to third operand, incorrect value may be proceeded if the result exceeds range of declared data type. Similar rules apply to other arithmetical operators.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Example making it simple... if A > B then ... if A > 47 then ... if A + B > A ... First, compiler evaluates the expression on the left. During runtime, result is stored in a variable of type that matches the largest data type in the expression. In this case it is byte, as variables A and B are both bytes. This is correct if the value does not exceed range 0..255, that is, if A + B is less than 255.
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Rules for Creating Legal Expressions You cannot mix signed and unsigned data types in expressions with arithmetic or logical operators. If you need to combine signed with unsigned, you will have to use explicit conversion. Example: dim Sa as short dim teA as byte dim Bb as word dim Sbb as integer dim Scccc as longint ...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Notes for Relation Operators Comparing variable or constant to variable or constant will always produce correct results. Comparing expressions requires a little more attention - when compiler is calculating value of the expression to be compared, it first has to evaluate the expression.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Unsigned and Conversion If number is converted from less complex to more complex data type, upper bytes are filled with zeros. If number is converted from more complex to less complex data type, data is simply truncated (upper bytes are lost). Signed and Conversion If number is converted from less complex to more complex data type, upper bytes are filled with ones if sign bit equals 1 (number is negative).
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Unary arithmetic operators Operator Operation Operand Types Result Types +(unary) sign identity short, integer, longint short, integer, longint - (unary) sign negation short, integer, longint short, integer, longint Unary arithmetic operators can be used to change sign of variables: a = 3 b = -a ' assign value -3 to b Runtime Behavior PIC microcontrollers are optimized to work with bytes.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Boolean Operators Boolean operators are not true operators, because there is no boolean data type defined in mikroBasic. These "operators" conform to standard Boolean logic. They cannot be used with any data type, but only to build complex conditional expression.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple...
mikroBASIC making it simple... Unsigned and Conversion mikroBASIC - Basic Compiler for Microchip PIC microcontrollers If number is converted from less complex to more complex data type, upper byte is filled with zeros; If number is converted from more complex to less complex data type, data is simply truncated (upper bytes are lost). Example for unsigned and logical operators : dim dim teA as byte Bb as word main: Bb = $F0F0 teA = $aa Bb = Bb and teA end.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers main: Sbb = $OFF0 Saa = $0a Sbb = Sbb and Sa end. making it simple... ' Bb becomes $0000 In this case, Sa is treated as an integer with the upper byte equal to $00 (this in fact is sign extending of short to integer) prior to the operation. Example dim teA as byte dim teB as byte dim teC as byte ' The logical operators perform bitwise manipulation ' on the operands.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ' You cannot mix signed and unsigned data types in ' expressions with logical operators: dim dim dim dim dim Sa as short teA as byte Bb as word Sbb as integer Scccc as longint main: teA = teA + Sa teA = teA and Sa ' compiler will report an error ' compiler will report an error ' But you can freely mix byte with word . . Bb = Bb and ( not teA) ' . . and short with integer and longint.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers If one operand is byte and another is word, byte is converted in word. If one operand is short and another is integer, short is converted to integer. If one operand is short and another is longint, short is converted to longint. If one operand is integer and another is longint, integer is converted to longint. 4.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Examples making it simple... Comparing variables and constants always produces the correct results regardless of the operands' type. if A > B then ... if A > 47 then ... if A + B > A ... First, compiler evaluates the expression on the left. During the run-time, result is stored in a variable of type that matches the largest data type in the expression. In this case it is byte, as variables A and B are both bytes.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CONDITIONAL STATEMENTS Conditional statements control which part(s) of the program will be executed, depending on a specified criteria. There are two conditional statements in mikroBasic: SELECT CASE statement, IF statement. We suggest browsing the chapters Relation Operators and Implicit Conversion and Relation Operators, if you have not done so already.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Select Case Statement Select Case statement is used for selecting one of several available branches in the program course. It consists of a selector variable as a switch condition, and a list of possible values. These values can be constants, numerals, or expressions. Eventually, there can be an else statement which is executed if none of the labels corresponds to the value of the selector.
mikroBASIC making it simple... Examples mikroBASIC - Basic Compiler for Microchip PIC microcontrollers select case W case 0 B = 1 PORTB = B case 1 A = 1 PORTA = A case else PORTB = 0 end select ...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... If Statement There are two forms of if statement: Syntax of if..then statement is: if expression then statements end if where expression returns a True or False value. If expression is True, then statement is executed, otherwise it's not. Syntax of if..then..else statement is: if expression then statements1 else statements2 end if where expression returns a True or False value.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Compiler treats the construction like this: if expression1 then [ if expression2 then statement1 else statement2 end if ] end if To force the compiler to interpret our example the other way around, we would have to write it explicitly: if expression1 then if expression2 then statement1 end if else statement2 end if Examples if J <> 0 then Res = I div J end if if j <> 0 then i = i + 1 j = 0 end if ...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... LOOPS Loops are a specific way to control the program flow. By using loops, you can execute a sequence of statements repeatedly, with a control condition or variable to determine when the execution stops. You can use the standard break and continue to control the flow of a do..loop until, while, or for statement.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers For Statement For statement requires you to specify the number of iterations you want the loop to go through. Syntax of for statement is: for counter = initialValue to finalValue [step step_value] statement_1 statement_2 ...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Example making it simple... Here is a simple example of a for loop used for emitting hex code on PORTB for 7-segment display with common cathode. Nine digits will be printed with one second delay, by incrementing the counter.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Do..Loop Until Statement Syntax of do..loop statement is: do statement_1 ... statement_N loop until expression where expression returns a True or False value. The repeat statement executes statement_1 ... statement_N continually, checking the expression after each iteration. Eventually, when expression returns True, do..loop statement terminates.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... While Statement Syntax of while statement is: while expression statement_0 statement_1 ... statement_N wend Expression is tested first. If it returns True, all the following statements enclosed by while and wend will be executed. It will keep on executing statements until the expression returns False. Eventually, as expression returns False, while will be terminated without executing statements.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ASM Statement Sometimes it can be useful to write part of the program in assembly. ASM statement allows you to embed PIC assembly instructions into Basic code. Note that you cannot use numerals as absolute addresses for SFR or GPR variables in assembly instructions. You may use symbolic names instead (listing will display these names as well as addresses).
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... PIC MCU SPECIFIC In order to get the most from your mikroBasic compiler, you should be familiar with certain aspects of PIC MCU. This chapter is not essential, but it can provide you a better understanding of PICs' capabilities and limitations, and their impact on the code writing.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers mikroBASIC SPECIFIC mikroBasic compiler was designed with reliability and comfort in mind. Thus, certain modifications of standard Basic were necessary in order to make the compiler more PIC MCU compatible. PIC SFR (Special Function Registers) are implicitly declared as global variables of byte type. Their scope is the entire project - they are visible in any part of the program or any unit.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... For example, when handling the interrupts from TMR0 (if no other interrupts are allowed): sub procedure interrupt counter = counter + 1 TMR0 = 96 INTCON = $20 end sub In case of multiple interrupts enabled, you must test which of the interrupts occurred and then proceed with the appropriate code (interrupt handling): sub procedure interrupt if INTCON.TMR0IF = 1 then counter = counter + 1 TMR0 = 96 INTCON.
mikroBASIC making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Warning Messages Hint Messages Linker Error Messages Type of Error Error No. _CODE_AFTER_END 200 _BAUD_ERROR 201 _UPPER_BYTES_IGNORED 202 _UPPER_WORDS_IGNORED 203 _IMPLICIT_TYPECAST 204 Type of Error Error No. _VAR_NOT_USED 300 _PROC_NOT_CALLED 301 Type of Error Error No.
mikroBASIC making it simple...
C H A P T E R 3 Built-in and Library Routines mikroBasic provides a number of built-in and library routines which help you develop your application faster and easier. Libraries for ADC, CAN, USART, SPI, I2C, 1-Wire, LCD, PWM, RS485, numeric formatting, bit manipulation, and many other are included along with practical, ready-to-use code examples.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers BUILT-IN ROUTINES mikroBasic compiler incorporates a set of built-in functions and procedures. They are provided to make writing programs faster and easier. You can call built-in functions and procedures in any part of the program.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Examples making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers LIBRARY ROUTINES Library procedures and functions represent a set of routines. This collection of functions and procedures is provided for simplifying the initialization and use of PIC MCU and its hardware modules (ADC, I2C, USART, SPI, PWM), driver for LCD, drivers for internal and external CAN modules, flexible 485 protocol, numeric formatting routines...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... 1-Wire Library 1-wire library provides routines for communicating via 1-wire bus, for example with DS1820 digital thermometer. Note that oscillator frequency Fosc needs to be at least 4MHz in order to use the routines with Dallas digital thermometers.
mikroBASIC making it simple...
mikroBASIC making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ADC Library ADC (Analog to Digital Converter) module is available with a number of PIC MCU models. Library function ADC_read is included to provide you comfortable work with the module. The function is currently unsupported by the following PIC MCU models: P18F2331, P18F2431, P18F4331, and P18F4431.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers program ADC_Test dim temp_res as word main: ' configure analog inputs and Vref ADCON1 = $80 TRISA = $ff ' designate PORTA as input TRISB = $3F ' designate RB7,RB6 pins as outputs TRISD = $0 ' designate PORTD as output while true temp_res = ADC_read(2) ' now you can use temp_res ... PORTD = temp_res ' send lower 8 bits to PORTD PORTB = word(temp_res >> 2) ' send two most significant bits to PORTB wend end.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CAN Library CAN (Controller Area Network) module is available with a number of PIC MCU models. mikroBasic includes a set of library routines to provide you comfortable work with the module. CAN routines are currently supported by PIC MCU models P18XXX8. Microcontroller must be connected to CAN tranceiver (MCP2551 or similar) which is connected to CAN bus.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CANSetOperationMode Prototype: sub procedure CANSetOperationMode(dim mode as byte, dim WAIT as byte) Parameters: mode - Operation mode code can take any of predefined constant values (see the constants below) WAIT - Should have value TRUE(255) or FALSE(0) Effects: CAN is set to requested mode Overview: Given mode byte is copied to CANSTAT Note: If WAIT is true, this is a blocking call.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CANInitialize Prototype: sub procedure CANInitialize(dim SJW as byte, dim BRP as byte, dim PHSEG1 as byte, dim PHSEG2 as byte, dim PROPSEG as byte,dim CAN_CONFIG_FLAGS as byte) Precondition: CAN must be in Configuration mode or else these values will be ignored.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CANSetBaudRate Prototype: sub procedure CANSetBaudRate(dim SJW as byte, dim BRP as byte, dim PHSEG1 as byte,dim PHSEG2 as byte,dim PROPSEG as byte,dim CAN_CONFIG_FLAGS as byte) Precondition: CAN must be in Configuration mode or else these values will be ignored.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CANSetFilter Prototype: sub procedure CANSetFilter(dim CAN_FILTER as byte, dim val as longint, dim CAN_CONFIG_FLAGS as byte) Precondition: CAN must be in Configuration mode. If not, all values will be ignored. Parameters: CAN_FILTER - One of predefined constant values val - Actual filter register value.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CANWrite Prototype: sub function CANwrite(dim id as longint, dim byref Data as byte[8], dim DataLen as byte, dim CAN_TX_MSG_FLAGS as byte) as byte Precondition: CAN must be in Normal mode. Parameters: id - CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended). Data - array of bytes up to 8 bytes in length DataLen - Data length from 1 thru 8.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CAN Library Constants You need to be familiar with constants that are provided for use with CAN library routines. See how to form values (from constants) that will be passed to or from routines in the example at the end of the chapter. All of the constants are predefined in CAN library. CAN_OP_MODE These constant values define CAN module operation mode. CANSetOperationMode() routine requires this code.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CAN_RX_MSG_FLAGS These constant values define flags related to reception of a CAN message. There could be more than one this flag ANDed together to form multiple flags. If a particular bit is set; corresponding meaning is TRUE or else it will be FALSE. e.g. if (MsgFlag and CAN_RX_OVERFLOW) <> 0 then ' Receiver overflow has occurred. ' We have lost our previous message. ...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CAN_FILTER These constant values define filter codes. Routine CANSetFilter() requires this code as one of its arguments. These enumerations must be used by itself i.e. it cannot be ANDed to form multiple values.
mikroBASIC making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ' ..
mikroBASIC making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CANSPI Library SPI (Serial Peripheral Interface) module is available with a number of PIC MCU models. Set of library procedures and functions is listed below to provide comfortable work with external CAN modules (such as MCP2515 or MCP2510) via SPI. CANSPI routines are supported by any PIC MCU model that has SPI interface on portc. Also, CS pin of MCP2510 or MCP2515 must be connected to RC0 pin.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CANSPISetOperationMode Prototype: sub procedure CANSPISetOperationMode(dim mode as byte, dim WAIT as byte) Parameters: mode - Operation mode code can take any of predefined constant values (see the constants below) WAIT - Should have value TRUE(255) or FALSE(0) Effects: CAN is set to requested mode Overview: Given mode byte is copied to CANSTAT Note: If WAIT is true, this is a blocking call.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CANSPIInitialize Prototype: sub procedure CANSPIInitialize(dim SJW as byte, dim BRP as byte, dim PHSEG1 as byte, dim PHSEG2 as byte, dim PROPSEG as byte,dim CAN_CONFIG_FLAGS as byte) Precondition: CAN must be in Configuration mode or else these values will be ignored.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CANSPISetBaudRate Prototype: sub procedure CANSPISetBaudRate(dim SJW as byte, dim BRP as byte, dim PHSEG1 as byte, dim PHSEG2 as byte, dim PROPSEG as byte,dim AN_CONFIG_FLAGS as byte) Precondition: CAN must be in Configuration mode or else these values will be ignored.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CANSPISetFilter Prototype: sub procedure CANSPISetFilter(dim CAN_FILTER as byte, dim val as longint, dim CAN_CONFIG_FLAGS as byte) Precondition: CAN must be in Configuration mode. If not, all values will be ignored. Parameters: CAN_FILTER - One of predefined constant values val - Actual filter register value.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CANSPIWrite Prototype: sub function CANSPIwrite(dim id as longint, dim byref Data as byte[8], dim DataLen as byte, dim CAN_TX_MSG_FLAGS as byte) as byte Precondition: CAN must be in Normal mode. Parameters: id - CAN message identifier. Only 11 or 29 bits may be used depending on message type (standard or extended). Data - array of bytes up to 8 bytes in length DataLen - Data length from 1 thru 8.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CAN Library Constants You need to be familiar with constants that are provided for use with CANSPI library routines. See how to form values (from constants) that will be passed to or from routines in the example at the end of the chapter. All of the constants are predefined in CAN library. For the complete list of constants refer to page 119. Example This code demonstrates use of CANSPI library routines.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers ' ..
mikroBASIC making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Compact Flash Library Compact Flash Library provides routines for accessing data on Compact Flash card (abbrev. CF further in text). CF cards are widely used memory elements, commonly found in digital cameras. Great capacity (8MB ~ 2GB, and more) and excellent access time of typically few microseconds make them very attractive for microcontroller applications.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CF_INIT_PORT Prototype: sub procedure CF_INIT_PORT(dim byref CtrlPort as byte, dim byref DataPort as byte) Precondition: None. Parameters: CtrlPort is control port, DataPort is data port to which CF is attached. Effects: Initializes ports appropriately. CF_DETECT Prototype: sub function Precondition: CtrlPort must be initialized (call CF_INIT_PORT first). Effects: Check for presence of CF.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... CF_WRITE_BYTE Prototype: sub procedure CF_WRITE_BYTE(dim byref CtrlPort as byte, dim byref DataPort as byte, dim BData as byte) Precondition: Ports must be initialized, CF must be initialized for write operation (see CF_WRITE_INIT). Parameters: CtrlPort - control port , DataPort - data port , dat - is data byte written to CF. Effects: Write 1 byte to CF.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers CF_READ_BYTE Prototype: sub function CF_READ_BYTE(dim byref CtrlPort as byte, dim byref DataPort as byte) as byte Precondition: Ports must be initialized, CF must be initialized for read operation (see CF_READ_INIT). Parameters: CtrlPort - control port , DataPort - data port. Effects: Read 1 byte from CF.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Example making it simple... This code demonstrates use of CF library procedures and functions.
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... EEPROM Library EEPROM data memory is available with a number of PIC MCU models. Set of library procedures and functions is listed below to provide you comfortable work with EEPROM. Routines Basically, there are two operations that can be performed on EEPROM data memory.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers I2C Library I2C (Inter Integrated Circuit) full master MSSP (Master Synchronous Serial Port) module is available with a number of PIC MCU models. Set of library procedures and functions is listed below to support the master I2C mode. Important Note that these functions support module on PORTC, and won't work with modules on other ports.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... After configuring the I2C master mode, you have the following functions and procedures at your disposal: sub function I2C_start as byte Determines if I2C bus is free and issues START condition; if there is no error, function returns 0. sub procedure I2C_Repeated_Start Performs repeated start condition.
mikroBASIC making it simple...
mikroBASIC making it simple...
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers LCD Library mikroBasic provides a set of library procedures and functions for communicating with commonly used LCD (4-bit interface) (LCD with the controller Hitachi HD44780). HW connection of PIC and LCD is shown in the following figure. Note Be sure to designate port with LCD as output, before using any of the following library procedures or functions.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers LCD Commands making it simple... Command Purpose Lcd_First_Row Moves cursor to 1st row Lcd_Second_Row Moves cursor to 2nd row Lcd_Third_Row Moves cursor to 3rd row Lcd_Fourth_Row Moves cursor to 4th row Lcd_Clear Clears display Lcd_Return_Home Returns cursor to home position, returns a shifted display to original position.
mikroBASIC making it simple... Example mikroBASIC - Basic Compiler for Microchip PIC microcontrollers program lcd18 dim text as char[16] main: TRISB = 0 Lcd_Init(PORTB) Lcd_Cmd(portb, LCD_CURSOR_OFF) text = "mikroElektronika" LCD_Out(PORTB, 1, 1, text) end.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... PWM Library CCP (Capture/ Compare/ PWM) module is available with a number of PIC MCU models. Set of library procedures and functions is listed below to provide comfortable work with PWM (Pulse Width Modulation). Note Note that these routines support module on PORTC pin RC2, and won't work with modules on other ports. Also, mikroBasic doesn't support enhanced PWM modules.
mikroBASIC making it simple... Example mikroBASIC - Basic Compiler for Microchip PIC microcontrollers This code demonstrates use of PWM library procedures and functions. If pin RC2 is connected to LED diode, light emitted will depend of PWM duty ratio and this change can be noticed. program PWMtest dim j as byte main: j = 0 PORTC = $FF PWM_init(5000) PWM_start while true delay_ms(100) j = j + 1 PWM_change_duty(j) wend ' initializes PWM module, freq = 5kHz ' starts PWM ' changes duty ratio end.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... RS485 Library RS485 is a multipoint communication which allows multiple devices to be connected to a single signal cable. mikroBasic provides a set of library routines to provide you comfortable work with RS485 system using Master/Slave architecture. Master and Slave devices interchange packets of information, each of these packets containing synchronization bytes, CRC byte, address byte, and the data.
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... RS485master_write Prototype: sub procedure RS485master_write(dim byref data as byte[2], dim datalen as byte, dim address as byte) Precondition: MCU must be initialized as Master in 485 communication. It is programmer's responsibility to ensure (by protocol) that only one device sends data via 485 bus at a time.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers RS485slave_write Prototype: sub procedure RS485slave_write(dim byref data as byte[2], dim datalen as byte) Precondition: MCU must be initialized as Slave in 485 communication.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Example making it simple...
mikroBASIC making it simple...
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... SPI Library SPI (Serial Peripheral Interface) module is available with a number of PIC MCU models. Set of library procedures and functions is listed below to provide initialization of slave mode and comfortable work with the master mode. You can easily communicate with other devices via SPI - A/D converters, D/A converters, MAX7219, LTC1290 etc.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Example: SPI_init(Master_OSC_div4, Data_SAMPLE_MIDDLE,LK_Idle_LOW,LOW_2_HIGH) This will set SPI to master mode, clock = Fosc/4, data sampled at the middle of interval, clock idle state low and data transmitted at low to high edge.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Parameter lth_htl determines transmit edge for data. It can have the following values: Note Value Meaning LOW_2_HIGH Data transmit on low to high edge HIGH_2_LOW Data transmit on high to low edge In order to keep this working, you shouldn't override the settings made by the procedures spi_init or spi_init_ordinary as it uses some of the PIC MCU resources.
mikroBASIC making it simple... Example mikroBASIC - Basic Compiler for Microchip PIC microcontrollers The folowing code demonstrates how to use SPI library procedures and functions. Same example along with m7219.pbas file is given in folder ../mikroBasic/examples. Assumed HW configuration is: max7219 (chip select pin) is connected to RC1, and SDO, SDI, SCK pins are connected to corresponding pins of max7219. program SPI include "m7219.
DOUT SEGD SEGDP SEGE SEGC V+ ISET SEGG SEGB SEGF SEGA CLK +5V PIC16F877 RB0/INT RB2 RB1 RB4 RB3/PGM RB5 MCLR/Vpp/THV RB7/PGD RA0/AN0 RB6/PGC RA1/AN1 RA2/AN2/VrefRA3/AN3/Vref+ RA4/TOCKI RE0/RD/AN5 Vdd Vss RA5/AN4 RE1/WR/AN6 RE2/CS/AN7 RD5/PSP5 RD6/PSP6 RD3/PSP3 g e f f g K d d a K a 8 e c b b c dp dp 8. 8. 8. 8. 8. 8. 8. 8.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers USART Library USART (Universal Synchronous Asynchronous Receiver Transmitter) hardware module is available with a number of PIC MCU models. Set of library procedures and functions is listed below to provide comfortable work with the Asynchronous (full duplex) mode.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... sub procedure USART_Init(const Baud_Rate) Parameter Baud_rate is the desired baud rate; Example: USART_init(2400) This will initialize PIC MCU USART hardware and establish the communication at baud rate of 2400. Refer to the device data sheet for baud rates allowed for specific Fosc. If you specify the unsupported baud rate, compiler will report an error.
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers program RS232com dim Received_byte as byte main: USART_init(2400) while true if USART_data_ready = 1 then Received_byte = USART_read USART_write(Received_byte) end if wend end. ' initialize USART module ' if data is received ' read received data, ' send data via USART +5V 1 6 2 7 3 8 4 9 5 4.7uF SUB-D 9-pin connector + C1+ V+ 4.7uF + 4.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Numeric Formatting Routines These routines convert byte, short, word, and integer to string.
mikroBASIC making it simple... Example mikroBASIC - Basic Compiler for Microchip PIC microcontrollers The following code demonstrates use of library procedure ShortToStr. Example prints the converted value to LCD display.
mikroBASIC mikroBASIC - Basic Compiler for Microchip PIC microcontrollers making it simple... Utilities mikroBasic provides a set of procedures and functions for faster development of your applications. Routines sub function Button(dim byref PORT as byte, dim PIN as byte, dim Time as byte, dim Astate as byte) as byte The Button function eliminates the influence of contact flickering due to the pressing of a button (debouncing).
mikroBASIC making it simple... mikroBASIC - Basic Compiler for Microchip PIC microcontrollers Contact us: If you are experiencing problems with any of our products or you just want additional information, please let us know. Technical Support for compiler If you are experiencing any trouble with mikroBasic, please do not hesitate to contact us - it is in our mutual interest to solve these issues. Discount for schools and universities MikroElektronika offers a special discount for educational institutions.