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

- 积分
- 780
- 最后登录
- 2023-11-17
|
嵌入式系统软件的典型开发框架
- 基本的超循环结构
- 使用中断的前后台结构
- 完全依赖中断的事件驱动系统
- 状态机驱动系统——玩转按键
- 总结
2. 使用中断的前后台结构
2.1 程序结构
当系统硬件支持的中断源被外部事件触发时,会产生中断信号传递到芯片内部的中断管理系统中,中断管理系统对中断信号进行仲裁,在判定可以响应后,打断CPU正常执行的程序,CPU临时转入执行中断源对应的服务程序,之后,CPU再返回到之前的正常程序继续执行,这就是单片机系统响应中断事件过程的简要描述。在应用程序对中断响应的设计需求下,将中断处理机制引入到基本超循环结构中去,从而产生了与之对应的“前后台”软件设计结构。
图2
在需要处理较多交互过程的单片机应用设计中,普遍使用的外部中断事件主要是定时器中断和引脚电平的脉冲中断,这两类中断事件通常作为一个“事件”的触发信号被系统所识别,从而在应用程序中被较为复杂的服务程序所处理。中断事件是由外部环境异步产生,对外部事件的响应“随叫随到”,对应的中断服务程序相对于main函数中的超循环相互独立,因此可以被看作是CPU执行程序的“另一条线程”。
特点:使用硬件实现了多线程的,通过异步的中断可实现对外部事件实时响应。
2.1 样例程序 (1个)
Blinky_v1.3_UARTRx
- /*!
- * @file main.c
- * @author suyong_yq@126.com
- */
- #include "app_inc.h"
- 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)
- {
- /* 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);
- GPIO_SetPinLogic(BSP_GPIO_LED3_PORT, BSP_GPIO_LED3_PIN, true);
- GPIO_SetPinDir(BSP_GPIO_LED3_PORT, BSP_GPIO_LED3_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");
- printf("Type anything into terminal to toggle the Led3\r\n");
- /* Enable the interrupt for UART0 Rx. */
- NVIC_EnableIRQ(UART0_RX_TX_IRQn);
- UART_SetIntEnabledOnRxBufferFull(UART0, true);
- 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);
- MyDelayMs(1000U);
- }
- 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);
- }
- }
- }
- /* ISR for UART0 */
- void UART0_RX_TX_IRQHandler(void)
- {
- volatile uint8_t tmp8;
- /* ISR for RX. */
- if (UART_IsRxBufferFull(UART0))
- {
- tmp8 = UART_GetRxData(UART0); /* Read rx data to clear rx flag. */
- /* Toggle the Led3. */
- GPIO_TogglePinLogic(BSP_GPIO_LED3_PORT, BSP_GPIO_LED3_PIN);
- }
- }
- /* EOF. */
复制代码
|
|