? ǰ��
Ƕ��ʽ����ʦ���Բ�����������������ע��Ӳ�����⡢ϵͳ˼ά��ʵ����Ŀ���顣����������Ƕ��ʽ�����еĸ�Ƶ����ʹ��⼼�ɣ�����������������ӱ������
? �������嵥
����֪ʶ��
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";
printf("sizeof(p) = %zu\n", sizeof(p)); printf("sizeof(arr) = %zu\n", sizeof(arr));
|
2. const�ؼ��ֵ��÷�
1 2 3 4 5 6 7 8 9 10
| const int a = 10; int const b = 20; const int *p1; int * const p2 = &a; const int * const p3 = &a;
|
3. volatile�ؼ���
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| volatile uint32_t *GPIO_REG = (uint32_t*)0x40020000;
uint32_t *reg = (uint32_t*)0x40020000; while(*reg & 0x01);
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; }
bool is_power_of_2(unsigned int n) { return (n != 0) && ((n & (n - 1)) == 0); }
int count_bits(unsigned int n) { int count = 0; while (n) { count++; n &= (n - 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) { if (UART->SR & UART_SR_RXNE) { uint8_t data = UART->DR; rx_buffer[rx_index++] = data; rx_flag = 1; } 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
| void uart_init(uint32_t baudrate) { RCC->APB2ENR |= RCC_APB2ENR_USART1EN; USART1->BRR = SystemCoreClock / baudrate; USART1->CR1 |= USART_CR1_TE | USART_CR1_RE; USART1->CR1 |= USART_CR1_UE; }
void spi_init(void) { RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; SPI1->CR1 |= SPI_CR1_MSTR; SPI1->CR1 |= SPI_CR1_BR_1; 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
|
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() {
}
void enter_sleep_mode(void) { RCC->APB1ENR &= ~RCC_APB1ENR_TIM2EN; EXTI->IMR |= EXTI_IMR_MR0; __WFI(); }
|
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() {
}
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
| char* get_string() { char str[] = "hello"; return str; }
char* get_string_correct() { static char str[] = "hello"; return str; }
void array_pointer_trap() { char arr1[] = "hello"; char *arr2 = "hello"; printf("sizeof(arr1) = %zu\n", sizeof(arr1)); printf("sizeof(arr2) = %zu\n", sizeof(arr2)); }
|
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);
#define SQUARE(x) ((x) * (x))
#define DEBUG_PRINT(x) printf("Debug: "); printf(x)
if (condition) DEBUG_PRINT("error");
#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����Ե������¼���
- ����רҵ������ѡ��һ�����������о�
- ʵ����Ŀ��ͨ����Ŀ���̺���������
- ����������ͨ�����͡���̳��������
��ס�����Բ����ǹ�˾ѡ���㣬Ҳ����ѡ��˾�Ĺ��̡��������ţ�չ����ʵ�ļ���ʵ����ѧϰ������������һ�����ҵ�����Ĺ�����
? ���Խ��: “��������ѧϰ������������˼ά�ͳ���ѧϰ������������Ҫ��”
��������Ƽ�: