Docsity

appunti di c embedded, Dispense di Sensori e Trasduttori

Anteprima parziale del testo

Scarica appunti di c embedded e più Dispense in PDF di Sensori e Trasduttori solo su Docsity!

C programming for embedded

microcontroller systems.

Assumes experience with

assembly language programming.

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

STM32L100RC μC memory map

0xE00F FFFF 0xE000 0000

0x4002 67FF (^) Peripheral

0x4000 0000^ registers

0x2000 0000

0x0803 FFFF

16KB RAM

256KB Flash Memory

Cortex registers

Control/data registers: Cortex-M3 CPU functions (NVIC, SysTick Timer, etc.)

Control/data registers: microcontroller peripherals (timers, ADCs, UARTs, etc.)

256K byte Flash memory: program code & constant data storage

Reset & interrupt vectors: in 1st^ 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”

• Keil MDK-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 uC information */

// 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 PAval to 16-bits from GPIOA

for(;;) {} /* execute forever */

C compiler data types

Data type declaration * Number of bits Range of values char k; unsigned char k; uint8_t k;

signed char k; int8_t k;

short k; signed short k; int16_t k;

unsigned short k; uint16_t k;

int k; signed int k; int32_t k;

unsigned int k; uint32_t k;

  • intx_t and uintx_t defined in stdint.h

Data type examples

• Read bits from GPIOA (16 bits, non-numeric)

  • uint16_t n; n = GPIOA->IDR; //or: unsigned short n;

• Write TIM2 prescale value (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 int a;

• System control value range [-1000…+1000]

  • int32_t ctrl; ctrl = (x + y)*z; //or: int ctrl;

• Loop counter for 100 program loops (unsigned)

  • uint8_t cnt; //or: unsigned char cnt;
  • for (cnt = 0; cnt < 20; cnt++) {

Program variables

• A variable is an addressable storage location

to information to be used by the program

– Each variable must be declared to indicate size

and type of information to be stored, plus name

to be used to reference the information

int x,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 automatic or static

Variable arrays

• An array is 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]

int n[5]; //declare array of 5 “int” values

n[3] = 5; //set value of 4th^ array element

n[0]

n[1]

n[2]

n[3]

n[4]

Address: n

n+

n+

n+

Note: Index of first element is always 0.^ n+

Automatic variable example

void delay () {

int i,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

time 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

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 global in scope, i.e. known to all functions of the program - Use “normal” declarations. Example: int count;
  • If declared within a function, insert key word static before the variable definition. The variable is local in scope, i.e. known only within this function. static unsigned char bob; static int pressure[10];

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

Arithmetic operations

  • C examples – with standard arithmetic operators

int i, 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.

Bit set/reset/complement/test

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

C = A & 0xFE; A a b c d e f g h 0xFE 1 1 1 1 1 1 1 0 C a b c d e f g 0

C = A & 0x01; A a b c d e f g h 0xFE 0 0 0 0 0 0 0 1 C 0 0 0 0 0 0 0 h

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

C = A ^ 0x01; A a b c d e f g h 0x01 0 0 0 0 0 0 0 1 C a b c d e f g h’

Clear selected bit of A

Set selected bit of A

Complement selected bit of A

Clear 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 2 of PORTB value if ((PORTA & 0x01) == 0) // test bit 0 of PORTA PORTA = c | 0x01; // write c to PORTA with bit 0 set to 1