C 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 userThe ser onl indirectl interacts ith the embedded s stem–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 Cpg y– Portable – code can be retargeted to different processors – Clarity – C is easier to understand than assemblyil d dhi l llkd–compilers produce code that is close to manually-tweaked assembly language in both code size and performanceV 0.8 1So Why Learn Assembly Language?yygg• The way that C is written can impact assembly language size and performanceand performance– i.e., if the int data type is used where char would suffice, both performance and code size will suffer. L i th bl l hit t f th t tP•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 V 0.8 2reasons.CCompilationV 0.8 3CCompilationCopyright Thomson/Delmar Learning 2005. All Rights Reserved.Microchip C18 Compiler•Programs for hardware experiments (labs 6-13) gp()are written in C• Will use the Microchip C18 C Compiler– Integrated with MPLAB, student edition available as free download on Microchip website•A .hex file is produced as a result of C compilation– A hex file is a ASCII-hex representation of the machine codecode• See lab manual for instructions on C compilation.V 0.8 4Importing .hex files within the MPLAB®IDE• If you have a .hex file produced outside of the MPLAB® IDE, it can be imported and executed within the MPLAB®IDE.• Select the correct PIC18 device by using “Configure →Select Device”→Select Device• Use the command “File →Import” to import a .hex file– Browse to the directory that contains your .hex file, select it, and click on ‘OPEN’• Once the .hex file is loaded, use ‘View → Program Memory’to verify that memory contains valid instructionsMemory to verify that memory contains valid instructions.V 0.8 5Referring to Special Registers#include <config.h>In our projects, must have this include statement at the top of a C file to include the processor header files for the PIC18 family. This ‘config.h’ file includes many other header files; one of these contains #defines for special registers so that special ibfdbregisters can be referred to by name:PORTB = 0x80;In C code, can refer to special register V 0.8 6,pgusing the register namebittst, bitclr, bitset Macros#define bitset(var,bitno) ((var) |= (1 << (bitno)))#define bitclr(var,bitno) ((var) &= ~(1 << (bitno)))#define bittst(var,bitno) (var & (1 << (bitno)))This utilityCmacros are also included as part of‘config h’This utility C macros are also included as part of config.h. Example usage:bitset(PORTB,7); // MSB ← 1 bitclr(PORTB,0); // LSB ← 0 Under C18, these macros compile to the if (bittst(PORTB, 0)) {// do something equivalent PIC18 bsf, bcf, btfsc, btfssinstructions.V 0.8 7// g}Referring to Bits within Special RegistersThe “config.h” include file also has definitions for individual gbits within special function registers:RB3 = 1;Both do the same thing; set ‘bit 3’ of PORTB to a‘1’bitset(PORTB,3);of PORTB to a 1.V 0.8 8Runtime Code Produced by C18The code produced by the C18 C compiler first executes run-time start-up code before jumping to your main() routine. The runtime code begins at the reset vector (location 0x0000). Users have theThe runtime code begins at the reset vector (location 0x0000). Users have the options of choosing different behaviors for runtime code in terms of global variable initialization. We will use the ‘init’ behavior, in which only globals with explicit initialization will be initialized. char a;int k;No explicit initialization, so value after power up is undefined. After a reset, the memory location is not touched so int j = 10;char*astring=“hello”;ycontains whatever it contained before reset.The initial values for these variables char astring hello ;main() {// your codeare stored in program memory. Reset code copies initial values from program memory to data memory so V 0.8 9// your code }that these are guaranteed to have these values when main() is entered.PIC18F2420Hardware lab exercises will use theexercises will use the PIC18F2420 (28-pin DIP)Note that most pins have multiple functionsfunctions.Pin functions are controlled via specialcontrolled via special registers in the PIC18F2420.Will download programs into the PIC18F2420 via a serial bootloader that allows the PIC18F2420 toV 0.8 10that allows the PIC18F2420 to program itself.Copyright Thomson/Delmar Learning 2005. All Rights Reserved.Initial HookupIf there are multiple VDD/VSS pins on your PIC18F2420, hook them all up!!!pV 0.8 11Copyright Thomson/Delmar Learning 2005. All Rights Reserved.Powering the 7805PwrWall Xfmr9V5VPIC18PIC18F2420Pwr ConnVddVss1.0μPower on+470 ohmLEDWall transformer provides 6V DC regulated. Maximum current from Xfmr is 1A.The LM2940 (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 (+6 v) is left side, middle is ground, right V 0.8 12pin is +5V regulated output voltage.Aside: How does an LED work?Aside: How does an LED work?5V Anode (long lead)470Power onLEDCathode (short lead)470 ohmcurrent limiting resistorA 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 g() g g– the brightness is proportional to the current flow.Current = Voltage/Resistance ~ (5v –0.7v)/470 Ω= 9.1 mAV 0.8 13g()ResetWhen reset button10K ohmWhen reset button is pressed, the Vpp/Mclr pin is bhtt dPIC18F2420VddVpp/++5Vbrought to ground. This causes the PIC18 program VddVss1.0μVpp/MclrReset Switch+counter to be reset to 0, so next instruction fetchedinstruction fetched will be from location 0. All μPs htlii10K resistor used to limit current have a reset line
View Full Document