[PDF] [PDF] C Programming for Embedded Systems

//global (static) variables – placed in RAM /* Function definitions*/ int function1( char x) { //parameter x passed to the function, function returns an integer value



Previous PDF Next PDF





[PDF] Pointers: Parameter Passing and Return Passing and Return

Useful for returning more than one value from a function □ Still call-by-value, but now the address is copied, Example: Swapping using pointers Example: Passing structure pointers By convention, a string in C is terminated by the



[PDF] C Programming Tutorial

The next line return 0; terminates main()function and returns the value 0 Compile Execute C Program Let's look at how to save the source code in a file, and 



[PDF] C++ Programming - Elearning IT Telkom Purwokerto

1301 INDEX 1331 vi C++ Programming: From Problem Analysis to Program Design, Fifth Edition More Examples of Value-Returning Functions 335



[PDF] LECTURE NOTE on PROGRAMMING IN “C” - VSSUT

The main function does not return any value when void (means null/empty) as void main(void ) or void main() { printf (“C language”); } Output: C language



[PDF] Origin C Programming Guide - OriginLab

The function parameters and return value need to meet certain criteria for the function to be accessible from script and there are techniques to make such



[PDF] C Programming for Embedded Systems

//global (static) variables – placed in RAM /* Function definitions*/ int function1( char x) { //parameter x passed to the function, function returns an integer value



[PDF] Programmation en langage C - Centre Inria de Paris

Table des mati`eres 1 Les bases de la programmation en C 9 1 1 Historique char do extern if return static union while que l'on peut ranger en catégories On appelle Lvalue (left value) tout objet pouvant être placé `a gauche d'un opérateur d'affectation prog c:11: warning: control reaches end of non-void function



[PDF] Sample C Test questions 1 What is the value of - Continental

Sample C Test questions 10 What does the following function return? int Func1( void) { int a = 30; int b = 9 - a / 5; int c; c = b * 4; if (c > 10) { c = c - 10; } return c 



[PDF] Solutions to Exercises

isalpha c = (isupper c) \/ (islower c) \/ (isdigit c) This approach Miranda type system; for example, any function which applied hd to a list of integers could not be sure whether the value returned was going to be an integer or a list Exercise 3 4



[PDF] Some Practice Problems for the C++ Exam and Solutions for the

greater than zero), then the function should return the value 0 as a sentinel value to indicate that an error occurred Thus, for example, cout

[PDF] values activities for college students

[PDF] values attitudes and goals in clothing

[PDF] valvoline oil filter chart

[PDF] valvoline premium oil filter

[PDF] valvoline vo 1 oil filter

[PDF] valvoline vo 39

[PDF] valvoline vo56

[PDF] van dijk critical discourse analysis

[PDF] van't hoff factor

[PDF] vandalia il arrests

[PDF] vande bharat mission flight booking

[PDF] vande bharat mission flight schedule phase 4

[PDF] vande bharat mission phase 4 schedule

[PDF] vänee timer

[PDF] vanguard institutional 500 index trust

C programming for embedded

microcontroller systems.

Assumes experience with

assembly language programming.

V. P. Nelson

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Outline

•Program organization and microcontroller memory •Data types, constants, variables •Microcontroller register/port addresses •Operators: arithmetic, logical, shift •Control structures: if, while, for •Functions •Interrupt routines

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Basic C program structure

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

#include "STM32L1xx.h" /* I/O port/register names/addresses for the STM32L1xx microcontrollers */

/* Global variables -accessible by all functions */ intcount, bob; //global (static) variables -placed in RAM /* Function definitions*/ intfunction1(char x) {//parameter x passed to the function, function returns an integer value inti,j;//local (automatic) variables -allocated to stack or registers --instructions to implement the function /* Main program */ void main(void) { unsigned char sw1; //local (automatic) variable (stack or registers) intk; //local (automatic) variable (stack or registers) /* Initialization section */ --instructions to initialize variables, I/O ports, devices, function registers /* Endless loop */ while (1) { //Can also use: for(;;) { --instructions to be repeated } /* repeat forever */

Declare local variables

Initialize variables/devices

Body of the program

STM32L100RC µC memory map

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

0xE00F FFFF

0xE000 0000

0x4002 67FF

Peripheral

registers

0x4000 0000

0x2000 0000

0x0803 FFFF

1

6KB RAM

256KB Flash

MemoryCortex

registersControl/data registers:Cortex-M3 CPU functions (NVIC, SysTickTimer, etc.)

Control/data registers:

microcontroller peripherals (timers, ADCs, UARTs, etc.)

256K byte Flash memory:

program code & constant data storage

Reset & interrupt vectors:

in 1 st words of flash memory

0x0800 0000

16K byte RAM: variable & stack storage

Vacant

Vacant

Vacant Address

