EECS 100/43 Lab 8 Introduction to the PIC Microcontroller

Similar documents
An Introduction to MPLAB Integrated Development Environment

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

Chapter 13. PIC Family Microcontroller

Fondamenti su strumenti di sviluppo per microcontrollori PIC

PICNet 1. PICNet 1 PIC18 Network & SD/MMC Development Board. Features. Applications. Description

AUTOMATIC NIGHT LAMP WITH MORNING ALARM USING MICROPROCESSOR

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

Lab Experiment 1: The LPC 2148 Education Board

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

Accurate Measurement of the Mains Electricity Frequency

MICROPROCESSOR AND MICROCOMPUTER BASICS

CHAPTER 11: Flip Flops

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

2.0 Command and Data Handling Subsystem

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

8051 MICROCONTROLLER COURSE

A+ Guide to Managing and Maintaining Your PC, 7e. Chapter 1 Introducing Hardware

EasyPIC4 User s Manual

K8048 PIC PROGRAMMER BOARD

Palaparthi.Jagadeesh Chand. Associate Professor in ECE Department, Nimra Institute of Science & Technology, Vijayawada, A.P.

Tutorial for MPLAB Starter Kit for PIC18F

M68EVB908QL4 Development Board for Motorola MC68HC908QL4

DsPIC HOW-TO GUIDE Creating & Debugging a Project in MPLAB

Lecture N -1- PHYS Microcontrollers

Microtronics technologies Mobile:

Serial Communications

MX PIC24F Educational Module User Manual

Keep it Simple Timing

- 35mA Standby, mA Speaking pre-defined phrases with up to 1925 total characters.

C8051F020 Utilization in an Embedded Digital Design Project Course. Daren R. Wilcox Southern Polytechnic State University Marietta, Georgia

ET-BASE AVR ATmega64/128

Surveillance System Using Wireless Sensor Networks

Microprocessor & Assembly Language

Decimal Number (base 10) Binary Number (base 2)

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

Measuring Resistance Using Digital I/O

PDA Software Manual For the Windows CE and Mobile Operating Systems

USBSPYDER08 Discovery Kit for Freescale MC9RS08KA, MC9S08QD and MC9S08QG Microcontrollers User s Manual

DS1621 Digital Thermometer and Thermostat

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

POCKET SCOPE 2. The idea 2. Design criteria 3

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

Chapter 6. Inside the System Unit. What You Will Learn... Computers Are Your Future. What You Will Learn... Describing Hardware Performance

Welcome to the tutorial for the MPLAB Starter Kit for dspic DSCs

The Operational Amplfier Lab Guide

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

QL-PROG Microchip MCU Programmer User s Manual Qianlongsheng Electronic Technology Co., Ltd.

Chapter 2 Logic Gates and Introduction to Computer Architecture

ezsystem elab16m Project 1F: Alarm System (Full Project description)

DS1307ZN. 64 x 8 Serial Real-Time Clock

EMBEDDED C USING CODEWARRIOR Getting Started Manual

BE635 User Manual. Rev. V Bolymin, Inc. All Rights Reserved.

The I2C Bus. NXP Semiconductors: UM10204 I2C-bus specification and user manual HAW - Arduino 1

Getting Started with PIC24F/PIC24H Programming and Interfacing in C

SYSTEM 45. C R H Electronics Design

A PIC16F628 controlled FLL (Frequency Locked Loop) VFO for HF

ECIO40P ECIO28P ECRM40. Page 1. USB programmable microcontrollers. 0.6 DIP footprint. Supplied with a free version of Flowcode. E-blocks compatible

Digital Signal Controller (DSC) Development Tools

DS1621 Digital Thermometer and Thermostat

Embedded Web Server for Infra Red Remote Control

Discovering Computers Living in a Digital World

EVAL-UFDC-1/UFDC-1M-16

(Refer Slide Time: 00:01:16 min)

Conversion Between Analog and Digital Signals

Chapter 4 System Unit Components. Discovering Computers Your Interactive Guide to the Digital World

Lab 1 Course Guideline and Review

