Your first program using MPLABx IDE

First 0f all

We will create a project from scratch.  To do that i will make a step by step guide.

  1.  File->New Project.  Choose ‘Standalone project’.  NXT
  2.  You need to select de device family : ’16-bit DSCs (dsPIC30)’ and the device : ‘dsPIC30F4013’.  Those depend of your chip model.  NXT
  3.  For the tool, you need to chose the simulator, if you have a PICkit you can choose it here.  NXT
  4.  For the complier i choose ASM30.  NXT
  5.  Choose a project name but more important choose a location that you will remember,  i put it directly on the c:/. FINISH
  6.  Now that you created you project you need to include the headers files and linkers files.  Right click, add existing item.
  7. For the headers go to

    C:\Program Files (x86)\Microchip\MPLAB C30\support\dsPIC30F\inc\p30f4013.inc

    For the linkers

    C:\Program Files (x86)\Microchip\MPLAB C30\support\dsPIC30F\gld\p30f4013.gld

  8. After you need to create your source file with this extension  ‘SAMPLE.s’

Create your first program

Now we are ready to create our first program in assembly language.  On my chip i have a resistor and a led build-in on the rc13 and rc14 pin of the portc.  If you are using the dspic witout this setup you maybe want to jump a wire from those pin and plugin a little resistor and a led to test this code.  to find rc13-rc14 i will refer you to the schematic of the chip.

First of all you need to include some functions and files at the top of the sample.s :

.equ __30F6014, 1
.equ SAMPLES, 64 ;Number of samples
.include "p30f6014.inc"

You then need to setup configurations options of your chip/board. Those are mine, i use an oscillator of 7.3mhz.

;..............................................................................
;Configuration bits:
;..............................................................................

config __FOSC, CSW_FSCM_OFF & XT_PLL4 ;Turn off clock switching and
;fail-safe clock monitoring and
;use the External Clock as the
;system clock

config __FWDT, WDT_OFF ;Turn off Watchdog Timer

config __FBORPOR, PBOR_ON & BORV_27 & PWRT_16 & MCLR_EN
;Set Brown-out Reset voltage and
;and set Power-up Timer to 16msecs

config __FGS, CODE_PROT_OFF ;Set Code Protection Off for the
;General Segment
.extern _Diagnostics

You need to add the basic global declarations and allocate space for data in memory

;..............................................................................
;Global Declarations:
;..............................................................................

.global _wreg_init ;Provide global scope to _wreg_init routine
;In order to call this routine from a C file,
;place "wreg_init" in an "extern" declaration
;in the C file.

.global __reset ;The label for the first line of code.

.global __T1Interrupt ;Declare Timer 1 ISR name global

;..............................................................................
;Uninitialized variables in X-space in data memory
;..............................................................................

.section xbss
x_input: .space 2*SAMPLES ;Allocating space (in bytes) to variable.

;------------------------------------------------------------------------------
;Program Specific Constants (literals used in code)

.equ Fcy, #7372800 ; this is the frequency of my crystal.

Good now this is time for your first real lines of code.  In the beggining we are doing a reset. We then make a ‘section’ named init.  I will name those section of code by their name, they are whiles.  In this while we need to call basic instructions of the code, those will be called functions.  These functions will be called once in your program they can be very usefull.  We use the ‘RCALL’ command to call those functions (they are also while).  The first functions i will call is named ‘_wreg_init’, it is situated at the bottom of the program. This piece of code clear the w0-w15. At the end of this while (function) we put a return command.  That command just make us go back right after the RCALL we used in the program. The nxt command is used to make the bit RC13 in exit mode,  i will explain that in another post.   To prepair the chip to send power to the led we clear the bit #RC13 in the register TRISC.

.text ;Start of Code section
__reset:
MOV #__SP_init, W15 ;Initalize the Stack Pointer
MOV #__SPLIM_init, W0 ;Initialize the Stack Pointer Limit Register
MOV W0, SPLIM
NOP ;Add NOP to follow SPLIM initialization
init:
RCALL _wreg_init ;Call _wreg_init subroutine
bclr TRISC, #RC13

After those initial functions, we need to make a while (that will be the main part of your program).  Here i am making a while named ‘boucle’, this is just a label you can put everyting you want here. After i use the command bset that set a specific bit in the register PORTC #RC13 to one. This close the signal and stop the opening of a led. The nxt command is bclr that clear the bit we set in the last command.  This in my configuration power up the led. At the end i put a command named bra, that make us go back to the while we want.  I want to return to my main program so i wrote ‘boucle’.

boucle:
bset PORTC, #RC13
bclr PORTC, #RC13
bra boucle

The last part of the code is at the bottom of the sample.s file. Here you can put functions that you will call with the command RCALL. Do not forget to put a RETURN after each functions.

;========================================================================================
;Subroutine: Initialization of W registers to 0x0000
;..............................................................................
_wreg_init:
CLR W0 ;
MOV W0, W14
REPEAT #12
MOV W0, [++W14]
CLR W14
RETURN

;--------End of All Code Sections ---------------------------------------------

.end ;End of program code in this file

So in this introduction we just made a led open and close, due to the crystal frequency we can’t literally see the light flash we just see it open. If you want to actualy see a led flash you need to use a timer. We will cover that in another blog post.
M.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s