Selasa, 14 Agustus 2012

Introduction to PIC Microcontroller

PIC microcontroller from microchip are very popular microcontroller. PICs are easily programmable cheap microcontroller. PICs is the name for the microchip microcontroller family (peripheral interface controller). Consisting of a microprocessor, I/O ports, timer(s), and other internal integrated hardware.  a wide range of chip sizes( from eight-pin up), great availability of compilers and source code and easy programming. Flash-type devices are re-programmable in-circuit, while OTP versions are very cheap to use at the final stage. A wide range of simple programmer hardware and software is downloadable from the net. PIC16f84 seems to be the “standard” for small projects. For the beginner with PICs, a PIC16f84A or PIC16f628 device is a good choice to start with. There are many different varieties of PICs that run at many frequencies, have different memory size, and different internal peripherals. The most famous MCU that microchip produces are: 12cxxx, 12fxxx, 16cxxx, 16fxxx, and 18fxxx.
  • Prefix 12 is for chips with 8 pins.
  • Prefix 16 is for 14-bit core chip with more than 8 pins.
  • Prefix 18 is for 16-bit core chip.
  • The letter after number tells the memory type:
    • C is for EPROM (OTP)
    • F is for flash chips
  • The number (2 or 3 digits) after this letter identifies specific chip version.
  • Improved new version of certain PICs types are identified by appending an (A) 

PIC16f84A microcontroller

  • 8-pin Enhanced FLASH/EEPROM 8-Bit microcontroller
High Performance RISC CPU Features:
  • Only 35 single word instructions to learn (instruction set)
  • All instructions single-cycle except for branches which are two-cycle
  • Operating speed: DC - 20 MHz clock input
                             DC - 200 ns instruction cycle

  • 1024 words of program memory
  • 68 bytes of Data RAM
  • 64 bytes of Data EEPROM
  • 14-bit wide instruction words
  • 8-bit wide data bus
  • Eight-level deep hardware stack
  • Four interrupt sources:
    • External RB0/INT pin
    • TMR0 timer overflow
    • PORTB<7:4> interrupt-on-change
    • Data EEPROM write complete
Peripheral Features:
  • 13 I/O pins with individual direction control
  • High current sink/source for direct LED drive
    • 25 mA sink max. per pin
    • 25 mA source max. per pin
  • TMR0: 8-bit timer/counter with 8-bit programmable prescaler.
Special Microcontroller Features:
  • 10,000 erase/write cycles Enhanced FLASH Program memory typical
  • 10,000,000 typical erase/write cycles EEPROM Data memory typical
  • EEPROM Data Retention >40 year
  • Selectable oscillator options
  1. RC oscillator : when using RC oscillator with CLKOUT
  2. HS oscillator : when using High speed Osc (> 4mhz)
  3. XT oscillator : when using external crystal oscillator with frequency <= 4Mhz
  4. LP oscillator : when using Low power osc < 200 khz
 

 

PORTA & PORTB

PORTA is a 5-bit wide, bi-directional port. The corresponding data direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output (i.e., put the contents of the output latch on the selected pin).
PORTB is an 8-bit wide, bi-directional port. The corresponding data direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input (i.e., put the corresponding output driver in a Hi-Impedance mode). Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output (i.e., put the contents of the output latch on the selected pin).
Now, let’s see how to write a program to microcontroller. Two methods are available to program the PIC, the first one is carefully study the memory organization, the function of each special register, and the 35 instruction set of this family. Then write the software in assembly language. This method is difficult and need good background in assembly programming. The second method is to write the code in HLL then compile this to machine code of PIC. Various HLL compilers are available as PICC, PICant, JAL, PICbasic, PICO-C and so on. We will use PICC compiler. After writing the C code using PICC compiler you must compile it to get hex filewhich will be loaded directly to PIC flash memory through programming circuit and software .

Simple Program : 

Now after this introduction we are going to write our first program PICC compiler.The program is to flash a LED while a push button is pressed.and we are going to explain how to use PICC in a nutshell.
when you run PICC for the 1st time you will see a window as below :
Now press the folder icon in the top left of the window then select new then select source file as shown in the figure below :
Then save it in your desired place and an editor will be opened in this editor we are going to write our program which is :

#include<16f84a.h>          // include library of microcontroller we are going to use
#fuses xt                           // tell the compiler that we are going to use external
                                   //crystal oscillator as a clock source for our microcntroller
#use delay(clock=4000000)   // tell the compiler the frequency of the clock
                                 // source is to be 4MHZ (used in calculation of delay time)

void main( )
{
   set_tris_a(0xff);               // set PORTA as input    
   set_tris_b(0xfe);              //set PORTB as input except PB0 which is
                                           // output for LED
   output_low(pin_b0);        // set PB0 output to 0v
   while(1)                      // main program
   {
     if(input(pin_a0))    // when switch is pressed >> true
                                   // and when released >> false
{     
     output_high(pin_b0);     //LED ON
     delay_ms(10);              // wait for 10ms doing nothing
     output_low(pin_b0);    // LED off
     delay_ms(10);            // wait for 10ms doing nothing
   }
}
}

Then after writing the code  go to the tab compile and press compile as shown below :


If there is no errors you will find that .hex file is generated which can be loaded in proteus for simulation :


Next Page >>

You can download the code and the similuation HERE
You can download this introduction in PDF HERE

0 komentar:

Posting Komentar