分享

PIC Microcontroller Intro – Tutorial #3

 张鹏p9sgkyeyio 2016-12-13

This chapter, like its predecessor, focuses both in hardware and software related to PIC microcontrollers. In the second part, we made an attempt to blink one LED through a PIC microcontroller PIC12F675 with the help of a pre-processed (hex) code. Here, we are going to learn more about writing our own code for such a simple LED project.

What is a compiler? Compiler is a program that decodes instructions written in a higher order language and produces an assembly language program. MikroC is a best compiler for beginners as it contains built in functions for most of the commonly used tasks. You can download a trial/limited version of MikroC from here (http://www./mikroc/pic/). Note that, this trial version is limited to 2K of program words, but is sufficient for most of our learning applications.

First of all, you should install the downloaded MikroC PRO For PIC in your PC and open it. Now click on Project and then New Project. In the New Project Wizard window, click Next, enter Project name, Project Folder, Device Name (microcontroller) , Device Clock (clock frequency), and click Next (here we use PIC 16F877A microcontroller with 8MHz crystal). At this time just click Next in the Add Files to Project window.

Click Next in Library Manager after selecting Include All (default) in Include Libraries, click Finish to complete the New Project Wizard. This will open the editor window where you can enter the code.

PIC3 -1
PIC3 -2
PIC3 -3

What is next? Enter the code (shown at the end of this paragraph), Save it and then Compile it by clicking Build (or Ctrl+F9). This will generate a hex file in your project folder. You need to burn this hex file into the PIC16F877A microcontroller using your PIC programmer, as done earlier. Finally, finish the hardware as per the wiring diagram included here.

wiring diagram

This time, the microcontroller is PIC16F877A! VDD and VSS of PIC Microcontroller is connected to +5V and GND respectively, and the 8MHz crystal is used to provide necessary clock for the microcontroller. Two 22pF capacitors stabilizes the oscillations of the crystal. One Red LED is connected to the one PORTB pin and a 1K Ω resistor is connected in series with it to limit the operating current of the LED.

Our code for PIC 16F877A microcontroller blinks the connected LED with a delay of 1 second. Okay, but how? What is behind this little code?

  1. void main()
  2. {
  3. TRISB.F0 = 0;
  4. while(1)
  5. {
  6. PORTB.F0 = 1;
  7. Delay_ms(1000);
  8. PORTB.F0 = 0;
  9. Delay_ms(1000);
  10. }
  11. }

TRIS stands for TriState, which determines the direction of each GPIO pin. Output pins of a PIC Microcontroller is divided in to different PORTS containing a group of GPIO (General Purpose Input Output Pins). Logic 1 at a particular bit of TRIS register makes the corresponding pin Input and Logic 0 at a particular bit of TRIS register makes the corresponding pin Output. And, PORT register is used to read data from or write data to GPIO pins. Logic 1 at a particular bit of PORT register makes the corresponding pin at Logic-High (H) state and Logic 0 at a particular bit of PORT register makes the corresponding pin at Logic-Low (L) state, if that pin is an Output pin (TRIS bit is 0).

TRISB.F0 = 0;→ Makes 0th bit of PORTB (PORTB0 or RB0) Output while(1)→ An infinite loop
PORTB.F0 = 1;→ Makes 0th bit of PORTB at Logic-High state
PORTB.F0 = 0;→ Makes 0th bit of PORTB at Logic-Low state

The Delay_ms (const unsigned long a) is a built in function of MikroC PRO which provides a delay of 'a’ milliseconds. The variable 'a’ must be a constant of type unsigned long integer. Okay, now our code is well-defined!

  1. void main()
  2. {
  3. TRISB.F0 = 0; // PORT B0 as output
  4. while(1) // Infinite Loop
  5. {
  6. PORTB.F0 = 1; // LED connected at RB0 ON
  7. Delay_ms(1000); // 1 Second Delay
  8. PORTB.F0 = 0; // LED connected at RB0 OFF
  9. Delay_ms(1000); // 1 Second Delay
  10. }
  11. }
PIC16F877A 40-pin Enhanced Flash Microcontroller

(PIC16F877A: 40-pin Enhanced Flash Microcontroller)

If you are in doubt, follow this wiring diagram to learn how to connect your PIC Kit 2 programmer with the PIC16F877A microcontroller. Also refer Part-2 of this tutorial!

PIC Kit 2 programmer

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多