资源预览内容
第1页 / 共52页
第2页 / 共52页
第3页 / 共52页
第4页 / 共52页
第5页 / 共52页
第6页 / 共52页
第7页 / 共52页
第8页 / 共52页
第9页 / 共52页
第10页 / 共52页
亲,该文档总共52页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
C programming for embedded microcontroller systems.Assumes experience with assembly language programming.V. P. NelsonFall 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 routinesFall 2014 - ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)Basic C program structureFall 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 */ int count, bob; /global (static) variables placed in RAM/* Function definitions*/ int function1(char x) /parameter x passed to the function, function returns an integer value int i,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) int k; /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 variablesInitialize variables/devicesBody of the programSTM32L100RC C memory mapFall 2014 - ARM VersionELEC 3040/3050 Embedded Systems Lab (V. P. Nelson)0xE00F FFFF0xE000 00000x4002 67FFPeripheral registers0x4000 00000x2000 00000x0803 FFFF16KB RAM256KB Flash MemoryCortex registersControl/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 /16-bit unsigned variable GPIOA-MODER / Set GPIOA pin PA0 as input PAval = GPIOA-IDR;/ Set PAval to 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 typesData type declaration *Number of bits Range of values char k; unsigned char k; uint8_t k;80255signed char k; int8_t k;8-128+127short k; signed short k; int16_t k;16-32768+32767unsigned short k; uint16_t k;16065535int k; signed int k; int32_t k;32-2147483648 +2147483647unsigned int k; uint32_t k;3204294967295* intx_t and uintx_t defined in stdint.hFall 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 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 IDR;/ sw = xxxxxxxxhgfedcba (upper 8 bits from PB15-PB8) sw = GPIOB-IDR / sw = 000e0000 (mask all but bit 4) / Result is sw = 00000000 or 00010000 if (sw = 0x01)/ NEVER TRUE for above sw, which is 000e0000 if (sw = 0x10)/ TRUE if e=1 (bit 4 in result of PORTB / 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 / Resets only bit e to 0 in GPIOB (GPIOB now hgf0dcba) if (GPIOB-IDR A1 0 1 1 0 1 0 1 (Right shift 2 bits)B0 0 1 0 1 1 0 1B = 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 b)m greater than b1(m = b)m greater than or equal to b1(m)m non-zero(1)always TRUE(0)always FALSE Test relationship between two variables/expressions1. Compiler uses signed or unsigned comparison, in accordance with data typesExample: unsigned char a,b; int j,k; if (a k) - signedBoolean operators Boolean operators THEN Turn the water valve off ldaa VALVE_OFF staa VALVE_PORT bra END_IF ; ELSE Turn the water valve on ELSE_PART: ldaa VALVE_ON staa VALVE_PORT END_IF: ; END IF temperature Allowed MaximumC version:#define MAX_TEMP 128 #define VALVE_OFF 0 #define VALVE_ON 1if (AD_PORT 0) if (a b) z = a;
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号