找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3664|回复: 8
收起左侧

stc12c5a60s2单片机串口不够用,TXD,RXD该怎么拓展啊?求大佬解答

[复制链接]
ID:381195 发表于 2018-8-1 11:22 | 显示全部楼层 |阅读模式
好像是有专门拓展芯片,求介绍那个比较好用

回复

使用道具 举报

ID:375003 发表于 2018-8-1 12:29 | 显示全部楼层
1,普通IO口摸拟,2,串口协义中指定地址要操作哪个从机,主机TXD,从机1,RXD,从机2,RXD,主机RXD,从机1,TXD,从机2,TXD

评分

参与人数 1黑币 +2 收起 理由
多德 + 2 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:366710 发表于 2018-8-1 16:06 | 显示全部楼层
用LS165或LS164芯片
回复

使用道具 举报

ID:308437 发表于 2018-8-2 08:45 | 显示全部楼层
那个烧录软件下有范例程序,串口二的
  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU Limited ---------------------------------------------*/
  3. /* --- STC12C5Axx Series MCU UART2 (8-bit/9-bit)Demo ---------------*/
  4. /* If you want to use the program or the program referenced in the  */
  5. /* article, please specify in which data and procedures from STC    */
  6. /*------------------------------------------------------------------*/

  7. #include "reg51.h"
  8. #include "intrins.h"

  9. typedef unsigned char BYTE;
  10. typedef unsigned int WORD;

  11. #define FOSC 18432000L      //System frequency
  12. #define BAUD 115200         //UART baudrate

  13. /*Define UART parity mode*/
  14. #define NONE_PARITY     0   //None parity
  15. #define ODD_PARITY      1   //Odd parity
  16. #define EVEN_PARITY     2   //Even parity
  17. #define MARK_PARITY     3   //Mark parity
  18. #define SPACE_PARITY    4   //Space parity

  19. #define PARITYBIT EVEN_PARITY   //Testing even parity

  20. /*Declare SFR associated with the UART2 */
  21. sfr AUXR  = 0x8e;           //Auxiliary register
  22. sfr S2CON = 0x9a;           //UART2 control register
  23. sfr S2BUF = 0x9b;           //UART2 data buffer
  24. sfr BRT   = 0x9c;           //Baudrate generator
  25. sfr IE2   = 0xaf;           //Interrupt control 2

  26. #define S2RI  0x01          //S2CON.0
  27. #define S2TI  0x02          //S2CON.1
  28. #define S2RB8 0x04          //S2CON.2
  29. #define S2TB8 0x08          //S2CON.3

  30. bit busy;

  31. void SendData(BYTE dat);
  32. void SendString(char *s);

  33. void main()
  34. {
  35. #if (PARITYBIT == NONE_PARITY)
  36.     S2CON = 0x50;           //8-bit variable UART
  37. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  38.     S2CON = 0xda;           //9-bit variable UART, parity bit initial to 1
  39. #elif (PARITYBIT == SPACE_PARITY)
  40.     S2CON = 0xd2;           //9-bit variable UART, parity bit initial to 0
  41. #endif

  42.     BRT = -(FOSC/32/BAUD);  //Set auto-reload vaule of baudrate generator
  43.     AUXR = 0x14;            //Baudrate generator work in 1T mode
  44.     IE2 = 0x01;             //Enable UART2 interrupt
  45.     EA = 1;                 //Open master interrupt switch

  46.     SendString("STC12C5A60S2\r\nUart2 Test !\r\n");
  47.     while(1);
  48. }

  49. /*----------------------------
  50. UART2 interrupt service routine
  51. ----------------------------*/
  52. void Uart2() interrupt 8 using 1
  53. {
  54.     if (S2CON & S2RI)
  55.     {
  56.         S2CON &= ~S2RI;     //Clear receive interrupt flag
  57.         P0 = S2BUF;         //P0 show UART data
  58.         P2 = (S2CON & S2RB8);//P2.2 show parity bit
  59.     }
  60.     if (S2CON & S2TI)
  61.     {
  62.         S2CON &= ~S2TI;     //Clear transmit interrupt flag
  63.         busy = 0;           //Clear transmit busy flag
  64.     }
  65. }

  66. /*----------------------------
  67. Send a byte data to UART
  68. Input: dat (data to be sent)
  69. Output:None
  70. ----------------------------*/
  71. void SendData(BYTE dat)
  72. {
  73.     while (busy);           //Wait for the completion of the previous data is sent
  74.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  75.     if (P)                  //Set the parity bit according to P
  76.     {
  77. #if (PARITYBIT == ODD_PARITY)
  78.         S2CON &= ~S2TB8;    //Set parity bit to 0
  79. #elif (PARITYBIT == EVEN_PARITY)
  80.         S2CON |= S2TB8;     //Set parity bit to 1
  81. #endif
  82.     }
  83.     else
  84.     {
  85. #if (PARITYBIT == ODD_PARITY)
  86.         S2CON |= S2TB8;     //Set parity bit to 1
  87. #elif (PARITYBIT == EVEN_PARITY)
  88.         S2CON &= ~S2TB8;    //Set parity bit to 0
  89. #endif
  90.     }
  91.     busy = 1;
  92.     S2BUF = ACC;            //Send data to UART2 buffer
  93. }

  94. /*----------------------------
  95. Send a string to UART
  96. Input: s (address of string)
  97. Output:None
  98. ----------------------------*/
  99. void SendString(char *s)
  100. {
  101.     while (*s)              //Check the end of the string
  102.     {
  103.         SendData(*s++);     //Send current char and increment string ptr
  104.     }
  105. }
