|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
void INIT_SYS_CLK (void)
{
int i; // delay counter
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
OSCICN = 0xc1; //select local oscillator as SYSCLK/4
CLKSEL = 0x00; //select SYSCLK from register OSCICN setup ;
while (!(OSCICN & 0X40)); //时钟就绪?
RSTSRC = 0x04 ; //启动时钟丢失复位
}
void INIT_PORT (void)
{
XBR0 = 0x05 ; // 使能 UART on P0.4(TX) and P0.5 I2C ON P0.0(SDA) I2C ON P0.1(SCL)
XBR1 = 0x40 ; // 弱上拉,交叉开关使能,
P0MDOUT |= 0x18 ; // 使能 UTX 为推挽输出
P1MDOUT = 0xff ; //P1口被设置为输出
P2MDOUT = 0xff ; //P2口被设置为推挽输出
P3MDOUT = 0x00 ; //P3口被设置为开漏输出,P3口对与C8051F311来说只有一个IO,P3^0与C2DATA复用
P0 = 0x08;
P1 = 0xfe; //测直流电压
P2 = 0xff;
}
/*串口初始化函数*/
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 |= 0x01; //串口波特率2400,时钟频率选择为系统时钟4分频
} 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,串口中断标志
}
/*
void Timer2_Init (int counts)
{
TMR2CN = 0x00; // STOP Timer2; Clear TF2H and TF2L;
// disable low-byte interrupt; disable
// split mode; select internal timebase
CKCON |= 0x30; // Timer2 uses SYSCLK as its timebase
TMR2RLL = (uchar)(0xffff-counts); // Init reload values
TMR2RLH = (uchar)((0xffff-counts)>>8);
TMR2L = TMR2RLL; // Init Timer2 with reload value
TMR2H = TMR2RLH;
ET2 = 1; // enable Timer2 interrupts
// TR2 = 1; // start Timer2
} */
void Timer2_Init (uchar countsL,uchar countsH)
{
TMR2CN = 0x28; // STOP Timer2; Clear TF2H and TF2L;
// enable low-byte interrupt; enable
// split mode; select internal timebase
CKCON |= 0x10; // Timer2L uses SYSCLK as its timebase
// Timer2H uses SYSCLK/12 as its timebase
TMR2RLL = 0xff-countsL; // Init reload values
TMR2RLH = 0xff-countsH;
TMR2L = TMR2RLL; // Init Timer2 with reload value
TMR2H = TMR2RLH;
PT2 = 1; //高优先级
ET2 = 1; // enable Timer2 interrupts
TR2 = 1; // start Timer2
} |
|