Java教程

51 单片机实战教程(9 中断库函数)

本文主要是介绍51 单片机实战教程(9 中断库函数),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

       STC15W系列单片机最多有21个中断请求源,每个中断请求源都有相应的控制寄存器。要记忆下来是一个比较麻烦的事情,也没有必要。使用时临时去查手册,也比较麻烦费时,一劳永逸的办法是就是写库函数。新建两个文件,分别以stcint.h, stcint.c存入C51 Template文件夹下Library文件夹中。

           需要说明的是:中断函数不可以像普通函数那样在头文件中声明,否则编译时会报错。请看下面例子:

/*stcint.h
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 11/29/2021
*/
#ifndef __STCINT_H_
#define __STCINT_H_

#include "mtype.h"
#include "stc15w4k.h"

void STC_Int0(void) interrupt 0;

#endif
/*stcint.c
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 11/29/2021
*/
#include "stcint.h"
void STC_Int0(void) interrupt 0
{
  
}
//End of STC_Int0(void)
/*main.c
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 11/29/2021
*/


#include "main.h" 
#include "stcint.h"

void main()
{
  
  
  while(1)
  {
      
  }
}
//End of main()

编译结果,如下:

 编译出错,问题出在中断函数的声明。现在把中断函数的声明注释起来,再看下编译结果:

/*stcint.h
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 11/29/2021
*/
#ifndef __STCINT_H_
#define __STCINT_H_

#include "mtype.h"
#include "stc15w4k.h"

//void STC_Int0(void) interrupt 0;

#endif

编译通过。 说明中断函数不用声明,直接在源程序中实现即可。另外需要说明的是,库函数中的中断函数没有实现代码,并被注释起来,需要用时,再去取消注释,添加实现代码,也可Copy到主程序中去实现。库函数主要是中断初始化函数,及中断标志位读写函数。

      头文件如下:

/*stcint.h
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 11/29/2021
*/
#ifndef __STCINT_H_
#define __STCINT_H_

#include "mtype.h"
#include "stc15w4k.h"

//External INT///
/**********************************************************************
Function: STC_Int0Init(BOOL intEnable, BOOL priority, BOOL falEdgeTrigger); 
Return value: void
Discription: INT0 init 
Example:
  STC_Int0Init(1, 0, 0);  //init to trigger interrupt at both of level rising edge & falling edge 
	STC_Int0Init(1, 0, 1);  //init to trigger interrupt at falling edge
***********************************************************************/
void STC_Int0Init(BOOL intEnable, BOOL priority, BOOL falEgeTrigger);

/**********************************************************************
Function: STC_Int1Init(BOOL intEnable, BOOL priority, BOOL falEdgeTrigger); 
Return value: void
Discription:  INT1 init 
Example:
  STC_Int1Init(1, 0, 0);  //init to trigger interrupt at both of level rising edge & falling edge 
	STC_Int1Init(1, 0, 1);  //init to trigger interrupt at falling edge,priorrity level 0
***********************************************************************/
void STC_Int1Init(BOOL intEnable, BOOL priority, BOOL falEgeTrigger);

/**********************************************************************
Function: STC_Int2Init(BOOL intEnable); 
Return value: void
Discription: init INT2
Example:
  STC_Int2Init(1);  //INT2 enable,trigger at falling edge
	STC_Int2Init(0);  //INT2 disable
***********************************************************************/
void STC_Int2Init(BOOL intEnable); 

/**********************************************************************
Function: STC_Int3Init(BOOL intEnable); 
Return value: void
Discription: INT3 init 
Example:
  STC_Int2Init(1);  //INT3 enable,trigger at falling edge
	STC_Int2Init(0);  //INT3 disable
***********************************************************************/
void STC_Int3Init(BOOL intEnable); 

/**********************************************************************
Function: STC_Int4Init(BOOL intEnable, BOOL priority); 
Return value: void
Discription: INT4 init 
Example:
  STC_Int4Init(1, 0);  //INT4 interrupt enable,priority level 0
	STC_Int4Init(0, 0);  //INT4 interrupt disable
***********************************************************************/
void STC_Int4Init(BOOL intEnable, BOOL priority);

/Timer/
/**********************************************************************
Function: STC_GetT0Flag(); 
Return value: ui8
Discription: get T0 counter overflow flag 
Example:
  ui8 t0flag = STC_GetT0Flag(); 
***********************************************************************/
ui8 STC_GetT0Flag(); 

/**********************************************************************
Function: STC_ClearT0Flag(); 
Return value: void
Discription: cleart T0 counter overflow flag 
Example:
  STC_ClearT0Flag(); 
***********************************************************************/
void STC_ClearT0Flag(); 

/**********************************************************************
Function: STC_IntT0Init(BOOL intEnable, BOOL priority); 
Return value: void
Discription: timer0 interrupt init 
Example:
  STC_IntT0Init(1, 0);  //T0 interrupt enable,priority level 0
	STC_IntT0Init(0, 0);  //T0 interrupt disable
***********************************************************************/
void STC_IntT0Init(BOOL intEnable, BOOL priority);

/**********************************************************************
Function: STC_GetT1Flag(); 
Return value: ui8
Discription: get T1 counter overflow flag 
Example:
  ui8 t1flag = STC_GetT1Flag(); 
***********************************************************************/
ui8 STC_GetT1Flag(); 

/**********************************************************************
Function: STC_ClearT1Flag(); 
Return value: void
Discription: cleart T1 counter overflow flag 
Example:
  STC_ClearT1Flag(); 
***********************************************************************/
void STC_ClearT1Flag();

/**********************************************************************
Function: STC_IntT1Init(BOOL intEnable, BOOL priority); 
Return value: void
Discription: timer1 interrupt init 
Example:
  STC_IntT1Init(1, 0);  //T1 interrupt enable,prioriyty level 0
	STC_IntT1Init(0, 0);  //T1 interrupt disable
***********************************************************************/
void STC_IntT1Init(BOOL intEnable, BOOL priority);

/**********************************************************************
Function: STC_IntT2Init(BOOL intEnable); 
Return value: void
Discription: timer2 interrupt init 
Example:
  STC_IntT2Init(1);  //T2 interrupt enable
	STC_IntT2Init(0);  //T2 interrupt disable
***********************************************************************/
void STC_IntT2Init(BOOL intEnable);

/**********************************************************************
Function: STC_IntT3Init(BOOL intEnable); 
Return value: void
Discription: timer3 interrupt init 
Example:
  STC_IntT3Init(1);  //T3 interrupt enable
	STC_IntT3Init(0);  //T3 interrupt disable
***********************************************************************/
void STC_IntT3Init(BOOL intEnable);

/**********************************************************************
Function: STC_IntT4Init(BOOL intEnable); 
Return value: void
Discription: timer4 interrupt init 
Example:
  STC_IntT4Init(1);  //T4 interrupt enable
	STC_IntT4Init(0);  //T4 interrupt disable
***********************************************************************/
void STC_IntT4Init(BOOL intEnable);

///Serial port
/**********************************************************************
Function: STC_GetS1RiFlag(); 
Return value: ui8
Discription: get serial port1 receiving interrupt flag
Example:
 ui8 tem = STC_GetS1RiFlag();
***********************************************************************/
ui8 STC_GetS1RiFlag();

/**********************************************************************
Function: STC_ClearS1RiFlag(); 
Return value: void
Discription: clear serial port1 receiving interrupt flag
Example:
 STC_ClearS1RiFlag(); 
***********************************************************************/
void STC_ClearS1RiFlag(); 

/**********************************************************************
Function: STC_GetS1TiFlag(); 
Return value: ui8
Discription: get serial port1 transmitting interrupt flag
Example:
 ui8 tem = STC_GetS1TiFlag();
***********************************************************************/
ui8 STC_GetS1TiFlag();

/**********************************************************************
Function: STC_ClearS1TiFlag(); 
Return value: void
Discription: clear serial port1 trnsmitting interrupt flag
Example:
 STC_ClearS1TiFlag(); 
***********************************************************************/
void STC_ClearS1TiFlag();

/**********************************************************************
Function: STC_IntS1Init(BOOL intEnable, BOOL priority); 
Return value: void
Discription: serial port1 interrupt init 
Example:
 STC_IntS1Init(1,0);  //serial port1 interrupt enable,priority level 0
 STC_IntS1Init(0,0);  //serial port1 interrupt disable
***********************************************************************/
void STC_IntS1Init(BOOL intEnable, BOOL priority);

/**********************************************************************
Function: STC_GetS2RiFlag(); 
Return value: ui8
Discription: get serial port2 receiving interrupt flag
Example:
 ui8 tem = STC_GetS2RiFlag();
***********************************************************************/
ui8 STC_GetS2RiFlag();

/**********************************************************************
Function: STC_ClearS2RiFlag(); 
Return value: void
Discription: clear serial port2 receiving interrupt flag
Example:
 STC_ClearS2RiFlag(); 
***********************************************************************/
void STC_ClearS2RiFlag(); 

/**********************************************************************
Function: STC_GetS2TiFlag(); 
Return value: ui8
Discription: get serial port2 transmitting interrupt flag
Example:
 ui8 tem = STC_GetS2TiFlag();
***********************************************************************/
ui8 STC_GetS2TiFlag();

/**********************************************************************
Function: STC_ClearS2TiFlag(); 
Return value: void
Discription: clear serial port2 trnsmitting interrupt flag
Example:
 STC_ClearS2TiFlag(); 
***********************************************************************/
void STC_ClearS2TiFlag();

/**********************************************************************
Function: STC_IntS2Init(BOOL intEnable, BOOL priority); 
Return value: void
Discription: serial port2 interrupt init 
Example:
 STC_IntS2Init(1,0);  //serial port2 interrupt enable,priority level 0
 STC_IntS2Init(0,0);  //serial port2 interrupt disable
***********************************************************************/
void STC_IntS2Init(BOOL intEnable, BOOL priority); 

/**********************************************************************
Function: STC_GetS3RiFlag(); 
Return value: ui8
Discription: get serial port3 receiving interrupt flag
Example:
 ui8 tem = STC_GetS3RiFlag();
***********************************************************************/
ui8 STC_GetS3RiFlag();

/**********************************************************************
Function: STC_ClearS3RiFlag(); 
Return value: void
Discription: clear serial port3 receiving interrupt flag
Example:
 STC_ClearS3RiFlag(); 
***********************************************************************/
void STC_ClearS3RiFlag(); 

