⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 新华龙单片机C8051F060的定时器程序
💻 C
字号:
#include <c8051f060.h>                    // SFR declarations

#define SYSCLK 22118400                   // SYSCLK frequency in Hz
#define SAMP_RATE    2000

sfr16 RCAP3    = 0xCA;                 // Timer3 reload value
sfr16 TMR3     = 0xCC;                 // Timer3 counter
sbit a = P0^0;

void SYSCLK_Init (void);
void PORT_Init (void);
void Timer3_Init (int counts);
void Timer3_ISR(void); 
int flag=0; 
void main (void)
{
    WDTCN = 0xde;                          // disable watchdog timer
	WDTCN = 0xad;

	SYSCLK_Init();                        // initialize SYSCLK
	PORT_Init();
    Timer3_Init(SYSCLK/SAMP_RATE); 
	a=0;
	EA=1;        //开总中断
	while(1)
	{
       if(flag==1)
	   {
		  a= !a;
		  flag=0;
	   }
	};                              // Done.
}

void PORT_Init (void)
{
   char old_SFRPAGE = SFRPAGE;
   SFRPAGE = CONFIG_PAGE;              // Switch to configuration page
   XBR2    = 0x40;                     // Enable crossbar and weak pull-ups
   P0MDOUT |= 0xFF;                    // enable Port0 outputs as push-pull
   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}
  
void SYSCLK_Init (void)
{
   char old_SFRPAGE = SFRPAGE;
   int i; 
   SFRPAGE = CONFIG_PAGE;              // Switch to Configuration Page
   OSCXCN = 0x67;                     
   for (i=0; i <5000; i++) ;           // XTLVLD blanking interval (>1ms)
   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
   RSTSRC = 0x04;                      // enable missing clock detector reset
   CLKSEL = 0x01;                      // change to external crystal
   OSCICN = 0x00;                      // disable internal oscillator
   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}

void Timer3_Init (int counts)  //中断时间为1ms
{
   char old_SFRPAGE = SFRPAGE;
   SFRPAGE = TMR3_PAGE;                // Switch to Timer 3 page

   TMR3CN = 0x00;                      // 停止T3,清TF3中断标志
   TMR3CF = 0x08;                      // 使用系统时钟
   RCAP3   = -counts;                  // T3赋初值
   TMR3    = 0xffff;                   // 立即重载
   EIE2 |= 0x01;                       // 开T3中断
   TR3 = 1;                            // 启动T3

   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}

void Timer3_ISR(void) interrupt 14 
{
	TMR3CN &= ~(0x80);	   //清TF3中断标志
    flag=1;
	//a= !a;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -