找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1935|回复: 1
收起左侧

STC12C5A60S2 串口2收不到程序 大神求指教!

[复制链接]
ID:194894 发表于 2017-4-29 23:02 | 显示全部楼层 |阅读模式
#include <reg52.h>
#define jingzhen     11059200UL       
#define botelv   9600UL                 
#define uchar  unsigned char
#define uint   unsigned int


unsigned char buf[6]={'S','t','r','i','n','g'};

unsigned char *TextStr;
unsigned char *RecStr;


void PutString(char *TextStr)  
{   
    ES=0;     
     while(*TextStr!=0)
    {                     
        SBUF=*TextStr;
        while(TI==0);
        TI=0;   
        TextStr++;
    }
    ES=1;
}   

void ReceiveString(char *RecStr)   
{
    char num=0;
    unsigned char count=0;  
    *RecStr=SBUF;
    count=0;
    RI=0;   
}

void init(void)               
{
                EA=0;
                TMOD&=0x0F;  
                TMOD|=0x20;   
                SCON=0x50;     
                TH1=256-jingzhen/(botelv*12*16);  
                TL1=256-jingzhen/(botelv*12*16);
                PCON=0x80;
                ES=1;         
                TR1=1;        
                REN=1;        
                EA=1;   
}

void main()
        {
                init();
                PutString(&buf);
//                 ReceiveString(&buf);
        }
       
void uart(void) interrupt 4                 
        {
        if(RI)   
                {
                        RI=0;
                        *RecStr = SBUF;
                        SBUF = *TextStr;
                }        else
                {TI=0;}
}

回复

使用道具 举报

ID:194894 发表于 2017-4-29 23:04 | 显示全部楼层
不好意思刚刚发错了 那个是串口1的,串口2如下:
#include <stdio.h>
#include <string.h>
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

#define S2RI 0x01 //S2CON.1
#define S2TI 0x02 //S2CON.2
#define ES2 0x01
unsigned char buf[7]={'S','t','r','i','n','g'};

unsigned char *TextStr;
unsigned char *RecStr;

void PutString(char *TextStr)
{
IE2=0x00;
while(*TextStr!=0)
{
S2BUF=*TextStr;
while(!(S2CON&S2TI));
S2CON &= ~S2TI;
TextStr++;
}
IE2=0x01;
}

void ReceiveString(char *RecStr)
{
char num=0;
unsigned char count=0;
*RecStr=S2BUF;
count=0;
S2CON &= ~S2RI;
}

void init(void)       
{

//        AUXR |= 0x08;        //????????S2SMOD
//        S2CON = 0x50;        //8???,?????
//        AUXR &= 0xFB;        //???????????Fosc/12,?12T
//        BRT = 0xFA;        //?????????????
//        AUXR |= 0x10;        //??????????
AUXR &= 0xf7; //??????
S2CON = 0x50; //8???,?????
BRT = 0xDC; //?????????????
AUXR |= 0x04; //???????????Fosc,?1T
AUXR |= 0x10; //??????????
IE2=0x01; //????2??ES2=1
EA=1; //????
}

void main()
{
init();
PutString(&buf);
ReceiveString(&buf);
while(1);
}

void UART2(void) interrupt 8       
{
if(S2CON & S2RI)
{
S2CON &= ~S2RI;
*RecStr = S2BUF;
S2BUF = *TextStr;
}
}
回复

使用道具 举报

无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

Powered by 单片机教程网

快速回复 返回顶部 返回列表