advanced familiarization lab for ee2001 project fam lab_pic_iar… · for adc, a potentiometer is...

19
Advanced Familiarization lab for EE2001 project Background: Before you proceed with this lab, please make sure you have successfully completed the Basic Fam. Lab, you are now familiar with the EWPic integrated environment, PIC programmer (hardware and software) and know how to test the PIC program using the PIC development board and the lab equipment. This Advanced Fam Lab will demonstrate the basic seven design steps needed to implement any given functionalities. The primary aim of taking you through a working project with sample code is to illustrate the good practices, such as planning, writing codes for easy debugging and hardware testing without PIC. This lab also aims to introduce some common features of the PIC and some electronics components typically needed to demonstrate these features. Objectives: I. To learn the basic seven design steps to implement the given functionalities II. To learn the good practices in carrying out embedded project III. To be familiar with PIC datasheet IV. To learn how to use some of the common PIC features V. To use common components with PIC to demonstrate these features Introduction: The sample program in this Advanced Fam Lab demonstrates the digital I/O (DIO) and the Analog- Digital Converter (ADC) features of the PIC microcontroller. To show the use of DIO port of the PIC, an LED (output) is made turned on/off through a pushbutton (PB), which is connected to a digital input. For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC port. When the voltage level is beyond a threshold range, a buzzer sounds an alarm. We will use a sample mini project to illustrate these. Hardware required in this lab: Software features demonstrated: Switch/Push button Potentiometer DIO ADC LEDs Buzzer PIC 16F877 (or 16F877A) PIC development board PIC programmer board Some resistors 13 Aug 2009 Page 1 of 19

Upload: lytuong

Post on 07-Oct-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

Advanced Familiarization lab for EE2001 project

Background: Before you proceed with this lab, please make sure you have successfully completed the Basic Fam. Lab, you are now familiar with the EWPic integrated environment, PIC programmer (hardware and software) and know how to test the PIC program using the PIC development board and the lab equipment. This Advanced Fam Lab will demonstrate the basic seven design steps needed to implement any given functionalities. The primary aim of taking you through a working project with sample code is to illustrate the good practices, such as planning, writing codes for easy debugging and hardware testing without PIC. This lab also aims to introduce some common features of the PIC and some electronics components typically needed to demonstrate these features. Objectives:

I. To learn the basic seven design steps to implement the given functionalities

II. To learn the good practices in carrying out embedded project III. To be familiar with PIC datasheet IV. To learn how to use some of the common PIC features V. To use common components with PIC to demonstrate these features

Introduction: The sample program in this Advanced Fam Lab demonstrates the digital I/O (DIO) and the Analog-Digital Converter (ADC) features of the PIC microcontroller. To show the use of DIO port of the PIC, an LED (output) is made turned on/off through a pushbutton (PB), which is connected to a digital input. For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC port. When the voltage level is beyond a threshold range, a buzzer sounds an alarm. We will use a sample mini project to illustrate these.

Hardware required in this lab:

Software features demonstrated:

Switch/Push button Potentiometer

DIO ADC

LEDs Buzzer PIC 16F877 (or 16F877A) PIC development board PIC programmer board Some resistors

13 Aug 2009 Page 1 of 19

Page 2: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

Pre-lab readings and preparation: 1. Go through this lab sheet and try to remember the seven steps. 2. With reference to the PIC datasheet, try to understand what the sample program and .h files do. 3. Patch the circuits to be used according to Figure 4. 4. Make use of the PIC pin allocation table below to help you plan the connections between the circuits and PIC. MCLR/Vpp 1 40 RB7/PGD RA0/AN0 2 39 RB6/PGC RA1/AN1 3 38 RB5 RA2/AN2/Vref- 4 37 RB4 RA3/AN3/Vref+ 5 36 RB3/PGM RA4/T0CKI 6 35 RB2 RA5/AN4/SS 7 34 RB1 RE0/RD/AN5 8 33 RB0/INT RE1/WR/AN6 9 32 Vdd RE2/CS/AN7 10 31 Vss Vdd 11 30 RD7/PSP7 Vss 12 29 RD6/PSP6 OSC1/CLKIN 13 28 RD5/PSP5 OSC2/CLKOUT 14 27 RD4/PSP4 RC0/T1OSO/T1CKI 15 26 RC7/RX/DT RC1/T1OSI/CCP2 16 25 RC6/TX/CK RC2/CCP1 17 24 RC5/SDO RC3/SCK/SCL 18 23 RC4/SDI/SDA RD0/PSP0 19 22 RD3/PSP3 RD1/PSP1 20

