C in ModusToolBox (MTB)
1. โครงสร้างโปรแกรม C ใน ModusToolbox
1.1 Header Files และ Include
ตัวอย่างโครงสร้าง #include ที่แนะนำ
#include ที่แนะนำ/* ===================== System headers ===================== */
#include <stdio.h> /* printf */
#include <stdint.h> /* uint32_t, int16_t */
#include <stdbool.h> /* bool, true, false */
#include <string.h> /* memcpy, strlen */
/* ================= Infineon / PSoC headers ================= */
#include "cy_pdl.h" /* Peripheral Driver Library */
#include "cyhal.h" /* Hardware Abstraction Layer */
#include "cybsp.h" /* Board Support Package */
#include "cy_retarget_io.h" /* UART printf redirection */Best Practice
📘 MISRA-C Note (สำหรับงานอุตสาหกรรม)
1.2 Simple Define (#define สำหรับค่าคงที่)
#define สำหรับค่าคงที่)ตัวอย่าง Simple Define พื้นฐาน
ตัวอย่างจากโค้ดจริงของ Infineon
Best Practice
📘 MISRA-C Note (สำหรับงานอุตสาหกรรม)
1.3 การใช้งานจริงของ Function-like Macros
ตัวอย่าง Function-like Macros ที่ใช้บ่อย
ตัวอย่างการใช้งานจริง (Real-world Usage)
ตัวอย่างจากโค้ดจริงของ Infineon (IMU)
Best Practice
⚖️ Macro vs Inline Function
Aspect
Macro
Inline Function
📘 MISRA-C Note (สำหรับงานอุตสาหกรรม)
1.4 โครงสร้าง main() ที่แนะนำสำหรับ PSoC™ Edge E84
main() ที่แนะนำสำหรับ PSoC™ Edge E84โครงสร้าง main() พื้นฐานที่แนะนำ
main() พื้นฐานที่แนะนำคำอธิบายแต่ละขั้นตอน
ตัวอย่างจริงจาก Infineon (hello-world project)
Best Practice
📘 MISRA-C Note (สำหรับงานอุตสาหกรรม)
สรุปสั้น ๆ
1.5 ANSI Escape Sequence สำหรับ Console Debug
ตัวอย่างพื้นฐานที่ใช้บ่อยที่สุด
ตัวอย่างการใช้งานจริง (Real-world Usage)
ทำไม ANSI Escape ถึงเหมาะกับ Embedded Debug
Best Practice
⚖️ ANSI Escape vs GUI Debug
Aspect
ANSI Escape
GUI Tool
สรุปสั้น ๆ
1.6 Coding Style จากโค้ดตัวอย่างจริงของ Infineon (Hello-world & MQTT Multi-tasking)
โครงสร้างโปรเจกต์ (Project Structure Mindset)
โครงสร้างที่พบได้บ่อย
Best Practice
Pattern ของ main() จาก Hello-world (Bare-metal Style)
main() จาก Hello-world (Bare-metal Style)Pattern ที่พบจริง
Best Practice
MISRA-C Note
สรุป 1.6
Last updated
Was this helpful?