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

📄 f34x_init.#1

📁 用c8051f340基于51单片机上网
💻 #1
📖 第 1 页 / 共 2 页
字号:

}

//-----------------------------------------------------------------------------
// get_ms_timer_flag()
//-----------------------------------------------------------------------------
//
// This routine returns the state of the ms_timer overflow flag.
//
char get_ms_timer_flag(void)
{
   return TF2H;
}

//-----------------------------------------------------------------------------
// clear_ms_timer_flag()
//-----------------------------------------------------------------------------
//
// This routine returns the state of the ms_timer overflow flag.
//
void clear_ms_timer_flag(void)
{
   TF2H = 0;
}

//-----------------------------------------------------------------------------
// CP220x_RST_Low
//-----------------------------------------------------------------------------
//
// Drives the CP220x's Reset Pin Low.
//
void CP220x_RST_Low(void)
{
   AB4_RST = 0;                        // Set P1.0 Low
}

//-----------------------------------------------------------------------------
// CP220x_RST_High
//-----------------------------------------------------------------------------
//
// Allows the CP220x's Reset Pin to be pulled up.
//
// Waits until the reset pin rises.
//
void CP220x_RST_High(void)
{
   AB4_RST = 1;                        // Set P1.0 High

   while(!AB4_RST);
}


//-----------------------------------------------------------------------------
// AB4_RST_State
//-----------------------------------------------------------------------------
//
// Returns the state of the AB4's reset pin.
//
unsigned char AB4_RST_State(void)
{
   char rst_pin_state;
   
   rst_pin_state = AB4_RST;          // Get P4.5 State
   
   return rst_pin_state;
}
//-----------------------------------------------------------------------------
// Local Initialization Routines
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// 
// P0.3 - /INT
// P0.4 - UART TX
// P0.5 - UART RX
// 
// P1.0 - AB4-/RST 
// P1.1 - AB4-SW1
// P1.2 - AB4-SW2
// P1.3 - AB4-LED1
// P1.4 - AB4-LED2
// P1.5 -
// P1.6 - /RD
// P1.7 - /WR
//
// P2.2 - LED - shared with address line
//
// P2 - Address High
// P3 - Address Low
// P4 - Data Bus
//

void PORT_Init (void)
{

   IT01CF = 0x03;                      // Enable Interrupt 0 on P0.3
   TCON &= ~0x01;                      // Make /INT0 level triggered

   XBR0    = 0x01;                     // Enable UART on P0.4(TX) and P0.5(RX)
   XBR1    = 0x40;                     // Enable Crossbar and weak pull-ups

   P0MDOUT |= 0x10;                    // Enable TX0 as push-pull output
   P1MDOUT |= 0xC0;                    // /WR and /RD are push-pull
   P1MDOUT |= 0x18;                    // AB4 LEDs are push-pull
   P1MDOUT &= ~0x01;                   // /RST is open-drain
      
   // Configure External Memory Interface Pins to push-pull except for
   // P2.0 and P2.1 which are used for target board switches.
   P2MDOUT |= 0xFC;                    // ADDR[15:8]
   P3MDOUT |= 0xFF;                    // ADDR[7:0]
   P4MDOUT |= 0xFF;                    // DATA[7:0]
   
   // Set initial values of External Memory Interface Pins
   P1 = 0xFE;                          // /WR, /RD, are high, RESET is low
   P1 &= ~0x18;                        // LEDs off
   P2 = 0xFF;                          // ADDR[15:8] initially high 
   P3 = 0xFF;                          // ADDR[7:0] initially high
   P4 = 0xFF;                          // DATA[7:0] intially high
}


//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// multiplied by 4 as the system clock. The resulting frequency is 48 MHz.
// Also enables the Missing Clock Detector and VDD monitor.
//

void SYSCLK_Init (void)
{
   int i;

   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency
  
   CLKMUL = 0x00;                      // Reset Clock Multiplier and select
                                       // internal oscillator as input source

   CLKMUL |= 0x80;                     // Enable the Clock Multiplier

   for(i = 0; i < 256; i++);           // Delay at least 5us
   
   CLKMUL |= 0xC0;                     // Initialize the Clock Multiplier
   
   while(!(CLKMUL & 0x20));            // Wait for MULRDY => 1
   
   RSTSRC = 0x06;                      // Enable missing clock detector
                                       // and VDD monitor
   
   FLSCL |= 0x10;                      // Set Flash Scale for 48MHz
   
   CLKSEL |= 0x03;                     // Select output of clock multiplier
                                       // as the system clock.

}

//-----------------------------------------------------------------------------
// EMIF_Init
//-----------------------------------------------------------------------------
//
// Configure the external memory interface to use upper port pins in 
// non-multiplexed mode to a mixed on-chip/off-chip configuration without 
// Bank Select.
//
void EMIF_Init (void)
{

   EMI0CF = 0x1B;                      // non-muxed mode; split mode 
                                       // with bank selec

   EMI0TC = 0xFF;                      // slowest timing (4-cycle MOVX)

   EMI0CN = 0x20;                      // Off-chip
}

//===============
// CONDITIONAL
//===============
#if(UART_ENABLED)

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void UART0_Init (void)
{
   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        ninth bits are zeros
                                       //        clear RI0 and TI0 bits
   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
      CKCON |=  0x09;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   }

   TL1 = TH1;                          // init Timer1
   TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
   TMOD |=  0x20;
   TR1 = 1;                            // START Timer1
   TI0 = 1;                            // Indicate TX0 ready
}

//-----------------------------------------------------------------------------
// _getkey
//-----------------------------------------------------------------------------
//
// SFR Paged version of _getkey
//
char _getkey ()  {

   char c;
  
   while (!RI0);
   c = SBUF0;
   
   RI0 = 0;
   return (c);
}

//-----------------------------------------------------------------------------
// putchar
//-----------------------------------------------------------------------------
//
// SFR Paged version of putchar
//
char putchar (char c)  {

   // output CR  
   if (c == '\n')  {
      while (!TI0);
      TI0 = 0;
      SBUF0 = 0x0d;                         
   }
   
   // output character
   while (!TI0);
   TI0 = 0;
   SBUF0 = c;   
   
   return (c);
}

#endif // UART_ENABLED


//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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