0x2000 3FFF

Vacant

0xFFFF FFFF

Vacant

Microcontroller "header file"

•KeilMDK-ARM provides a derivative-specific"header file" for each microcontroller, which defines memory addresses and symbolic labels for CPU and peripheral function register addresses.

#include "STM32L1xx.h" /* target uCinformation */ //GPIOA configuration/data register addresses are defined in STM32L1xx.h void main(void) { uint16_t PAval;//16-bit unsigned variable GPIOA->MODER &= ~(0x00000003);// Set GPIOA pin PA0 as input PAval= GPIOA->IDR;// Set PAvalto 16-bits from GPIOA for(;;) {} /* execute forever */

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

C compiler data types

•Always match data type to data characteristics! •Variable type indicates how data is represented •#bits determines range of numeric values •signed/unsigned determines which arithmetic/relational operators are to be used by the compiler •non-numeric data should be "unsigned" •Header file "stdint.h" defines alternate type names for standard C data types •Eliminates ambiguity regarding #bits •Eliminates ambiguity regarding signed/unsigned (Types defined on next page)

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

C compiler data types

Data type

declaration *Number of bitsRange of values char k; unsigned char k; uint8_t k;80..255 signed char k; int8_t k;8-128..+127 short k; signed short k; int16_t k;16-32768..+32767 unsigned short k; uint16_t k;160..65535 intk; signed intk; int32_t k; -2147483648.. +2147483647
unsigned intk; uint32_t k;32

0..4294967295

* intx_tand uintx_tdefined in stdint.h

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Data type examples

•Read bits from GPIOA (16 bits, non-numeric) -uint16_t n; n = GPIOA->IDR; //or: unsigned shortn; •Write TIM2 prescalevalue (16-bit unsigned) -uint16_t t; TIM2->PSC = t; //or: unsigned short t; •Read 32-bit value from ADC (unsigned) -uint32_t a; a = ADC; //or: unsigned inta; •System control value range [-1000...+1000] -int32_t ctrl; ctrl = (x + y)*z; //or: intctrl; •Loop counter for 100 program loops (unsigned) -uint8_t cnt; //or: unsigned char cnt; -for (cnt= 0; cnt< 20; cnt++) {

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Constant/literal values

•Decimalis the default number format intm,n;//16-bit signed numbers m = 453; n = -25; •Hexadecimal:preface value with 0x or 0X m = 0xF312; n = -0x12E4; •Octal:preface value with zero (0) m = 0453; n = -023; Don't use leading zeros on "decimal" values. They will be interpreted as octal. •Character: character in single quotes, or ASCII value following "slash" m = 'a'; //ASCII value 0x61 n = '\13'; //ASCII value 13 is the "return" character •String(array) of characters: unsigned char k[7]; strcpy(m,"hello\n"); //k[0]='h', k[1]='e', k[2]='l', k[3]='l', k[4]='o', //k[5]=13 or '\n' (ASCII new line character), //k[6]=0 or '\0' (null character -end of string)

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Program variables

•A variableis an addressable storage location to information to be used by the program -Each variable must be declaredto indicate size and type of information to be stored, plus name to be used to reference the information intx,y,z;//declares 3 variables of type "int" char a,b;//declares 2 variables of type "char" -Space for variables may be allocated in registers,

RAM, or ROM/Flash (for constants)

-Variables can be automaticor static

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Variable arrays

•An arrayis a set of data, stored in consecutive memory locations, beginning at a named address -Declare array name and number of data elements, N -Elements are "indexed", with indices [0 .. N-1] intn[5]; //declare array of 5 "int" values n[3] = 5;//set value of 4 th array element

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson) n[0] n[1] n[2] n[3] n[4]

Address:

n n+4 n+8 n+12 n+16

Note: Index of first element is always 0.

Automatic variables

•Declare within a function/procedure •Variable is visible (has scope) only within that function -Space for the variable is allocated on the system stack when the procedure is entered •Deallocated, to be re-used, when the procedure is exited -If only 1 or 2 variables, the compiler may allocate them to registers within that procedure, instead of allocating memory. -Values are not retained between procedure calls

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Automatic variable example