/**********************************************************************
Function: STC_GetS3TiFlag(); 
Return value: ui8
Discription: get serial port3 transmitting interrupt flag
Example:
 ui8 tem = STC_GetS3TiFlag();
***********************************************************************/
ui8 STC_GetS3TiFlag();

/**********************************************************************
Function: STC_ClearS3TiFlag(); 
Return value: void
Discription: clear serial port3 trnsmitting interrupt flag
Example:
 STC_ClearS3TiFlag(); 
***********************************************************************/
void STC_ClearS3TiFlag();

/**********************************************************************
Function: STC_IntS3Init(BOOL intEnable); 
Return value: void
Discription: serial port3 interrupt init 
Example:
 STC_IntS3Init(1);  //serial port3 interrupt enable
 STC_IntS3Init(0);  //serial port3 interrupt disable
***********************************************************************/
void STC_IntS3Init(BOOL intEnable); 

/**********************************************************************
Function: STC_GetS4RiFlag(); 
Return value: ui8
Discription: get serial port4 receiving interrupt flag
Example:
 ui8 tem = STC_GetS4RiFlag();
***********************************************************************/
ui8 STC_GetS4RiFlag();

/**********************************************************************
Function: STC_ClearS4RiFlag(); 
Return value: void
Discription: clear serial port4 receiving interrupt flag
Example:
 STC_ClearS4RiFlag(); 
***********************************************************************/
void STC_ClearS4RiFlag(); 

/**********************************************************************
Function: STC_GetS4TiFlag(); 
Return value: ui8
Discription: get serial port4 transmitting interrupt flag
Example:
 ui8 tem = STC_GetS4TiFlag();
***********************************************************************/
ui8 STC_GetS4TiFlag();

/**********************************************************************
Function: STC_ClearS4TiFlag(); 
Return value: void
Discription: clear serial port4 trnsmitting interrupt flag
Example:
 STC_ClearS4TiFlag(); 
***********************************************************************/
void STC_ClearS4TiFlag();

/**********************************************************************
Function: STC_IntS4Init(BOOL intEnable); 
Return value: void
Discription: serial port4 interrupt init 
Example:
 STC_IntS4Init(1);  //serial port4 interrupt enable
 STC_IntS4Init(0);  //serial port4 interrupt disable
***********************************************************************/
void STC_IntS4Init(BOOL intEnable); 

//ADC
/**********************************************************************
Function: STC_GetAdcFlag(); 
Return value: ui8
Discription: get ADC  interrupt flag
Example:
 ui8 tem = STC_GetAdcFlag();
***********************************************************************/
ui8 STC_GetAdcFlag();

/**********************************************************************
Function: STC_ClearAdcFlag(); 
Return value: void
Discription: clear ADC interrupt flag
Example:
 STC_ClearAdcFlag(); 
***********************************************************************/
void STC_ClearAdcFlag(); 

/**********************************************************************
Function: STC_IntAdcInit(BOOL intEnable, BOOL proirity); 
Return value: void
Discription: ADC interrupt init 
Example:
 STC_IntAdcInit(1,0);  //ADC interrupt enable, priority level 0
 STC_IntAdcInit(0,0);  //ADC interrupt disable
***********************************************************************/
void STC_IntAdcInit(BOOL intEnable, BOOL proirity); 

///LVD/
/**********************************************************************
Function: STC_GetLvdFlag(); 
Return value: ui8
Discription: get low voltage detected  flag
Example:
 ui8 tem = STC_GetLvdFlag();
***********************************************************************/
ui8 STC_GetLvdFlag();

/**********************************************************************
Function: STC_ClearLvdFlag(); 
Return value: void
Discription: clear low voltage detected flag
Example:
 STC_ClearLvdFlag(); 
***********************************************************************/
void STC_ClearLvdFlag();

/**********************************************************************
Function: STC_IntLvdInit(BOOL intEnable, BOOL proirity); 
Return value: void
Discription: LVD interrupt init 
Example:
 STC_IntLvdInit(1,0);  //LVD interrupt enable, priority level 0
 STC_IntLvdInit(0,0);  //LVD interrupt disable
***********************************************************************/
void STC_IntLvdInit(BOOL intEnable, BOOL proirity); 

///SPI///
/**********************************************************************
Function: STC_GetSPIFlag(); 
Return value: ui8
Discription: get SPI sinish  flag
Example:
 ui8 tem = STC_GetSPIFlag();
***********************************************************************/
ui8 STC_GetSPIFlag();

/**********************************************************************
Function: STC_ClearSPIFlag(); 
Return value: void
Discription: clear SPI finish flag
Example:
 STC_ClearSPIFlag();
***********************************************************************/
void STC_ClearSPIFlag();

/**********************************************************************
Function: STC_IntSpiInit(BOOL intEnable, BOOL proirity); 
Return value: void
Discription: SPI interrupt init 
Example:
 STC_IntSpiInit(1,0);  //SPI interrupt enable, priority level 0
 STC_IntSpiInit(0,0);  //SPI interrupt disable
***********************************************************************/
void STC_IntSpiInit(BOOL intEnable, BOOL proirity); 

/PCA///
typedef enum
{
  
  SCCF0 = 0x01,
  SCCF1 = 0x02,
  SCCF2 = 0x04,
  SCF = 0x80
}PCA_INTSOURCE;

