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

📄 system.c

📁 菜鸟,详细NRF24E1运用,程序,电路
💻 C
字号:


#include "stdint.h"
#include "system.h"

volatile unsigned char Udata;

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void system_init(void)
{ 
	EA = 0;

	// CLK
	CLKCTRL = 0x28;			// Clock sourced by XCOSC16M  	
	CLKLFCTRL = 0x01; 		// RCOSC32K 
	
	Timer_Ms(50);	

	// RTC		
	OPMCON = 0x00; 

   	RTC2CMP0 = 0x00;
	RTC2CMP1 = 0x80; 
								  
	RTC2CON = 0x07;						

	WUOPC1 = 0x00;
	WUOPC0 = 0x00;

	WUCON = 0xEB;


	// TIMER
	TMOD |= 0x01;                     // Timer0: Mode 0, 13-bit timer
  	TF0 = 0;                          // Timer0: interrupt flag clear
  	ET0 = 1;                          // Timer0: interrupt enable
  	TCON = 0;                         // Timer0: run control
  	TR0 = 0;                          // Timer0: stop timer 
	
	TH0 = 0xFA;
	TL0 = 0xCA;	
	
	// IO
	ADCCON1 = 0x00;
	ADCCON2 = 0x00;
	ADCCON3 = 0x00;
	COMPCON = 0x00; 
	PWMCON = 0x00;

	P0 = 0; 
	P1 = 0;
	P0DIR = 0x00;
	P1DIR = 0x00; 
 
	Uart_Init();	
	
	// RF
	SPIRCON1 = 0x0B;   
  	RFCKEN = 1;                         // enable L01 clock		  	
  	RF = 0;	  
  
	// INTERRUPT
	WUIRQ = 0;                        	// wakeup int enabled 	   	
  	EA = 1;                           	// global interrupt enable	  
	
}


//---------------------------------------------------------------------------
void Uart_Init(void)
{
		// UART	
	P0DIR &= 0xF7;				// P03 (TxD) is output
  	P0DIR |= 0x10;     			// P04 (RxD) is input 
	P0|=0x18;	 
		
	S0CON = 0x50;  
   	PCON |= 0x80; 				// SMOD = 1
  	WDCON |= 0x80;   			// Select internal baud rate generator 
	
	//S0RELL = 0xFB; 	
  	S0RELL = 0xF3; 				// BAUD_38K4
  	S0RELH = 0x03;	
	//P0CON = 0x13;
	//P0CON = 0x44;	 	  	 
  	ES0 = 1;          			// Enable UART0 interrupt
}

//---------------------------------------------------------------------------
//
// 等待指定的微秒数
//
//---------------------------------------------------------------------------
void Timer_Us(void)
{ 
	_nop_();_nop_();_nop_();_nop_();
	_nop_();_nop_();_nop_();_nop_();
	_nop_();_nop_();_nop_();_nop_();

	_nop_();_nop_();_nop_();_nop_();
	_nop_();_nop_();_nop_();

}

//---------------------------------------------------------------------------
void Timer_10Us(unsigned char nTime)
{ 	
	while((nTime--)!=0) Timer_Us();	
}


//---------------------------------------------------------------------------
//
// 等待指定的毫秒数
//
//---------------------------------------------------------------------------

void Timer_Ms(int nTime)
{ 
	int j;
	while((nTime--)!=0) 
	for( j = 0; j < 350; j++);	
}
//---------------------------------------------------------------------------


void hal_wdog_init(uint16_t start_value)
{
  	WDSV = LSB(start_value);      // Write the 8 LSB to the WD counter
  	WDSV = MSB(start_value);      // Write the 8 MSB to the WD counter
}

//---------------------------------------------------------------------------
void hal_wdog_restart(void)
{
  	uint8_t wd_msb, wd_lsb;

  	wd_lsb = WDSV;
  	wd_msb = WDSV;

  	WDSV = wd_lsb;           // Write the 8 LSB to the WD counter
  	WDSV = wd_msb;           // Write the 8 MSB to the WD counter
}

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
static void ifp_interrupt(void) interrupt INTERRUPT_IFP { } 
//--------------------------------------------------------------------------- 
static void tf0_interrupt(void) interrupt INTERRUPT_TF0 
{
	// 时钟中断函数(1ms发生一次)	
	TH0 = 0xFA;
	TL0 = 0xCA;	
}
//---------------------------------------------------------------------------
static void pofirq_interrupt(void) interrupt INTERRUPT_POFIRQ {} 
//---------------------------------------------------------------------------
static void tf1_interrupt(void) interrupt INTERRUPT_TF1  {} 
//---------------------------------------------------------------------------
static void tf2_interrupt(void) interrupt INTERRUPT_TF2 {}  
//---------------------------------------------------------------------------  
static void msdone_interrupt(void) interrupt INTERRUPT_MSDONE	  {}  
//---------------------------------------------------------------------------
static void wuopirq_interrupt(void) interrupt INTERRUPT_WUOPIRQ	{ } 
//---------------------------------------------------------------------------
static void miscirq_interrupt(void) interrupt INTERRUPT_MISCIRQ	  {} 
//---------------------------------------------------------------------------
static void tick_interrupt(void) interrupt INTERRUPT_TICK{} 
//--------------------------------------------------------------------------- 

static void ri0_interrupt(void) interrupt INTERRUPT_UART	 
{
	if (RI0 == 1)
  	{     	
		Udata = S0BUF;		 
		S0BUF = Udata;	
		
		RI0 = 0;					
  	}
	else if(TI0 == 1)
	{
		TI0 = 0;  					
	}
} 
//---------------------------------------------------------------------------


⌨️ 快捷键说明

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