Copyright © mikroElektronika, January 2012. All rights reserved.
TO OUR VALUED CUSTOMERS I want to express my thanks to you for being interested in our products and for having confidence in MikroElektronika. The primary aim of our company is to design and produce high quality electronic products and to constantly improve the performance thereof in order to better suit your needs. Nebojsa Matic General Manager The Microchip name, logo and products names are trademarks of Microchip Technology in the U.S.A and other countries.
Table of Contents 1. Introduction to mikroBasic PRO for dsPIC® . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 04 2. Hardware Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 05 3. Creating a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 06 Step 1 - Project Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1. Introduction to mikroBasic PRO for dsPIC® mikroBasic PRO for dsPIC® organizes appli cations into projects consisting of a single project file (file with the .mbpds extension) and one or more source files (files with the .mbas extension). The mikroBasic PRO for dsPIC® compiler allows you to manage several projects at a time. Source files can be compiled only if they are part of the project.
2. Hardware Connection LD4 LED RB0 RB1 RB2 RB3 RB4 RB5 AVCC RB0 AGND RB1 RB9 RB2 RB10 RB3 RB11 RB4 RB12 RB5 RB6 RB6 RB7 LED blinking is just turning ON and OFF LEDs that are connected to desired PORT pins. In order to see the example in action, it is necessary to connect the target microcontroller according to schematics shown on Figure 2-1. In the project we are about to write, we will use only PORTB, so you should connect the LEDs to PORTB only.
3. Creating a New Project The process of creating a new project is very simple. Select the New Project option from the Project menu as shown below. The New Project Wizard window appears. It can also be opened by clicking the New Project icon from the Project toolbar. The New Project Wizard (Figure 3-1) will guide you through the process of creating a new project. The introductory window of this application contains a list of actions to be performed when creating a new project. 01 Click Next.
Step 1 - Project Settings First thing we have to do is to specify the general project information. This is done by selecting the target microcontroller, it’s operating clock frequency, and of course - naming our project. This is an important step, because compiler will adjust the internal settings based on this information. Default configuration is already suggested to us at the begining. We will not change the microcontroller, and we will leave the default dsPIC30F4013 as the choice for this project.
Step 1 - Project Settings If you do not want to use the suggested path for storing your new project, you can change the destination folder. In order to do that, follow a simple procedure: 01 01 Click the Browse button of the Project Settings window to open the Browse for Folder dialog. 02 Select the desired folder to be the destination path for storing your new project files. 02 03 Click the OK button to confirm your selection and apply the new path.
Step 1 - Project Settings Once we have selected the destination project folder, let’s do the rest of the project settings: 01 01 Enter the name of your project. Since we are going to blink some LEDs, it’s appropriate to call the project “LedBlinking” 02 02 For this demonstration, we will use the default 80MHz clock with PLL enabled. Clock speed depends on your target hardware, and whether you are using PLL or not.
Step 2 - Add files This step allows you to include additional files that you need in your project: some headers or source files that you already wrote, and that you might need in further development. Since we are building a simple application, we won’t be adding any files at this moment. 01 Click Next.
Step 3 - Include Libraries Following step allows you to quickly set whether you want to include all libraries in your project, or not. Even if all libraries are included, they will not consume any memory unless they are explicitely used from within your code. The main advantage of including all libraries is that you will have over 500 functions available for use in your code right away, and visible from Code Assistant [CTRL+Space].
Step 4 - Finishing After all configuration is done, final step allows you to do just a bit more. 01 There is a check-box called “Open Edit Project window to set Configuration bits” at the final step. Edit Project is a specialized window which allows you to do all the necessary oscillator and PLL settings, as well as to set other configuration bits. We made sure that everything is described in plain English, so you will be able to do the settings without having to open the datasheet.
Blank new project created New project is finally created. A new source file called “LedBlinking.mbas” is created and it contains the main: function, which will hold the program. You may notice that project is configured according to the settings done in the New Project Wizard.
4. Code Example Time has come to do some coding. First thing we need to do is to disable analog function of PORTB pins, so they act as digital only: ‘ Configure AN pins as digital I/O ADPCFG = 0xFFFF Now we have to initialize PORTB to act as digital output. TRISB register, associated with PORTB, is used to set whether each pin acts as input or output. ‘ set PORTB to be digital output TRISB = 0 LATB register is used instead of PORTB for digital output.
Figure 4-2: This is how the code looks written in compiler code editor window Page 15
5. Building the Source When we are done writing our first LedBlinking code, we can now build the project and create a .HEX file which can be loaded into our target microcontroller, so we can test the program on real hardware. “Building” includes compilation, linking and optimization which are done automatically. Build your code by clicking on the icon in the main toolbar, or simply go to Build menu and click Build [CTRL+F9]. Message window will report the details of the building process (Figure 5-2).
6. Changing Project Settings If you need to change the target microcontroller or clock speed, you don’t have to go through the new project wizard all over again. This can be done quickly in the Edit Project window. You can open it using Project->Edit Project [CTRL+SHIFT+E] menu option. 02 01 To change your MCU, just select the desired microcontroller from the dropdown list. 01 02 04 03 02 To change your settings enter the oscillator value and adjust configu ration register bits using drop-down boxes.
7. What’s next? More examples mikroBasic PRO for dsPIC® comes with over 150 examples which demonstrate a variety of features. They represent the best starting point when developing a new project. You will find projects written for mikroElektronika development boards, additional boards, internal MCU modules and other examples. This way you always have a starting point, and don’t have to start from scratch. In most cases, you can combine different simple projects to create a more complex one.
DISCLAIMER All the products owned by MikroElektronika are protected by copyright law and international copyright treaty. Therefore, this manual is to be treated as any other copyright material. No part of this manual, including product and software described herein, may be reproduced, stored in a retrieval system, translated or transmitted in any form or by any means, without the prior written permission of MikroElektronika.
If you want to learn more about our products, please visit our website at www.mikroe.com. If you are experiencing some problems with any of our products or just need additional information, please place your ticket at www.mikroe.com/esupport If you have any questions, comments or business proposals, do not hesitate to contact us at office@mikroe.com Designed by MikroElektronika, January 2012. Creating the first project in mikroBasic PRO for dsPIC ver. 1.