/**********************************************************************
Function: STC_GetPcaFlag(); 
Return value: ui8
Discription: get PCA counter  overflow  flag
Example:
 ui8 tem = STC_GetPcaFlag(); 
***********************************************************************/
ui8 STC_GetPcaFlag(); 

/**********************************************************************
Function: STC_GetPCA0Flag(); 
Return value: ui8
Discription: get PCA0 interrupt  flag
Example:
 ui8 tem = STC_GetPCA0Flag();
***********************************************************************/
ui8 STC_GetPCA0Flag();

/**********************************************************************
Function: STC_GetPCA1Flag(); 
Return value: ui8
Discription: getPCA1 interrupt  flag
Example:
 ui8 tem = STC_GetPCA1Flag();
***********************************************************************/
ui8 STC_GetPCA1Flag();

/**********************************************************************
Function: STC_GetPCA2Flag(); 
Return value: ui8
Discription: get PCA2 interrupt  flag
Example:
 ui8 tem = STC_GetPCA2Flag(); 
***********************************************************************/
ui8 STC_STC_GetPCA2Flag(); 

/**********************************************************************
Function: STC_ClearPcaFlag(); 
Return value: void
Discription: clear PCA interrupt flag
Example:
 STC_ClearPcaFlag(); 
***********************************************************************/
void STC_ClearPcaFlag(); 

/**********************************************************************
Function: STC_IntPcaInit(BOOL intEnable, PCA_INTSOURCE msource, BOOL priority); 
Return value: void
Discription: PCA interrupt init 
Example:
 STC_IntSpiInit(1,1,0);  //PCA CF interrupt enable, priority level 0
 STC_IntSpiInit(0,1,0);  //PCA interrupt disable
***********************************************************************/
void STC_IntPcaInit(BOOL intEnable, PCA_INTSOURCE msource, BOOL priority);

///Comparator
/**********************************************************************
Function: STC_ClearCmpFlag(); 
Return value: void
Discription: clear comparator interrupt flag
Example:
 STC_ClearCmpFlag();  
***********************************************************************/
void STC_ClearCmpFlag(); 

/**********************************************************************
Function: STC_IntCmpInit(BOOL nEnable, BOOL pEnable); 
Return value: void
Discription: comparator interrupt init 
Example:
 STC_IntCmpInit(1,0);  //comparator negtive edge INT enable,
 positive edge INT disable
***********************************************************************/
void STC_IntCmpInit(BOOL nEnable, BOOL pEnable);

///PWM FD
/**********************************************************************
Function: STC_PwmFdClearFlag(); 
Return value: void
Discription: clear PWM found exception interrupt flag
Example:
  STC_PwmFdClearFlag(); 
***********************************************************************/
void STC_PwmFdClearFlag();

/**********************************************************************
Function: STC_IntPwmFdInit(BOOL intEnable, BOOL priority); 
Return value: void
Discription: PWM found exception interrupt init 
Example:
  STC_IntPwmFdInit(1,0);  //PWM found exception interrupt INT enable,
  priority level 0
***********************************************************************/
void STC_IntPwmFdInit(BOOL intEnable, BOOL priority); 

//PWM

/**********************************************************************
Function: STC_PwmClearFlag(); 
Return value: void
Discription: clear PWM interrupt flag
Example:
  STC_PwmClearFlag();  
***********************************************************************/
void STC_PwmClearFlag();

/**********************************************************************
Function: STC_IntPwmInit(BOOL cResetIntEnable, BOOL priority); 
Return value: void
Discription: PWM counter reset interrupt init
Example:
  STC_IntPwmInit(1,0);  
***********************************************************************/
void STC_IntPwmInit(BOOL cResetIntEnable, BOOL priority); 

/**********************************************************************
Function: STC_IntPWM2Init(BOOL intT1, BOOL intT2, BOOL priority); 
Return value: void
Discription: PWM2 interrupt init
Example:
  STC_IntPWM2Init(1,0,0);  
***********************************************************************/
void STC_IntPWM2Init(BOOL intT1, BOOL intT2, BOOL priority);

/**********************************************************************
Function: STC_IntPWM3Init(BOOL intT1, BOOL intT2, BOOL priority); 
Return value: void
Discription: PWM3 interrupt init
Example:
  STC_IntPWM3Init(1,0,0);  
***********************************************************************/
void STC_IntPWM3Init(BOOL intT1, BOOL intT2, BOOL priority);

/**********************************************************************
Function: STC_IntPWM4Init(BOOL intT1, BOOL intT2, BOOL priority); 
Return value: void
Discription: PWM4 interrupt init
Example:
  STC_IntPWM4Init(1,0,0);  
***********************************************************************/
void STC_IntPWM4Init(BOOL intT1, BOOL intT2, BOOL priority);

/**********************************************************************
Function: STC_IntPWM5Init(BOOL intT1, BOOL intT2, BOOL priority); 
Return value: void
Discription: PWM5 interrupt init
Example:
  STC_IntPWM5Init(1,0,0);  
***********************************************************************/
void STC_IntPWM5Init(BOOL intT1, BOOL intT2, BOOL priority);

/**********************************************************************
Function: STC_IntPWM6Init(BOOL intT1, BOOL intT2, BOOL priority); 
Return value: void
Discription: PWM6 interrupt init
Example:
  STC_IntPWM6Init(1,0,0);  
***********************************************************************/
void STC_IntPWM6Init(BOOL intT1, BOOL intT2, BOOL priority);

