📄 f020config.c
字号:
#include <C8051F020.h> // Register definition file.
void config (void)
{
int i = 0;
/****************交义开关配置**********************************/
//XBR0
//Bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
//CP0E ECI0E PCA0ME UART0EN SPI0EN SMB0EN
XBR0 = 0x07; // UART0EN SPI0EN SMB0EN
//XBR1
//Bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
//SYSCKE T2EXE T2E INT1E T1E INT0E T0E CP1E
XBR1 = 0x00; // XBAR1: Initial Reset Value
//XBR2
//Bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
//WEAKPUD XRARE - T4EXE T4E UART1E EMIFLE CNVST0E
XBR2 = 0x40; // XRARE=1 交义开关允许
/*****************************************************************/
/***************端口输入输出方式配置****************************/
P0MDOUT = 0x1c; //SDA SCL RXD0 RXD1必需为漏极开路输出
P1MDIN = 0x00; //ADC1口,所以配置为模拟输入
P1MDOUT = 0x00;
P2MDOUT = 0x00;
P3MDOUT = 0xFF;//P3.0为推挽输出
P74OUT = 0xFF;//高端口为推挽方式
/***************************************************************/
/**************外部存储器配置**********************************/
EMI0CF=0x3f; //外部存储器接口配置
EMI0TC=0xff; //外部存储器时序控制 //一次XRAM操作要22个sysclk
/***************************************************************/
/*************比较器配置***************************************/
CPT0CN = 0x00; // Comparator 0 Control Register
CPT1CN = 0x00; // Comparator 1 Control Register
/***************************************************************/
/******************振荡器配置***************************************/
OSCXCN = 0x67; // start external oscillator with 22.1184MHz crystal
for (i=0; i < 256; i++) ; // Wait for osc. to start up
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x08; // Disable the internal osc.
/***************************************************************/
/******************电压基准配置**********************************/
REF0CN = 0x03; // Reference Control Register
/***************************************************************/
/*******************ADC0配置*************************************/
AMX0CF = 0x00; // AMUX0 Configuration Register
AMX0SL = 0x00; // AMUX0 Channel Select Register
ADC0CF = 0x48; //ADC0 Configuration Register增益=1
ADC0CN = 0xC1; //ADC0使能,寄存器数据左对齐
//ADC0CN = 0x80; //ADC0使能,寄存器数据右对齐
ADC0L = 0x00; // ADC0 Data Word LSB
ADC0H = 0x00; // ADC0 Data Word MSB
//ADC0LTH = 0x00; // ADC0 Less-Than High Byte Register
//ADC0LTL = 0x00; // ADC0 Less-Than Low Byte Register
//ADC0GTH = 0xFF; // ADC0 Greater-Than High Byte Register
//ADC0GTL = 0xFF; // ADC0 Greater-Than Low Byte Register
/***************************************************************/
/*******************ADC1配置*************************************/
ADC1CF = 0x41; // ADC2 Configuration Register增益=1
AMX1SL = 0x00; // AMUX1 Chanel Select Register
ADC1CN = 0xc0; // ADC2 Control Register ADC2使能
ADC1 = 0x00; //ADC1 Data Word
/***************************************************************/
/*******************DA配置*************************************/
DAC0CN = 0x80; // 打开DAC0,右对齐模式
DAC0L = 0xFF; // DAC0 Low Byte Register
DAC0H = 0x0F; // DAC0 High Byte Register
DAC1CN = 0x80; // DAC1 Control Register
DAC1L = 0xff; // DAC1 Low Byte Register
DAC1H = 0x08; // DAC1 High Byte Register
/***************************************************************/
/*******************SPI配置*************************************/
SPI0CFG = 0xc7; // SCK周期的第2个边沿采数,SCK空闲为高电平,8位数据
SPI0CN = 0x03; //允许主方式,SPIO。BIT1=MSTEN,BIT0=SPIEN
SPI0CKR = 0x00; //Fsck=22118400/2(0+1)=22.1184Mhz
/***************************************************************/
/*******************SMBUS配置*************************************/
SMB0CN = 0x44; // SMBus Control Register //ENSMB=1,使能SMB AA=1,在应答周期内返回ACK。
SMB0ADR = 0x00; // SMBus Address Register
SMB0CR = 0xe0; // SMBus Clock Rate Register ////SCL速率设置 TLOW=1.44us
/***************************************************************/
/*******************UART0配置*************************************/
SADEN0 = 0x00; // Serial 0 Slave Address Enable
SADDR0 = 0x00; // Serial 0 Slave Address Register
SCON0 = 0x50; // Serial Port Control Register
PCON = 0x00; // Power Control Register
/***************************************************************/
/*******************UART1配置*************************************/
SCON1 = 0x00; // Serial Port 1 Control Register
/***************************************************************/
/*******************PCA配置*************************************/
PCA0MD = 0x00; // PCA Mode Register
PCA0CN = 0x00; // PCA Control Register
PCA0L = 0x00; // PCA Counter/Timer Low Byte
PCA0H = 0x00; // PCA Counter/Timer High Byte
//Module 0
PCA0CPM0 = 0x00; // PCA Capture/Compare Register 0
PCA0CPL0 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH0 = 0x00; // PCA Counter/Timer High Byte
//Module 1
PCA0CPM1 = 0x00; // PCA Capture/Compare Register 1
PCA0CPL1 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH1 = 0x00; // PCA Counter/Timer High Byte
//Module 2
PCA0CPM2 = 0x00; // PCA Capture/Compare Register 2
PCA0CPL2 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH2 = 0x00; // PCA Counter/Timer High Byte
//Module 3
PCA0CPM3 = 0x00; // PCA Capture/Compare Register 3
PCA0CPL3 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH3 = 0x00; // PCA Counter/Timer High Byte
//Module 4
PCA0CPM4 = 0x00; // PCA Capture/Compare Register 4
PCA0CPL4 = 0x00; // PCA Counter/Timer Low Byte
PCA0CPH4 = 0x00; // PCA Counter/Timer High Byte
/***************************************************************/
/*******************定时器配置*************************************/
CKCON = 0x00; // Clock Control Register sysclk/12
TH0 = 0x88; //0.065MS a3
TL0 = 0x88; // Timer 0 Low Byte 8次中断完成一次全通道AD采样,即一次AD时间为0.4MS F=2.5K
//TH0 = 0x82; // Timer 0 High Byte
TH1 = 0xFF; // Timer 1 High Byte
TL1 = 0xFF; // Timer 1 Low Byte
TMOD = 0x22; // Timer Mode Register
//TCON = 0x50; // Timer Control Register TR0=1;TR1=1;
TCON = 0x40; // Timer Control Register TR0=0;TR1=1;
T2CON = 0x00; // Timer 2 Configuration
RCAP2L = 0x00; // Timer 2 Reload Register Low Byte
RCAP2H = 0x00; // Timer 2 Reload Register High Byte
TL2 =0x00;
TH2 =0x00;
TMR3CN = 0x00; // Timer 3 Control Register
TMR3H = 0x00; // Timer 3 High Byte
TMR3L = 0x00; // Timer 3 Low Byte
TMR3RLH=0x00;
T4CON =0x00;
RCAP4L = 0x00; // Timer 4 Reload Register Low Byte
RCAP4H = 0x00; // Timer 4 Reload Register High Byte
TL4 =0x00;
TH4 =0x00;
/***************************************************************/
/*******************复位配置*************************************/
RSTSRC = 0x00; // Reset Source Register
/***************************************************************/
/*******************中断配置*************************************/
IE = 0x12; //Interrupt Enable EA=0 , ET0=1; ES0=1;
IP = 0x00; //Interrupt Priority
EIE1 = 0x00;
EIE2 = 0x00; //Extended Interrupt Enable 2
EIP1 = 0x00; //Extended Interrupt Priority 1
EIP2 = 0x00; //Extended Interrupt Priority 2
/***************************************************************/
} //End of config
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -