Home
Lectures 9 & 10
Contents
1. Increase each element by 1 exit if an element increases to 0 int my_array 50 1 2 3 4 1 int array my_array do Yes array while array http class ece iastate edu cpre288 17 http class ece iastate edu cpre288 18 Variable scope 9 28 2015 Variable scope Global vs Local Global variable Declared outside of all functions May be initialized upon program startup Visible and usable everywhere from c file What happens when local global have the same name Local takes precedence Summary Local declared inside of a function visible only to function Global declared outside all functions visible to all functions Fall 2011 http class ece iastate edu cpre288 19 Variable scope What happens when you want a local variable to stick around but do not want to use a global variable Create a static variable Syntax static Type Name Static variables are initialized once Think of static variables as a local global Sticks around has persistence but only the function can access it Variable scope C global variable visible to all program files int global var C file wide static variables visible only in this file static int static var Local static variables any func static int static var Visibility Scope Across Multiple Files Example How to define and use global variables In header file myvar h extern int global_var I
2. CprE 288 Introduction to Embedded Systems Instructors Dr Phillip Jones http class ece iastate edu cpre288 1 Announcement e HW4 due on Tuesday 9 29 e Exam 1 In class Thursday 10 8 e Lab 4 Clock Interrupts Debugging http class ece iastate edu cpre288 3 Interrupt Service Routine Interrupt Hardware may raise interrupt to inform the CPU of exceptional events Timer expires ADC gets new data Anetwork packet arrives Conceptually it like the CPU calls your ISR function You will learn more low level details when studying assembly ISR Interrupt Service Routine http class ece iastate edu cpre288 5 9 28 2015 Overview Announcements Interrupts Precedence Scope Memory layout Recursive Function C Library functions Casting http class ece iastate edu cpre288 2 http class ece iastate edu cpre288 4 Interrupt Service Routine ISR is a function that runs when there is an interrupt from aa y N a internal or external source An interrupt occurs Foreground program is suspended The ISR is executed Foreground program is resumed An ISR is a special type of function No explicit return value and no explicit parameters http class ece iastate edu cpre288 6 9 28 2015 Interrupt Service Routine ISR Example Lab 4 Example of stack use in ISR execution int main Icd_init main timer_init enable interrupt func1 func1 func1 while
3. Implicit Conversion In an expression e A shorter value is converted to a longer value before the operation e The expression has the type of the longer one c h c is extended to 16 bit and then added with h n 1 n is extended to 32 bit and then added with f d f is extended to double precision before being added with d http class ece iastate edu cpre288 54 Explicit Conversion From String to Others include lt inttype h gt include lt stdlib h gt n strtol 10 MaE 10 ESti 2 5 d strtod 2 5 f 2 5 in single precision d 2 5 in double precision strtol string to long strtof string to float strtod string to double http class ece iastate edu cpre288 55 Explicit Casting int i 60 float f 2 5 f float i 3 http class ece iastate edu cpre288 57 C Library Functions In C many things are carried out by library functions Simple language rich libraries Commonly used libraries File J O include user input output String manipulations Mathematical functions Process management Networking http class ece iastate edu cpre288 59 9 28 2015 Type Casting Explicitly convert one data type to another data type type name expression int n1 1 unsigned int n2 1 if n1 lt int n2 this is true if unsigned int n1 lt n2 this is false http class ece iastate edu cpre288 56 http class ece iast
4. e for i 0 i lt 10 i for i 0 i lt 10 i 995 995 PC Address for line 9 doNothing PC Address for line 9 994 994 return 0 993 return 0 993 http class ece iastate edu cpre288 Eyi http class ece iastate edu cpre288 38 Example Stack Example Stack void doNothing void doNothing of 999 int main int main 998 re char x y Z char x y Z 997 int i int i Bae for i 0 i lt 10 i for i 0 i lt 10 i doNothing doNothing ae CS e a return O return 0 993 an 2 http class ece iastate edu cpre288 39 http class ece iastate edu cpre288 40 Stack Memory Layout Example Stack Memory Layout Example include cavr io h gt charx IVE 2AE e Class work out on board inti 8 i void bey int pi Final values for all ie aii eo ee Gi pi char p1 i est Gata ox ypi oon p memory locations s aro leet char p2 a Sata ax1otD Tepp 006 chan app dat ca al ee avoid test y B igi Fy y 2 2 3 eo an i i s cata Oxiers 05 naa aaa pi amp i inet 22 ee ni ee ci K asa liS pi 87 NEET goe mii aoa leon a6 a m i i t pl amp x pt 87 Tr p2 amp z s S Eee pp3 amp p2 E a p1 pps x z i 993 5 pp3 amp y pp3 5 y 5 Note Before calling test the stack pointer started at Ox10FB added the program counter and the current stack poi
5. l return l return events F t t ISR TIMER3_COMPA_VECT Processing TIMER MPA Adapted from fundamentals of embedded software fig 7 1 http class ece iastate edu cpre288 11 http class ece iastate edu cpre288 12 ISR Macro 9 28 2015 e Two easy steps to using interrupts 1 Enable the interrupt every interrupt has an enable bit e Look up in the datasheet to see what register name and bit position you will need to set 2 Write the ISR interrupt service routine e The ISR is a function or block of code that the processor will call for you whenever the interrupt event occurs e The ISR macro needs one parameter the name of your interrupt vector You can find a list of interrupt vectors here http www nongnu org avr libc user manual group avr __interrupts html http class ece iastate edu cpre288 13 Primary Expression Se Ex porera Sl aeliedoeel od http class ece iastate edu cpre288 15 Are s required http class ece iastate edu cpre288 14 Exercise Operation Precedence a b c d same as a b c d How about the following expression and condition x y zt k x y z k str str str str 1 if a 10 amp amp b 20 if a 10 amp amp b 20 if a amp OxOF b amp OxOF if a amp OxOF b amp OxOF if a amp 1 0 http class ece iastate edu cpre288 16 x amp 0x10 0x10 No x amp y ve x 23 amp amp y lt 12 No
6. 1 ISR do nothing ISR_func1 An ISR function saves register context to be studied may call other functions and restores register context and stack top before it returns http class ece iastate edu cpre288 7 http class ece iastate edu cpre288 8 ISR Example Lab 4 Volatile Varaibles j Tamer interrupt source lg the function will be Volatile variable The memory content may change even if the called every one second to update clock running code doesn t change it ISR TIMER1 COMPA vect i l l 3 7 volatile unsigned char pushbutton reading M YOU CODE LSR TIMERS COMBA VECE Timer interrupt source 2 for checking push rae HG ae oe geen button button five times per second eee Oe ie ISR TIMER3 COMPA vect 7 LOUR CODE ee Wolke lpusnmoucton sreading E An ISR Macro automatically associates the ISR function with an interrupt source other code TIMER1 COMPA vect ATMega128 Timer 1 Output Compare A match to be studied TIMER3 COMPA vect ATMega128 Timer 3 Output Compare A match 10 Interrupt in Embedded Systems Interrupt in Embedded Systems e A low level simplified hardware figure to show how an event is mapped to a ISR vector Interrupt Vector Table Events gt ISR TIMER1_COMPA_VECT Interrupt Interrupt Interrupt aS TP ISR for ISR for ISR for Initialize task 1 task 2 task 3 TIMER1 COMPA vect Wait for l return
7. ate edu cpre288 58 C Library Functions Use standard file I O include the header file for I O lib include lt stdio h gt main use the fprintf function fprintf stdout s n Hello World n http class ece iastate edu cpre288 60 10 C Library Functions Formatted output printf fprintf sprintf and more use conversion specifiers as follows os string d signed decimal Jou unsigned decimal Px hex of floating point float or double How to output the following variables in format a b str ina single line int a float b Int e char str 10 http class ece iastate edu cpre288 61 C Library Functions Error processing and reporting use exit function include lt stdio h gt include lt stdlib h gt void myfunc int x if x lt 0 fprintf stderr s n x is out of range exit 1 http class ece iastate edu cpre288 63 C Library Functions How to find more On Linux machines Use man man printf man string man string h man math h Most functions are available on Atmel platform http class ece iastate edu cpre288 65 9 28 2015 C Library Functions String operations copy compare parse strings and more include lt string h gt strcpy copy one string to another strcmp compare two strings strlen calculate the length of a string strstr search a string for the occurrence
8. class ece iastate edu cpre288 48 Type Conversion and Casting Recall C has the following basic data types char short int long float double Assume charc shorth intn long float f double d What s the meaning of eS ln n lne f N f gt d http class ece iastate edu cpre288 49 Implicit Conversion A shorter integer value is extended before being assigned to a longer integer variable laf the 16 bit value of h is extended to 32 bit h c the 8 bit value of c is extended to 16 bit signed extension or not is dependent on the system http class ece iastate edu cpre288 Sil Implicit Conversion A float double is floored to the closest integer when assigned to an integer char variable i 05s n f H m io d 20 5 d 20 http class ece iastate edu cpre288 53 9 28 2015 Implicit Conversion A longer integer value is cut short when assigned to a shorter integer variable or char variable char c short h 257 long l ce lh The rightmost 8 bit of h is copied into c nal The rightmost 16 bit of is copied into n http class ece iastate edu cpre288 50 Implicit Conversion A double type is converted to float type and vice versa using IEEE floating point standard d 10 0 10 0 with double precision i a 10 0 with single precision f 200 20 0 with single precision d 20 0 with double presion http class ece iastate edu cpre288 52
9. ction and Stack Function Frame Local storage for a function Example 1 A is called 2 A calls B 3 B calls C 4 C returns high 1 2 3 4 Addresses gt ny sp SP ot stack growth Figure 1 Stack Frame creation and destruction Example Stack e The following example shows the execution of a simple program left and the memory map of the stack right http class ece iastate edu cpre288 33 Example Stack void doNothing 999 int main 998 char j i 997 996 for i 0 i lt 10 i doNothing F 994 return 0 993 http class ece iastate edu cpre288 35 Function and Stack What can put in a stack frame e Function return address e Parameter values e Return value e Local variables e Saved register values May 18 2011 http class ece iastate edu cpre288 9 28 2015 w N Example Stack void doNothing char c int Be int i for i 0 i lt 10 i doNothing return 0 http class ece iastate edu cpre288 1000 999 998 997 995 994 998 w 5 Example Stack void doNothing char c int main char x y Z int i doNothing return 0 http class ece iastate edu cpre288 1000 999 998 997 996 995 993 w D 9 28 2015 Example Stack Example Stack void doNothing char c 1000 999 int main 998 int main char x y Z 997 char x y Z on Int i e Int l
10. in its own file not visible main modifier see later in any other file printt td m http class ece iastate edu cpre288 25 Understanding Data e Stack Stores data related to function variables function calls parameters return variables etc Data on the stack can go out of scope and is then automatically deallocated Starts at the top of the program s data memory space and addresses move down as more variables are allocated e Heap Stores dynamically allocated data Dynamically allocated data usually calls the functions alloc or malloc or uses new in C to allocate memory and free to or delete in C deallocate There s no garbage collector Starts at bottom of program s data memory space and addresses move up as more variables are allocated http class ece iastate edu cpre288 27 http class ece iastate edu cpre288 28 Function and Stack Function and Stack Conventional program stack grows downwards New items Auto local variables have their storage in stack are put at the top and the top grows down Why stack e The LIFO order matches perfectly with functions call return order LIFO Last In First Out stack growth Function Last called first returned high address stack top e Efficient memory allocation and de allocation low address Allocation Decrease SP stack top De allocation Increase SP Fun
11. n program file myvar c include myvar h int global var In program file usevar c include myvar h use myvar Visibility Scope Across Multiple Files File1 c File2 c global variable extern int count int count 0 int x count This instance of count is visible This is how to use the global in all files in the same project variable count declared in file1 c extern declaration is usually put in a header file http class ece iastate edu cpre288 23 File1 c File2 c global variable another global variable with the same name int count 0 int count 100 Another scenario We want to Bad use The compiler linker will use the same name count in report conflicting use of name multiple program files eachasa count unique variable instance Some complier may tolerate it still bad practice http class ece iastate edu cpre288 24 9 28 2015 Visibility Scope Across Multiple Files Variable scope Visibility scope Where a variable is visible File1 c File2 c int m 0 static global variable count for file2 c oe static int count 0 static int count 100 int any func Outside the functions static file2 c gets its own count means to limit the visibility of There is no conflict int m count to this program file only Each instance of count is static is a also a storage class visible
12. nter to the stack at address 0x10F9 and Ox10FB http class ece iastate edu cpre288 41 http class ece iastate edu cpre288 42 9 28 2015 Memory Address Space General Memory Layout It is the addressability of the memory Upper bound of memory that can be accessed by a program The larger the space the more bits in memory addresses static char greeting _ 32 bit address accessibility to 4GB memory Ce ees a High end main What are a l ime Le D na ee Virtual memory address space a Physical memory address space Physical memory size I O addresses ports IED InN LCD PutStringlgreeting http class ece iastate edu cpre288 43 http class ece iastate edu cpre288 44 Harvard Architecture Two separate memory address spaces for A function that calls itself instruction and data Program Memory 90000 calculate the greatest common edivyvisor ime Gee int m ime m Application Flash Section if m 0 return m else relunrm Gea m e n Boot Flash Section er ee SFFFF http class ece iastate edu cpre288 45 http class ece iastate edu cpre288 46 The use of stack by a recursive function main main main main main feo EKO gcd 10 4 gcd 10 4 gcd 10 4 gcd 10 4 gcd 4 2 gcd 4 2 gcd 4 2 gcd 2 0 What happens if a function keeps calling itself and does not end the recursion http class ece iastate edu cpre288 47 http
13. of another string http class ece iastate edu cpre288 62 C Library Functions Math library functions include lt math h gt n round x FP round function To build gcc Wall o myprogram Im myprogram c http class ece iastate edu cpre288 64 C Library Functions More information on C Library functions http www acm uiuc edu webmonkeys book c _guide Other commonly used stdlib h Some general functions and macros assert h Run time self checking ctype h Testing and converting char values http class ece iastate edu cpre288 66 11 AVR C Library Functions AVR Libc Home Page http www nongnu org avr libc Non AVR specific alloca h Allocate space in the stack assert h Diagnostics ctype h Character Operations errno h System Errors inttypes h Integer Type conversions math h Mathematics setjmp h Non local goto stdint h Standard Integer Types stdio h Standard IO facilities stdlib h General utilities string h Strings http class ece iastate edu cpre288 67 AVR C Library Functions AVR Libc Home Page htt AVR specific avr interrupt h Interrupts Www nongnu or avr io h AVR device specific IO definitions avr power h Power Reduction Management avr libc avr sleep h Power Management and Sleep Modes util setbaud h Helper macros for baud rate calculations Many others http class ece iastate edu cpre288 9 28 2015 12
Download Pdf Manuals
Related Search
Related Contents
日本エマージェンシーアシスタンス(6063) Sanyo SANUPS E11A202U User's Manual ET 8 and 12 QUICK PROGRAMMING GUIDE 200804 - iscea Hyundai W240S User Guide Manual ImmunoScreener Module 1.12 User's Guide March 03, 2009 デジタル眼底カメラCR RHPS Series BD(H)20,25 (-DP) User Manual (MS-CRD Beijing Oriental Wison Mechanical & Electronic Co., Ltd. Copyright © All rights reserved.
Failed to retrieve file