void delay () { inti,j; //automatic variables -visible only within delay() for ( i=0; i<100; i++) { //outer loop for (j=0; j<20000; j++) { //inner loop } //do nothing

Variables must be initialized each

t ime the procedure is entered since values are not retained when the procedure is exited MDK-ARM (in my example): allocated registers r0,r2 for variables i,j

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Static variables

•Retained for use throughout the program in RAM locations that are not reallocated during program execution.

•Declare either within or outside of a function

-If declared outside a function, the variable is globalin scope, i.e. known to all functions of the program

•Use "normal" declarations. Example: intcount;

-If declared within a function, insert key word staticbefore the variable definition. The variable is localin scope, i.e. known only within this function.

static unsigned char bob; static intpressure[10];

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Static variable example

unsigned char count; //global variable is static -allocated a fixed RAM location //count can be referenced by any function void math_op() { inti;//automatic variable -allocated space on stack when function entered static intj; //static variable -allocated a fixed RAM location to maintain the value if (count == 0) //test value of global variable count j = 0;//initialize static variable j first time math_op() entered i= count;//initialize automatic variable ieach time math_op() entered j = j + i; //change static variable j -value kept for next function call }//return & deallocatespace used by automatic variable i void main(void) { count = 0;//initialize global variable count while (1) { math_op(); count++;//increment global variable count

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

C statement types

•Simple variable assignments -Includes input/output data transfers •Arithmetic operations •Logical/shift operations •Control structures -IF, WHEN, FOR, SELECT •Function calls -User-defined and/or library functions

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Arithmetic operations

•C examples -with standard arithmetic operators inti, j, k;// 32-bit signed integers uint8_t m,n,p ; // 8 bit unsigned numbers i= j + k;// add 32-bit integers m = n -5;// subtract 8-bit numbers j = i* k;// multiply 32-bit integers m = n / p;// quotient of 8-bit divide m = n % p;// remainder of 8-bit divide i= (j + k) * (i-2); //arithmetic expression *, /, % are higher in precedence than +, -(higher precedence applied 1 st

Example: j * k + m / n = (j * k) + (m / n)

Floating-point formats are not directly supported by Cortex-M3 CPUs.

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Bit-parallel logical operators

Bit-parallel (bitwise) logical operators produce n-bit results of the corresponding logical operation: &(AND) |(OR) ^(XOR) ~(Complement)

C = A & B;A0 1 1 0 0 1 1 0

(AND)B1 0 1 1 0 0 1 1

C0 0 1 0 0 0 1 0

C = A | B;A0 1 1 0 0 1 0 0

(OR)B0 0 0 1 0 0 0 0

C0 1 1 1 0 1 0 0

C = A ^ B;A0 1 1 0 0 1 0 0

(XOR)B1 0 1 1 0 0 1 1

C1 1 0 1 0 1 1 1

B = ~A;A0 1 1 0 0 1 0 0

(COMPLEMENT)B1 0 0 1 1 0 1 1

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Bit set/reset/complement/test

•Use a "mask" to select bit(s) to be altered

0xFE1 1 1 1 1 1 1 0

Ca b c d e f g 0

C = A & 0x01;Aa b c d e f g h

0xFE00 0 0 0 0 0 1

C0 0 0 0 0 0 0 h

C = A | 0x01;Aa b c d e f g h

0x010 0 0 0 0 0 0 1

Ca b c d e f g 1

C = A ^ 0x01;Aa b c d e f g h

0x010

0 0 0 0 0 0 1

Ca b c d e f g h'

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Clear selected bit of A

Set selected bit of A

Complement selected bit of AClear all but the selected bit of A

Bit examples for input/output

•Create a "pulse" on bit 0 of PORTA (assume bit is initially 0)

PORTA = PORTA | 0x01; //Force bit 0 to 1

PORTA = PORTA & 0xFE; //Force bit 0 to 0

•Examples: if ( (PORTA & 0x80) != 0 ) //Or: ((PORTA & 0x80) == 0x80) bob();// call bob() if bit 7 of PORTA is 1 c = PORTB & 0x04;// mask all but bit 2of PORTB value if ((PORTA & 0x01) == 0) // test bit 0 of PORTA PORTA = c | 0x01;// write c to PORTA with bit 0 set to 1

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson) Example of µC register address definitions in STM32Lxx.h (read this header file to view other peripheral functions)

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson) #define PERIPH_BASE ((uint32_t)0x40000000) //Peripheral base address in memory #define AHBPERIPH_BASE (PERIPH_BASE + 0x20000) //AHB peripherals /* Base addresses of blocks of GPIO control/data registers */ #define GPIOA_BASE (AHBPERIPH_BASE + 0x0000) //Registers for GPIOA #define GPIOB_BASE (AHBPERIPH_BASE + 0x0400 )//Registers for GPIOB #define GPIOA ((GPIO_TypeDef*) GPIOA_BASE) //Pointer to GPIOA register block #define GPIOB ((GPIO_TypeDef*) GPIOB_BASE) //Pointer to GPIOB register block /* Address offsets from GPIO base address -block of registers defined as a "structure" */ typedefstruct __IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */

__IO uint16_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */

uint16_t RESERVED0; /*!< Reserved, 0x06 */

__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */

__IO uint32_t PUPDR; /*!< GPIO port pull -up/pull-down register, Address offset: 0x0C */

__IO uint16_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */

