What you want to do with a microcontroller? Microchip produce many types of PIC microcontrollers that has various memory and other peripherals. You wouldn’t want to start programming with a high-end microcontroller because you might burn 1 or 2 in the process. Get one 16F84A microcontroller, 4MHz crystal, some LEDs and a breadboard. Refer PIC Microcontroller Programming in Assembly Language if you get stuck in the program.
Here I want to define W register. W register (working register) is the 8 bit register you use as a temp. In Special Function Registers of the PIC Microcontroller you learned that there are 8 bit SFRs and GPRs. When you want to put a 8 bit value on a SFR/GPR, you must put that value in W register first. Then only you can put it in the destination SFR/GPR. The instruction “movlw” moves the literal to the the W register. The instruction “movwf” moves the literal value of W register to a specified file register.
In Special Function Registers of the PIC Microcontroller we learned how to set a pin as an input or an output. What if you want to make all pins as inputs/outputs. You can do this as shown below. When dealing with inputs and outputs we use binary because it’s very clear for us. Let’s say you wanted to make all pins in PORTA to outputs. You already know you do this by setting the bits of TRISA register. And you know that RA0 is controlled by bit 0 of the TRISA register. Likewise RA1, RA2, RA3, RA4 are controlled by bits 1,2,3,4 of TRISA register. So if you put a 5 bit value in TRISA register you are actually telling the microcontroller what pins should be inputs/outputs. For example the binary 00110 will make RA1, RA2 as inputs and rest of the pins of PORTA as outputs. You can do this by the code below.
bsf 03h, 5 ; changing to bank 1 in order to access TRISA ; register. movlw b'00110' ; store the binary value in the W register. movwf 85h ; putting the literal value of W to the ; following file register (TRISA in this case). bcf 03h, 5 ; changing back to bank 0.
Suppose you want RA3, RA4 pins go logic high if RA1, RA2 inputs are logic high. You can do this by making a loop with btfss command, or you can put the value of PORTA in a GPR. Lets try doing this by putting the value to a GPR. Assume RA1 and RA2 are logic high and other pins are logic low. Then the value of PORTA will be 00110. Then using rlf command twice on the GPR you can this value two bites left. The result will be 11000. Now if you put this value to PORTA, RA3 and RA4 pins go logic high. I will use 0Ch as my GPR.
movf 05h ; get the value of PORTA to W register. movwf 0Ch ; put the value of W register into 0Ch register. rlf 0Ch ; move 1 bite to the left. rlf 0Ch ; move 1 bite to the left. movf 0Ch ; get the new value of 0Ch to W register. movwf 05h ; put this new value of W register into PORTA.
Fed up writing 0Ch, 05h, 03h address again and again? You can use equ syntax to give names to these addresses. I will put the complete program now with equ syntax. Another important change I did here is adding a loop in order to keep the program running so that the led’s will keep updating.
; Lable the addresses STATUS equ 03h TRISA equ 85h PORTA equ 05h MYTEMP equ 0Ch ; Setting input/output pins bsf STATUS, 5 ; changing to bank 1 in order to access TRISA ; register. movlw b'00110' ; store the binary value in the W register. movwf TRISA ; putting the literal value of W to the ; following file register (in this case TRISA). bcf STATUS, 5 ; changing back to bank 0. ; The program LOOP movf PORTA ; get the value of PORTA to W register. movwf MYTEMP ; put the value of W register into 0Ch register. rlf MYTEMP ; move 1 bite to the left. rlf MYTEMP ; move 1 bite to the left. movf MYTEMP ; get the new value of 0Ch to W register. movwf PORTA ; put this new value of W register into PORTA. goto LOOP end ; some compilers need this
Another directive you will see here is “end”. This is needed at the end of the program for some compilers like MPLAB IDE. If its not there, it will generate an error.
For info on how to compile this and to burn the program into the microcontroller read How to start Programming with MPLAB IDE.
The circuit needed for this program is provided below. Please note that you will have to put the oscillator and give power according to Powering the Microcontroller, Choosing your Oscillator mode posts. Also MCLR should be connected to the positive rail. Then only the circuit will be complete and functional.
I have connected the LED’s directly but its a poor practice. Sometimes it can draw too much current from the microcontroler and your microcontroller will be fried. You can check the datasheet for maximum output current of the microcontroller. Any pin’s voltage when its high will almost equal to the supply voltage. A LED dont want more than 5mA. So put a resistor series. Get the resistance you need with V=IR. For example if the supply voltage is 5V and the LED’s forward bias voltage is 2V, and if you want only 5ma,
Put a 600Ohm resistor.