C8051F120单片机与PC机通讯,如果单纯的通讯,不做任何IO口控制,正常,但是一旦加入IO口控制就没响应了,求救?
程序如下:
/*使用C8051F120,外部晶振22.1184M*/
//波特率设为9600
#include "C8051F120.h"
#include "string.h"
unsigned char buffer[100];
unsigned int RevCount = 0;
unsigned int TimerRev = 0;
unsigned char *Ptr_Send = 0;
unsigned int SendCount = 0;
unsigned char busy = 0;
unsigned int i; // Software timer
sbit LED2=P4^1; // LED1 ='0' means ON
sbit LED3=P4^0;
void delay(int i)
{
int j;
for(;i>0;i--)
for(j=1000;j>0;j--);
}
void Timer2ISR( void ) interrupt 5
{
TMR2CN &= 0x3F;
if( RevCount != 0 )
{
if( TimerRev != 0 ) { TimerRev --; }
}
}
void uart0(void) interrupt 4
{
SFRPAGE = UART0_PAGE;
if(TI0)
{
TI0=0;
if( SendCount != 0 )
{
SBUF0 = *Ptr_Send++;
SendCount--;
busy = 1;
}
else
{
busy = 0;
}
}
else if(RI0)
{
RI0=0;
buffer[RevCount++] = SBUF0;
TimerRev = 5;
}
SFRPAGE = CONFIG_PAGE;
}
void UART0_Send( unsigned char *buf, unsigned int size )
{
while( busy );
Ptr_Send = buf;
SendCount = size;
SFRPAGE = UART0_PAGE;
SCON0 |= ( 1<<1 );
while( busy );
}
main(void)
{
int temp=0;
SFRPAGE = CONFIG_PAGE;
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
XBR2 = 0x40; // Enable crossbar and enable
// weak pull-ups
OSCICN = 0x80; // Set internal oscillator to run
// at its slowest frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSTEMCLOCK source
/*使用外部22.1184M晶振*/
OSCXCN = 0x67; // Enable external crystal osc.
for (i=0; i < 256; i++); // Wait at least 1ms
while (!(OSCXCN & 0x80)); // Wait for crystal osc to settle
CLKSEL = 0x01; // Select external crystal as SYSTEMCLOCK source
/*引脚配置*/
XBR0 = 0x04; // Enable UART0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-up
P0MDOUT |= 0x01; // Set TX pin to push-pull
/*初始化定时器1*/
SFRPAGE = TIMER01_PAGE;
TMOD|=0x20; //定时器1工作在8位自动重装方式
TH1=250; //采用默认的22.1184/12时钟,根据公式计算当波特率为9600时,TH1=250;
TL1=0;
TR1=1;
//ET1=1; //开启定时器1中断允许
/*初始化串口*/
SFRPAGE = UART0_PAGE;
SCON0 = 0x50; //8位的UART工作方式,接收允许
TI0 = 1; // Indicate TX0 ready
ES0=1; //打开串口中断
/*初始化定时器2*/
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x04;
RCAP2H = 49536>>8;
RCAP2L = 49536;
CKCON |= ( 1<<5 );
IE |= ( 1<<5 );
SFRPAGE = CONFIG_PAGE; // set SFR page
EA=1; //允许所有中断
UART0_Send( (unsigned char*) "UART0 test start!\r\n", strlen( "UART0 test start!\r\n" ));
while (1)
{
if( RevCount != 0 && TimerRev == 0 )
{
//UART0_Send( buffer, RevCount );
//RevCount = 0;
if (buffer[0]=='S')
{
UART0_Send((unsigned char*) "UART0 S\r\n", strlen( "UART0 S\r\n" ));
//LED2=1; //一旦加入这句话,LED始终没反应
}
else
if (buffer[0]=='A')
{
UART0_Send((unsigned char*) "UART0 A\r\n", strlen( "UART0 A\r\n" ));
//LED3=1;//一旦加入这句话,LED始终没反应
}
else
UART0_Send((unsigned char*) "UART0 \r\n", strlen( "UART0 \r\n" ));
RevCount = 0;
}
} // end of while(1)
}
|