1. 程式人生 > >時間觸發嵌入式系統設計模式 第14章 筆記

時間觸發嵌入式系統設計模式 第14章 筆記

1 函式指標 程式碼:

/*------------------------------------------------------------------*-

   Main.C (v1.00)

  ------------------------------------------------------------------
   
  函式指標的演示


-*------------------------------------------------------------------*/

#include "Main.h"
#include "Printf51.h"
#include <stdio.h>

// ------ 私有函式原型 ------------------------------
void Square_Number(int, int*);

/* ................................................................. */
/* ................................................................. */

int main(void)
   {
   int a = 2, b = 3;      
   void (* pFn)(int, int*); /* 宣告 pFn 為 fn 的指標的,fn帶有 
                               int 引數和 int 指標引數 
                               (返回 void) */
   int Result_a, Result_b;
   
   //準備在 Keil 硬體模擬器上使用printf()
   Printf51_Init();

   pFn = Square_Number;   //  pFn存放 Square_Number 	的地址
 
   printf("Function code starts at address: %u\n", (tWord) pFn);
   printf("Data item a starts at address:   %u\n\n", (tWord) &a);
   
   // 以傳統的方式 呼叫'Square_Number' 
   Square_Number(a,&Result_a);
   
   // 使用函式指標呼叫'Square_Number'
   (*pFn)(b,&Result_b);
                         
   printf("%d squared is %d (using normal fn call)\n", a, Result_a); 
   printf("%d squared is %d (using fn pointer)\n", b, Result_b); 

   while(1);

   return 0;
   }

/*------------------------------------------------------------------*/

void Square_Number(int a, int* b)
   {
   // 演示- 計算a的平方
   *b = a * a;
   }

/*------------------------------------------------------------------*-
  ---- 檔案結束 -------------------------------------------------
-*------------------------------------------------------------------*/


2 簡單例子程式碼

一個用來重複閃爍LED的排程器 一秒亮 一秒滅 如此迴圈 在這裡插入圖片描述

3 將簡單的例子 移植到 Nu-LB-NUC140 ARM CM0 微控制器上面

page210

以下程式碼 來自 keil freertos

/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
SysTick_Handler

void xPortSysTickHandler( void )
{
uint32_t ulPreviousMask;

	ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();
	{
		/* Increment the RTOS tick. */
		if( xTaskIncrementTick() != pdFALSE )
		{
			/* Pend a context switch. */
			*(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
		}
	}
	portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
}
    void prvSetupTimerInterrupt( void )
{
	/* Configure SysTick to interrupt at the requested rate. */
	*(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
	*(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
}

#define portNVIC_SYSTICK_CTRL		( ( volatile uint32_t *) 0xe000e010 )
#define portNVIC_SYSTICK_LOAD		( ( volatile uint32_t *) 0xe000e014 )

#define __HSI       (50000000UL)    /*!< PLL default output is 50MHz */
uint32_t SystemCoreClock  = __HSI;             /*!< System Clock Frequency (Core Clock) */
#define configCPU_CLOCK_HZ              ( SystemCoreClock )
#define configTICK_RATE_HZ              ( ( portTickType ) 1000 )

(稍後補充)