/**********************************************************************
Function: STC_IntPWM7Init(BOOL intT1, BOOL intT2, BOOL priority); 
Return value: void
Discription: PWM7 interrupt init
Example:
  STC_IntPWM7Init(1,0,0);  
***********************************************************************/
void STC_IntPWM7Init(BOOL intT1, BOOL intT2, BOOL priority);



#endif

源程序如下:

/*stcint.c
  Designed by Bill Liu
  Version 0.0 
  Modified last by Bill Liu on 11/29/2021
*/
#include "stcint.h"


/*
///
void STC_Int0(void) interrupt 0
{
	
}
//End of STC_Int0(void)

///
void STC_Int1(void) interrupt 2
{
	
}
//End of STC_Int1(void)

///
void STC_Int2(void) interrupt 10
{
	
}
//End of STC_Int2(void)

///
void STC_Int3(void) interrupt 11
{
	
}
//End of STC_Int3(void)

///
void STC_Int4(void) interrupt 16
{
	
}
//End of STC_Int4(void)

///
void STC_T0Int(void) interrupt 1
{
	
}
//End of STC_T0Int(void)

///
void STC_T1Int(void) interrupt 3
{
	
}
//End of STC_T1Int(void)

///
void STC_T2Int(void) interrupt 12
{
	
}
//End of STC_T2Int(void)

///
void STC_T3Int(void) interrupt 19
{
	
}
//End of STC_T3Int(void)

///
void STC_T4Int(void) interrupt 20
{
	
}
//End of STC_T4Int(void)

///
void STC_S1Int(void) interrupt 4
{
	
}
//End of STC_S1Int(void)

///
void STC_S2Int(void) interrupt 8
{
	
}
//End of STC_S2Int(void)

///
void STC_S3Int(void) interrupt 17
{
	
}
//End of STC_S3Int(void)

///
void STC_S4Int(void) interrupt 18
{
	
}
//End of STC_S4Int(void)

///
void STC_ADCInt(void) interrupt 5
{
	
}
//End of STC_ADCInt(void)

///
void STC_LvdInt(void) interrupt 6
{
	
}
//End of STC_LvdInt(void)

///
void STC_SpiInt(void) interrupt 9
{
	
}
//End of STC_SpiInt(void)

///
void STC_PCAInt(void) interrupt 7
{
	
}
//End of STC_PCAInt(void)

///
void STC_CmpInt(void) interrupt 21    //comparator interrupt function
{
	
}
//End of STC_CmpInt(void)

///
void STC_PwmInt(void) interrupt 22    
{
	
}
//End of STC_PwmInt(void)

///
void STC_PwmFdInt(void) interrupt 23    
{
	
}
//End of STC_PCAInt(void)
*/
///External INT/
//********************************************************************
void STC_Int0Init(BOOL intEnable, BOOL priority, BOOL falEgeTrigger)
{
  if(intEnable)
  {
    EA = 1;
    EX0 = 1;
  }
  else
    EX0 = 0;
  if(falEgeTrigger)
    IT0 = 1;
  else
    IT0 = 0;
	if(priority)
    PX0 = 1;
  else
    PX0 = 0;
}
//End of STC_Int0Init(BOOL intEnable, BOOL priority, BOOL dnEgeTrigger)

//**********************************************************************
void STC_Int1Init(BOOL intEnable, BOOL priority, BOOL falEdgeTrigger)
{
  if(intEnable)
  {
    EA = 1;
    EX1 = 1;
  }
  else
    EX1 = 0;
  if(falEdgeTrigger)
    IT1 = 1;
  else
    IT1 = 0;
	if(priority)
		PX1 = 1;
	else
		PX1 = 0;
}
//End of STC_Int1Init(BOOL intEnable, BOOL priority, BOOL falEdgeTrigger)

//**********************************************************************
void STC_Int2Init(BOOL intEnable)
{
	ui8 tem = INT_CLKO;
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 4);
	}
	else
		ResetBit(&tem, 4);
	INT_CLKO = tem;
}
//End of STC_Int2Init(BOOL intEnable)

//**********************************************************************
void STC_Int3Init(BOOL intEnable)
{
	ui8 tem = INT_CLKO;
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 5);
	}
	else
		ResetBit(&tem, 5);
	INT_CLKO = tem;
}
//End of STC_Int3Init(BOOL intEnable)

//**********************************************************************
void STC_Int4Init(BOOL intEnable, BOOL priority)
{
	ui8 tem = INT_CLKO;
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 6);
	}
	else
		ResetBit(&tem, 6);
	INT_CLKO = tem;
	if(priority)
	{
		ui8 tem1 = IP2;
		SetBit(&tem1, 4);
		IP2 = tem1;
	}
	else
	{
		ui8 tem1 = IP2;
		ResetBit(&tem1, 4);
		IP2 = tem1;
	}
}
//End of STC_Int4Init(BOOL intEnable, BOOL priority)

//Timer/
//**********************************************************************
ui8 STC_GetT0Flag()
{
  return TF0;
}
//End of STC_GetT0Flag()

//**********************************************************************
void STC_ClearT0Flag()
{
  TF0 = 0;
}
//End of STC_ClearT0Flag()

