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 )
(稍后补充)