? ǰ��

Ƕ��ʽ����ʦ���Բ�����������������ע��Ӳ�����⡢ϵͳ˼ά��ʵ����Ŀ���顣����������Ƕ��ʽ�����еĸ�Ƶ����ʹ��⼼�ɣ�����������������ӱ������

? ����׼���嵥

����֪ʶ׼��

1
2
3
4
5
6
7
8
? C���Ի����͸߼�����
? ���ݽṹ���㷨
? ��Ƭ��ԭ����Ӧ��
? Ӳ����·����
? ͨ��Э�飨UART��SPI��I2C�ȣ�
? ʵʱ����ϵͳ��RTOS��
? ���Թ��ߺͷ���
? ��Ŀ�����ܽ�

�����Ż�Ҫ��

1
2
3
4
? ͻ����Ŀ����ͼ���ջ
? ������Ŀ�ɹ��͹���
? չʾ������������
? ���ֳ���ѧϰ��̬��

? C���������⾫ѡ

�����﷨��

1. ָ������������

1
2
3
4
5
6
7
8
9
10
// ���Թٳ��ʣ����´��������
char *p = "hello"; // ָ�룬ָ���ַ�������
char arr[] = "hello"; // ���飬��ջ�Ϸ���ռ�

// �ؼ�����
// 1. �ڴ����λ�ò�ͬ
// 2. �Ƿ�����޸�����
// 3. sizeof�����ͬ
printf("sizeof(p) = %zu\n", sizeof(p)); // ָ���С��4��8�ֽڣ�
printf("sizeof(arr) = %zu\n", sizeof(arr)); // �����С��6�ֽڣ�

2. const�ؼ��ֵ��÷�

1
2
3
4
5
6
7
8
9
10
// ������const�÷�
const int a = 10; // ����
int const b = 20; // ͬ��
const int *p1; // ָ������ָ��
int * const p2 = &a; // ����ָ��
const int * const p3 = &a; // ָ�����ij���ָ��

// ���似�ɣ����������
// const int *p��p��ָ�룬ָ��const int
// int * const p��p��constָ�룬ָ��int

3. volatile�ؼ���

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Ƕ��ʽ��volatile����Ҫ��
volatile uint32_t *GPIO_REG = (uint32_t*)0x40020000;

// Ϊʲô��Ҫvolatile��
// 1. ��ֹ�������Ż�
// 2. ȷ��ÿ�ζ����ڴ��ȡ
// 3. Ӳ���Ĵ������ʱ���ʹ��

// ����ʾ����û��volatile��
uint32_t *reg = (uint32_t*)0x40020000;
while(*reg & 0x01); // ���ܱ��Ż�Ϊ��ѭ��

// ��ȷʾ����ʹ��volatile��
volatile uint32_t *reg = (volatile uint32_t*)0x40020000;
while(*reg & 0x01); // ÿ�ζ����ȡ�Ĵ���

�ڴ������

4. ջ�Ͷѵ�����

���� ջ(Stack) ��(Heap)
�����ٶ� �� ��
�ڴ��С ���ޣ�ͨ����KB����MB�� �ϴ�
������ʽ �Զ����� �ֶ�����
�ڴ���Ƭ �� ���ܲ���
�����ٶ� �� �����
�������� ���������Զ��ͷ� ��Ҫ�ֶ��ͷ�
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// ջ����ʾ��
void stack_example() {
int arr[100]; // ��ջ�Ϸ���
// ��������ʱ�Զ��ͷ�
}

// �ѷ���ʾ��
void heap_example() {
int *p = malloc(100 * sizeof(int)); // �ڶ��Ϸ���
if (p != NULL) {
// ʹ���ڴ�
free(p); // �����ֶ��ͷ�
p = NULL; // ��ֹҰָ��
}
}

5. �ڴ�й©���

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// �򵥵��ڴ�й©������
static int malloc_count = 0;
static int free_count = 0;

void* debug_malloc(size_t size) {
void *ptr = malloc(size);
if (ptr) {
malloc_count++;
printf("Malloc: %p, count: %d\n", ptr, malloc_count);
}
return ptr;
}

void debug_free(void *ptr) {
if (ptr) {
free(ptr);
free_count++;
printf("Free: %p, count: %d\n", ptr, free_count);
}
}

// ����ڴ�й©
void check_memory_leak() {
printf("Malloc count: %d, Free count: %d\n", malloc_count, free_count);
if (malloc_count != free_count) {
printf("Memory leak detected!\n");
}
}

�����

6. �����������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Ƕ��ʽ�����е�λ��������
#define SET_BIT(reg, bit) ((reg) |= (1U << (bit)))
#define CLEAR_BIT(reg, bit) ((reg) &= ~(1U << (bit)))
#define TOGGLE_BIT(reg, bit) ((reg) ^= (1U << (bit)))
#define READ_BIT(reg, bit) (((reg) >> (bit)) & 1U)

// ���Գ�����������ʱ��������������
void swap_without_temp(int *a, int *b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}

// �ж�һ�����Ƿ�Ϊ2����
bool is_power_of_2(unsigned int n) {
return (n != 0) && ((n & (n - 1)) == 0);
}

// ����һ������1�ĸ���
int count_bits(unsigned int n) {
int count = 0;
while (n) {
count++;
n &= (n - 1); // �������1
}
return count;
}

? Ӳ��֪ʶ������

��Ƭ������

7. ��Ƭ����������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1. �ϵ縴λ
������ Ӳ����λ��·����
������ ���������
������ PCָ��ָ��λ��ַ

2. ʱ�ӳ�ʼ��
������ ����ϵͳʱ��Դ
������ ���÷�Ƶϵ��
������ ����PLL�����ʹ�ã�

3. �ڴ��ʼ��
������ ��ʼ��RAM
������ ���Ƴ�ʼ������
������ ����BSS��

4. �����ʼ��
������ ����GPIO
������ ��ʼ����ʱ��
������ �����������

5. ��ת��main����

8. �жϴ�������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// �жϴ����Ĺؼ�����
void interrupt_concepts() {
/*
�ж����ȼ���
- �����ȼ��жϿ��Դ�ϵ����ȼ��ж�
- ��ͬ���ȼ������жϺ�����

�ж�Ƕ�ף�
- ���������ȼ��ж�Ƕ��
- ��Ҫ������Ʊ���ջ���

�ж��ӳ٣�
- �ж���Ӧʱ��
- �жϴ���ʱ��
- �жϻָ�ʱ��
*/
}

// �жϷ���������ԭ��
void UART_IRQHandler(void) {
// 1. ����ȷ���ж�Դ
if (UART->SR & UART_SR_RXNE) {
// 2. ���ٴ��������ⳤʱ��ռ��
uint8_t data = UART->DR;
rx_buffer[rx_index++] = data;

// 3. ���Ӵ����ŵ���ѭ��
rx_flag = 1;
}

// 4. ����жϱ�־
UART->SR &= ~UART_SR_RXNE;
}

ͨ��Э��

9. UART��SPI��I2C�Ա�

���� UART SPI I2C
���� 2�ߣ�TX/RX�� 4�ߣ�MOSI/MISO/SCK/CS�� 2�ߣ�SDA/SCL��
ͨ�ŷ�ʽ �첽 ͬ�� ͬ��
�ٶ� �е� �� ��
�豸���� ��Ե� һ����� �������
Ӳ�����Ӷ� �� �е� ��
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// UART����ʾ��
void uart_init(uint32_t baudrate) {
// 1. ʹ��ʱ��
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;

// 2. ����GPIO
// PA9: TX, PA10: RX

// 3. ������
USART1->BRR = SystemCoreClock / baudrate;

// 4. ʹ�ܷ��ͺͽ���
USART1->CR1 |= USART_CR1_TE | USART_CR1_RE;

// 5. ʹ��UART
USART1->CR1 |= USART_CR1_UE;
}

// SPI����ʾ��
void spi_init(void) {
// 1. ʹ��ʱ��
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;

// 2. ����Ϊ����ģʽ
SPI1->CR1 |= SPI_CR1_MSTR;

// 3. ����ʱ�ӷ�Ƶ
SPI1->CR1 |= SPI_CR1_BR_1; // ��Ƶ4

// 4. ʹ��SPI
SPI1->CR1 |= SPI_CR1_SPE;
}

��·����

10. �����������������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Ϊʲô��Ҫ����/�������裿
/*
1. ȷ��Ĭ�ϵ�ƽ״̬
2. ��ֹ��������
3. �ṩ��������
4. ���͹���

����Ӧ�ã�
- �������루�������裩
- I2C���ߣ��������裩
- ��λ��·���������裩
*/

// GPIO����ʾ��
void gpio_config_example(void) {
// ����Ϊ���룬�ڲ�����
GPIOA->MODER &= ~(3U << (0 * 2)); // ����ģʽ
GPIOA->PUPDR |= (1U << (0 * 2)); // ����

// ����������������
GPIOA->MODER |= (1U << (1 * 2)); // ���ģʽ
GPIOA->OTYPER &= ~(1U << 1); // �������
}

? ��Ŀ��������

��Ŀ����ģ��

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
��Ŀ������
- ��ĿĿ�������
- ������ս���ѵ�
- �Ŷӹ�ģ�ͽ�ɫ

����������
- Ӳ��ƽ̨ѡ��
- �����ܹ����
- �ؼ�������

���˹��ף�
- ������
- ���������
- ���µ���Ż�

��Ŀ�ɹ���
- ����ָ��
- ��ҵ��ֵ
- �����ܽ�

������Ŀ����

11. ����Ż�ϵͳ���ģ�

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ����������
void power_optimization_strategies() {
/*
Ӳ�����棺
1. ѡ��͹���MCU
2. �Ż���·���
3. ʹ�õ͹�������

�������棺
1. ʹ��˯��ģʽ
2. ��̬��Ƶ��ѹ
3. �رղ��õ�����
4. �Ż��㷨���ټ���
*/
}

// ˯��ģʽʾ��
void enter_sleep_mode(void) {
// �رղ���Ҫ������
RCC->APB1ENR &= ~RCC_APB1ENR_TIM2EN;

// ���û���Դ
EXTI->IMR |= EXTI_IMR_MR0; // ʹ���ⲿ�ж�0

// ����˯��ģʽ
__WFI(); // Wait For Interrupt
}

12. ��δ���ʵʱ��Ҫ��

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// ʵʱ�Ա�֤��ʩ
void real_time_strategies() {
/*
1. �ж����ȼ����
- �����ȼ���ʵʱ����
- ���������ж����ȼ�

2. ���������
- ʹ��RTOS
- ���ú��ʵ��������ȼ�
- �������ȼ���ת

3. ������
- �����жϴ���ʱ��
- ʹ��DMA����CPU����
- �Ż��ؼ�·������
*/
}

// ʵʱ����ʾ��
void real_time_task_example(void) {
// �����ȼ��жϴ���
void TIM1_IRQHandler(void) {
// ���ٴ��������ñ�־
real_time_flag = 1;
TIM1->SR &= ~TIM_SR_UIF;
}

// ��ѭ���д���
while (1) {
if (real_time_flag) {
real_time_flag = 0;
// ִ��ʵʱ����
process_real_time_data();
}

// ������ʵʱ����
process_background_tasks();
}
}

? �㷨�����ݽṹ

Ƕ��ʽ�����㷨

13. ���λ�����ʵ��

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// ���λ����� - Ƕ��ʽ�еľ������ݽṹ
typedef struct {
uint8_t *buffer;
uint32_t size;
uint32_t head;
uint32_t tail;
uint32_t count;
} ring_buffer_t;

// ��ʼ��
void ring_buffer_init(ring_buffer_t *rb, uint8_t *buffer, uint32_t size) {
rb->buffer = buffer;
rb->size = size;
rb->head = 0;
rb->tail = 0;
rb->count = 0;
}

// �����
bool ring_buffer_put(ring_buffer_t *rb, uint8_t data) {
if (rb->count >= rb->size) {
return false; // ��������
}

rb->buffer[rb->head] = data;
rb->head = (rb->head + 1) % rb->size;
rb->count++;

return true;
}

// ��ȡ����
bool ring_buffer_get(ring_buffer_t *rb, uint8_t *data) {
if (rb->count == 0) {
return false; // ��������
}

*data = rb->buffer[rb->tail];
rb->tail = (rb->tail + 1) % rb->size;
rb->count--;

return true;
}

14. ״̬��ʵ��

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// ״̬�� - Ƕ��ʽ�����߼�������
typedef enum {
STATE_IDLE,
STATE_RUNNING,
STATE_PAUSED,
STATE_ERROR
} system_state_t;

typedef enum {
EVENT_START,
EVENT_STOP,
EVENT_PAUSE,
EVENT_RESUME,
EVENT_ERROR
} system_event_t;

// ״̬ת����
typedef struct {
system_state_t current_state;
system_event_t event;
system_state_t next_state;
void (*action)(void);
} state_transition_t;

// ״̬ת��������
static const state_transition_t state_table[] = {
{STATE_IDLE, EVENT_START, STATE_RUNNING, start_action},
{STATE_RUNNING, EVENT_STOP, STATE_IDLE, stop_action},
{STATE_RUNNING, EVENT_PAUSE, STATE_PAUSED, pause_action},
{STATE_PAUSED, EVENT_RESUME, STATE_RUNNING, resume_action},
// ... ����״̬ת��
};

// ״̬����������
void state_machine_process(system_event_t event) {
static system_state_t current_state = STATE_IDLE;

for (int i = 0; i < sizeof(state_table)/sizeof(state_table[0]); i++) {
if (state_table[i].current_state == current_state &&
state_table[i].event == event) {

// ִ�ж���
if (state_table[i].action) {
state_table[i].action();
}

// ״̬ת��
current_state = state_table[i].next_state;
break;
}
}
}

? ���Լ��ɺ�ע������

�������Բ���

�ش������STAR������

