際際滷

際際滷Share a Scribd company logo
ANSI C
Language
Introduction
Lection 8
弌仂亟亠亢舒仆亳亠
 仆仂于仆亠 亟亳亠从亳于
 亳亠从亳于: 仗亳仄亠
 舒从仂从仂仄舒仆亟舒
 MIN, MAX macros
 SUM, MULT macros
 Bit Modifications macros
23.05.2019 0:20 Introduction 2
仆仂于仆亠 亟亳亠从亳于
3
亳亠从亳于舒 仗亳舒仆亳亠
#include 于舒于仍磳 亠从 亳亰 从舒亰舒仆仆仂亞仂 舒亶仍舒
#define 亰舒亟舒 仄舒从仂仂仗亠亟亠仍亠仆亳亠 (仄舒从仂) 亳仍亳 亳仄于仂仍亳亠从 从仂仆舒仆
#undef 仂仄亠仆磳 仗亠亟亟亠亠 仂仗亠亟亠仍亠仆亳亠
#line 仗亠仗仂亠仂 亳亰仄亠仆磳 仆仂仄亠 亠从亠亶 仂从亳 亳 亳仄
从仂仄仗亳仍亳亠仄仂亞仂 舒亶仍舒
#error 于亟舒舒 亟亳舒亞仆仂亳亠从仂亞仂 仂仂弍亠仆亳 亳 仂舒仆仂于从舒 从仂仄仗亳仍亳亳
#pragma 亟亠亶于亳亠, 亰舒于亳亠亠 仂 从仂仆从亠仆仂亶 亠舒仍亳亰舒亳亳 从仂仄仗亳仍仂舒
仆仂于仆亠 亟亳亠从亳于
4
亳亠从亳于舒 仗亳舒仆亳亠
#if 仂亠于仍磳 仍仂于仆 从仂仄仗亳仍亳 仗亳
亳亳仆仆仂亳 从仂仆舒仆仆仂亞仂 于舒亢亠仆亳
#ifdef 仂亠于仍磳 仍仂于仆 从仂仄仗亳仍亳 仗亳
仂仗亠亟亠仍仆仆仂亳 亳仄于仂仍亳亠从仂亶 从仂仆舒仆
#ifndef 仂亠于仍磳 仍仂于仆 从仂仄仗亳仍亳 仗亳
仆亠仂仗亠亟亠仍仆仆仂亳 亳仄于仂仍亳亠从仂亶 从仂仆舒仆
#else 于亠从舒 仍仂于仆仂亶 从仂仄仗亳仍亳亳 仗亳 仍仂亢仆仂亳 于舒亢亠仆亳
#elif 于亠从舒 仍仂于仆仂亶 从仂仄仗亳仍亳亳, 仂弍舒亰亠仄舒 仍亳礌亳亠仄 else 亳 if
#endif 从仂仆亠 于亠从亳 仍仂于仆仂亶 从仂仄仗亳仍亳亳
亳亠从亳于: 仗亳仄亠 #1
5
#include "main.h
#define BUFFER_SIZE_MAX 1024
#line 500
int main(void)
{
unsigned int i;
while (1)
{
// Insert some code here...
}
return 0;
}
亳亠从亳于: 仗亳仄亠 #2
6
#define LEDS_ON
#ifdef LEDS_ON
HAL_GPIO_WritePin(
GPIOD,
GPIO_PIN_15|GPIO_PIN_14,
GPIO_PIN_SET);
#else
HAL_GPIO_WritePin(
GPIOD,
GPIO_PIN_15|GPIO_PIN_14,
GPIO_PIN_RESET);
#endif
舒从仂从仂仄舒仆亟舒
舒从仂从仂仄舒仆亟舒 亳仍亳 仄舒从仂  仂 亳仄于仂仍仆仂亠
亳仄 于 舒弍仍仂仆舒, 亰舒仄亠仆磳仄仂亠 仗亳 仂弍舒弍仂从亠
仗亠仗仂亠仂仂仄 仆舒 仗仂仍亠亟仂于舒亠仍仆仂
亳仄于仂仍仂于.
丼舒仂 仄舒从仂 仗亳仄亠仆ム 亟仍 于仗仂仍仆亠仆亳
亳仆仆 亟亠亶于亳亶
7
MIN, MAX macros
8
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
unsigned char a,b;
a = MIN(5, 10);
b = MAX(a, 20);
SUM macro
9
#define SUM(a, b) a + b
unsigned char a,b;
a = SUM(2, 2);
b = 3 * SUM(2, 2);
SUM modified macro
10
#define SUM(a, b) (a + b)
unsigned char a,b;
a = SUM(2, 2);
b = 3 * SUM(2, 2);
MULT macro
11
#define MULT(a, b) (a * b)
unsigned char a,b;
a = MULT(2, 2);
b = MULT(2, 2 + 3);
MULT macro modified
12
#define MULT(a, b) ((a) * (b))
unsigned char a,b;
a = MULT(2, 2);
b = MULT(2, 2 + 3);
SQR macro
13
#define SQR(x) ((x) * (x))
unsigned char a,b;
b = 4;
a = SQR(b++);
Bit Modifications macros
14
#define SET_BIT(p,n) ((p) |= (1 << (n)))
#define CLEAR_BIT(p,n) ((p) &= ~(1 << (n)))
#define TOGGLE_BIT(p,n) ((p) ^= (1 << (n)))
SET_BIT macro
15
#define SET_BIT(p,n) ((p) |= (1 << (n)))
a = 0x03;
SET_BIT(a, 7);
舒亰于仂舒亳于舒亠仄 仗仂舒亞仂于仂
a |= (1 << 7)
a |= (0b10000000)
a = 0b00000011 |
0b10000000
------------
a = 0b10000011
 亳仂亞亠 于仂亟亳
a = 0x83
CLEAR_BIT macro
16
#define CLEAR_BIT(p,n) ((p) &= ~(1 << (n)))
a = 0xFF;
CLEAR_BIT(a, 3);
舒亰于仂舒亳于舒亠仄 仗仂舒亞仂于仂
a &= ~(1 << 3)
a &= ~(0b00001000)
a &= (0b11110111)
a = 0b11111111 &
0b11110111
------------
a = 0b11110111
 亳仂亞亠 于仂亟亳
a = 0xF7
TOGGLE_BIT macro
17
#define TOGGLE_BIT(p,n) ((p) ^= (1 << (n)))
a = 0x55;
TOGGLE_BIT(a, 6);
舒亰于仂舒亳于舒亠仄 仗仂舒亞仂于仂
a ^= (1 << 6)
a ^= 0b01000000
a = 0b01010101 ^
0b01000000
------------
a = 0b00010101
 亳仂亞亠 于仂亟亳
a = 0x15
REVERSE_BITS macro
18
#define REVERSE_BITS(a) 
( (((a) & 0x80) >> 7) | 
(((a) & 0x40) >> 5) | 
(((a) & 0x20) >> 3) | 
(((a) & 0x10) >> 1) | 
(((a) & 0x08) << 1) | 
(((a) & 0x04) << 3) | 
(((a) & 0x02) << 5) | 
(((a) & 0x01) << 7) )
0x 8 F -> 0x F 1
0b10001111 -> 0b11110001

More Related Content

C language lect_08_introduction

  • 2. 弌仂亟亠亢舒仆亳亠 仆仂于仆亠 亟亳亠从亳于 亳亠从亳于: 仗亳仄亠 舒从仂从仂仄舒仆亟舒 MIN, MAX macros SUM, MULT macros Bit Modifications macros 23.05.2019 0:20 Introduction 2
  • 3. 仆仂于仆亠 亟亳亠从亳于 3 亳亠从亳于舒 仗亳舒仆亳亠 #include 于舒于仍磳 亠从 亳亰 从舒亰舒仆仆仂亞仂 舒亶仍舒 #define 亰舒亟舒 仄舒从仂仂仗亠亟亠仍亠仆亳亠 (仄舒从仂) 亳仍亳 亳仄于仂仍亳亠从 从仂仆舒仆 #undef 仂仄亠仆磳 仗亠亟亟亠亠 仂仗亠亟亠仍亠仆亳亠 #line 仗亠仗仂亠仂 亳亰仄亠仆磳 仆仂仄亠 亠从亠亶 仂从亳 亳 亳仄 从仂仄仗亳仍亳亠仄仂亞仂 舒亶仍舒 #error 于亟舒舒 亟亳舒亞仆仂亳亠从仂亞仂 仂仂弍亠仆亳 亳 仂舒仆仂于从舒 从仂仄仗亳仍亳亳 #pragma 亟亠亶于亳亠, 亰舒于亳亠亠 仂 从仂仆从亠仆仂亶 亠舒仍亳亰舒亳亳 从仂仄仗亳仍仂舒
  • 4. 仆仂于仆亠 亟亳亠从亳于 4 亳亠从亳于舒 仗亳舒仆亳亠 #if 仂亠于仍磳 仍仂于仆 从仂仄仗亳仍亳 仗亳 亳亳仆仆仂亳 从仂仆舒仆仆仂亞仂 于舒亢亠仆亳 #ifdef 仂亠于仍磳 仍仂于仆 从仂仄仗亳仍亳 仗亳 仂仗亠亟亠仍仆仆仂亳 亳仄于仂仍亳亠从仂亶 从仂仆舒仆 #ifndef 仂亠于仍磳 仍仂于仆 从仂仄仗亳仍亳 仗亳 仆亠仂仗亠亟亠仍仆仆仂亳 亳仄于仂仍亳亠从仂亶 从仂仆舒仆 #else 于亠从舒 仍仂于仆仂亶 从仂仄仗亳仍亳亳 仗亳 仍仂亢仆仂亳 于舒亢亠仆亳 #elif 于亠从舒 仍仂于仆仂亶 从仂仄仗亳仍亳亳, 仂弍舒亰亠仄舒 仍亳礌亳亠仄 else 亳 if #endif 从仂仆亠 于亠从亳 仍仂于仆仂亶 从仂仄仗亳仍亳亳
  • 5. 亳亠从亳于: 仗亳仄亠 #1 5 #include "main.h #define BUFFER_SIZE_MAX 1024 #line 500 int main(void) { unsigned int i; while (1) { // Insert some code here... } return 0; }
  • 6. 亳亠从亳于: 仗亳仄亠 #2 6 #define LEDS_ON #ifdef LEDS_ON HAL_GPIO_WritePin( GPIOD, GPIO_PIN_15|GPIO_PIN_14, GPIO_PIN_SET); #else HAL_GPIO_WritePin( GPIOD, GPIO_PIN_15|GPIO_PIN_14, GPIO_PIN_RESET); #endif
  • 7. 舒从仂从仂仄舒仆亟舒 舒从仂从仂仄舒仆亟舒 亳仍亳 仄舒从仂 仂 亳仄于仂仍仆仂亠 亳仄 于 舒弍仍仂仆舒, 亰舒仄亠仆磳仄仂亠 仗亳 仂弍舒弍仂从亠 仗亠仗仂亠仂仂仄 仆舒 仗仂仍亠亟仂于舒亠仍仆仂 亳仄于仂仍仂于. 丼舒仂 仄舒从仂 仗亳仄亠仆ム 亟仍 于仗仂仍仆亠仆亳 亳仆仆 亟亠亶于亳亶 7
  • 8. MIN, MAX macros 8 #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) unsigned char a,b; a = MIN(5, 10); b = MAX(a, 20);
  • 9. SUM macro 9 #define SUM(a, b) a + b unsigned char a,b; a = SUM(2, 2); b = 3 * SUM(2, 2);
  • 10. SUM modified macro 10 #define SUM(a, b) (a + b) unsigned char a,b; a = SUM(2, 2); b = 3 * SUM(2, 2);
  • 11. MULT macro 11 #define MULT(a, b) (a * b) unsigned char a,b; a = MULT(2, 2); b = MULT(2, 2 + 3);
  • 12. MULT macro modified 12 #define MULT(a, b) ((a) * (b)) unsigned char a,b; a = MULT(2, 2); b = MULT(2, 2 + 3);
  • 13. SQR macro 13 #define SQR(x) ((x) * (x)) unsigned char a,b; b = 4; a = SQR(b++);
  • 14. Bit Modifications macros 14 #define SET_BIT(p,n) ((p) |= (1 << (n))) #define CLEAR_BIT(p,n) ((p) &= ~(1 << (n))) #define TOGGLE_BIT(p,n) ((p) ^= (1 << (n)))
  • 15. SET_BIT macro 15 #define SET_BIT(p,n) ((p) |= (1 << (n))) a = 0x03; SET_BIT(a, 7); 舒亰于仂舒亳于舒亠仄 仗仂舒亞仂于仂 a |= (1 << 7) a |= (0b10000000) a = 0b00000011 | 0b10000000 ------------ a = 0b10000011 亳仂亞亠 于仂亟亳 a = 0x83
  • 16. CLEAR_BIT macro 16 #define CLEAR_BIT(p,n) ((p) &= ~(1 << (n))) a = 0xFF; CLEAR_BIT(a, 3); 舒亰于仂舒亳于舒亠仄 仗仂舒亞仂于仂 a &= ~(1 << 3) a &= ~(0b00001000) a &= (0b11110111) a = 0b11111111 & 0b11110111 ------------ a = 0b11110111 亳仂亞亠 于仂亟亳 a = 0xF7
  • 17. TOGGLE_BIT macro 17 #define TOGGLE_BIT(p,n) ((p) ^= (1 << (n))) a = 0x55; TOGGLE_BIT(a, 6); 舒亰于仂舒亳于舒亠仄 仗仂舒亞仂于仂 a ^= (1 << 6) a ^= 0b01000000 a = 0b01010101 ^ 0b01000000 ------------ a = 0b00010101 亳仂亞亠 于仂亟亳 a = 0x15
  • 18. REVERSE_BITS macro 18 #define REVERSE_BITS(a) ( (((a) & 0x80) >> 7) | (((a) & 0x40) >> 5) | (((a) & 0x20) >> 3) | (((a) & 0x10) >> 1) | (((a) & 0x08) << 1) | (((a) & 0x04) << 3) | (((a) & 0x02) << 5) | (((a) & 0x01) << 7) ) 0x 8 F -> 0x F 1 0b10001111 -> 0b11110001