在线时间58 小时
UID175586
注册时间2010-3-14
NXP金币0
TA的每日心情 | 奋斗 2017-1-17 10:45 |
---|
签到天数: 3 天 连续签到: 1 天 [LV.2]偶尔看看I
高级会员

- 积分
- 780
- 最后登录
- 2023-11-17
|
嵌入式系统软件的典型开发框架
- 基本的超循环结构
- 使用中断的前后台结构
- 完全依赖中断的事件驱动系统
- 状态机驱动系统——玩转按键
- 总结
1. 基本的超循环结构
1.1 程序结构
超循环结构是最基本的嵌入式应用程序组织架构。
在通常情况下,整个单片机应用程序就是在运行一个周而复始、无休无止的超循环。因此,单片机应用程序的设计内容都必须围绕着这个超循环的执行方式进行。在没有中断等其它通路的情况下,程序就在这个超循环中“一根筋”地顺序执行,一旦在中间某个过程被阻塞,那么整个循环的进程都会被卡在这里,这就是基本的超循环程序结构的特点。
优点:逻辑最简单、最易于实现。
缺点:多个功能之间的耦合性强,任何一个环节出现问题,整个应用程序就会被卡住。
图1
1.2 样例程序(3个)
超循环程序结构样例工程之一——BlinkyLED
小灯闪烁
- /*!
- * @file main.c
- * @author suyong_yq@126.com
- */
- #include "app_inc.h"
- /*!
- * @brief 空跑延时函数
- */
- void MyDelayMs(uint32_t ms)
- {
- uint32_t i, j;
- for (i = 0U; i < ms; i++)
- {
- for (j = 0U; j < 60000; j++)
- {
- __ASM("NOP");
- }
- }
- }
- int main(void)
- {
- /* 初始化板级配置 */
- BSP_InitSystem();
- BSP_InitStdioUART(115200);
- BSP_InitLEDGPIO();
- printf("\r\nBlinky_轮询 Project.\r\n");
- /* Led1. */
- GPIO_SetPinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true);
- GPIO_SetPinDir(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true);
- while (1)
- {
- GPIO_TogglePinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN);
- MyDelayMs(200U);
- }
- }
- /* EOF. */
复制代码 超循环程序结构样例工程之二——Blinky_Polling工程
在超循环结构中周而复始地轮询按键状态,对应改变LED灯的亮灭状态。
- /*!
- * @file main.c
- * @author suyong_yq@126.com
- */
- #include "app_inc.h"
- int main(void)
- {
- /* Initialize the board. */
- BSP_InitSystem();
- BSP_InitStdioUART(115200);
- BSP_InitLEDGPIO();
- BSP_InitKeyGPIO();
- printf("\r\nBlinky_Key Project.\r\n");
- /* Led. */
- GPIO_SetPinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true);
- GPIO_SetPinDir(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true);
- GPIO_SetPinLogic(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN, true);
- GPIO_SetPinDir(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN, true);
- /* Key. */
- GPIO_SetPinDir(BSP_GPIO_KEY1_PORT, BSP_GPIO_KEY1_PIN, false);
- GPIO_SetPinDir(BSP_GPIO_KEY2_PORT, BSP_GPIO_KEY2_PIN, false);
- printf("Press Key1/2 to switch on the Led1/2 ...\r\n");
- while (1)
- {
- /* Key1 -> Led1. */
- if (!GPIO_GetPinLogic(BSP_GPIO_KEY1_PORT, BSP_GPIO_KEY1_PIN)) /* Key down. */
- {
- GPIO_SetPinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, false);
- }
- else /* Key up. */
- {
- GPIO_SetPinLogic(BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN, true);
- }
- /* Key2 -> Led2. */
- if (!GPIO_GetPinLogic(BSP_GPIO_KEY2_PORT, BSP_GPIO_KEY2_PIN)) /* Key down. */
- {
- GPIO_SetPinLogic(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN, false);
- }
- else /* Key up. */
- {
- GPIO_SetPinLogic(BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN, true);
- }
- }
- }
- /* EOF. */
复制代码
超循环程序结构样例工程之三——Blinky_GetCharx4工程
开始引入人机交互,使用getchar()阻塞超循环的执行,从而控制程序运行的节奏。
- /*!
- * @file main.c
- * @author suyong_yq@126.com
- */
- #include "app_inc.h"
- struct GpioPinStruct
- {
- GPIO_Type *PortDevPtr;
- uint32_t PinIdx;
- } gAppGpioLedArray[] =
- {
- {BSP_GPIO_LED1_PORT, BSP_GPIO_LED1_PIN},
- {BSP_GPIO_LED2_PORT, BSP_GPIO_LED2_PIN},
- {BSP_GPIO_LED3_PORT, BSP_GPIO_LED3_PIN},
- {BSP_GPIO_LED4_PORT, BSP_GPIO_LED4_PIN}
- };
- #define APP_LED_COUNT (sizeof(gAppGpioLedArray)/sizeof(gAppGpioLedArray[0]))
- int main(void)
- {
- uint32_t i;;
- /* Initialize the board. */
- BSP_InitSystem();
- BSP_InitStdioUART(115200);
- BSP_InitLEDGPIO();
- printf("\r\nHello, World\r\n");
- for (i = 0U; i < APP_LED_COUNT; i++)
- {
- /* Set GPIOs' initial voltage level as high. */
- GPIO_SetPinLogic(gAppGpioLedArray[i].PortDevPtr, gAppGpioLedArray[i].PinIdx, true);
- /* Set GPIOs' pin dir as output. */
- GPIO_SetPinDir(gAppGpioLedArray[i].PortDevPtr, gAppGpioLedArray[i].PinIdx, true);
- }
- printf("Press any key to blink the LED ...\r\n");
- while (1)
- {
- for (i = 0U; i < APP_LED_COUNT; i++)
- {
- getchar();
- GPIO_TogglePinLogic(gAppGpioLedArray[i].PortDevPtr, gAppGpioLedArray[i].PinIdx);
- }
- }
- }
- /* EOF. */
复制代码
|
|