1
2
3
4
Situation���������������Ŀ����
Task�����񣩣�˵�����ְ��
Action���ж�����������ľ����ж�
Result���������չʾ���ճɹ�

��������ⲽ����

1
2
3
4
5
1. �������⣺ȷ�����������Լ������
2. ����˼·��˵������˼·��ʱ�临�Ӷ�
3. ��д���룺ע��߽������ʹ�����
4. ������֤����ʾ��������֤��ȷ��
5. �Ż��Ľ������ۿ��ܵ��Ż�����

������������

15. ָ��������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ����1�����ؾֲ�������ַ
char* get_string() {
char str[] = "hello"; // �ֲ�����
return str; // Σ�գ�����ջ��ַ
}

// ��ȷ����
char* get_string_correct() {
static char str[] = "hello"; // ��̬����
return str; // ��ȫ
}

// ����2����������ָ�������
void array_pointer_trap() {
char arr1[] = "hello";
char *arr2 = "hello";

printf("sizeof(arr1) = %zu\n", sizeof(arr1)); // 6
printf("sizeof(arr2) = %zu\n", sizeof(arr2)); // 4��8

// arr1[0] = 'H'; // �����޸�
// arr2[0] = 'H'; // ����ʱ����
}

16. �궨������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// ���壺�궨��û�м�����
#define SQUARE(x) x * x

int result = SQUARE(3 + 2); // ����25��ʵ��13
// չ��Ϊ��3 + 2 * 3 + 2 = 11

// ��ȷ����
#define SQUARE(x) ((x) * (x))

// ���壺������
#define DEBUG_PRINT(x) printf("Debug: "); printf(x)

if (condition)
DEBUG_PRINT("error"); // ֻ�е�һ��printf��if��

// ��ȷ����
#define DEBUG_PRINT(x) do { \
printf("Debug: "); \
printf(x); \
} while(0)

н��̸�м���

н��̸�в�����

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1. �˽��г�����
- �鿴��Ƹ��վн�ʷ�Χ
- ��ѯͬ������
- �ο���ҵ����

2. ����������ֵ
- ��������ˮƽ
- ��Ŀ����ḻ��
- �����������

3. ̸�м���
- ��Ҫ��һ������
- ����������Χ
- ��������package
- ����רҵ̬��

? ���Ը�ϰ��Դ

�Ƽ��鼮

  • ��C��ָ�롷- Kenneth A.Reek
  • ��Ƕ��ʽC���Գ�����ơ�- Mark Siegesmund
  • ��ARM Cortex-M3Ȩ��ָ�ϡ�- Joseph Yiu
  • ������Ա���Խ�䡷- Gayle McDowell

������Դ

ʵ����Ŀ����

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
���ż���Ŀ��
- LED��ˮ�ƿ���
- �������ʾ
- ������������
- UARTͨ��

������Ŀ��
- �¶ȼ��ϵͳ
- �����������
- �򵥵�RTOSӦ��
- ����ͨ��ģ��

�߼���Ŀ��
- �����IJ�Ʒԭ��
- �ഫ���������ں�
- ����ͨ��Ӧ��
- ͼ����Ӧ��

? �ܽ�

Ƕ��ʽ����ʦ���Գɹ��Ĺؼ����ڣ�

��������

  • ��ʵ��C���Ի�����ָ�롢�ڴ������λ����
  • Ӳ��������������Ƭ��ԭ������·������ͨ��Э��
  • ϵͳ˼ά����Ӳ����������ȫջ����
  • �������������ٶ�λ�ͽ������

��Ŀ����

  • ��������Ŀ������������ʵ�ֵ�ȫ����
  • ����������������������η����ͽ��
  • ��������������Ŀ�еļ�������ʹ���
  • �Ŷ�Э�������Ŷӳ�Ա����Ϻ͹�ͨ

���Լ���

  • ���׼��������֪ʶ����Ŀ���顢��������
  • �����������߼�������ص�ͻ��
  • ��ʵ�ش�������������ʵ˵����չʾѧϰ����
  • ����̬����չ�ֶԼ���������ͳ���ѧϰ����Ը

����ѧϰ

  • ���ϼ���������IoT��AI����Ե������¼���
  • ����רҵ������ѡ��һ�����������о�
  • ʵ����Ŀ��ͨ����Ŀ���̺���������
  • ����������ͨ�����͡���̳��������

��ס�����Բ����ǹ�˾ѡ���㣬Ҳ����ѡ��˾�Ĺ��̡��������ţ�չ����ʵ�ļ���ʵ����ѧϰ������������һ�����ҵ�����Ĺ�����


? ���Խ��: “��������ѧϰ������������˼ά�ͳ���ѧϰ������������Ҫ��”

��������Ƽ�: