This is a VERY lightweight quickstart guide to writing one's first PIC24
assembly language program. It assumes you're familiar with Microchip
microcontrollers and the MPLAB IDE (Integrated Development Environment).
Material you have to read::
1.) Datasheet of your PIC24 microcontroller.
- I used a PIC24HJ12GP201
- At the very least go through:
- CPU
- Memory Organization
- Flash Program Memory
- Oscillator Configuration
- I/O Ports
- Eventually you'll wind up reading most of the datasheet
but this is a good start.
2.) Read and/or download the PIC24F or PIC24H Family Reference Manual
- You can probably defer much of the FRM reading for now.
- http://www.microchip.com
- "Products"
- "16-Bit PIC24 MCUs & dsPIC DSCs"
- "Reference Manuals"
- Pick the manual for your family.
- It's a listing of chapters (each is a .PDF file).
3.) MPLAB IDE "Help"
- Topics
- MPLAB ASM30
- MPLAB Assembler for PIC24 MCUs and dsPIC DSCs
- Go through:
- Overview (especially Input/Output Files)
- Syntax
- Expression Syntax and Operation
- Expressions
- Operators
- Special Operators
- Symbols (All Subtopics)
- Directives (All Subtopics)
4.) My circuit is as follows:
- PIC24HJ12GP201
- Vdd (pin18) = 3.3V.
- Vss (pins17 & 13) = 0V (GND).
- VddCore ( pin14) = 1uF tantulum electrolytic cap to GND.
- 10K Ohm resistor from pin1 (MCLR_L) to Vdd/3.3V.
- 150 Ohm resistor from pin4 (RB0) to LED anode.
- 150 Ohm resistor from pin5 (RB1) to LED anode.
- Both LED cathodes to GND.
- 0.100" pitch header for PICkit2/3 or ICD2/3 wired as follows:
- MCLR_L/Vpp (PIC24HJ12GP201 pin1 / connector pin1)
- Vdd (PIC24HJ12GP201 pin18 / connector pin2)
- GND (PIC24HJ12GP201 pins17 & 13 / connector pin3)
- PGD2 (PIC24HJ12GP201 pin2 / connector pin4)
- PGC2 (PIC24HJ12GP201 pin3 / connector pin5)
- It's up to you to get these 5 connections to your
debugger. I used a flying leads-to-MMJ pigtail
to connect to my ICD2.
- External power supply providing 3.3V.
5.) Before you connect to your hardware:
- MAKE SURE YOU DO NOT POWER THE TARGET FROM YOUR DEBUGGER.
(check your debugger settings in MPLAB IDE BEFORE
connecting to the hardware)
- My ICD2 would supply 5V which would be bad for the PIC24H.
6.) Create an MPLAB IDE project using the Project Wizard.
7.) Set the Configuration-Bits in MPLAB IDE as follows:
- Oscillator Mode
- Set to: Internal Fast RC (FRC divide by 16)
- Watchdog Timer Enable
- Set to: Disable
- OSC0/OSC2 Pin Function
- Set to: OSC0 pin has clock out function
- Internal EXternal Switch Over Mode
- Set to: Startup with user-selected oscillator
- Clock Switching and Monitor
- Set to: Sw Disabled, Mon Disabled
8.) Add files to your project:
- Add the linker script file: p24HJ12GP201
- It can be found at: \Microchip\MPLAB\MPLAB ASM30
Suite\Support\PIC24H\gld\p24HJ12GP201.gld
- Add the device include file: p24HJ12GP201.inc
- It can be found at: \Microchip\MPLAB\MPLAB ASM30
Suite\Support\PIC24H\inc\p24HJ12GP201.inc
- Add your source file: .s
- My example is below.
If all goes well, the two LEDs will go back and forth from ON-OFF to OFF-ON a
little quicker than once-per-second.
My source code:
--------------
.title " Sample dsPIC Assembler Source Code"
.sbttl " LED Toggle"
.include "p24HJ12GP201.inc"
.text
.global __reset
__reset:
mov #0x0a00,W8
mov W8,SPLIM
mov #0x0980,W15
mov #0xfc,W4
mov W4,TRISB
mloop: mov #0x02,W5
mov W5,PORTB
call delay
mov #0x01,W5
mov W5,PORTB
call delay
bra mloop
delay: mov #0x7fff,W6
dloop: dec W6,W6
cp0 W6
bra NZ,dloop
return
.end
I didn't have to do anything special to declare PGD2 & PGC2, the PIC just
knew how to deal with those connections. I claim nothing on this post except
that I got from zero to running a very simple PIC24 assembly language program.
The datasheet has register maps and a terse overview of the instruction set.
One should realize that the PIC24 family has many more intricacies in
instruction operation, pipelining, addressing modes, etc. So RTFP (read the
fine print)!
>From here, there tons of features to explore and
master. You could probably get there quicker with C language programming.
I'm just a dinosaur who likes to be able to do assembly language as a basis
first. I hopes this helps and isn't too full of errors.
Coop, AA1WW