www.pudn.com > STC.rar > serial.c, change:2009-08-28,size:1819b


#define RELOAD_COUNT  0xfd		//11.0592MHz,12T,SMOD=0,9600bps

void serial_port_initial();
void send_UART(unsigned char);
void send_string(unsigned char *s);
void UART_Interrupt_Receive(void);


void serial_port_initial()
{
    SCON    =   0x50;   //0101,0000 8位可变波特率,无奇偶校验位
    TMOD   |=   0x20;   //0011,0001 设置顶时器1为8位自动重装计数器
    TH1     =   RELOAD_COUNT;   //设置定时器1自动重装数
    TL1     =   RELOAD_COUNT;
    TR1     =   1;    //开定时器1
    ES      =   1;    //允许串口中断
    EA      =   1;    //开总中断
}

void send_UART(unsigned char i)
{
    ES     =   0;  //关串口中断
    TI     =   0;  //清零串口发送完成中断请求标志
    SBUF   =   i;
    while(TI ==0); //等待发送完成
    TI     =   0;  //清零串口发送完成中断请求标志
    ES     =   1;  //允许串口中断
} 
void send_uchar_ascii(unsigned char ucdat)
{
	
		send_UART(ucdat/100 + '0');
		send_UART((ucdat/10)%10 + '0');
		send_UART(ucdat%10 + '0');
		send_UART(' ');
	
}
void send_uint_ascii(unsigned int uidat)
{
	
	if(uidat<100)
	{
		send_UART((uidat/10)%10 + '0');
		send_UART(uidat%10 + '0');
		send_UART(' ');
	}
	else if(uidat<1000)
	{
		send_UART(uidat/100 + '0');
		send_UART((uidat/10)%10 + '0');
		send_UART(uidat%10 + '0');
		send_UART(' ');
	}
	else
	{
		send_UART(uidat/10000 + '0');
		send_UART((uidat/1000)%10 + '0');
		send_UART((uidat/100)%10 + '0');
		send_UART((uidat/10)%10 + '0');
		send_UART(uidat%10 + '0');
		send_UART(' ');
	}
}


void send_string(unsigned char *s)
{
	do
	{
		send_UART(*s);
	} while(*(++s) != '\0') ;
}

void UART_Interrupt_Receive(void) interrupt 4
{
    static unsigned char   k   =   0;
    if(RI==1)
    {
        RI  =   0;
        k   =   SBUF;
        if(k=='1')   //是自定义下载命令
        {
            
        }
        
    }
    else
    {
        TI  =  0;
    }
}