This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1V 0.2 1C and Embedded Systems•A µP-based system used in a device (i.e, a car engine) performing control and monitoring functions is referred to as an embedded system.– The embedded system is invisible to the user– The user only indirectly interacts with the embedded system by using the device that contains the µP• Most programs for embedded systems are written in C– Portable – code can be retargeted to different processors – Clarity – C is easier to understand than assembly– compilers produce code that is close to manually-tweaked assembly language in both code size and performanceV 0.2 2So Why Learn Assembly Language?• The way that C is written can impact assembly language size and performance– i.e., if the int data type is used where char would suffice, both performance and code size will suffer. • Learning the assembly language, architecture of the target µP provides performance and code size clues for compiled C– Does the uP have support for multiply/divide?– Can it shift only one position each shift or multiple positions? (i.e, does it have a barrel shifter?)– How much internal RAM does the µP have?–Does the µP have floating point support?• Sometimes have to write assembly code for performance reasons.V 0.2 3C CompilationC Code (.c)CompilerAssembly (.asm, .as) Machine code(.obj) general optimization options, target µPAssemblerLinkerExecutable(.hex) µP-specific general optimizationexternal libraries (math, IO, etc)This general tool chain is used for all high-level programming languages.C is portable because a different compiler can target a different processor. Generally, some changes are always required, just fewer changes than if trying port an assembly language program to a different processor.Assembly language or machine code is not portable.V 0.2 4PICC Lite C Compiler• Programs for hardware experiments (labs 6-13) will be written in C• Will use the PICC Lite C Compiler– Demo version of professional C compiler from Hi-Tech Software (www.htsoft.com)– Excellent compiler, generates very good code• When creating a project, select the “Hi-Tech PICC Toolsuite” as the language toolsuite– See Experiment #5 in Lab manual for full instructions on using PICC LiteV 0.2 5PICC C OptimizationsBy default, all code optimizations are turned off in during compilation. To enable assembly level optimizations (-O flag), do Project →Build →Options Projectto open the build options window. Click on the PICC Compiler tab.Check this to enable the –O optionSet value between 1 (lowest effort) to 9 (highest effort). Generally, > 3 does not help much.V 0.2 6PICC Lite C Optimization Results (Lab #13)76941198-O –Zg3Level 3 global optimization119812281425Code Size(words)7694-O –Zg9Level 9 global optimization7694-O7694None (default)Bank 1 Ram(bytes)Bank 0 Ram(bytes)Optimization2V 0.2 7Referring to Special RegistersPORTB = 0x80;#include <pic.h>Must have this include statement at top of each file. Will include a processor-specific header file based on device chosen in MPLAB.This header file contains #defines for all special registers: #static volatile unsigned char PORTB @ 0x06;special registerfound in pic1687x.h in PICC Lite installation directorymemory location in PICIn C code, can refer to special register using the register nameV 0.2 8bittst, bitclr, bitset Macros#define bitset(var,bitno) ((var) |= (1 << (bitno)))#define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))#define bittst(var,bitno) (var & (1 << (bitno)))Include these utility C macros at the top of all of your C files (does not matter where, just have them defined before you use them).Example usage:bitset(PORTB,7); /* MSB ← 1 */bitclr(PORTB,0); /* LSB ← 0 */if (bittst(PORTB, 0)) {/* do something */}Under PICC Lite, these macros compile to the equivalent PIC bsf, bcf, btfsc, btfssinstructions.V 0.2 9PICF16873Hardware lab exercises will use the PICF16873 (28-pin DIP)Note that most pins have multiple functions.Pin functions are controlled via special registers in the PIC.In-Circuit Programming(ICP) will be used to program memory contents from a PC without removing the device from the protoboard.V 0.2 10Initial Hookup7805Pwr ConnWall Xfmr9V5V16F873VddVss1.0µVpp/MclrVpp/Mclr VddVssRB7/PGDRB6/PGCNote polarity of LED!! Should turn on when reset button is pressed.RB7/PGDRB6/PGCUse 5-pin header for ISP connector. The ordering of the pins is up to you, but the picture in the prototyping appendix has 1-to-5, right to left.If there are multiple VDD/VSS pins on your PIC, hook them all up!!!RB110K ohmReset SwitchOscillatorOE Vdd5VVss OutOsc1/Clkin470 ohm470 ohmPower onLED+V 0.2 11Powering the PIC7805Pwr ConnWall Xfmr9V5V16F873VddVss1.0µ470 ohmPower onLED+Wall transformer provides 9V DC unregulated (unregulated means that voltage can vary significantly depending on current being drawn). Maximum current from Xfmr is 650 mA.The 7805 voltage regulator provides a regulated +5V. Voltage will stay stable up to maximum current rating of device.With writing on device visible, input pin (+9 v) is left side, middle is ground, right pin is +5V regulated output voltage.V 0.2 12Aside: How does an LED work?5V470 ohmPower onLEDAnode (long lead)Cathode (short lead)A diode will conduct current (turn on) when the anode is at approximately 0.7V higher than the cathode. A Light Emitting Diode (LED) emits visible light when conducting – the brightness is proportional to the current flow.current limiting resistorCurrent = Voltage/Resistance ~ (5v – 0.7v)/470 Ω = 9.1 mA3V 0.2 13In-Circuit Programming16F873VddVss1.0µVpp/MclrVpp/Mclr VddVssRB7/PGDRB6/PGCRB7/PGDRB6/PGC10K ohmReset Switch++5Vmodular cableICD-2 programmerDuring programming, this pin will have +12 V. This diode is very important – it protects the other devices connected to the +5V supply from the +12 V that is applied during programming. The diode does not conduct if the cathode voltage > anode voltage. Be sure you have the polarity correct; the diode should turn on (dimly) when the reset button pressed.Picture stolen from microchip WWW site.V 0.2 14Reset16F873VddVss1.0µVpp/Mclr10K ohmReset Switch++5VWhen reset button is pressed, the Vpp/Mclr pin is brought to ground. This causes the PIC program counter to be reset to 0, so next instruction fetched will be from location 0. All µPs have a reset line in order to force the µP to a known state.10K resistor used to limit current when reset button is pressed. Diode will be very


View Full Document

MSU ECE 3724 - C and Embedded Systems

Documents in this Course
Timers

Timers

38 pages

TEST 4

TEST 4

9 pages

Flags

Flags

6 pages

Timers

Timers

6 pages

Timers

Timers

54 pages

TEST2

TEST2

8 pages

Load more
Download C and Embedded Systems
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C and Embedded Systems and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C and Embedded Systems 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?