作者 主题: COM盾演示程序(Keil ARM源码下载)  (阅读 514 次)

hotpower

  • 管理员
  • Full Member
  • *****
  • 帖子: 134
COM盾演示程序(Keil ARM源码下载)
« 于: 一月 04, 2020, 04:09:41 下午 »
HotCOM.h
程序代码: [选择]
#include <stdio.h>                /* prototype declarations for I/O functions */
#include <LPC21xx.H>              /* LPC21xx definitions                      */

typedef void (*CALLBACK)(void);//通用回调函数指针

#define interface struct
typedef interface IComFn_Interface IComFn_Interface;//接口定义

interface IComFn_Interface//回调函数指针或回调函数数组结构
{
union{
CALLBACK FnArray[8];//回调函数数组
struct{//回调函数指针结构,//Fn0~Fn6为重要函数,Fn7为错误处理函数
CALLBACK Fn0;//0
CALLBACK Fn1;//1
CALLBACK Fn2;//2
CALLBACK Fn3;//3
CALLBACK Fn4;//4
CALLBACK Fn5;//5
CALLBACK Fn6;//6
CALLBACK Fn7;//7
}FnPtr;//回调函数指针
}HotCOM;
};//回调函数指针或回调函数数组结构

extern IComFn_Interface IComFn;//回调函数COM接口全局变量
extern void HotCOMWeekExec(unsigned long date);
extern void HotCOMInit(void);
extern void HotCOMExec(void);
extern void Fn0(void);
extern void Fn1(void);
extern void Fn2(void);
extern void Fn3(void);
extern void Fn4(void);
extern void Fn5(void);
extern void Fn6(void);
extern void Fn7(void);

HotCOM.c
程序代码: [选择]
#include "hotcom.H"
/*------------------------------------------------------------------------------
COMDemo
-------------------------------------------------------------------------------*/
IComFn_Interface IComFn;
unsigned int  HotCOMInstallCallbackFn(unsigned int Fn)
{
unsigned int result = 0;
const CALLBACK HotCOMFnTable[8] = {//Fn0~Fn6为重要函数,Fn7为错误处理函数
Fn0, Fn1, Fn2, Fn3, Fn4, Fn5, Fn6, Fn7
};
if (Fn < 8 && IComFn.HotCOM.FnArray[Fn] == NULL)
{
IComFn.HotCOM.FnArray[Fn] = HotCOMFnTable[Fn];//装载回调函数指针
result = 1;
}
return result;
}

unsigned int HotCOMUnInstallCallbackFn(unsigned int Fn)
{
unsigned int  result = 0;
if (Fn < 8 && IComFn.HotCOM.FnArray[Fn] != NULL)
{
IComFn.HotCOM.FnArray[Fn] = (CALLBACK)NULL;
result = 1;
}
return result;
}

//菜农星期公式
unsigned char HotWeek(unsigned long date)
{//20200104->6
unsigned char week;
signed char century, year, month, day;
day = date % 100;
month = (date / 100) % 100;
year = (date / 10000) % 100;
century = (date / 1000000) % 100;
week = 7;//出错
if(month > 0 && month <=12 && day > 0 && day <= 31)
{
if(month <= 2)//1,2月变为去年的13,14月
{
year --;
if (year < 0)
{
year = 99;
century --;
if (century < 0)
{
century = 99;
}
}
month |= 4;//1,2月查5,6月表
// month += 12;//去年的13,14月
}
//下面是菜农星期公式算法
week = ((century & 3) * 5 + year + (year >> 2) + (13 * month + 8) / 5 + day) % 7;
}
return week;//返回星期0-6,7出错处理
}

void HotCOMWeekExec(unsigned long date)
{
unsigned char week;
week = HotWeek(date);//菜农星期公式,取出星期0-6,7出错处理
HotCOMInstallCallbackFn(week);//临时安装回调函数
IComFn.HotCOM.FnArray[week]();//运行一次回调函数
HotCOMUnInstallCallbackFn(week);//自毁回调函数
}

void HotCOMInit(void) {
int i;
for(i = 0;i < 8;i++){
HotCOMInstallCallbackFn(i);//安装全部回调函数
}
return;
}

void HotCOMExec(void)
{
Fn0();//直接调用函数Fn0(),容易被反汇编
HotCOMWeekExec(20191230);//Fn1()
HotCOMWeekExec(20191231);//Fn2()
HotCOMWeekExec(20200101);//Fn3()
IComFn.HotCOM.FnArray[4]();//Fn4()
IComFn.HotCOM.FnPtr.Fn5();//Fn5()
IComFn.HotCOM.FnArray[6]();//Fn6()
IComFn.HotCOM.FnPtr.Fn7();//Fn7()
HotCOMWeekExec(20200106);//Fn1()
HotCOMWeekExec(20200107);//Fn2()
Fn1();//运行
IComFn.HotCOM.FnPtr.Fn1();//Fn1()复位
HotCOMUnInstallCallbackFn(0);//卸载回调函数
IComFn.HotCOM.FnArray[0]();//Fn0()复位
Fn1();
}

void Fn0(void) {
IComFn.HotCOM.FnPtr.Fn0 = (CALLBACK)NULL;//卸载回调函数
printf ("Hello World\n");
return;
}

void Fn1(void) {
printf ("Hello World\n");
return;
}

void Fn2(void) {
printf ("Hello World\n");
return;
}

void Fn3(void) {
printf ("Hello World\n");
return;
}

void Fn4(void) {
printf ("Hello World\n");
return;
}

void Fn5(void) {
printf ("Hello World\n");
return;
}

void Fn6(void) {
printf ("Hello World\n");
return;
}

void Fn7(void) {
printf ("Hello World\n");
return;
}

main()
程序代码: [选择]
int main (void)  {                /* execution starts here                    */

  /* initialize the serial interface   */
  PINSEL0 = 0x00050000;           /* Enable RxD1 and TxD1                     */
  U1LCR = 0x83;                   /* 8 bits, no Parity, 1 Stop bit            */
  U1DLL = 97;                     /* 9600 Baud Rate @ 15MHz VPB Clock         */
  U1LCR = 0x03;                   /* DLAB = 0                                 */

  printf ("Hello World\n");       /* the 'printf' function call               */

  HotCOMInit();
  HotCOMExec();
  while (1) {                          /* An embedded program does not stop and       */
    ;  /* ... */                       /* never returns. We use an endless loop.      */
  }                                    /* Replace the dots (...) with your own code.  */

}