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

📄 initializes.c

📁 全国电子设计大赛:智能电动车的设计.实现功能:以AT89C52单片机为核心
💻 C
📖 第 1 页 / 共 2 页
字号:
//*************************************************************************************************
//  Module Name :  Initializes.C
//  CreateDate  :  2005-01-09
//  ModifData   :  2005-05-04
//  Description :  Initializes Program For C8051F022
//  Author      :  李远正
//  Version     :  V1.0  
//*************************************************************************************************

#include <C8051F020.H>
#include "Initializes.H"

//-------------------------------------------------------------------------------------------------
// Initialization Subroutines
//-------------------------------------------------------------------------------------------------

//=================================================================================================
// SYSCLK_Init
//=================================================================================================
//
// This routine initializes the system clock to use an 11.0592MHz crystal
// as its clock source.
// 
void SYSCLK_Init( void )
{
   	int i;                              // delay counter

   	OSCXCN = 0x67;                      // start external oscillator with
                                       	// 11.0592MHz crystal

   	for (i=0; i < 256; i++) ;           // wait for XTLVLD to stabilize

   	while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   	OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       	// source and enable missing clock
                                      	// detector
}

//=================================================================================================
// PORT_Init
//=================================================================================================
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init( void )
{	
// Disable Watchdog timer
	WDTCN = 0xDE;
	WDTCN = 0xAD;

// Watchdog timer
//	WDTCN  	= 0x07;		// 设顶喂狗最大时间间隔为94.8mS
//	WDTCN	= 0xA5;		// 允许看门狗工作;

//-------------------------------------------------------------------------------------------------
// XRAM
//	EMI0CN  = 0x00;     // ASM 编程时需要详解, EMI高8位地址值  
	EMI0CF	= 0x2A;		// EMIF在P4~P7,工作于复用方式,接口工作于模式3,ALE为 3 个SYSCLK
						// EMIF工作于模式3,带块选择的分片方式,
						// 0x0000~0x0fff为内部XRAM,0x1000~0xffff为外部XRAM
	EMI0TC  = 0xD7;     // EAS:11     EMIF 地址建立时间:   3 个 SYSCLK 周期
	                    // EWR:0101   /WR 和 /RD 脉冲宽度: 6 个 SYSCLK 周期
						// EAH:11     EMIF 地址保存时间:   3 个 SYSCLK 周期
	
//-------------------------------------------------------------------------------------------------
// PORT Configure 
    XBR2    = 0x40;     // 交叉开关允许

// PORT 0
    XBR0   |= 0x04;    	// 打开 UART0 : P0.0---TX0, P0.1---RX0
	XBR0   |= 0x02;		// 打开 SPI口 : P0.2---SCK, P0.3---MISO, P0.4---MOSI, P0.5---NSS
	XBR0   |= 0x01;		// 打开 IIC口 : P0.6---SDA, P0.7---SCL (SMBus)

// PORT 1
	XBR2   |= 0x04;		// 打开 UART1 : P1.0---TXD1, P1.1---RXD1
    XBR0   |= 0x28;		// 打开 PCA   : P1.2---CEX0, P1.3---CEX1, P1.4---CEX2, P1.5---CEX3, P1.6---CEX4 
	XBR1   |= 0x02;     // 打开 T0    : P1.7---T0

// PORT 2
	XBR1   |= 0x04;     // 打开 INT0  : P2.0---INT0
	XBR1   |= 0x08;     // 打开 T1    : P2.1---T1
	XBR1   |= 0x10;     // 打开 INT1  : P2.2---INT1

//-------------------------------------------------------------------------------------------------	
// 
    P74OUT |= 0x30;     // P6 (A8--A15) 输出引脚 

  	P0MDOUT|= 0x01;     // 设置 TX0 为推挽输出(否则将强制为输入方式)
	P1MDOUT|= 0x01;     // 设置 TX1 为推挽输出(否则将强制为输入方式)

	P4  &= 0xFE;		// 485 Recieve 
	P4  &= 0xF7;        // LED0 Disable 
}