isppac-powr1220at8 I 2 C Hardware Verification Utility User s Guide

Work with Arduino Hardware

Special Lecture. Basic Stamp 2 Programming. (Presented on popular demand)

Bluetooth HC-06 with serial port module Easy guide

TRILOGI 5.3 PLC Ladder Diagram Programmer and Simulator. A tutorial prepared for IE 575 by Dr. T.C. Chang. Use On-Line Help

Am186ER/Am188ER AMD Continues 16-bit Innovation

Massachusetts Institute of Technology

Cell Phone Based Liquid Inventory Management Using Wireless System

ECEN 1400, Introduction to Analog and Digital Electronics

DKWF121 WF121-A B/G/N MODULE EVALUATION BOARD

PICmicro tm Development Board

Lab 3: Introduction to Data Acquisition Cards

Wireless Temperature

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Description of High Accuracy Digital Pressure Gauge Design

Technical Aspects of Creating and Assessing a Learning Environment in Digital Electronics for High School Students

3. Programming the STM32F4-Discovery

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

The 104 Duke_ACC Machine

Wireless Home Security System

How to read this guide

Lab 3 Microcontroller programming Interfacing to Sensors and Actuators with irobot

CAN bus board. EB018

Embedded Patient Monitoring System

FlowKit in-circuit debug system

User Manual. AS-Interface Programmer

Design And Implementation Of Bank Locker Security System Based On Fingerprint Sensing Circuit And RFID Reader

E-Blocks Easy RFID Bundle

Microcomputer Based Electronics: Using the C Stamp in Pre- Engineering, Technology, and Engineering Programs

WIZ-Embedded WebServer User s Manual (Ver. 1.0)

Arduino Lab 1 - The Voltage Divider

Disturbance Recoder SPCR 8C27. Product Guide

Transcription:

EECS 100/43 Lab 8 Introduction to the PIC Microcontroller 1. Objective In this lab, you will learn about how to use your PIC Microcontroller. This lab is the first lab for the digital component of the EE100 project. The goal of the project is to simply display the strain gauge measurement from your Strain Gauge lab on the LCD display on your PICDEM 2 Plus demo board. 2. Equipment a. Breadboard b. Wire cutters c. Wires d. Oscilloscope e. Function Generator f. Power supply g. PICDEM 2 Plus demo board h. MPLAB ICD 2 (Incircuit Debugger) i. MPLAB IDE installed on lab computer 1 j. Various connectors for power supply, function generator and oscilloscope. 3. Theory a. Introduction to microcontrollers [1] You have all heard of the Intel and AMD microprocessors. These little devices are the brains of your computers. However, there is another very important CPU (Central Processing Unit) in the digital world: the microcontroller. Microcontroller differs from a microprocessor in many ways. First and the most important is functionality. In order for a microprocessor to be used, other components such as memory, or components for receiving and sending data must be added to it. On the other hand, microcontroller is designed with most of the components above built-in. Very few external components are needed for its application. Thus, we save the time and space needed to construct devices. You will learn in lecture how digital electronics and systems are a beautiful abstraction of the analog world. You will also learn how you can construct computational circuits and memory units using digital electronics 2. In lab, you will learn how to use a very popular microcontroller the PIC from Microchip corporation. 1 Note: The microcontroller programming tools are installed in 140 Cory ONLY for ease of maintenance 2 EE100 Summer 2007 lectures at the

Be advised that we will view the microcontroller from a very high level perspective. That is, we will not go into the nitty-gritty details of designing microcontrollers because it is beyond the scope of this lab. However, the aim of this lab (and the project) is to give you a working idea behind PIC microcontrollers so that you may build useful circuits and continue to explore the microcontroller world on your own. We will explore the most important components of the microcontroller unit in this section. i. The CPU You will see how memory can be understood using the concept of digital feedback in the EE100 Summer 2007 lectures. For now, an abstract view of memory will suffice, refer to Figure 1. Figure 1. Simplified view of a memory unit You will also see how you can build simple circuits that add, subtract and compare: the Arithmetic Logic Unit (ALU). The CPU simply consists of the ALU along with internal memory locations called registers, refer to figure 2. Figure 2. A simplified CPU

