This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

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

Unformatted text preview:

C and Embedded Systems•A μC-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 μC•Many programs for embedded systems are written in Cyp g y– Portable – code can be retargeted to different processors – Clarity – C is easier to understand than assemblyMd il d dhi l ll–Modern compilers produce code that is close to manually-tweaked assembly language in both code size and performance1V0.7So Why Learn Assembly Language?yygg• The way that C is written can impact assembly language size and performanceand performance– i.e., if the uint32 data type is used where uint8 would suffice, both performance and code size will suffer. L i th bl l hit t f th t tC•Learning the assembly language, architecture of the target μC provides performance and code size clues for compiled C–Does the μC have support for multiply/divide?– Can the μC shift only one position each shift or multiple positions? (i.e, does it have a barrel shifter?)– How much internal RAM does the μC have?μ–Does the μC have floating point support?• Sometimes have to write assembly code for performance 2reasons.V0.7C CompilationV0.7 3MPLAB PIC24 C Compiler•Programs for hardware experiments are written in Cgp• Will use the MPLAB PIC24 C Compiler from Microchip• Excellent compiler, based on GNU C, generates very good code• Use the MPLAB example projects that come with the ZIP archive associated with the first hardware lab as a start for your projects4V0.7Referring to Special Function Registers#include "pic24.h"hhiild ffil i l d hMust have this include statement at top of a Cfile to include the all of the header files for the support libraries.S i l F ti R i t b d lik i blSpecial Function Registers can be accessed like variables:extern volatile unsigned int PORTB attribute (( sfr ));extern volatile unsigned int PORTB __attribute__((__sfr__));Special ftiDefined in compiler header filesRegister NamePORTB = 0xF000;function registerIn C code, can refer to special register Name5,pgusing the register nameV0.7Referring to Bits withinSpecial Function RegistersSpecial Function RegistersThe compiler include file also has definitions for individual bits within special function registers. Can use these to access i di id l bit d bit fi ldindividual bits and bit fields:PORTBbits.RB5 = 1; //set bit 5 of PORTBO bit 2 0 // l bit 2 f OPORTBbits.RB2 = 0; //clear bit 2 of PORTBif (PORTBbits.RB0) {//1//execute if-body if LSb of PORTB is '1'....}A bit field in a SFR is a grouping of consecutive bits; can also be assigned a value. 6OSCCONbits.NOSC = 2; //bit field in OSSCON register V0.7Referring to Bits withinSpecial Function RegistersSpecial Function RegistersUsing registername.bitname requires you to remember both the register name and the bitname. For bitnames that are UNIQUE j tbiUNIQUE, can use just _bitname._RB5 = 1; //set bit 5 of PORTB2 0 // l bit 2 f O_RB2 = 0; //clear bit 2 of PORTBif (_RB0) {//1//execute if-body if LSb of PORTB is '1'....}_NOSC = 2; //bit field in OSSCON register 7V0.7Variable Qualifiers, InitializationQ,If a global variable does not have an initial value, by default the runtime code initializes it to zero–this includes staticthe runtime code initializes it to zero this includes static arrays. To prevent a variable from being initialized to zero, use the _PERSISTENT macro in front of it:uint16 u16_k; //initialized to 0uint8 u8_k = 4; //initialized to 4_PERSISTENT uint8 u8_resetCount; //uninitialized, value// is unknownThe C runtime code is run before main() entry, so run on every power-up, every reset. Use _PERSISTENT variables to V0.7 8track values across processor resets.C Macros, Inline FunctionsThe support library and code examples makes extensive use ofThe support library and code examples makes extensive use of C macros and Inline functions. The naming convention is all uppercase:#define DEFAULT_BAUDRATE 57600Mthlfthd#define LED1 _RB15Macros, the left hand label is replaced by the right hand textstatic inline void CONFIG_RB1_AS_DIG_INPUT(){DISABLE_RB1_PULLUP();TRISB1=1;Inline functions expand _TRISB1 1;_PCFG3 = 1;}without a subroutine call.V0.7 9PIC24HJ32GP202 µCHardware lab exercises will use the PIC24HJ32GP202 µC (28-pin DIP)Note that most pins have multiple functions.functions.Pin functions are controlled via special pregisters in the PIC. Will download programs into the PIC24 µC via a serial bootloader that allows the PIC24 µC to program itself10the PIC24 µC to program itself.Copyright Delmar Cengage Learning 2008. All Rights Reserved.From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.V0.7Initial HookupThere are multiple VDD/VSS pinsAny input voltage from 5 V to 15 V will workVDD/VSS pins on your PIC24 µC; hook them all up!!!will work.Not included in your board.11Copyright Delmar Cengage Learning 2008. All Rights Reserved.From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”.V0.7Powering the PIC24 µCPIC24 µCThe POWER LED provides a visual indication that power is on.A Wall transformer provides 15 to 6V DC unregulated (unregulated means that voltage can vary significantly depending on current being drawn). The particular wall Xfmr in the parts kit g)p pprovides 6V with a max current of 1000 mA.The LM2937-3.3 voltage regulator provides a regulated +3.3V. 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 +3.3V 12,g,gpregulated output voltage.V0.7Aside: How does an LED work?Aside: How does an LED work?3.3V Anode (long lead)470Power onLEDCathode (short lead)470 ohmA diode will conduct current (turn on) when the anode is atcurrent 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 hbih i i l h fl Th–the brightness is proportional to the current flow. The voltage drop across LEDs used in the lab is about 2V.Current = Voltage/Resistance (3 3vLED voltage drop)/470Ω13Current = Voltage/Resistance ~ (3.3v –LED voltage drop)/470 Ω=


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?