Announcements Homework #3 due today Online LMS Assessment Everybody submits their own on LMS You should have a lab notebook by now You should have the Lab Manual by now Outline for Today C8051 SFRs & Port I/O Worksheet #4 - simple I/O code Ports Ports are sets of connections on the microcontroller They are used to communicate with the external circuitry (input/output) The C8051 has 8 ports, P0, P1, P7 We will only use Ports P0, P1, P2, and P3 The C8051 manual lists Ports and the use of their pins 2 3 C8051F020 Block Diagram (pg 18) Using Ports Ports 0 7 These input and output pins can be connected to wires where signals are produced Either a sensor will detect something and send a signal to the microcontroller or the microcontroller will send out a signal to control a hardware feature We will show you where you can make these connections using a prototyping board (commonly called a protoboard or bread board) We need a way to read the signals on these pins, send signals out these pins, and configure the system to know which way the system is wired (input or output) 4 5
Special Function Registers Special Function Registers A Special Function Register (SFR) is a microprocessor feature capable of receiving information, holding it, and transferring it. SFRs are used in the C8051 for: Input/Output Control (Configuring pins, A/D, ) C8051 SFRs are listed in the header file: c8051f020.h SFRs have a size of 1 byte (8 bits) The SFRs look like memory locations to the software Counting Timing Bit Identification (One Byte) Arithmetic operations, addressing, 7 6 5 4 3 2 1 0 6 7 Special Function Registers, Ports & Communication Each I/O port has a SFR associated with it Example: P2 is the SFR associated with Port 2 8-bit register where each bit is associated with a specific pin of Port 2 (C8051 manual pg 175) Special Function Registers, Ports & Communication Using Port 2 as an example: The software communicates with the pins of Port 2 by using SFR P2 Input example: the current value (status of all 8 bits) can be used to give a variable, input, a value: input = P2; /* read PORT 2 */ Output example: the 8 pins of Port 2 can be assigned a value: P2 = 0x30; /* write to PORT 2*/ The hardware communicates by connecting wires to the pins of Port 2 8 9
More Examples of SFRs Application of SFRs Input/Output Port SFR P0 Configuration P1MDOUT output mode for P1 Control ADC1CN control of analog to digital converter Example: In the case of I/O ports, we need to input settings into some SFRs to determine how we will use the port. I/O Ports can be set to operate in different modes by switching transistors on and off 10 11 Configuration of Port 3 Output Mode The eight lines of Port 3 can be individually configured as push-pull (output) or open-drain (input). We set the configuration for each line through the special function register P3MDOUT Writing a 0 to a bit sets the corresponding pin of Port 3 to open drain; writing a 1 to a bit sets the corresponding pin of Port 3 to push-pull. Example: P3MDOUT = 0x0F; /* Pins 0-3 push-pull, pins 4-7 open-drain */ Configuring I/O Pins When using P0-P3 for digital output, set the bits in the corresponding output mode SFR for push-pull. Data can then be sent to the pins with the Port SFR When using P0-P3 for digital input, set the bits in the corresponding output mode SFR for open-drain. Additionally, you need to send a high value to those pins using the port SFR. Note: Port 1 can also be used for analog input, so additional settings need to be made in the P1MDIN SFR to set it for digital or analog input (C8051 manual pg 174). Since hardware configuration does not change in a typical application, these settings only need to be done once at the beginning of the code. (C8051 manual pg 176) 12 13
Configuring I/O Ports Here is an example using pins 2-7 of Port 2 and pins 0-2 of Port 3 for digital input (open-drain, set to 0) and pins 0-1 of Port 2 and 3-7 of Port 3 for digital output (push-pull, set to 1). Put these commands in a function that you will call at the beginning of the program 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 Port 2 config: 0x03 = 0000 0011 Port 3 config: 0xF8 = 1111 1000 Special Function Registers, Ports & Communication Each I/O port has a SFR associated with it Example: P2 is the SFR associated with Port 2 8-bit register where each bit is associated with a specific pin of Port 2 (C8051 manual pg 175) void Port_Init(void) { P2MDOUT = 0x03; // Pins 0-1 push-pull, pins 2-7 open-drain P2 = 0xFC; // Set pins 2-7 of P2 P3MDOUT = 0xF8; // Pins 3-7 push-pull, pins 0-2 open-drain P3 = 0x07; // Set pins 0-2 of P3 } 14 15 Bitwise operations, example { P2 = 0x19; // assignment operation P2 = 0xC1; // bitwise OR P2 &= ~0x33; // bitwise AND } P2 = 0x19 0 0 0 1 1 0 0 1 0xC1 = 1 1 0 0 0 0 0 1 P2 = 0xC1 1 1 0 1 1 0 0 1 0x33 = 0 0 1 1 0 0 1 1 ~0x33 = 1 1 0 0 1 1 0 0 P2 &= ~0x33 1 1 0 0 1 0 0 0 Sample Code, sbit Remember, we can use logic operations to change state of a pin, if P2.2 is connected to LED0 To turn on LED0, force P2.2 to go low P2 &= ~0x04; The C8051 also has the ability to change the state of individual bits on certain ports sbit at 0xA2 LED0; /* LED0 associated with P2.2 */ // the " " is 2 underscore characters together LED0 = 0; /* Light LED0 */ Same can be done with input ports to read state of individual pins 16 17
Reading & Writing to Bits & Ports Connections to the Microcontroller When should you use sbit? If you want to read or change the state of one or two individual bits of a port, using the sbit command can be very efficient. If you want to read or change the state of multiple pins, it is recommended that you use the port register. Example: To make bits 3-7 of Port 2 low: P2 = P2 & 0x07; or P2 &= 0x07; Each protoboard has an EVB Port Connector, see back cover of lab manual and pin sheet For example, hardware connects to Port 3 by using pins 31-38. Review the numbering scheme on the protoboard connector block to EVB. Note the location of pins 1 and 2 to see how they are sequenced. 18 19 Pin-out Form available on LMS Lab Check-Off Before implementing code and setting up the hardware, determine the initialization and EVB pin connections. Example: complete the pin-out form using the following information P2.6 is a pushbutton input with an sbit label PB1 P2.7 is a slide switch input with an sbit label SS P3.2 is an LED output with an sbit label LED0 Indicate Port pins used, sbit labels and Port initialization code When you complete a lab assignment, you will need to demonstrate your lab to a TA NOTE: We NO LONGER require it to be a TA from your section! Check-off procedure All partners should be present (but missing member will lose points) Neatness of the wiring matters Lab Notebook needs to be completed to that point You will be asked questions from the Demonstration and Verification section at end of lab as well as some additional questions All partners need to know lab well Questions will be directed towards each partner If the laboratory does not completely meet the requirements: You will need to correct the problem Request a new check-off and there may be a small penalty 20 21
IMPORTANT: Compiler Issues Lab 1, part 1 Pseudo Code Remember Do not use special characters in the file name - and _ are OK, but NOT # Only use a single. in the name Save your program as a.c file (.c files are actually just plain text files) compiler directives declare global variables function prototypes main function declare local variables initialization functions while (TRUE) read sensors set outputs for LED, buzzer and BILED end while end main function 22 23 Pseudo Code continued Pseudo Code in general function initialize ports() set ports 2 and 3 for input/output Alternative approach (not recommended): can create individual functions for each input bit, but using sbits directly is much simpler function sensor_1() read push button 1 and return appropriate value function sensor_2() read push button 2 and return appropriate value function sensor_3() read slide switch and return appropriate value function set outputs() determine LED, buzzer and BILED states based on the four conditions In general, there is no single best code implementation The pseudo-code should reflect your approach to solving the specified problem 24 25
Continue Lab 1, part 1 Complete Pin-out form for Lab 1, part 1 Include it in your notebook If you finish and need to get checked off See Demonstration and Verification at end of Lab 1, part 1 description. At a minimum, start Homework 4 (see due date on course calendar) By this point, you can use open shop hours to complete the lab and get checked off All partners should be present for check-off. Quiz 1 next Class RPILMS BRING YOUR LAPTOP 40 Minutes; open printed book and notes NO old quiz copies, NO other web pages except LMS Calculators OK, but NOT on cellphones Coverage Logic review Worksheet #2 Pseudo Code Indent for loops Must logically work Number conversion review Worksheet #1 Wiring see example problem 26 27 Next Class Quiz #1 (see sample problem on wiring) Homework #4 due Lab 1, part 1 check-off Before you leave Turn off the power to the car. Return your protoboard to cabinet slot that matches number on protoboard. Parts bags should be left with protoboard. Return your logic probe to the back cabinet. 28