//=================================================================================================
// Function_Init
//=================================================================================================
// 
void FUNCTION_Init(void)
{
//------------------------------------------------------------------------------------
// VOLTAGE REFERENCE
//------------------------------------------------------------------------------------
    REF0CN = 0x07;      // Temperature Sensor Enable, ADC/DAC Bias Generator Enable
	                    // Voltage reference from VREF0 pin.
						// 
	                    // Bit4: AD0VRS: ADC0 Voltage Reference Select
						//            0: ADC0 voltage reference from VREF0 pin.
						//            1: ADC0 voltage reference from DAC0 output.
						// 
						// Bit3: AD1VRS: ADC1 Voltage Reference Select
						// Bit2: TEMPE: Temperature Sensor Enable Bit.
						// Bit1: BIASE: ADC/DAC Bias Generator Enable Bit. (Must be ‘1’ if using ADC or DAC).
						// Bit0: REFBE: Internal Reference Buffer Enable Bit.
// 

//------------------------------------------------------------------------------------
// DAC0_Init
//------------------------------------------------------------------------------------
// Configure DAC0 to DAC output updates occur on a write to DAC0H.
// 
//
    DAC0CN = 0x80;      // DAC0CN: DAC0 Enable, Output updates occur on a write to DAC0H.
	                    // 
	                    // Bit   7: DAC0EN   : DAC0 Enable Bit.
	                    // Bits4-3: DAC0MD1-0: DAC0 Mode Bits.
						//          00: DAC output updates occur on a write to DAC0H.
						//          01: DAC output updates occur on Timer 3 overflow.
						//          10: DAC output updates occur on Timer 4 overflow.
						//          11: DAC output updates occur on Timer 2 overflow.
						// Bits2-0: DAC0DF2-0: DAC0 Data Format Bits:
						//          000: The most significant nibble of the DAC0 Data Word is in DAC0H[3:0], 
						//               while the least significant byte is in DAC0L.
						//          001: The most significant 5-bits of the DAC0 Data Word is in DAC0H[4:0], 
						//               while the least significant 7-bits are in DAC0L[7:1].
						//          010: The most significant 6-bits of the DAC0 Data Word is in DAC0H[5:0], 
						//               while the least significant 6-bits are in DAC0L[7:2].
						//          011: The most significant 7-bits of the DAC0 Data Word is in DAC0H[6:0], 
						//               while the least significant 5-bits are in DAC0L[7:3].
						//          1xx: The most significant 8-bits of the DAC0 Data Word is in DAC0H[7:0], 
						//               while the least significant 4-bits are in DAC0L[7:4].

//------------------------------------------------------------------------------------
// DAC1_Init
//------------------------------------------------------------------------------------
// Configure DAC1 to DAC output updates occur on a write to DAC1H.
// 
//
    DAC1CN = 0x80;      // DAC1CN: DAC1 Enable, Output updates occur on a write to DAC1H.
	                    // 
	                    // Bit   7: DAC1EN   : DAC1 Enable Bit.
	                    // Bits4-3: DAC1MD1-0: DAC1 Mode Bits.
						//          00: DAC output updates occur on a write to DAC1H.
						//          01: DAC output updates occur on Timer 3 overflow.
						//          10: DAC output updates occur on Timer 4 overflow.
						//          11: DAC output updates occur on Timer 2 overflow.
						// Bits2-0: DAC1DF2-0: DAC1 Data Format Bits:
						//          000: The most significant nibble of the DAC1 Data Word is in DAC1H[3:0], 
						//               while the least significant byte is in DAC1L.
						//          001: The most significant 5-bits of the DAC1 Data Word is in DAC1H[4:0], 
						//               while the least significant 7-bits are in DAC1L[7:1].
						//          010: The most significant 6-bits of the DAC1 Data Word is in DAC1H[5:0], 
						//               while the least significant 6-bits are in DAC1L[7:2].

⌨️ 快捷键说明

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