Lecture02 - Before Class Install SiLabs – Instructions in “Installing_SiLabs-SDCC” “Installing_SiLabsdocument SDCC Install SDCC –

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: January 18, 2008 Before Class Install SiLabs – Instructions in “Installing_SiLabs-SDCC” “Installing_SiLabsdocument SDCC Install SDCC – Instructions in “Installing_SiLabs-SDCC” s-SDCC” document document Install SecureCRT – In the student build, also available online by going to the computer store site. http://www.rpi.edu/dept/arc/web/software/sw_available.html http://www.rpi.edu/dept/arc/web/software/sw_available.html Solutions to Number Systems Worksheet 1) 0000 1110 2) 1011 1101 3) 0001 0000 0001 4) 0001 1101 1000 5) 0x0E 6) 0xBD 7) 0x101 8) 0x1D8 9) 55 10) 171 11) 327 12) 10977 13) 0011 0111 14) 1010 0111 15) 0000 0001 0100 0111 16) 0010 1010 1110 0001 17) 0x2D 18) 0xAA 19) 0xE3 20) 0x29B5 21) 45 22) 170 23) 227 24) 10677 Download the c8051_SDCC.h header file – Available on the front page of the course LMS web page Install USB-serial driver – Link in Homework 1 USB8/26/2010 Lecture #2 2 8/26/2010 Lecture #2 3 Announcements Homework 2 due at beginning of next class Overview Homework softcopy Compile, download, and run Homework #1 download, and run Homework #1 Edit code, compile, download, and run Discussion of variable types Operators LOGIC Worksheet Labs 1 and 2 Overview Common digital gates di Some basic circuitry Considered late if turned in after class starts If you are still not registered for this class, please take care of it. TAs We are still working on this – stay tuned 8/26/2010 Lecture #2 4 8/26/2010 Lecture #2 5 1 January 18, 2008 SiLabs IDE Software Microcontroller commands are converted to Assembly Communication Connections The serial port found on the C8051 board face should be connected with a serial cable to the computer running connected SecureCRT or Hyperterminal This is for text communication with the microcontroller (printf, scanf, getchar, etc) printf, scanf, getchar, The USB adaptor (picture is out of date) strapped behind the C8051 should be connected with a USB cable to the computer running the SiLabs IDE SiL IDE This is for downloading the code We will be writing code in the C programming language and compiling it into Assembly using the SiLabs IDE and the SDCC compili it th SiL IDE th SDCC compiler. compiler. The SiLabs IDE will check for syntax errors, compile the code, and download it into the microcontroller flash memory The microprocessor will execute the programs stored in its memory, and does not need to be connected to any other computer to function – the processor is “embedded” in the system Many of the programs we will write in this course, though, will have user interaction in the form of text printed on a screen and keyboard inputs Feedback can be provided by other means such as turning on/off LEDs or buzzers or making adjustments to the motors Lecture #2 6 Turn on power to C8051 TURN OFF AFTER DONE!! 8/26/2010 Lecture #2 7 8/26/2010 Running Silicon Labs IDE Refer to instructions on LMS (also in Installing_SiLabs-SDCC-Drivers.pdf) Installing_SiLabs-SDCC Under Course Materials -- Lab1 -- “How to run codes” Running the listed code listed in this handout is optional. It proves that everything is working. You are required to run the home work 1 code. Running HW1 Code Make sure you have installed the software completely Read instructions from course website Be sure to have saved the c8051_SDCC.h header file in C:\ C:\Program Files\SDCC\include\mcs51 Files\SDCC\include\ Run SiLabs IDE, Click on Project – New Project Device family – C8051F02x Give it a name Pick a location – strongly recommend creating a folder for projects Suggestion – right click on “Header Files” and remove, same for “Source Files” Right click in white area on left and “Add files to Project ___ Load your .c file Connect the USB serial port adaptor check device manager to get port Connect the USB serial port adaptor – check device manager to get port numbers numbers A COM port will be used by SecureCRT or Hyperterminal to receive feedback Add Files to Project Open the SecureCRT software, and set the COM port Compile/Build/Download Compile/Build/Download Note the error messages – Add the following to the beginning of main: Run the program for imax values of 18 and 270. imax Observe the results Things to check Options connection options Options – connection options – USB Debug Adapter Debug Adapter Connect Assemble/compile Build/Make Download Go/Stop Lecture #2 8 8/26/2010 Lecture #2 9 Click the Icons 8/26/2010 2 January 18, 2008 Modify It, Run It Again!! Change variable count from unsigned character unsigned type to integer type. inte Appropriate Variable Choices Why do we care? Check lab manual index for printf syntax %d is for printing signed decimal values %u is for an unsigned decimal value %x is for hexadecimal values %c is for printing single byte character values Embedded controllers become more expensive as their memory requirements increase. The system also becomes more complicated. Always use the smallest variable type consistent with our objective. Use unsigned char for digital input and output (I/O) and the analog analog-to-digital conversion subsystem conversion subsystem Can use the bit variable type for simple T/F variables Use int or unsigned int when larger variables needed Use long and float types as a last resort What should we do to help ourselves? Recommended practices: Compile, download and run for the same values of imax (18 and 270). imax Note the differences. 8/26/2010 8/26/2010 Lecture #2 10 8/26/2010 Lecture #2 11 Appropriate Variable Choices (Cx51 (Cx51 C compiler manual) Operators There are many operators available in C to perform operations on numerical values perform operations on numerical values mathematical: +, -, *, /, % (mod – remainder) relational tests: <, >, <=, >= equality tests: ==, != logical tests: || (OR), && (AND) bitwise: & (AND), | (OR), ^ (XOR), << (left shift), >> (right shift) unary: ++ (increment), -- (decrement), ~ (ones complement), ! (logical negation) others listed in lab manual Lecture #2 13 8/26/2010 Lecture #2 12 8/26/2010 3 January 18, 2008 True/False Logic When the computer performs decision making, it evaluates whether a given argument is True or False For binary operations, we are familiar with the following concept • 0 → False • 1 → True For example, the following lines of code will only print print “Goodbye” (and return to the next line) (and return to the next line) if (0) printf(“Hello \r\n”); printf(“Hello if (1) printf(“Goodbye \r\n”); printf(“Goodbye 8/26/2010 Lecture #2 14 8/26/2010 True/False Logic The C code is a little more flexible than simple binary inputs for the argument. Anything that is not a “0” is interpreted as a “true” “t For example, the following lines of code will print both “Hello” and “Goodbye” if (1) printf(“Hello \r\n”); printf(“Hello if (9) printf(“Goodbye \r\n”); if printf rintf n Lecture #2 15 Operators Examples of usage in programming: A = 0x09; B = 0x05; C = 0x01; 0x09; 0x05; 0x0 if (A < B) C++; /* C++ is short form of C=C+1; */ else C=C<<2; If A were less than B, then C would be incremented by 1 (C=0x02), Since A is greater than B, C all of the bits in C are shifted to the left by 2 bits (C=0x04) Operators Examples of usage in programming: A = 0x09; B = 0x05; C = 0x01; 0x09; 0x05; 0x01; if (A == B) C+=3; /* C+=3; is short form of C=C+3; */ else C=~C; If A were equal to B, then 3 would be added to C 3; • C += 3; is same as C = C + 3; Since A is not equal to B, the ones complement is taken of (C 0xFE) taken of C (C = 0xFE) C = 0000 0001 << 2 0000 0001 << C = 0000 0100 = 0x04 Note: Values of added bits are always ‘0’ 8/26/2010 Lecture #2 16 ~C = ~(0000 0001) ~C = 1111 1110 = 0xFE Note: each bit is ‘flipped’ 8/26/2010 Lecture #2 17 4 January 18, 2008 Operators Examples of usage in programming: A = 0x09; B = 0x05; C = 0x00; (Using digital logic, the above variables may be considered as: A=true, B=true, C=false) D = A || C; \\ Byte operation, E = A | B; \\ Bit operation F = A && C; \\ Byte operation G = A & B; \\ Bit operation In this example, both D and F are single bit variables whereas E and G are byte variables D and F use LOGICAL operations: and use LOGICAL Note: 0 is false; 1 (or anything else) is true D: A is true, C is false, therefore, D is true F: A is true, C is false, therefore, F is false 8/26/2010 Lecture #2 Truth Table: OR X|Y=Q Operators Examples of usage in programming: A = 0x09; B = 0x05; C = 0x00; D = A || C; \\ Byte operation E = A | B; \\ Bit operation F = A && C; \\ Byte operation G = A & B; \\Bit operation Truth Table: OR X|Y=Q X 0 0 1 1 Y 0 1 0 1 Q 0 1 1 1 X 0 0 1 1 Y 0 1 0 1 Q 0 1 1 1 Truth Table: AND X&Y=Q E and G use BITWISE operations: 0000 1001 Conduct operation on | 0000 0101 0000 0101 bit bits in each column E = 0000 110 1 = 0x0D 0000 1001 & 0000 0101 G = 0000 000 1 = 0x01 8/26/2010 Lecture #2 Truth Table: AND X&Y=Q X 0 0 1 1 Y 0 1 0 1 Q 0 0 0 1 18 18 X 0 0 1 1 Y 0 1 0 1 Q 0 0 0 1 19 Application to Microcontroller You can use these operators to read or change the state of bit or groups of bits change the state of a bit or groups of bits in in an I/O port. Make pin 2 of Port 2 (P2.2) high and leave others as they were: P2 = P2 | 0x04; P2 P2: XXXX XXXX (X can be 0 or 1) 0x04: 0000 0100 0x04: 0000 0100 conduct a bitwise OR operation bitwise OR operation XXXX X 1X X Note: P2.2 is high, rest are unchanged Alternate form of writing this: P2 |= 0x04; P2 8/26/2010 Lecture #2 20 Application to Microcontroller Another application – testing the status on one or more input pins. Test if pin 4 of Port 3 (P3.4) is high or low: Statement if(P3) will check status of entire port if(P3) entire If P3 = 0000 0000, statement is false; anything else is true To check one pin (P3.4), use: test = P3 & 0x10; P3: XXXX XXXX (X can be 0 or 1) 0x10: 0001 0000 conduct a bitwise AND operation test test = 000 X 000 0 Note: just status of pin 4 is preserved test test will be true if P3.4 was high, or false if it was low This is called bit-masking (the 0x10 is the mask value) bit 8/26/2010 8/26/2010 Lecture #2 21 5 January 18, 2008 Lab 1-1 Overview 1Lab 1 focuses on the hardware and the functions used to interface the hardware to the C8051 Lab 1-1 specifically introduces the use of digital 1inputs and outputs Lab 1-2 Overview 1Lab 1-2 presents the use of interrupts and the 1system timer system timer Added components and code Acquire digital input from external source Use this to determine a digital output Use of functions in your programming of functions in your programming It is important to develop reusable code More switches Random number generator GOAL: Control 2 LEDs and a buzzer with two switches GOAL: Turn on a bi-color LED to red or green biafter a random delay. And then to measure a ft th reaction time. Use timer overflows to control the delay length of the LEDS and to determine the reaction time. Lecture #2 23 8/26/2010 Lecture #2 22 8/26/2010 Lab 2 Overview Lab 2 focuses on the development of an interactive game using the hardware and th the the primitive functions developed in Lab 1. The completion of Lab 2 should give you enough experience to get started on the Smart Car and Blimp systems. Smart Car and Blimp Now, let’s look further into the hardware aspects of the labs. 8/26/2010 Lecture #2 24 Preview Lab 1-1 1Refer to the Sample code for Lab 1-1, available 1on LMS This code will control one LED with one switch You need to modify this code to control 2 LEDs and a buzzer with two switches (as described previously) Things to note as a review of C programming Include header files – we need to include c8051_SDCC.h (website) (website) Function prototypes – note what is returned/passed Variable declarations – must declare at beginning of function Use of indentation & brackets – proper use is very helpful Function calls – need () for functions, not variables 8/26/2010 Lecture #2 25 6 January 18, 2008 PseudoPseudo-Code Homework 2 is due next class. It requires you to write Pseudo you to write Pseudo-Code. PseudoPseudo-Code is an outline for the actual code. Pseudo Code, General Form compiler directives declare global variables global variables function prototypes main function declare local variables initialization functions while (TRUE) get sensor information act on sensor information end while end main function 26 8/26/2010 Lecture #2 27 Read Lab 1, part 1 on LMS and make an attempt at writing Pseudo-Code to complete Pseudothe lab We will accept any valid attempt the lab. We will accept any valid attempt. Indentation Sequence Lecture #2 Readability, Readability, Structure 8/26/2010 Pseudo Code, Subroutines function sensor0 read and return appropriate value and return appropriate value function sensor1 read and return appropriate value function physical_action change system state Suggestion: Make choices that will keep your code simple and elegant. There is no single best answer. elegant. 8/26/2010 Lecture #2 28 Logic Worksheet Exercise!! Use this worksheet to test your knowledge of how bitwise and logical operators work. bit Work on this now, and we’ll resume lecture in 15 minutes Save the worksheet and put it in your lab notebook. What no lab notebook yet? Get one. One per pair. 8/26/2010 Lecture #2 29 7 January 18, 2008 Rest of Today Finish Worksheet 2 Make sure you have submitted HW1 both Make sure you have submitted HW1 both on on LMS and a hardcopy Common Digital Gates Inverter X Q 7404 X 0 1 Q 1 0 Each individual should upload the file on LMS, only one hardcopy should be submitted Turn off power to the C8051 on car Save any changes to code Take all your possessions with you Lecture #2 30 Buffer Buffer X Q X 0 1 Q 0 1 Before you leave 7407 7465 OR X Y 7432 Q X 0 0 1 1 Y 0 1 0 1 Q 0 1 1 1 31 31 8/26/2010 8/26/2010 Lecture #2 Common Digital Gates AND X Y Q Hardware Components Q 0 0 0 1 Q 1 1 1 0 7408 X 0 0 1 1 Y 0 1 0 1 Y 0 1 0 1 NAND NAND X Y Q 7400 X 0 0 1 1 There are many, many other digital circuits, but these are the common ones we will use. 8/26/2010 8/26/2010 Lecture #2 Refer to the components when discussing the following slides following slides Buffer chips LEDs Pushbuttons Slide switches Note: voltage inputs/outputs corresponding to ‘On/Off’ are not necessarily ‘0/5 [V]’ Circuit examples Lecture #2 33 32 32 8/26/2010 8 January 18, 2008 Chip Numbering On the physical chip, the pin numbers are arranged pin numbers are arranged from from the top-left around topthe chip in a countercounterclockwise (CCW) direction. The top-left is found by toplocating the small dot or notch on the chip. 8/26/2010 Lecture #2 Resistors & Potentiometers Example of a resistor component: SN74LS05N VCC 14 13 1 1 2 6 3 12 Physical appearance Electrical symbol 2 4 11 5 5 10 3 6 9 4 7 GND 8 Resistance value is indicated by the colored bars. Resistor color codes can be found in Appendix D of the lab manual and on the poster on center column in the lab. 8/26/2010 Lecture #2 35 34 LightLight-Emitting Diodes LEDs are common components that provide visible indication of digital provide a visible indication of a digital output. output. Diodes only allow current to flow through in one direction. Application in Circuit Connect an LED and resistor in series Choose resistor to limit Choose resistor to limit current in LED to rated value in LED to rated i<=10 mA Can use an output pin on microcontroller to turn on/off From the cathode to the anode Must be a potential drop across it to emit light it li Power +5V Current (i) Power +5V Ground 0V Port 2, pin 3 (P2.3) Logic High – no current flow Logic Low – current flow Use buffer gate to protect microcontroller (acts as a current sink) 8/26/2010 Current (i) Lecture #2 36 8/26/2010 Lecture #2 37 9 January 18, 2008 Switches Different types of switches can provide di digital input to the microprocessor. th Application in Circuit Connect a switch and resistor in series Resistor limits current and provides a voltage drop limits current and provides voltage drop When open circuit, no current flow, no voltage drop across resistor, therefore reading +5V at point A When closed circuit, current flows, voltage drops across resistor, therefore reading 0V at point A Microcontroller can read the status at this point A - Voltage at this point w.r.t. ground is: when not pressed (no current High when not pressed (no current) Low when pressed Power +5V common Pushbuttons provide different status only when pressed. Slide switches can be placed in either status. 8/26/2010 Lecture #2 38 8/26/2010 Current (i) only when pressed Ground 0V Lecture #2 39 Setting Port Bits We can use logic operations to change state of a pin pin Reading & Writing to Bits & Ports When should you use sbit? sbit? Example to turn on LED0 which is connected to P2.2, output a low voltage on the port pin 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 */ LED0 = 0; /* Light LED0 */ Same can be done with input ports to read state of individual pins Lecture #2 40 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 th the port register. Example: To make bits 3-7 of Port 2 low: 3P2 = P2 & 0x07; or P2 &= 0x07; or P2 8/26/2010 8/26/2010 Lecture #2 41 41 10 ...
View Full Document

This note was uploaded on 04/08/2011 for the course ENGR 2350 taught by Professor Fukanari during the Spring '08 term at Rensselaer Polytechnic Institute.

Ask a homework question - tutors are online