因仑“3+1”工程特种兵精英论坛

标题: 串口2演示 [打印本页]

作者: 张衍波    时间: 2015-5-6 09:42
标题: 串口2演示
/*--------------------------------------
芯片    :STC12C5A60S2
晶振    :11.0592MHZ
开发平台:KEIL
功能    :串口二收发8位数据。(采用中断方式,波特率发生器产生波特率)
波特率  :9600BPS
日期    :11.5.19
注意    :下载完程序后请复位系统!!(切记!)
--------------------------------------*/

#include "STC12C5A60S2.h"
#define uchar unsigned char
#define uint unsigned int

uchar dat=0;

/*--------------------------------------
          延时函数
--------------------------------------*/
void delay(uchar t)
{
        uchar i,j;

        for(i=0;i<t;i++)

        for(j=0;j<250;j++);
}

/*--------------------------------------
          串口初始化
--------------------------------------*/
void init(void)
{

        S2CON = 0x50;                                      //方式1,八位数据,可变波特率

        AUXR1 = 0x00;                                          //1T工作方式

        PCON  = 0x00;                                         //不倍增波特率

        IP2   = 0x00;                                         //优先级默认

        BRT   = 0XFD;                                          //设置波特率9600

        AUXR  = 0x10;                                         //启动波特率发生器

        EA    = 1;                                          //开总中断
       
        IE2   = 0x01;                                         //开串口2中断
                         
}

/*--------------------------------------
         发送一个字符
--------------------------------------*/
void  txd_c(uchar x)
{
        S2BUF = x;
}

/*--------------------------------------
          发送接收中断函数
--------------------------------------*/
void uart2_isr()  interrupt 8
{
        if( S2CON & 0x01 ){

                S2CON &= ~0x01;

                dat    = S2BUF;
                               
                                   }

        if( S2CON & 0x02 ){
               
                S2CON &= ~0x02;
                       
                                    }

}

/*--------------------------------------
          主函数
--------------------------------------*/
void main()
{
        uchar tab[]={"liguangyang!!"};
       
        uchar *p;
       
        uchar x;

        init();

        p=tab;

        for( x=0;x<14;x++ ){

                txd_c(p[x]);

                delay(200);
                                    }

        while(1) P0 = dat;       

}


[attach]2772[/attach]







欢迎光临 因仑“3+1”工程特种兵精英论坛 (http://bbs.enlern.com/) Powered by Discuz! X3.4