uint16_t RESERVED1; /*!< Reserved, 0x12 */

__IO uint16_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */

uint16_t RESERVED2; /*!< Reserved, 0x16 */

__IO uint16_t BSRRL; /*!< GPIO port bit set/reset low registerBSRR, Address offset: 0x18 */

__IO uint16_t BSRRH; /*!< GPIO port bit set/reset high registerBSRR, Address offset: 0x1A */

__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */

__IO uint32_t AFR[2]; /*!< GPIO alternate function low register, Address offset: 0x20-0x24 */

} GPIO_TypeDef;

Example: I/O port bits

(using bottom half of GPIOB)

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson) uint16_t sw;//16-bit unsigned type since GPIOB IDR and ODR = 16 bits sw= GPIOB->IDR;// sw= xxxxxxxxhgfedcba(upper 8 bits from PB15-PB8) sw= GPIOB->IDR & 0x0010;// sw= 000e0000 (mask all but bit 4) // Result is sw= 00000000 or 00010000 if ( sw== 0x01)// NEVER TRUEfor above sw, which is 000e0000 if ( sw== 0x10)// TRUE if e=1 (bit 4 in result of PORTB & 0x10) if ( sw== 0)// TRUE if e=0 in PORTB & 0x10 (sw=00000000) if ( sw!= 0)// TRUE if e=1 in PORTB & 0x10(sw=00010000) GPIOB->ODR = 0x005a;// Write to 16 bits of GPIOB; result is 01011010 GPIOB->ODR |= 0x10;// Sets only bit e to 1 in GPIOB (GPIOB now hgf1dcba) GPIOB->ODR &= ~0x10;// Resets only bit e to 0 in GPIOB (GPIOB now hgf0dcba) if ((GPIOB->IDR & 0x10) == 1) // TRUE if e=1 (bit 4 of GPIOB)

7 6 5 4 3 2 1 0

GPIOB

Switch connected to bit 4 (PB4) of GPIOBh g f e d c b a

Shift operators

Shift operators:

x >> y (right shift operand x by y bit positions) x << y (left shift operand x by y bit positions)

Vacated bits are filled with 0's.

Shift right/left fast way to multiply/divideby power of 2

B = A << 3;A1 0 1 0 1 1 0 1

(Left shift 3 bits)B0 1 1 0 1 0 0 0

B = A >> 2;A1 0 1 1 0 1 0 1

(Right shift 2 bits)B0 0 1 0 1 1 0 1

B = '1';B =0 0 1 1 0 0 0 1 (ASCII 0x31)

C = '5';C = 0 0 1 1 0 1 0 1(ASCII 0x35)

D = (B << 4) | (C & 0x0F);

(B << 4) = 0 0 0 10 0 0 0 (C & 0x0F)= 0 0 0 0 0 1 0 1

D = 0 0 0 1 0 1 0 1 (Packed BCD 0x15)

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

C control structures

•Control order in which instructions are executed (program flow) •Conditional execution -Execute a set of statements if some condition is met

-Select one set of statements to be executed from several options, depending on one or more conditions

•Iterative execution -Repeated execution of a set of statements •A specified number of times, or •Until some condition is met, or •While some condition is true

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

IF-THEN structure

•Execute a set of statements if and only if some condition is met if (a < b) statement s1; statement s2; a < b Yes

NoS1;S2;...

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

TRUE/FALSE condition

Relational Operators

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

TestTRUEconditionNotes

(m == b)mequal to bDouble = (m!= b)m not equal to b (m < b)m less than b1 (m <= b)m less than or equal to b1 (m > b)mgreater than b1 (m >= b)m greater than or equal to b1 (m)mnon-zero (1)always TRUE (0)always FALSE •Test relationship between two variables/expressions

1.Compiler uses

signed or unsigned comparison, in accordance with data types

Example:

Boolean operators

•Boolean operators &&(AND) and ||(OR) produce TRUE/FALSE results when testing multiple TRUE/FALSE conditions if ((n > 1) && (n < 5)) //test for n between 1 and 5 if ((c = 'q') || (c = 'Q')) //test c = lower or upper case Q •Note the difference between Booleanoperators &&, || and bitwise logical operators &, | if ( k && m) //test if k and m both TRUE (non-zero values) if ( k & m) //compute bitwise AND between m and n, thentest whether the result is non-zero (TRUE)

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)

Common error

•Note that ==is a relationaloperator, whereas =is an assignmentoperator. if ( m == n) //tests equality of values of variables m and n if (m = n) //assignsvalue of n to variable m, and then //tests whether that value is TRUE (non -zero) The second form is a common error (omitting the second equal sign), and usually produces unexpected results, namely a TRUE condition if n is 0 and

FALSE if n is non

-zero.

Fall 2014

-ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)quotesdbs_dbs17.pdfusesText_23