Interfacing PIC18F45K20 Microcontrollers to PICKit3

<<You Can Use **C** To Complete This Assignmment!>>

Make sure you answer to all the quesitons!

Revised: April 4, 2017

Purpose: The purpose of this lab is to learn how to program PIC18F45K20 using C language and XC8 compiler. Refer to Lab 7 for more information. This lab contains the following parts:

  1. Writing a simple C-Code to blink an LED
  2. Using C Library Functions
  3. Understanding digital input using XC8
  4. Combining C code and Assembly
  5. Difference between data types

Parts Required:

Useful Links:

Procedure:

Part I - Writing a simple C-Code

Purpose: Write a simple C-Code for your PIC18F45K20 chip to flash an LED.

  • Download MPLAB XC8. Make sure you select the correct version for your operating system.
  • After you properly installed the compiler, open an existing project in MPLAB X.
  • Open an existing project in MPLAB X. Go to File --> Project Properties. Under Compiler Tool Chain you should see xc8 option as shown below:
  • XC8 Installed

    C_Code_Example

     

     

    NOTE: In case you are having trouble communicating with the chip review the following steps:

    Part II - Using C Library Functions: You become familiar with using different libraries in xc8.

    Watch_window

    function_example2

     

    Part III - Understanding Digital Inputs Using XC8: In this part you become familiar with how to setup IO ports as digital Inputs. YOU NEED A SCOPE for this section!

      1. Which registers are associated with setting up PORTE - name them. (HINT: there are 5 registers)?
      2. Which registers are associated with setting up PORTB - name them. (HINT: there are 10 registers)?
      3. Refer to Table 1. Which IO ports have their own internal (weak) Pull-Ups?
      4. What is the purpose of WPUB register?
      5. REx is typically multiplexed with ANy. What is ANy refers to?
      6. How many different ways asm code can be combines in the C code? Show them. (HINT: There are TWO different way)
      7. Consider the following assembly and C codes:
        - Q EQU 0x11 & MOVLW 0x12 & MOVWF Q
        - char Q; Q=0x12;
        Answer the following questions:
        1. Are these performing the same operation?
        2. Which one do you think will be executed faster? Why?
        3. Is there any difference between these two codes in terms of memory assignment? (HINT: You may want to run both codes and use teh Stopwatch.
      8. Using an Scope specify what is the the exact delay when you have Delay1TCYx(100) or __delay(); Take your own snapshot from the scope. Do not copy! Make sure you can read the grids on the scope.
      9. Using an Scope specify what is the the exact delay when you have Delay1TCYx(1000) or __delay(); Take your own snapshot from the scope. Do not copy! Make sure you can read the grids on the scope. You can do this on the second port.

    WHAT TO SUBMIT: Use the template!

    1. Answer to Part I Questions (15 points).
    2. Answer to Part II Questions. (15 points).
    3. Answer to Part III Questions. (15 points).
    4. Make sure Part III (shifting LED) is fully working. (25 points).
    5. Provide the following for Part III: (30 points).
      1. Draw the flowchart for your program. Your decision blocks must be clearly identified.
      2. You must draw your complete circuit schedmatic; make sure you include all the components. You can use Digikey Schematic capturer.
      3. Take a snapshot of the heading of your code. In the heading you should clearly explain the purpose of your program. Describe all the inputs and outputs in your program.
      4. Using the simulator you must clearly prove that your design works for all the cases. Show a snapshot of your simulator.
      5. Make sure you have the snapshot from the scope.

    *** ATTENTION: You must work on this assignment individually! If ANY PART of this assignment, including snapshots, is shared between two or more students all parties will receive ZERO. Absolutely no exceptions! ****

    ______________________ FOR PRACTICE ONLY ___________________________________

    Prat IV - Parking Counter (design Problem): In this part you need to design an up/down counter to count the number of cars going into and leaving the D-parking at SSU. The idea is that you have two digital input switches, say A & B. If switch A is enabled first and then switch B is enabled, the value on the 7-segment display should increases, starting with zero. Otherwise, If switch B is enabled first and then switch A is enabled, the value on the 7-segment display should decreases. Also, you must make sure if only one switch is pressed and the second one is not pressed after some times, the count should be ignored and start over (that is if A followed by nothing after 2 seconds, then the count should not change). Assume we never have a case that a single switch is pressed multiple times before the other switch is enabled. For simplicity, the maximum and minimum count values are assumed to be 9 and zero, respectively. You must use C code. You can use the code example here to display values on your 7-Segment. Pay attendtion to how we do the code!

    Take a snapshot of your code - you must have appropriate comments - codes without comments will not receive grade. Your code must include proper format as described in class. (solution)

    Draw the finite state machine diagram for this system identifying the transition between various states of the switches.

    Part V - Combining C code and Assembly: In this part we discuss how to pass variables between C and assembly:

    1. Read the program example for ccombining C and assembly. Carefully, examine the code. Pay attention to how the variables are passed from one function to another.
    2. How many different ways asm code can be combines in the C code?
    3. Explain how a variable which is defined as Global can be passed to #asm portion of the code.
    4. How is #asm portion of the code is ended?
    5. What is the difference between the following asm commands? You may want to run the code and enable the Variables window to see what happens (as shown below).