我最近开始学习嵌入式 C,并一直在使用基于 ARM 的 FRDMKL25Z 微控制器。现在,为了使用开关连接 LED,我遇到了两种类型的编码方法:
裸机:
while(1) {
if(GPIO_D->PDIR & (1<<4)) {
/* Make the LED as OFF */
GPIO_C->PSOR = (1 << 8);
}
else {
/* Make the LED as ON */
GPIO_C->PCOR = (1 << 8);
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序编程接口:
while(1) {
if(GPIO_ReadPinInput(GPIOA, 12)) {
GPIO_SetPinsOutput(GPIOC, (1<<9));
}
else {
GPIO_ClearPinsOutput(GPIOC, (1 << 9));
}
}
Run Code Online (Sandbox Code Playgroud)
现在这两个代码都做完全相同的事情,但是它们之间有什么区别,哪一个更好呢?