ii. The Bus We now have two independent entities (memory and CPU). Thus any exchange of data is hindered, as well as its functionality. If, for example, we wish to add the contents of two memory locations and return the result again back to memory, we would need a connection between memory and CPU. Simply stated, we must have some "way" through data goes from one block to another. Enter the concept of a bus. Physically, a bus represents a group of 8, 16, or more wires. There are two types of buses: address and data bus. The first one consists of as many lines as the amount of memory we wish to address and the other one is as wide as data, in our case 8 bits or the connection line. First one serves to transmit address from CPU memory, and the second to connect all blocks inside the microcontroller. Figure 3 shows the new situation. iii. Input Output unit Figure 3. Connecting memory and CPU using instruction and data bus As far as functionality, the situation has improved. But a new problem has appeared: we have a unit that is capable of working by itself, but it does not have any contact with the outside world! In order to remove this deficiency, let's add a block which contains several memory locations. One end of this block is connected to the data bus and the other has connection with the output lines on the microcontroller. These output lines can be seen outside the microcontroller as output pins (just like the output pin of your op-amp). Refer to figure 4.

Figure 4. Example of a simplified input-output (I/O) unit that provides communication with the real-world A pin on the microcontroller chip that lets you talk to the outside world is called as a port. There are several types of ports input, output or bidirectional (input and output). When working with ports, it is necessary to choose which port we need to work with and then send data to or receive data from the port. Notice also from figure 4 that working with a port is like reading and writing to a memory location. This is accomplished using registers. A register is a memory location mapped to a port. When you write to or read from the register, you are reading or writing from the corresponding port. iv. Serial communication We can now communicate with the outside world, but what if we wanted to communicate kilobytes of data? Lets make a quick calculation on how many lines we would need. 1 kilobyte = 1024 bytes. 1 byte = 8 bits. Thus, we would need: 1024 bytes x 8 bits/byte = 8192 bits. If a port carries 1-bit of data, then we would need 8192 ports or 8192 pins! What if we wanted to carry a megabyte (1024 kilobytes) of data? You would need 8,388,608 lines! Thus it is obvious that if you want to communicate large amounts of data with the outside world, a port is very inefficient. Ports are useful only for carry a bit of data. In order to overcome this problem, we have to come up with a way to reduce the number of lines and not lose functionality. Suppose we are working with three lines only and one line is used for sending data, other for receiving, and the third is used as a reference line for both the input and the output. In order for this to work, we need to set a few rules for the exchange of data. These rules are called protocols. Let us suppose we have the following protocol: The logical unit "1" is set up on the transmitting line until transfer begins. Once the transfer starts, we lower the transmission line to logical "0" for a period of time (which we will designate as T), so the receiving side will know that it is receiving data. Hence, the receiving side will activate its mechanism for reception. Now the transmitting side can start sending binary data. Let each bit stay on the line for a time period which is equal to T. In the end (after transmitting all the data) let us bring the

logical unit "1" back on the line. The protocol we've just described is called in professional literature: NRZ (Non-Return to Zero). Figure 5 illustrates this concept. Figure 6 shows a block diagram of the serial communication unit. Figure 5. NRZ protocol. Notice that there is a clock which tells us how long a logic 1 and logic 0 are. We will discuss clocks in the next section v. Timer unit Figure 6. The Serial communication unit. We have seen in the previous section that we need a clock signal as a reference. You have seen how you can generate a square wave using a simple op-amp circuit. Figure 7 shows the capacitive square wave oscillator you are already familiar with.

Figure 7. Square-wave generator. Output V o will oscillate between Vpp and Vnn. However, there are more efficient methods to generate oscillators. We will not go into these techniques. It is sufficient to understand that the basic unit of the timer block is a free running counter whose numeric value increments by one in even intervals. Therefore, if we read the value of this counter at T1 and then read it again at T2, the difference would indicate how much time has elapsed. vi. Watchdog Timer Unlike a regular PC, your microcontroller will be functioning in time critical applications (like aircraft control) and/or in remote in-accessible areas (like seismic sensors). Hence, if your microcontroller crashes then there is probably no one to push the reset button (unlike a PC). To overcome this obstacle, designers introduced the concept of a Watchdog Timer. This block is in fact another free-run counter where our program will write a zero every time it executes correctly. If the program gets "stuck" due to some external condition, zero will not be written and the Watchdog timer will reset the microcontroller upon achieving its maximum value. This will result in executing the program again, and correctly this time around. The Watchdog timer is thus an important element of every

microcontroller. It helps the system recover from errors without human supervision. Notice that the Watchdog timer is NOT a substitute for correct program functionality. It is assumed that the program is functioning correctly. Figure 8 shows a block diagram of the Watchdog timer. vii. Analog to Digital (A/D) converter Figure 8. The Watchdog timer As external signals are usually substantially different from the ones that microcontroller can understand (zero and one), they have to be converted into a pattern which can be comprehended by a microcontroller. This task is performed by a block for analog to digital conversion or by an ADC. Figure 9 shows a block diagram of the A/D. Figure 9. Block diagram of the A/D

viii. The Microcontroller Block Diagram Now we are ready to assemble our microcontroller based on the building blocks discussed above. Refer to figure 10. ix. Programming a Microcontroller Figure 10. Microcontroller block diagram Now that we have the hardware in place, we need to program the microcontroller to accomplish a task. First, we will write a simple program in pseudo-code. By pseudocode we mean this program is not written using any programming language (like C). Refer to the code below.

START REGISTER 1 = MEMORY LOCATION A REGISTER 2 = MEMORY LOCATION B PORT A = REGISTER 1 + REGISTER 2 END The program above simply adds two memory locations and puts their sum on port A. The first line of the program moves the contents of memory location A into a CPU register. As we need the other data as well, we will move it into register 2. The next instruction tells the CPU to add the contents of those registers and send it to port A, so the sum would be visible to the outside world. Of course we program in the real world using languages like C. However, programming a microcontroller is more complex than programming on the PC. This is because of the fundamental issue in microcontroller programming: you are writing a program that is going to run on a completely different system. This process is called cross-compiling. Hence you need sophisticated tools (hardware and software) to help you program a microcontroller. In the next section, we will deal with the hardware the PICDEM 2 Plus development board. b. The PICDEM 2 Plus development board [2] In this section, we will give an overview of the PICDEM 2 plus development board, shown in figure 11. Figure 11. The PICDEM 2 plus development board (PICDEM 2 plus demo board). This board has at its core a P18F4520 microcontroller. The board also has a host of other peripherals to ease interfacing to the microcontroller and programming the microcontroller. The most important of these are covered below. i. Board Hardware Features ([2], p. 10) The PICDEM 2 plus development board has the following hardware features (refer to figure 12):

1. 18, 28 and 40-pin DIP sockets. Although three sockets are provided, only one device may be used at a time. In your case you have a PIC18F4520 in the 40-pin DIP socket. 2. On-board +5V regulator for direct input from 9V, 100 ma AC/DC wall adapter or 9V battery, or hooks for a +5V, 100 ma regulated DC supply. 3. Serial (RS-232) socket and associated hardware for direct connection to an RS-232 interface. 4. In-Circuit Debugger (ICD) connector. 5. 5 KΩ potentiometer for devices with analog inputs. 6. Three push button switches for external stimulus and Reset. 7. Power-on indicator LED. 8. Four LEDs connected to PORTB. 9. Jumper J6 to disconnect LEDs from PORTB. 10. 4 MHz canned crystal oscillator. 11. Unpopulated holes provided for crystal connection. 12. 32.768 khz crystal for Timer1 clock operation. 13. Jumper J7 to disconnect on-board RC oscillator (approximately 2 MHz). 14. 32K x 8 Serial EEPROM. 15. LCD display. 16. Piezo buzzer. 17. Prototype area for user hardware. 18. Microchip TC74 thermal sensor. Figure 12. PICDEM 2 Plus hardware

You should relate the board components above to the high-level microcontroller blocks you learned about in section (a). For example, the serial hardware on the board helps a computer interface to the microcontroller s serial unit. For a detailed description of the board hardware, refer to pp. 15 and 16 in [2]. Now that you have an idea of the hardware platform, it is time to play with the actual board. c. Introduction to MPLAB IDE and MPLAB C18 C Compiler In this section, you download the digital code to your microcontroller board. You will use the ICD2 (In-Circuit Debugger) module to do so. Note that since you do not cover digital systems etc. in great detail, we have designed all the code for you. The digital portion of your project simply requires you to change a few bits to instruct the microcontroller to read from a different A/D the goal of the final lab. For now, you will just burn the code onto the PIC: 1. Download the StrainGauge project folder from the EE100 labs homepage. Save this to your U: (network) drive. DO NOT SAVE THIS ONTO YOUR DESKTOP. FILES SAVED ONTO YOUR DESKTOP ARE AUTOMATICALLY DELETED WHEN YOU LOG OFF!! 2. Navigate to your folder and double-click on icon. A window similar to figure 13 will pop up. Figure 13. MPLAB IDE has opened your Strain Gauge project

3. Now, double click on main.c in the StrainGaugeInterface.mcw window. The main program should pop up. Look through it. Find the line that displays VOLTMETER. Replace VOLTMETER with EE100 ROCKS! 4. You should debug the program before downloading it to the board. To do this: Click on Debugger Select Tool. Make sure MPLAB ICD 2 is selected. Refer to figure 14. Figure 14. Make sure MPLAB ICD 2 is selected as the debugger 5. The debugger should ask you to download an operating system to the board. Defaults are OK for everything. Before you connect to the board, MAKE SURE THE BOARD IS POWERED ON AND CONNECTED TO THE ICD2 DEBUGGER. MAKE SURE THE DEBUGGER IS CONNECTED TO THE COMPUTER (check with a TA). Next, connect to the board: Debugger Connect. 6. Build your program. Select Project Build All 7. Program the board with your code: Debugger Program. 8. Run the program: Debugger Run. 9. If all goes well you should see the LCD screen display EE100 ROCKS! and a voltage reading on the second line. Use the pot (RA0) on the PICDEM 2 plus board to adjust the voltage value (from 0.00 to 5.01 V).

10. If you are satisfied that your program is working, you can permanently download code to EEPROM 3. First, halt debug mode: Debugger Halt. 11. Select Debugger Select Tool None. You cannot use the programmer and the debugger at the same time. 12. You pretty much repeat the same process for programming as debugging: Select Programmer Select Tool MPLAB ICD 2. 13. Select Programmer Connect 14. Select Programmer Program 15. Select Programmer Release from Reset. 16. Power off the board and power-on again. Your code will start running again. 3 EEPROM: Electrically Erasable Programmable Read Only Memory

4. PRELAB NAME: /SECTION Please turn in INDIVIDUAL COPIES of the prelab. They are due 10 MINUTES after start of lab, NO EXCEPTIONS! a. TASK 1: THOROUGHLY READ THIS DOCUMENT! (YES, THIS PRELAB IS A GIMME) PRELAB COMPLETE: (TA CHECKOFF)

5. REPORT NAME(S): /SECTION a. TASK 1: PICDEM 2 Plus LCD displays EE100 ROCKS! TA Checkoff: b. TASK 2: Program still functional after power cycle. TA Checkoff: TURN IN ONE REPORT PER GROUP AT THE END OF YOUR LAB SESSION. THERE IS NO TAKE HOME REPORT. 6. REFERENCES [1] PIC Microcontrollers: Chapter 1 Introduction to Microcontrollers. Online at: http://www.mikroe.com/en/books/picbook/1_chapter.htm. Last Accessed: July 8 th 2007 [2] PICDEM 2 Plus Board User s Guide. PDF format. Microchip corporation, http://www.microchip.com 7. REVISION HISTORY Date/Author Summer 2007/Bharathwaj Muthuswamy Revision Comments Typed up source documentation, organized lab report, typed up solutions.