//**********************************************************************
void STC_IntT0Init(BOOL intEnable, BOOL priority)
{
  TF0 = 0;
	if(intEnable)
	{
		EA = 1;
		ET0 = 1;    //T0 INT enable
	}
	else
		ET0 = 0;   //T0 INT disable
	if(priority)
		PT0 = 1;
	else
		PT0 = 0;
}
//End of STC_IntT0Init(BOOL intEnable, BOOL priority)

//**********************************************************************
ui8 STC_GetT1Flag()
{
  return TF1;
}
//End of STC_GetT1Flag()

//**********************************************************************
void STC_ClearT1Flag()
{
  TF1 = 0;
}
//End of STC_ClearT1Flag()

//**********************************************************************
void STC_IntT1Init(BOOL intEnable, BOOL priority)
{
  TF1 = 0;
	if(intEnable)
	{
		EA = 1;
		ET1 = 1;    //T1 INT enable
	}
	else
		ET1 = 0;   //T1 INT disable
	if(priority)
		PT1 = 1;
	else
		PT1 = 0;
}
//End of STC_IntT1Init(BOOL intEnable, BOOL priority)

//**********************************************************************
void STC_IntT2Init(BOOL intEnable)
{
	ui8 tem = IE2;
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 2);
	}
	else
		ResetBit(&tem, 2);
	IE2 = tem;	
}
//End of STC_IntT2Init(BOOL intEnable)

//**********************************************************************
void STC_IntT3Init(BOOL intEnable)
{
	ui8 tem = IE2;
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 5);
	}
	else
		ResetBit(&tem, 5);
	IE2 = tem;	
}
//End of STC_IntT3Init(BOOL intEnable)

//**********************************************************************
void STC_IntT4Init(BOOL intEnable)
{
	ui8 tem = IE2;
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 6);
	}
	else
		ResetBit(&tem, 6);
	IE2 = tem;	
}
//End of STC_IntT3Init(BOOL intEnable)

//Serial port///
//**********************************************************************
ui8 STC_GetS1RiFlag()
{
  return RI;
}
//End of STC_GetS1RiFlag()

//**********************************************************************
void STC_ClearS1RiFlag()
{
  RI = 0;
}
//End of STC_ClearS1RiFlag()

//**********************************************************************
ui8 STC_GetS1TiFlag()
{
  return TI;
}
//End of STC_GetS1TiFlag()

//**********************************************************************
void STC_ClearS1TiFlag()
{
  TI = 0;
}
//End of STC_ClearS1TiFlag()

//**********************************************************************
void STC_IntS1Init(BOOL intEnable, BOOL priority)
{
  RI = 0;
  TI = 0;
	if(intEnable)
	{
		EA = 1;
		ES = 1;
	}
	else
		ES = 0;
	if(priority)
		PS = 1;
	else
		PS = 0;
}
//End of STC_IntS1Init(BOOL intEnable, BOOL priority)

//**********************************************************************
ui8 STC_GetS2RiFlag()
{
 return GetBit(S2CON,0);
}
//End of STC_GetS2RiFlag()

//**********************************************************************
void STC_ClearS2RiFlag()
{
  S2CON &= ~0x01;
}
//End of STC_ClearS2RiFlag()

//**********************************************************************
ui8 STC_GetS2TiFlag()
{
  return GetBit(S2CON,1);
}
//End of STC_GetS2TiFlag()

//**********************************************************************
void STC_ClearS2TiFlag()
{
  S2CON &= ~(0x01 << 1);
}
//End of STC_ClearS2TiFlag()

//**********************************************************************
void STC_IntS2Init(BOOL intEnable, BOOL priority)
{
  STC_ClearS2RiFlag();
  STC_ClearS2TiFlag();
	if(intEnable)
	{
		EA = 1;
		IE2 |= 0x01;  //S2 INT enable
	}
	else
			IE2 &= ~0x01;  //S2 INT enable
	if(priority)
		IP2 |= 0x01;
	else
		IP2 &= ~0x01;
}
//End of STC_IntS2Init(BOOL intEnable, BOOL priority)

//**********************************************************************
ui8 STC_GetS3RiFlag()
{
  return (S3CON & 0x01);
}
//End of STC_GetS3RiFlag()

//**********************************************************************
void STC_ClearS3RiFlag()
{
  S3CON &= 0xFE;
}
//End of STC_ClearS3RiFlag()

//**********************************************************************
ui8 STC_GetS3TiFlag()
{
  return GetBit(S3CON,1);
}
//End of STC_GetS3TiFlag()

//**********************************************************************
void STC_ClearS3TiFlag()
{
  S3CON &= ~(0x01 << 1);
}
//End of STC_ClearS3TiFlag()

//**********************************************************************
void STC_IntS3Init(BOOL intEnable)
{
	ui8 tem = IE2;
  STC_ClearS3RiFlag();
  STC_ClearS3TiFlag();
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 3);
	}
	else
		ResetBit(&tem, 3);
	IE2 = tem;
}
//End of STC_IntS3Init(BOOL intEnable)

//**********************************************************************
ui8 STC_GetS4RiFlag()
{
  return (S4CON & 0x01);
}
//End of STC_GetS4RiFlag()

//**********************************************************************
void STC_ClearS4RiFlag()
{
  S4CON &= 0xFE;
}
//End of STC_ClearS4RiFlag()

//**********************************************************************
ui8 STC_GetS4TiFlag()
{
  return GetBit(S4CON,1);
}
//End of GetBit(S4CON,1)

