嵌入式c语言程序设计

上传人:第*** 文档编号:55663194 上传时间:2018-10-03 格式:PDF 页数:52 大小:263.66KB
返回 下载 相关 举报
嵌入式c语言程序设计_第1页
第1页 / 共52页
嵌入式c语言程序设计_第2页
第2页 / 共52页
嵌入式c语言程序设计_第3页
第3页 / 共52页
嵌入式c语言程序设计_第4页
第4页 / 共52页
嵌入式c语言程序设计_第5页
第5页 / 共52页
点击查看更多>>
资源描述

《嵌入式c语言程序设计》由会员分享,可在线阅读,更多相关《嵌入式c语言程序设计(52页珍藏版)》请在金锄头文库上搜索。

1、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 regis

2、ter/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

3、“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 i

4、nteger 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

5、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 - AR

6、M 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: microcontrolle

7、r 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

8、 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 f

9、ile “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 *Nu

10、mber 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 uin

11、tx_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

12、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 (up

13、per 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;/ Set

14、s 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

15、 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;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号