Saturday 20 October 2012

Understanding Instructions

In this post we'll have a go at de-cyphering the first line of code in the world's smallest (and worst) PIC microcontroller program. Here it is:

bsf 7,0

What does bsf 7,0 mean? It means set bit 0 of file 7. Make sense? Nope? That's because we haven't read any of the data sheets yet. If you genuinely want to learn to program PIC microcontrollers, you'll need to read data sheets - and lots of them.

You probably have at least two windows open on your computer right now, one for your browser (to read this blog) and another for the MPLAB IDE (Integrated Development Environment). Well now it's time to open another, and that's Adobe Reader.

Do a Google search for 16F690 and click the first result with [PDF] at the beginning. Also make sure it's coming from microchip.com. Save the PDF to your computer. Open the PDF and make sure it's the data sheet for the PIC16F690.

In the bookmarks, find the section called Instruction Set Summary (it's section 15.0 in my data sheet). Scroll down to the table PIC16F685/687/689/690 INSTRUCTION SET and there's our instruction BSF in the section labelled BIT-ORIENTED FILE REGISTER OPERATIONS.


BSF or bsf (doesn't matter if it's upper or lower case) means Bit Set File. The first number after the instruction is the file number and the second number is the bit number. The numbers must be separated by a comma.

We know that bit 0 corresponds to LED DS1 on the demo board. And that bit 1 is LED DS2. You can predict the rest, but notice that there's an 'out by one' discrepancy here. You'll see these crop up again and again.

So what's file 7?

In the bookmarks, find the section called Memory Organisation (it's section 2.0 in my data sheet). Scroll down to the table PIC16F690 SPECIAL FUNCTION REGISTERS and there's our file at address 07h. It's called PORTC.


Finally, what does set mean? It's the opposite of clear. It means 1 (the opposite of zero). It means high (the opposite of low). It means 5 volts (the opposite of 0 volts). It means true (the opposite of false).

Quick task: find the instruction that does the opposite of BSF

1 comment:

  1. Hi Julian, sorry to bother you! I have recently been following your PIC microcontroller vidieos, as there is time gap since you posted them would they still be relevant today? has microchip changed assembly instructions?
    Cheers Dougie

    ReplyDelete