South Manchester Radio Club 010101 PIC Tutorial 1010101 |
...... |
In this lesson we will write our first program. Its a pretty simple program, it will cause the LED attached to PIN RD0 to light. We need to know how to change the state of this pin. Whenever we want to interact with the:-
We do this by means of a "Register File". On the 16F877 on the 44-pin demo board the register file contains 4 banks (0 thru 3) by 64 registers. Each register contains eight bits, that is its a byte long. Think of them as a Patch Panel in a network cabinet, or a plug board on old fashioned telephone switchboard. The diagram below shows the first few entries in banks 0 and 1. Note that the file addresses are all in Hexadecimal (Hex). If you don't understand Hex don't worry, we will explain it as we proceed.
The entries of interest to us are:-
Bits 5 and 6 of this register control which bank of registers is selected. It is always accessible regardless of which bank is selected.
The Digital I/O pins can either be
As it has three possible states, these are referred to as "Tri-State" pins. Setting Bit 0 in TRISD to a "1" will make Port D , Bit 0 an input. Clearing it makes the pin an output.
The "PORT" registers are used to interact with the data lines. If the PORTs are configured as outputs then writing to these will set the corresponding out pin. If they are inputs, then reading will return the logic levels of the PIN. Our First InstructionsNow its time to look at our first three instructions:-
Assembler InstructionsWhilst the above three instructions are enough for the PIC, the assembler needs a couple of directives, to tell it how to assemble the program.
Also note that the Assembler ignores any thing on a line after a ";" At Last The Program!So here is the program. org
0 ; Start at location 0 I hope its pretty self explanatory. The "h" on the end of the numbers tells the assembler these are HEX. We have used the ";" to allow us to add comments to each line to explain what it does. This is a good idea as figuring out what code does weeks, months or even years after it was written can be tricky if haven't added any "Aid Memoirs" If you have MPLAB want to download this code and run it you can do that from HERE. Quick Quiz1. What is "bsf" an abbreviation for ? 2. END is used to:- 3. If "bsf 08h,0" turns on RD0 which of these would turn it off |
South Manchester Radio & Computer Club PIC Tutorial If you need support and help visit the SMRCC PIC Support Group on Yahoo For more information about SMRCC visit the main web site or e-mail "chairman <at> smrcc.org.uk"
|