PIC

16F8

77(A

)

21 RD2/PSP2

Table 1: PIC pin allocation table: In-lab exercises: Steps:

1) Test all the hardware components (in the circuits that you’ve patched) and ensure that they work independently without PIC.

2) Download the sample program and .h files. 3) Create an IAR project and include the required .c and .h files. 4) Download the program onto PIC. 5) Connect the circuits to the PIC. 6) Verify that the system work according to the specifications.

Post lab exercises: You will be tasked to modify the sample program &/or include another PIC feature &/or add/change the hardware components to meet new specifications and explain the behaviors of the changes made.

13 Aug 2009 Page 2 of 19

Page 3: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

Experiment: The “Smart Chair” Sample Project Specifications: Implement the following smart features to a chair in a waiting area using a PIC16F877 microcontroller:

1) When someone sits on the smart chair, the weight of the person is monitored. If the weight of the person sitting down is more than the safety limit set by us, a buzzer sounds as warning.

2) As long as the chair is occupied, a study light is on.

We will take a 7-steps approach to complete this task. Step1: Draw the process flow chart.

Figure 1. Process Flow Chart for “Smart Chair” Project

Step2: Select the input and output devices.

i) For checking if someone is sitting on the chair, we can use a PB. The PB is pressed when the seat is occupied and is released when the seat is vacant.

ii) A potentiometer can be used to simulate the load cell. It will give an analog voltage proportional to the person’s weight.

iii) The overweight warning is raised using a buzzer. iv) An LED is used for the table lamp.

Step3: Familiarize with the PIC16F877 features. All the PIC16F877(A) features available for the user are shown on the pinout diagram of the microcontroller itself. Figure 3 summarizes these features in a bubble diagram.

13 Aug 2009 Page 3 of 19

Page 4: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

For features which we have sample programs eg. “ADC.h” files, you can copy the file into the compiler project folder and then put an #include “ADC.h” statement in your program.

Figure 2. PIC16F877(A) Pinout Diagram.

Figure 3. Bubble Diagram summarizing PIC16F877(A) Features.

Step4: Select the PIC ports to be used.

i) The PB closed/open can be taken as a digital input. We have chosen PORTB pin 0 i.e. RB0 for connecting the PB as input.

13 Aug 2009 Page 4 of 19

Page 5: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

ii) The analog voltage output from the potentiometer can be connected to an ADC port. RA0 has been used as the analog input channel for this.

iii) The LED and buzzer can be connected to the digital outputs. PORTD pin 1 i.e. RD1 has been chosen as the digital output pin for the buzzer as overweight warning. PORTD pin 2 i.e. RD2 has been chosen as the digital output for the table lamp.

Always give intuitive names to I/Os and list them out using #define statements, and create the function initialize_IO_ports() function according to the above I/Os setting.

Step5: Test all the hardware components used. Patch up the hardware circuits individually as given in the schematic diagrams below and test them one-by-one. PIC outputs can be simulated using 5V or ground.

5V5V

RB0 RA0

100 ohm

BUZZER

10 Kohm Potentiometer

RD1470 ohm

LED

RD2+

Push Button Switch

4.7 Kohm

