This preview shows page 1-2-3-20-21-22-41-42-43 out of 43 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 43 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 43 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 43 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 43 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 43 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 43 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 43 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 43 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 43 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 43 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C and Embedded SystemsSo Why Learn Assembly Language?C CompilationPICC18 C CompilerUsing the PICC18 compilerImporting .hex files within the MPLAB® IDEReferring to Special Registersbittst, bitclr, bitset MacrosReferring to Bits within Special RegistersBit Testing within CRuntime Code Produced by PICC18PIC18F242Initial HookupPowering the PIC18F242Aside: How does an LED work?ResetThe ClockConfiguration BitsSpecifying Configuration Options in CProgramming the PIC18F242 Flash MemoryA Serial BootloaderMAXIM 232/202 driver/receiverHyperterminalReading the PIC18Fxx2 DatasheetPIC18 Datasheet: Example Register SummaryPIC18F242 ResetPIC18Fxx2 Reset SourcesWhat RESET type occurred?What RESET type occurred? (cont)Watchdog TimerWDT SpecificsPower ConsumptionStatic Power/SLEEP ModeDynamic PowerFrequency versus Voltagereset.c Programreset.c Program Listingreset.c Program Listing (cont).Sleep Mode and WDTMore on reset.cOptional In-Circuit ProgrammingWhat do you have to know?V 0.7 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.7 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.7 3C CompilationCopyright Thomson/Delmar Learning 2005. All Rights Reserved.V 0.7 4PICC18 C Compiler• Programs for hardware experiments (labs 6-13) are written in C• Will use the PICC18 C Compiler– Company is Hi Tech (www.htsoft.com)– Excellent compiler, generates very good code• See lab manual for instructions on C compilation.V 0.7 5Using the PICC18 compilerTo compile a file called ‘myfile.c’, do:picc18 –O –a200 –18F242 myfile.c-a200 locates the code starting at location 0x200 in memory (must do this for code programmed by serial bootloader).Specifies the particular PIC18 device-O option turns on compiler optimizations (reduces number on instructions generated).Output file produced is called ‘myfile.hex’ (A hex file is a ASCII-hex representation of the machine code).Other compile options:picc18 –O –a200 –lf –18F242 myfile.cRequired for printf() statements containing ‘longs’, ‘floats’.The option –lf is letter L, letter F, in lowercase.V 0.7 6Importing .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”• 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’– If you get the error ‘Unexpected End of File’, then on a Unix machine such as ‘yavin.ece.msstate.edu’, do “unix2dos myfile.hex” – This converts the end-of-line format within the hex file from Unix-style to DOS style• Once the .hex file is loaded, use ‘View → Program Memory’ to verify that memory contains valid instructions.V 0.7 7Referring to Special Registers#include <pic18.h>Must have this include statement at top of a C file to include the processor header files for the PIC18 family. The This header file contains #defines for all special registers: PORTB = 0x80;#static volatile near unsigned char PORTB @ 0xF81;found in /usr/local/hitech/include/pic18fxx2.h memory location in PIC18special registerIn C code, can refer to special register using the register nameV 0.7 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:Under PICC18, these macros compile to the equivalent PIC18 bsf, bcf, btfsc, btfssinstructions.bitset(PORTB,7); // MSB ← 1 bitclr(PORTB,0); // LSB ← 0 if (bittst(PORTB, 0)) {// do something }V 0.7 9Referring to Bits within Special RegistersThe pic18fxx2.h include file also has definitions for individual bits within special function registers:#static volatile near bit CARRY @((unsigned)&STATUS*8)+2;bit data typenamed bitlocation that contains this bitbit offset within registerBoth do the same thing. The bitdata type is not standard C – it is a non-standard extension of the language. But commonly done, so we will use it.CARRY = 1;bitset(STATUS,2);V 0.7 10Bit Testing within Cif (!CARRY) {// do if carry == 0 }if (CARRY) {// do if carry == 1 }if (!bittst(STATUS,2) {// do if carry == 1 }if (bittst(STATUS,2) {// do if carry == 1 }The above are all valid code fragments. Using the named bit symbols improves code clarity.However, must still know that ‘CARRY’ refers to a bit and not a register!!!! Is PIR1 a bit or a special function register? How do you know? Look in the data sheet!!!!V 0.7 11Runtime Code Produced by PICC18The code produced by PICC18 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), and it clears any uninitialized values to zero, or initializes variables to the value specifiedin the C code.char a;int k;int j = 10;char *astring = “hello”;persistent int s;main() {// your code }Initialized to ‘0’ by reset code, which is the C default value for global variables.The initial values for these variables are stored in program memory. Reset code copies initial values from


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?