//**********************************************************************
void STC_ClearS4TiFlag()
{
  S4CON &= ~0x02;
}
//End of STC_ClearS4TiFlag()

//**********************************************************************
void STC_IntS4Init(BOOL intEnable)
{
	ui8 tem = IE2;
  STC_ClearS4RiFlag();
  STC_ClearS4TiFlag();
	if(intEnable)
	{
		EA = 1;
		SetBit(&tem, 4);
	}
	else
		ResetBit(&tem, 4);
	IE2 = tem;
}
//End of STC_IntS4Init(BOOL intEnable)

/ADC///
//**********************************************************************
ui8 STC_GetAdcFlag()
{
  return GetBit(ADC_CONTR,4);
}
//End of GetBit(ADC_CONTR,4)

//**********************************************************************
void STC_ClearAdcFlag()
{
  ADC_CONTR &= ~(0x01 << 4);
}
//End of STC_ClearAdcFlag()

//**********************************************************************
void STC_IntAdcInit(BOOL intEnable, BOOL proirity)
{
  STC_ClearAdcFlag();
	if(intEnable)
	{
		EA = 1;
		EADC = 1;
	}
	else
		EADC = 0;
	if(proirity)
		PADC = 1;
	else
		PADC = 0;
}
//End of STC_IntAdcInit(BOOL intEnable, BOOL proirity)

///LVD/
//**********************************************************************
ui8 STC_GetLvdFlag()
{
  return GetBit(PCON,5); 
}
//End of STC_GetLvdFlag()

//**********************************************************************
void STC_ClearLvdFlag()
{
  PCON &= ~(0X01 << 5);
}
//End of STC_ClearLvdFlag()

//**********************************************************************
void STC_IntLvdInit(BOOL intEnable, BOOL proirity)
{
  STC_ClearLvdFlag();
  if(intEnable)
	{
		EA = 1;
		ELVD = 1;
	}
	else
		ELVD = 0;
	if(proirity)
		PLVD = 1;
	else
		PLVD = 0;
}  
//End of 	STC_IntLvdInit(BOOL intEnable, BOOL proirity)

///SPI///
//**********************************************************************
ui8 STC_GetSPIFlag()
{
  return GetBit(SPSTAT,7); 
}
//End of STC_GetSPIFlag()

//**********************************************************************
void STC_ClearSPIFlag()
{
  SPSTAT &= ~0x80;
}
//End of STC_ClearSPIFlag()

//***********************************************************************
void STC_IntSPIInit(BOOL intEnable, BOOL proirity)
{
  ui8 tem = IE2;
  STC_ClearSPIFlag();
  if(intEnable)
  {
    EA = 1;
    SetBit(&tem, 1);
  }
  else
    ResetBit(&tem, 1);
  IE2 = tem;
  tem = IP2;
  if(proirity)
    SetBit(&tem, 1);
  else
    ResetBit(&tem, 1);
  IP2 = tem;
}
//End of STC_IntSPIInit(BOOL intEnable, BOOL proirity)

PCA
//**********************************************************************
ui8 STC_GetPcaFlag()
{
  return CF;
}
//End of STC_GetPcaFlag()

//**********************************************************************
ui8 STC_GetPCA0Flag()
{
  return CCF0;
}
//End of STC_GetPCA0Flag()

//**********************************************************************
ui8 STC_GetPCA1Flag()
{
  return CCF1;
}
//End of STC_GetPCA1Flag()

//**********************************************************************
ui8 STC_GetPCA2Flag()
{
  return CCF2;
}
//End of STC_GetPCA2Flag()


//**********************************************************************
void STC_ClearPcaFlag()
{
  CCON &= (0x01 << 6);
}
//End of STC_ClearPcaFlag()

//**********************************************************************
void STC_IntPcaInit(BOOL intEnable, PCA_INTSOURCE msource, BOOL priority)
{
  switch(msource)
  {
    case 0x80:
      if(intEnable)
      { 
        EA = 1;
        CMOD |= 0x01;
      }
      else
        CMOD &= 0xFE;
      break;
    case 0x04:
      if(intEnable)
      { 
        EA = 1;
        CCAPM2 |= 0x01;
      }
      else
        CCAPM2 &= 0xFE;
      break;
    case 0x02:
      if(intEnable)
      { 
        EA = 1;
        CCAPM1 |= 0x01;
      }
      else
        CCAPM1 &= 0xFE;
      break;
    case 0x01:
      if(intEnable)
      { 
        EA = 1;
        CCAPM0 |= 0x01;
      }
      else
        CCAPM0 &= 0xFE;
      break;
  }
  if(priority)
    PPCA = 1;
  else
    PPCA = 0;
}
//End of STC_IntPcaInit(BOOL intEnable, PCA_INTSOURCE msource, BOOL priority)

/Comprator/
//**********************************************************************
void STC_ClearCmpFlag()
{
  CMPCR1 &= ~(0x01 << 6);
}
//End of STC_ClearCmpFlag()

//**********************************************************************
void STC_IntCmpInit(BOOL nEnable, BOOL pEnable)
{
  STC_ClearCmpFlag();
  if(nEnable)
    CMPCR1 |= 0x01 << 4;
  else
    CMPCR1 |= ~(0x01 << 4);
  if(pEnable)
      CMPCR1 |= 0x01 << 5;
  else
    CMPCR1 |= ~(0x01 << 5); 
}
//End of STC_IntCmpInit(BOOL nEnable, BOOL pEnable)