复制代码


回复

使用道具 举报

ID:394112 发表于 2018-9-5 16:06 | 显示全部楼层
你现在找到合适的了吗,求分享,也遇到同样的问题了
回复

使用道具 举报

ID:387733 发表于 2018-9-5 18:32 | 显示全部楼层
可以使用STC15W4K32S4替代,这个有4个串口可用
回复

使用道具 举报

ID:262530 发表于 2018-9-5 20:12 | 显示全部楼层
普通IO口就可以模拟
回复

使用道具 举报

ID:303383 发表于 2018-9-5 20:21 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

ID:375003 发表于 2020-2-3 23:20 | 显示全部楼层
还可这样IOto232
#include <reg52.h>
sbit BT_SND =P3^1;
sbit BT_REC =P3^0;
/**********************************************

IO 口模拟232通讯程序

使用两种方式的C程序 占用定时器0

**********************************************/
#define F_TM F0
#define TIMER0_ENABLE TL0=TH0; TR0=1;
#define TIMER0_DISABLE TR0=0;
#define uchar unsigned char
#define uint unsigned int



sbit ACC0= ACC^0;
sbit ACC1= ACC^1;
sbit ACC2= ACC^2;
sbit ACC3= ACC^3;
sbit ACC4= ACC^4;
sbit ACC5= ACC^5;
sbit ACC6= ACC^6;
sbit ACC7= ACC^7;

uchar bufSND[8],bufRCV[8],putRCV,getRCV,curSND,lenSND,staRCV;
uchar putKEY,getKEY,nFUN,nSTN,Cxy,Cxy0,nTIM;

void IntTimer0() interrupt 1
{
F_TM=1;
}

//发送一个字符
void PSendChar(unsigned char inch)
{
unsigned char ii;
ii=0;
F_TM=0;
BT_SND=0; //start bit
TIMER0_ENABLE; //启动
while(!F_TM);
while(ii<8){
    if(inch&1)BT_SND=1;
    else BT_SND=0;
    F_TM=0;
    while(!F_TM);
    ii++;
        inch>>=1;
        }
BT_SND=1;
F_TM=0;
while(!F_TM);
TIMER0_DISABLE; //停止timer
}

//接收一个字符
unsigned char PGetChar()
{
unsigned char rch,ii;
TIMER0_ENABLE;
F_TM=0;
ii=0;
rch=0;
while(!F_TM); //等过起始位

while(ii<8)
{
rch>>=1;
if(BT_REC)
{
rch|=0x80;
}
ii++;
F_TM=0;
while(!F_TM);
}
F_TM=0;
while(!F_TM)
{
if(BT_REC)
{
break;
}
}
TIMER0_DISABLE; //停止timer
return rch;
}

//检查是不是有起始位
bit StartBitOn()
{
return (BT_REC==0);

}

void main()
{
unsigned char gch;
TMOD=0x22; //定时器1为工作模式2(8位自动重装),0为模式2(8位自动重装)
PCON=00;
TR0=0; //在发送或接收才开始使用
TF0=0;
TH0=(256-104); //9600bps 就是 1000000/9600=104.167微秒 执行的timer是104.167*11.0592/12= 96
TL0=TH0;
ET0=1;
EA=1;

//PSendChar(0x55);
//PSendChar(0xaa);
//PSendChar(0x00);
//PSendChar(0xff);

while(1)
{
if(StartBitOn())
{
gch=PGetChar();
bufRCV[putRCV++]=PGetChar();
putRCV &= 0X07;
   if(bSD){if(curSND<lenSND) SBUF=bufSND[curSND++];else {bSD=OFF;}}
PSendChar(gch);
}
}
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

Powered by 单片机教程网

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