立即注册 登录
返回首页

uid:325161的个人空间

日志

STC15F2K60s2单片机外接串口实验

已有 1035 次阅读2018-12-3 16:12 |个人分类:STC单片机

#include"STC15F2K60S2.h"
#include "string.h"
#define uchar unsigned char
#define Machine_Focs 11059200L //晶振频率  
#define BAUD1 19200 //波特率

sbit Key3 = P1^7;
sbit Key1 = P3^2;
sbit Key2 = P3^3;
//sbit P_RXD=P1^0; //模拟串口接收
//sbit P_TXD=P1^1; //模拟串口发送

/*收发显示数据相关*/
bit Uart1_Sendbusy = 0 ;   //标记是否发送完成
uchar G_count;
unsigned char display; //数码管显示数据
uchar flag;
bit flg_1ms;    //1ms的标志
uchar duanxuan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //显示0-f 

/**********************
函数名称:Timer0
功能描述:定时器0的中断响应函数 ,定时100us
***********************/
void Timer0() interrupt 1   
{
TH0=(65535-1000)/256;    //定时器初始值
TL0=(65535-1000)%256;
G_count++;
if(G_count==10)            //中断10次对应1ms
{
G_count=0;
flg_1ms=1;
}
    flag++;
if(flag==2)
flag=0;
switch(flag)
{
case 0: P2=0x00;P0=duanxuan[display/16];break;
    case 1: P2=0x01;P0=duanxuan[display%16];break;
}
}
void GPIO()
{
    P3M1 = 0x00;  P3M0 = 0x00; //准双向口
P2M1 = 0x00;  P2M0 = 0xff;
P1M1 = 0x00;  P1M0 = 0x00;   //开漏模式
//    P0M1 = 0x00;  P0M0 = 0xff;   //推完模式
}

void Uart2_Init(void) //19200bps@11.0592MHz
{
S2CON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0x70; //设定定时初值
T2H = 0xFF; //设定定时初值
IE2 &= 0xFB; //禁止定时器2中断
// IE2 |= 0x01;       //串口2中断允许位
AUXR |= 0x10; //启动定时器2
}

void Timer5_Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0xCD; //设置定时初值
T2H = 0xD4; //设置定时初值
AUXR |= 0x10; //定时器2开始计时
}
void Send_Ascii(unsigned char dat)
{
// P_TXD = 0;
while(Uart1_Sendbusy);
Uart1_Sendbusy=1;
S2BUF=dat;
}

void main()
{
  GPIO();
  Timer5_Init();
  Uart2_Init();
  EA = 1;
//   Send_Ascii(0x12);
  while(1)
  { 
Send_Ascii(0x12);
  }
}
void Uart2_isr() interrupt 8
{
if (S2CON & 0x02)   //发送中断
{
    S2CON &= ~0x02;
Uart1_Sendbusy = 0;
// P_TXD = 1;   
}
if(S2CON & 0x01)
{
   S2CON &= ~0x01;
   display = S2BUF;
}
}

路过

鸡蛋

鲜花

握手

雷人

全部作者的其他最新日志

评论 (0 个评论)

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

返回顶部