立即注册 登录
返回首页

uid:325161的个人空间

日志

STC15F2K60s2单片机串口通信实验

已有 1443 次阅读2018-12-3 15:52 |个人分类: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; //模拟串口发送

#define stringLenth 16
bit Uart1_Sendbusy = 0;    //标记符号
unsigned char uartSbuf[17];
unsigned char aciiCount;
unsigned char display;

void GPIO()
{
    P3M1 = 0x00;  P3M0 = 0x00; //准双向口
P2M1 = 0x00;  P2M0 = 0xff;
P1M1 = 0x00;  P1M0 = 0x00;   //开漏模式
//    P0M1 = 0x00;  P0M0 = 0xff;   //推完模式
}

void Uart1_Init(void)
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0x70; //设定定时初值
TH1 = 0xFF; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES=1; //串口中断允许位

}
void Timer0_Init() //1ms@11.0592MHZ
{
    AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}

/******************
STC单片机的串口发送字节的函数
*******************/
void Send_Ascii(unsigned char dat)
{
P_TXD = 0;
while(Uart1_Sendbusy);
Uart1_Sendbusy=1;
SBUF=dat;
}

void main()
{
  GPIO();
  Timer0_Init();
  Uart1_Init();
  EA = 1;
//   Send_Ascii(0x12);
  while(1)
  {
     Send_Ascii(0x12);
  }
void Uart_isr()interrupt 4
{
ES=0;//关闭中断
if (RI)
{
RI=0;
P_RXD = 0;
uartSbuf[aciiCount]=SBUF;
aciiCount++;
if (aciiCount > stringLenth) //根据各个模式截取合适长度的字符串
{
aciiCount=0;
P_RXD = 1;
}
}
if (TI)
{
    TI = 0;
Uart1_Sendbusy = 0;
P_TXD = 1;   
}
ES=1;
}

路过

鸡蛋

鲜花

握手

雷人

全部作者的其他最新日志

评论 (0 个评论)

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

Powered by 单片机教程网

返回顶部