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

📄 f34x_init.#3

📁 用c8051f340基于51单片机上网
💻 #3
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// F34x_Init.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
// 
// Contains Initialization Routines for the F340.
//
// FID:            
// Target:         C8051F34x
// Tool chain:     Keil C51 7.20 / Keil EVAL C51
//                 Silicon Laboratories IDE version 2.72 
// Command Line:   See Readme.txt
// Project Name:   CP220x_Ethernet_Routines
//
// 
//
// Release 1.1
//    - Configures C8051F120 SYSCLK to 98 MHz
//
// Release 1.0
//    -Initial Release (FB)
//    -30 MAY 2006
//

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "global.h"

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------
sfr16 TMR2RL   = 0xca;                    // Timer2 reload value
sfr16 TMR2     = 0xcc;                    // Timer2 counter
sfr16 TMR3RL   = 0x92;                    // Timer3 reload value
sfr16 TMR3     = 0x94;                    // Timer3 counter

//-----------------------------------------------------------------------------
// Local Constants
//-----------------------------------------------------------------------------
#define SYSCLK  48000000

sbit AB4_RST = P1^0;

//-----------------------------------------------------------------------------
// Local Global Variables 
//-----------------------------------------------------------------------------

static unsigned int timeout;

//-----------------------------------------------------------------------------
// Exported Function Prototypes
//-----------------------------------------------------------------------------
void Reset_Init(void);
void SYSCLK_Init (void);

void wait_ms(int ms);  
void init_timer3(void);
void CP220x_RST_Low(void);
void CP220x_RST_High(void);
extern UINT volatile event_word;
extern ULONG idata initial_sequence_nr;

#if(UART_ENABLED)
char _getkey ();
char putchar (char c);
#endif

//-----------------------------------------------------------------------------
// Local Function Prototypes
//-----------------------------------------------------------------------------

void PORT_Init (void);

void EMIF_Init (void);

#if(UART_ENABLED)
void UART0_Init (void);
#endif

//-----------------------------------------------------------------------------
// Exported Functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Reset_Init
//-----------------------------------------------------------------------------
void Reset_Init(void)
{
   
   // Disable Watchdog timer
   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                       // enable)
   // Initialize the MCU
   PORT_Init();
   SYSCLK_Init();   
   EMIF_Init();
init_timer3();


   EX0 = 1;                            // Enable External Interrupt 0   

   #if(UART_ENABLED)
   UART0_Init();
   puts("\n*** Reset ***\nC8051F34x MCU Initialized\n");
   #endif

}

//--------------------------------------------------------------------------
// Timer 3 interrupt service routing. Intr vector location is
// address 0x002B. The timer generates an interrupt every 25 msec
// It is set to auto reload so do not need to reload it.
//--------------------------------------------------------------------------
void timer3_interrupt(void) interrupt 14
{
	static UINT count1 = 0;
	static UINT count2 = 0;
	
	TMR3CN |= 0x80;		// Clear interrupt flag

	
	// Advance the initial sequence number
	initial_sequence_nr += 6250;
   // Keep it some distance from rolling over
   if (initial_sequence_nr & 0x10000000L) initial_sequence_nr = 1;
	
	count1++;
	// These events happens every 0.50 seconds, not simultaneously
	if (count1 == 5) event_word |= EVENT_ARP_RETRANSMIT;
	
	if (count1 == 10) event_word |= EVENT_TCP_INACTIVITY;
	
	if (count1 == 15) event_word |= EVENT_READ_ANALOG;
	
	if (count1 == 20)
	{
	 	count1 = 0;
	 	event_word |= EVENT_TCP_RETRANSMIT;
	}
	
	count2++;
	if (count2 == 2401)
	{
		count2 = 0;
		event_word |= EVENT_AGE_ARP_CACHE;
	}
}





//--------------------------------------------------------------------------
// 将定时器2设置为模式1自动重载
// rate = 4 MHz /(12 * (65536 - reload value))
//  25 毫秒 reload value = 19456 = 0x4C00
//--------------------------------------------------------------------------
void init_timer3(void)
{
    TMR3CN  = 0x00;                     // Stop Timer3; Clear TF3;
                                       // use SYSCLK/12 as timebase
   CKCON  &= ~0xC0;                    // Timer2 clocked based on T2XCLK;

   // Initialize Reload Value
   TMR3RL = -(SYSCLK/12/40);  
   TMR3 = TMR3RL;
    EIE1 |= 0x80;
   TMR3CN  |= 0x04;                    // Start Timer 3 (TR3 = 1)       
}

//-----------------------------------------------------------------------------
// External Interrupt 0
//-----------------------------------------------------------------------------
//
// This interrupt service routine is routed to the ethernet interrupt
//
void INT0_ISR(void) interrupt 0
{
   Ethernet_ISR();
}



//-----------------------------------------------------------------------------
// wait_ms
//-----------------------------------------------------------------------------
//
// This routine inserts a delay of <ms> milliseconds.
//
void wait_ms(int ms)
{
   TMR2CN  = 0x00;                     // Stop Timer2; Clear TF2;
                                       // use SYSCLK/12 as timebase
   CKCON  &= ~0x30;                    // Timer2 clocked based on T2XCLK;

   TMR2RL = -(SYSCLK/1000/12);         // Timer 2 overflows at 1 kHz
   TMR2 = TMR2RL;

   ET2 = 0;                            // Disable Timer 2 interrupts

   TR2 = 1;                            // Start Timer 2

   while(ms){
      TF2H = 0;
      while(!TF2H);                    // wait until timer overflows
      ms--;                            // decrement ms
   }

   TR2 = 0;                            // Stop Timer 2

}


//-----------------------------------------------------------------------------
// reset_timout
//-----------------------------------------------------------------------------
//
// This routine resets the global timeout.
//
void reset_timeout(unsigned int ms)
{
   timeout = ms;
   start_ms_timer();
      
}


//-----------------------------------------------------------------------------
// timeout_expired
//-----------------------------------------------------------------------------
//
// This routine manages the timeout and returns TRUE if timeout expired.
//
unsigned char timeout_expired(void)
{
   if(get_ms_timer_flag()){
      clear_ms_timer_flag();
      if(timeout > 0){
         timeout--;
      }
   }
   
   return (timeout == 0);
     
   
}


//-----------------------------------------------------------------------------
// start_ms_timer
//-----------------------------------------------------------------------------
//
// This routine starts a timer with a 1kHz overflow rate (1ms period).
//
void start_ms_timer()
{

   TMR2CN = 0x00;                      // Stop Timer2; Clear TF2;
                                       // use SYSCLK/12 as timebase
   CKCON  &= ~0x30;                    // Timer2 clocked based on T2XCLK;
   
   TMR2RL = -(SYSCLK/1000/12);         // Timer 2 overflows at 1 kHz
   TMR2 = TMR2RL;

   ET2 = 0;                            // Disable Timer 2 interrupts

   TR2 = 1;                            // Start Timer 2

⌨️ 快捷键说明

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