Figure 4. Schematic diagrams for PIC testing Step6: Program the PIC. Download the Adv Fam Lab Sample Program, and follow the Basic Fam Lab’s instructions on how to make the .hex file and program it onto PIC. Refer to the appendices for a copy of the sample code. Step7: Verify the functionality of the complete system. We will test with sample inputs and always be observant. E.g. LED should not lit if PB is not pressed, etc. If anything happens to go wrong, we will have to troubleshoot the circuit patiently. To do this well, we need to know these PIC features better. Refer to those extracted sections of the PIC datasheet namely: I/O ports and ADC, in the appendices. Generally, for each PIC feature you intend to use, always look for the following:

1) What are the registers (SFR) associated with this feature? 2) What should be the values for various configuration bits? 3) What are the steps to follow in order to use the feature properly?

Conclusion: You have come to the end of the Advanced Fam. Lab for EE2001. It is hoped that through this lab and its experiment, you have gained enough confidence to try out other PIC features and build more complex electronics prototypes. Have fun and enjoy learning!

13 Aug 2009 Page 5 of 19

Page 6: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

Appendices: A1. Advance FamLab Sample Program //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// //~ Advance FamLab Sample Program ~// //~ To demonstrate common PIC features: DIO & ADC. ~// //~Created by Henry Tan & SK Sahoo for EE2001. All rights reserved. ~// //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// //Define compiler error message. #ifndef __CPU_16F877__ #error "This program is tailored for PIC16F877 controller" #endif //Include required header files here. #include "io16f877.h" //the hardware register definition file. #include "DELAYS.h" #include "ADC.h" //List out all inputs and outputs #define SEAT_OCCUPIED RB0 #define WEIGHT read_ADC(0) #define OVERWEIGHT_BUZZER RD1 #define TABLE_LAMP RD2 #define OVERWEIGHT_LIMIT 0x1FF #define TEST_LED RD7 /********************************************************************** This function initializes the IO ports. Remember the PORT pins can be bit programmed, e.g. B0 can be input pin & B1 can be output pin. Pin RB0 configured as input, pins RD1 & RD2 configured as output. **********************************************************************/ void initialize_IO_ports(void) { //set the digital IO ports as per requirement. TRISB = 0xFF ; //portB as input. TRISD = 0x00 ; //portD as output. //clear the output ports at the beginning. PORTD = 0x00 ; //clear portD. } // Finally, define the main function here. void main() { // initialization of the peripherals to be used by // calling the respective initialization functions. initialize_IO_ports(); initialize_ADC(); // put the infinite loop here while(1) { TEST_LED=1; // This is a good way to check that this program is running. // Always do something like this to help you find out if your program & PIC are ok. if (SEAT_OCCUPIED==0)//i.e. the chair is occupied { TABLE_LAMP=1;

13 Aug 2009 Page 6 of 19

Page 7: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

/**************************************************************** Check the anlog input voltage and set/clear the OVERWEIGHT_LED accordingly. In this program the safe weight is equivalent to 2.5V. For the 10-bit ADC, with VDD=5V and VSS=0, gives 2.5V as digital output 0x01FF. ****************************************************************/ if (WEIGHT>OVERWEIGHT_LIMIT) // analog input voltage is more than 2.5V. OVERWEIGHT_BUZZER=1; else OVERWEIGHT_BUZZER=0; }//end if. else { OVERWEIGHT_BUZZER=0; TABLE_LAMP=0; } } //endWhile. }//endmain /* The end of AdvFamLab sample program*/

13 Aug 2009 Page 7 of 19

Page 8: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

A2. PIC Summary Datasheet

13 Aug 2009 Page 8 of 19

Page 9: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

A3. I/O Ports Datasheet

13 Aug 2009 Page 9 of 19

Page 10: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 10 of 19

Page 11: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 11 of 19

Page 12: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 12 of 19

Page 13: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

A4. ADC Datasheet

13 Aug 2009 Page 13 of 19

Page 14: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 14 of 19

Page 15: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 15 of 19

Page 16: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 16 of 19

Page 17: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 17 of 19

Page 18: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 18 of 19

Page 19: Advanced Familiarization lab for EE2001 project Fam Lab_PIC_iar… · For ADC, a potentiometer is used to vary the voltage level to an input channel of the PIC that is set as an ADC

13 Aug 2009 Page 19 of 19