///PWM FD
//**********************************************************************
void STC_PwmFdClearFlag()
{
  PWMFDCR &= 0xFE;
}
//End of STC_PwmFdClearFlag()

//**********************************************************************
void STC_IntPwmFdInit(BOOL intEnable, BOOL priority)
{
  if(intEnable)
  { 
    EA = 1;
    PWMFDCR |= 0x01 << 3;
  }
  else
    PWMFDCR &= ~(0x01 << 3);
  if(priority)
    IP2 |= 0x01 << 3;
  else
    IP2 &= ~(0x01 << 3);
}
//End of STC_IntPwmFdInit(BOOL intEnable, BOOL priority)

//PWM

//**********************************************************************
void STC_PwmClearFlag()
{
   PWMIF = 0x00;
}
//End of STC_PwmClearFlag()

//**********************************************************************
void STC_IntPwmInit(BOOL cResetIntEnable, BOOL priority)
{
    PWMIF = 0x00;
    if(cResetIntEnable) //PWM counter reset INT enable
    {
      EA = 1;
      PWMCR |= 0x01 << 6;
    }
    else
      PWMCR &= ~(0x01 << 6);
    if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPwmInit(BOOL cResetIntEnable, BOOL priority)

//**********************************************************************
void STC_IntPWM2Init(BOOL intT1, BOOL intT2, BOOL priority)
{
  P_SW2 |= 0x80;
  if(intT1&&intT2)
  {
    EA = 1;
    PWM2CR |=  0x01 << 2;
  }
  if(intT1)
    PWM2CR |= 0x01;
  else
    PWM2CR &= 0xFE;
  if(intT2)
    PWM2CR |= 0x02;
  else
    PWM2CR &= ~0x02;
  P_SW2 &= ~0x80;
  if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPWM2Init(BOOL intT1, BOOL intT2, BOOL priority)

//**********************************************************************
void STC_IntPWM3Init(BOOL intT1, BOOL intT2, BOOL priority)
{
  P_SW2 |= 0x80;
  if(intT1&&intT2)
  {
    EA = 1;
    PWM3CR |=  0x01 << 2;
  }
  if(intT1)
    PWM3CR |= 0x01;
  else
    PWM3CR &= 0xFE;
  if(intT2)
    PWM3CR |= 0x02;
  else
    PWM3CR &= ~0x02;
  P_SW2 &= ~0x80;
  if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPWM3Init(BOOL intT1, BOOL intT2, BOOL priority)

//**********************************************************************
void STC_IntPWM4Init(BOOL intT1, BOOL intT2, BOOL priority)
{
  P_SW2 |= 0x80;
  if(intT1&&intT2)
  {
    EA = 1;
    PWM4CR |=  0x01 << 2;
  }
  if(intT1)
    PWM4CR |= 0x01;
  else
    PWM4CR &= 0xFE;
  if(intT2)
    PWM4CR |= 0x02;
  else
    PWM4CR &= ~0x02;
  P_SW2 &= ~0x80;
  if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPWM4Init(BOOL intT1, BOOL intT2, BOOL priority)

//**********************************************************************
void STC_IntPWM5Init(BOOL intT1, BOOL intT2, BOOL priority)
{
  P_SW2 |= 0x80;
  if(intT1&&intT2)
  {
    EA = 1;
    PWM5CR |=  0x01 << 2;
  }
  if(intT1)
    PWM5CR |= 0x01;
  else
    PWM5CR &= 0xFE;
  if(intT2)
    PWM5CR |= 0x02;
  else
    PWM5CR &= ~0x02;
  P_SW2 &= ~0x80;
  if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPWM5Init(BOOL intT1, BOOL intT2, BOOL priority)

//**********************************************************************
void STC_IntPWM6Init(BOOL intT1, BOOL intT2, BOOL priority)
{
  P_SW2 |= 0x80;
  if(intT1&&intT2)
  {
    EA = 1;
    PWM6CR |=  0x01 << 2;
  }
  if(intT1)
    PWM6CR |= 0x01;
  else
    PWM6CR &= 0xFE;
  if(intT2)
    PWM6CR |= 0x02;
  else
    PWM6CR &= ~0x02;
  P_SW2 &= ~0x80;
  if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPWM6Init(BOOL intT1, BOOL intT2, BOOL priority)

//**********************************************************************
void STC_IntPWM7Init(BOOL intT1, BOOL intT2, BOOL priority)
{
  P_SW2 |= 0x80;
  if(intT1&&intT2)
  {
    EA = 1;
    PWM7CR |=  0x01 << 2;
  }
  if(intT1)
    PWM7CR |= 0x01;
  else
    PWM7CR &= 0xFE;
  if(intT2)
    PWM7CR |= 0x02;
  else
    PWM7CR &= ~0x02;
  P_SW2 &= ~0x80;
  if(priority)
      IP2 |= 0x01 << 2;
  else
    IP2 &= ~(0x01 << 2);
}
//End of STC_IntPWM7Init(BOOL intT1, BOOL intT2, BOOL priority)

该函数库源码已上传到CSDN,文件名为STC15W INT Library Code.rar。如可需要线下查看,可搜索下载。

这篇关于51 单片机实战教程(9 中断库函数)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!