Thursday, September 20, 2012

"Hello World" with micro controllers (PIC16F690)

"There shall be light..."

(For this article I used information from the microchip website)
In today's article we are going to learn how to say "Hello world" in the language of micro-controllers, in other words how to give light to a led. We are going to need:
  1. Micro-controller (I use PIC16F690)
  2. LED
  3. Resistor (about 250 ohms is fine)
  4. And off course power, 5Vdc
First of all take a look on the schematic of the circuit (I used PROTEUS software, more here):
Led circuit
Open MPLAB and create a new project. When you create the new project, you can start by writing your first program, for the most of the programs we are going to follow these steps:
  1. Initialization of the ports (set I/Os, analogs and digitals)
  2. Main function (main program, call subroutines)
  3. Back to the Main Function.

Initialization

  1. Initialization
  2. bsf STATUS, RP0
  3. bcf STATUS, RP1 ;bank 1
  4. movlw 0xff
  5. movwf TRISA ;PORTA is input
  6. clrf TRISB ;PORTB is output
  7. clrf TRISC ;PORTC is output
  8. bcf STATUS, RP0
  9. bsf STATUS, RP1 ;bank 2
  10. clrf ANSEL
  11. clrf ANSELH ;set all I/Os as digitals
  12. bcf STATUS, RP1 ;bank0
  13. clrf PORTC
  14. clrf PORTB
  15. return

 Before I try to explain the code above, I think you have to know more about the data memory organization of the PIC16F690.(Below its a part of the pdf file  located at the microchip website)
"...The data memory is partitioned into four banks which contain the General  Purpose Registers (GPR) and the Special Function Registers (SFR). The Special Function Registers are located in the first 32 locations of each bank. Register locations 20h-7Fh in Bank 0 and A0h-EFh (A0-BF, PIC16F687 only) in Bank 1 are General Purpose Registers, implemented as static RAM. Register locations F0h-FFh in Bank 1, 170h-17Fh in Bank 2 and 1F0h-1FFh in Bank 3 point to addresses 70h-7Fh in Bank 0. Other General Purpose Resisters (GPR) are also available in Bank 1 and Bank 2, depending on the device. All other RAM is unimplemented and returns ‘0’ when read. RP<1:0> (STATUS<6:5>) are the bank select bits: 
RP1 RP0
0 0 → Bank 0 is selected
0 1 → Bank 1 is selected
1 0 → Bank 2 is selected
1 1 → Bank 3 is selected 



 So if we want to use any register first we have to select the bank that the register is in to, and this can be done by modifying the bits RP0, RP1 of the STATUS register. As you can see at the lines 2 and 3 we select the Bank one, because we need to use the register TRISA which is inside bank 1.
The register TRISA:

This register (TRISA) is responsible for controlling the "direction" of PORTA. For example if I set the bit TRISA<0> (when I say set the bit it means make the bit equal to logically "1") the corresponding bit of the PORTA which is the RA0 will be configured as input and if I unset that bit of TRISA, the corresponding bit of PORTA will be configured as output.
Now we know what TRISA is lets go back to the initial code:

movlw 00xF  --> move inside W (the accumulator) 0xFF (hexademixal format)
movwf TRISA --> move W into TRISA

After these two commands the contents of TRISA will be: xx11 1111. So the corresponding pins of the PORTA will be set as inputs.

We keep on with lines 6 and 7:
clrf TRISB --> clrf (clear register ) TRISB <--0x00
clrf TRISC -->TRISB <--0x00

By clearing these two registers, TRISB and TRISC we are setting the corresponding ports, PORTB and PORTC as outputs.

At this point it is a good idea to make an overview of what we saw until now. First we set the entire PORTA as input by setting all the bits of the register TRISA, next we set the ports B and C as outputs by unssetting all the bits of the registers TRISB and TRISC. This is the first part of the initialization.

The second part is by setting the type of the I/Os, analog or digital. To do that we have to modify the contents of ANSEL and ANSELH registers.
ANSEL:

ANSELH:

 At the lines 8 and 9 we select the bank 2 where the registers ANSEL and ANSELH can be found. And then (lines 10 and 11) we clear (clrf) the contents of those registers in order to make all the pins (ANS0-ANS11) digital I/Os. The next 3 lines is a rule of mine always return to bank 0, and always clear the output ports before leaving the initialization.

Main function (main program, call subroutines)

The main function isn't nothing more than this:

  1. start
  2. ; remaining code goes here
  3. call Initialization
  4. loop
  5. bsf PORTC, 0
  6. goto  loop
Let me explain.
Line 1: is nothing more than a label, when we want to jump to a different part of our code we have to redirect the PC (program counter) to the address of the memory cell that stores the first command of  that part of code, so you can remember all the address (in a hex format) or you can put labels (like we do) and let the assembler do all the work.
Line 2: is comments, everything in the same line after the ; are overlooked by the assembler.
Line 3: Using the command call we redirect the PC to the subroutine with name Initialization. Don't forget to end every subroutine with the return command.
Line 4: Another label.
Line 5: Finally the magic code that will give light to led, the command bsf, it can be translated into "set bit <0> of register PORTC".
Line 6: With the command goto we redirect the PC to the specific memory location depending of the label after the command. The reason we do that is to keep the PC from not leaving that part of code.(that's the third part Back to the Main Function.)

When you are ready you can build your project by clicking on the build all button.
If everything is ok a hex file (among others) will be created. That file (my_project_name.hex) we are going to use with the simulation software (proteus). So open PROTEUS, design the circuit and:

  1. Double click on microcontroller symbol.
  2. Click the browse button next to "Program File" text box.
  3. Browse for the hex file, when you find it select it and click ok.
  4. When you are ready, click on the "Run" button and the simulation will start.
If everything is ok you should see something like that:

That's all for today!!! 

(you can find all the files under this link.)



No comments:

Post a Comment