PMB Home

PIC Assembler

Which Micro and Why ?

I use the PIC micro. The reason is simply "because I do". I've used it for quite a while now, it gets the job done, it's cheap, easy to get, I've got plenty of code already written, it's not worth changing to another micro.

There are other popular hobby micros like the Atmel range. Most people religously recommend one or the other, but it really doesn't matter. For most relatively simple hobby or low volume applications it makes about as much difference as favorite colour.

Where Do I Start ?

This is a common question but it's too general. Smaller more specific questions are easier to answer.

The first thing to do is: Search the Internet.

Even if this doesn't provide the answers it should narrow the questions a bit.


A Quick Introduction

You don't have to be able to read Binary or HEX but it's all part of the bigger picture. I don't know of any simple or getting started assembly language tutorials.

Motorola put out a good book called "M68HC05 Applications Guide". It's for their HC05 micro but that doesn't matter too much for the basics. I think you can download it (Google search).

ASM (Assembly Language)

ASM requires that you know what's inside the micro in the form of registers and hardware features. The guts of assembly programming is the same for all micros but the individual micros "workings" determine how you apply it.

The best way to start is probably to get a development kit or at least some chips, a board to run it on and a way of loading the program (a programmer).

  • The micro chips are easy enough to get. I recommend the PIC16F628 or PIC16F876 to begin with.
  • The board can be a bit of that white prototype board from DSE or Jaycar, the stuff you just push bits into. You'll also need a 5V regulator or a seperate 5V DC regulated power supply.
  • The programmer is a bit more tricky. It consists of 2 parts; the PC software and the programmer interface hardware. There are a few good software packages free to download. I recommend starting with WinPIC. You need a cable or interface to connect the PIC chip to the PC (serial port for WinPIC). This is usually quite simple, you can make it yourself or buy it ready made.

Microchip provide free a development pacakge for the PICs called MPLAB. This includes the editor and assembler, and has some advanced features for later on. The assembler produces a "hex or s19" output file that the programmer loads into the chip.

You create a simple acsii text source file containing the assembly program source code. The assembler program creates output files including a listing of the assembled program and a file containing the raw code that gets programmed into the PIC.

Example to pulse an LED for a short time:

This is a simple example that produces one pulse of an LED connected to PORT-B of the PIC. It occupies only 7 memory locations within the program memory of the PIC.

This program assumes that you have already defined a few things earlier in the asm source file. These definitions tell the assembler program where port-b and the timer variable GPTIM1 are located within the PIC and which port-b pin the LED is connected to.

label

instruction

comment

FLASH

bsf

portb,led

; turn on the LED connected to portb

movlw

250d

; load W with 250 decimal (sets pulse length)

movwf

GPTIM1

; copy W to GPTIM1

FL_1

decfsz

GPTIM1

; count -1 and end if zero otherwise loop

goto

FL_1

; loop back and -1 again

bcf

portb,led

; turn off the LED connected to portb

FINISH

goto

FINISH

; program stops here in an endless loop

end

; end of program

To make the asm source file more readable we assign labels to things like port-b and the LED. This measn that we don't have to remember all the numbers that would otherwise refer to the specifics of the PIC and the way it's connected.

The assembler program reads the assignments and definitions and works through the asm instructions of the source file swapping things about and doing lots of lookups and exchanges to produce the specific instruction codes that go into the PIC program memory.

There are usually some instructions inthe source file that are not actually PIC instructions but are there to tell the assembler program what to do. These are called assembler directives.

The actual PIC asm instructions are described in the data sheet for the micro which can be downloaded from the Microchip website. There are also lots of examples and applications notes.

The next step:

  • download data sheet for the micro (PIC16F628) and browse through it.
  • download the WinPIC program.
  • build or buy a programmer interface (see WinPIC for details).
  • read a bit - test a bit - ask questions - repeat

 

PMB home

last updated: 16 July 2004