找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2475|回复: 0
打印 上一主题 下一主题
收起左侧

1T单片机STC15的4个串口通讯驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:842875 发表于 2020-11-12 23:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、串口通讯
  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU International Limited -------------------------------*/
  3. /* --- STC 1T Series MCU RC 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. /*********************************************************/
  8. #define MAIN_Fosc                22118400L        //定义主时钟
  9. //#define MAIN_Fosc                11059200L        //定义主时钟

  10. #include        "STC15Fxxxx.H"


  11. /*************        功能说明        **************

  12. 4串口全双工中断方式收发通讯程序。

  13. 通过PC向MCU发送数据, MCU收到后通过串口把收到的数据原样返回.

  14. 默认参数:
  15. 所有设置均为 1位起始位, 8位数据位, 1位停止位, 无校验.
  16. 每个串口可以使用不同的波特率.
  17. 串口1(P3.0 P3.1): 115200bps.
  18. 串口2(P1.0 P1.1):  57600bps.
  19. 串口3(P0.0 P0.1):  38400bps.
  20. 串口4(P0.2 P0.3):  19200bps.


  21. ******************************************/

  22. /*************        本地常量声明        **************/
  23. #define        RX1_Length        128                /* 接收缓冲长度 */
  24. #define        RX2_Length        128                /* 接收缓冲长度 */
  25. #define        RX3_Length        128                /* 接收缓冲长度 */
  26. #define        RX4_Length        128                /* 接收缓冲长度 */

  27. #define        UART_BaudRate1        115200UL         /* 波特率 */
  28. #define        UART_BaudRate2         57600UL         /* 波特率 */
  29. #define        UART_BaudRate3         38400UL         /* 波特率 */
  30. #define        UART_BaudRate4         19200UL         /* 波特率 */


  31. /*************        本地变量声明        **************/
  32. u8        xdata        RX1_Buffer[RX1_Length];        //接收缓冲
  33. u8        xdata        RX2_Buffer[RX2_Length];        //接收缓冲
  34. u8        xdata        RX3_Buffer[RX3_Length];        //接收缓冲
  35. u8        xdata        RX4_Buffer[RX4_Length];        //接收缓冲

  36. u8        TX1_read,RX1_write;        //读写索引(指针).
  37. u8        TX2_read,RX2_write;        //读写索引(指针).
  38. u8        TX3_read,RX3_write;        //读写索引(指针).
  39. u8        TX4_read,RX4_write;        //读写索引(指针).

  40. bit        B_TX1_Busy,B_TX2_Busy,B_TX3_Busy,B_TX4_Busy;        // 发送忙标志


  41. /*************        本地函数声明        **************/
  42. void        UART1_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
  43. void        UART2_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
  44. void        UART3_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
  45. void        UART4_config(u8 brt);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
  46. void         PrintString1(u8 *puts);
  47. void         PrintString2(u8 *puts);
  48. void         PrintString3(u8 *puts);
  49. void         PrintString4(u8 *puts);




  50. /**********************************************/
  51. void main(void)
  52. {

  53.         P0n_standard(0xff);        //设置为准双向口
  54.         P1n_standard(0xff);        //设置为准双向口
  55.         P2n_standard(0xff);        //设置为准双向口
  56.         P3n_standard(0xff);        //设置为准双向口
  57.         P4n_standard(0xff);        //设置为准双向口
  58.         P5n_standard(0xff);        //设置为准双向口

  59.         UART1_config(1);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
  60.         UART2_config(2);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
  61.         UART3_config(3);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
  62.         UART4_config(4);        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.

  63.         EA = 1;

  64.         PrintString1("STC15F4K60S4 USART1 Test Prgramme!\r\n");
  65.         PrintString2("STC15F4K60S4 USART2 Test Prgramme!\r\n");
  66.         PrintString3("STC15F4K60S4 USART3 Test Prgramme!\r\n");
  67.         PrintString4("STC15F4K60S4 USART4 Test Prgramme!\r\n");

  68.         while (1)
  69.         {
  70.                 if((TX1_read != RX1_write) && !B_TX1_Busy)        //收到过数据, 并且发送空闲
  71.                 {
  72.                         B_TX1_Busy = 1;                //标志发送忙
  73.                         SBUF = RX1_Buffer[TX1_read];        //发一个字节
  74.                         if(++TX1_read >= RX1_Length)        TX1_read = 0;        //避免溢出处理
  75.                 }

  76.                 if((TX2_read != RX2_write) && !B_TX2_Busy)        //收到过数据, 并且发送空闲
  77.                 {
  78.                         B_TX2_Busy = 1;                //标志发送忙
  79.                         S2BUF = RX2_Buffer[TX2_read];        //发一个字节
  80.                         if(++TX2_read >= RX2_Length)        TX2_read = 0;        //避免溢出处理
  81.                 }

  82.                 if((TX3_read != RX3_write) && !B_TX3_Busy)        //收到过数据, 并且发送空闲
  83.                 {
  84.                         B_TX3_Busy = 1;                //标志发送忙
  85.                         S3BUF = RX3_Buffer[TX3_read];        //发一个字节
  86.                         if(++TX3_read >= RX3_Length)        TX3_read = 0;        //避免溢出处理
  87.                 }

  88.                 if((TX4_read != RX4_write) && !B_TX4_Busy)        //收到过数据, 并且发送空闲
  89.                 {
  90.                         B_TX4_Busy = 1;                //标志发送忙
  91.                         S4BUF = RX4_Buffer[TX4_read];        //发一个字节
  92.                         if(++TX4_read >= RX4_Length)        TX4_read = 0;        //避免溢出处理
  93.                 }
  94.         }
  95. }


  96. //========================================================================
  97. // 函数: SetTimer2Baudraye(u16 dat)
  98. // 描述: 设置Timer2做波特率发生器。
  99. // 参数: dat: Timer2的重装值.
  100. // 返回: none.
  101. // 版本: VER1.0
  102. // 日期: 2014-11-28
  103. // 备注:
  104. //========================================================================
  105. void        SetTimer2Baudraye(u16 dat)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
  106. {
  107.         AUXR &= ~(1<<4);        //Timer stop
  108.         AUXR &= ~(1<<3);        //Timer2 set As Timer
  109.         AUXR |=  (1<<2);        //Timer2 set as 1T mode
  110.         TH2 = dat / 256;
  111.         TL2 = dat % 256;
  112.         IE2  &= ~(1<<2);        //禁止中断
  113.         AUXR |=  (1<<4);        //Timer run enable
  114. }


  115. //========================================================================
  116. // 函数: void        UART1_config(u8 brt)
  117. // 描述: UART1初始化函数。
  118. // 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
  119. // 返回: none.
  120. // 版本: VER1.0
  121. // 日期: 2014-11-28
  122. // 备注:
  123. //========================================================================
  124. void        UART1_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
  125. {
  126.         u8        i;
  127.         /*********** 波特率使用定时器2 *****************/
  128.         if(brt == 2)
  129.         {
  130.                 AUXR |= 0x01;                //S1 BRT Use Timer2;
  131.                 SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / UART_BaudRate1);
  132.         }

  133.         /*********** 波特率使用定时器1 *****************/
  134.         else
  135.         {
  136.                 TR1 = 0;
  137.                 AUXR &= ~0x01;                //S1 BRT Use Timer1;
  138.                 AUXR |=  (1<<6);        //Timer1 set as 1T mode
  139.                 TMOD &= ~(1<<6);        //Timer1 set As Timer
  140.                 TMOD &= ~0x30;                //Timer1_16bitAutoReload;
  141.                 TH1 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate1) / 256;
  142.                 TL1 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate1) % 256;
  143.                 ET1 = 0;        //禁止中断
  144.                 INT_CLKO &= ~0x02;        //不输出时钟
  145.                 TR1  = 1;
  146.         }
  147.         /*************************************************/

  148.         SCON = (SCON & 0x3f) | (1<<6);        // 8位数据, 1位起始位, 1位停止位, 无校验
  149. //        PS  = 1;        //高优先级中断
  150.         ES  = 1;        //允许中断
  151.         REN = 1;        //允许接收
  152.         P_SW1 = P_SW1 & 0x3f;        //切换到 P3.0 P3.1
  153. //        P_SW1 = (P_SW1 & 0x3f) | (1<<6);        //切换到P3.6 P3.7
  154. //        P_SW1 = (P_SW1 & 0x3f) | (2<<6);        //切换到P1.6 P1.7 (必须使用内部时钟)

  155.         for(i=0; i<RX1_Length; i++)                RX1_Buffer[ i] = 0;
  156.         B_TX1_Busy  = 0;
  157.         TX1_read    = 0;
  158.         RX1_write   = 0;
  159. }


  160. //========================================================================
  161. // 函数: void        UART2_config(u8 brt)
  162. // 描述: UART2初始化函数。
  163. // 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
  164. // 返回: none.
  165. // 版本: VER1.0
  166. // 日期: 2014-11-28
  167. // 备注:
  168. //========================================================================
  169. void        UART2_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
  170. {
  171.         u8        i;
  172.         /*********** 波特率固定使用定时器2 *****************/
  173.         if(brt == 2)        SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / UART_BaudRate2);

  174.         S2CON &= ~(1<<7);        // 8位数据, 1位起始位, 1位停止位, 无校验
  175.         IE2   |= 1;                        //允许中断
  176.         S2CON |= (1<<4);        //允许接收
  177.         P_SW2 &= ~1;                //切换到 P1.0 P1.1
  178. //        P_SW2 |= 1;                        //切换到 P4.6 P4.7

  179.         for(i=0; i<RX2_Length; i++)                RX2_Buffer[ i] = 0;
  180.         B_TX2_Busy  = 0;
  181.         TX2_read    = 0;
  182.         RX2_write   = 0;
  183. }
  184. //========================================================================
  185. // 函数: void        UART3_config(u8 brt)
  186. // 描述: UART3初始化函数。
  187. // 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
  188. // 返回: none.
  189. // 版本: VER1.0
  190. // 日期: 2014-11-28
  191. // 备注:
  192. //========================================================================
  193. void        UART3_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer3做波特率.
  194. {
  195.         u8        i;
  196.         /*********** 波特率固定使用定时器2 *****************/
  197.         if(brt == 2)
  198.         {
  199.                 S3CON &= ~(1<<6);        //BRT select Timer2
  200.                 SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / UART_BaudRate3);
  201.         }
  202.         /*********** 波特率使用定时器3 *****************/
  203.         else
  204.         {
  205.                 S3CON |= (1<<6);        //BRT select Timer3
  206.                 T4T3M &= 0xf0;                //停止计数, 清除控制位
  207.                 IE2  &= ~(1<<5);        //禁止中断
  208.                 T4T3M |=  (1<<1);        //1T
  209.                 T4T3M &= ~(1<<2);        //定时
  210.                 T4T3M &= ~1;                //不输出时钟
  211.                 TH3 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate3) / 256;
  212.                 TL3 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate3) % 256;
  213.                 T4T3M |=  (1<<3);        //开始运行
  214.         }
  215.         S3CON &= ~(1<<5);        //禁止多机通讯方式
  216.         S3CON &= ~(1<<7);        // 8位数据, 1位起始位, 1位停止位, 无校验
  217.         IE2   |=  (1<<3);        //允许中断
  218.         S3CON |=  (1<<4);        //允许接收
  219.         P_SW2 &= ~2;                //切换到 P0.0 P0.1
  220. //        P_SW2 |= 2;                        //切换到 P5.0 P5.1
  221.         for(i=0; i<RX3_Length; i++)                RX3_Buffer[ i] = 0;
  222.         B_TX3_Busy  = 0;
  223.         TX3_read    = 0;
  224.         RX3_write   = 0;
  225. }

  226. //========================================================================
  227. // 函数: void        UART4_config(u8 brt)
  228. // 描述: UART4初始化函数。
  229. // 参数: brt: 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
  230. // 返回: none.
  231. // 版本: VER1.0
  232. // 日期: 2014-11-28
  233. // 备注:
  234. //========================================================================
  235. void        UART4_config(u8 brt)        // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer4做波特率.
  236. {
  237.         u8        i;
  238.         /*********** 波特率固定使用定时器2 *****************/
  239.         if(brt == 2)
  240.         {
  241.                 S4CON &= ~(1<<6);        //BRT select Timer2
  242.                 SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / UART_BaudRate4);
  243.         }
  244.         /*********** 波特率使用定时器3 *****************/
  245.         else
  246.         {
  247.                 S4CON |= (1<<6);        //BRT select Timer4
  248.                 T4T3M &= 0x0f;                //停止计数, 清除控制位
  249.                 IE2   &= ~(1<<6);        //禁止中断
  250.                 T4T3M |=  (1<<5);        //1T
  251.                 T4T3M &= ~(1<<6);        //定时
  252.                 T4T3M &= ~(1<<4);        //不输出时钟
  253.                 TH4 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate4) / 256;
  254.                 TL4 = (65536UL - (MAIN_Fosc / 4) / UART_BaudRate4) % 256;
  255.                 T4T3M |=  (1<<7);        //开始运行
  256.         }

  257.         S4CON &= ~(1<<5);        //禁止多机通讯方式
  258.         S4CON &= ~(1<<7);        // 8位数据, 1位起始位, 1位停止位, 无校验
  259.         IE2   |=  (1<<4);        //允许中断
  260.         S4CON |=  (1<<4);        //允许接收
  261.         P_SW2 &= ~4;                //切换到 P0.2 P0.3
  262. //        P_SW2 |= 4;                        //切换到 P5.2 P5.3

  263.         for(i=0; i<RX4_Length; i++)                RX4_Buffer[ i] = 0;
  264.         B_TX4_Busy  = 0;
  265.         TX4_read    = 0;
  266.         RX4_write   = 0;
  267. }


  268. void PrintString1(u8 *puts)
  269. {
  270.     for (; *puts != 0;        puts++)
  271.         {
  272.                 B_TX1_Busy = 1;                //标志发送忙
  273.                 SBUF = *puts;                //发一个字节
  274.                 while(B_TX1_Busy);        //等待发送完成
  275.         }
  276. }

  277. void PrintString2(u8 *puts)
  278. {
  279.     for (; *puts != 0;        puts++)
  280.         {
  281.                 B_TX2_Busy = 1;                //标志发送忙
  282.                 S2BUF = *puts;                //发一个字节
  283.                 while(B_TX2_Busy);        //等待发送完成
  284.         }
  285. }

  286. void PrintString3(u8 *puts)
  287. {
  288.     for (; *puts != 0;        puts++)
  289.         {
  290.                 B_TX3_Busy = 1;                //标志发送忙
  291.                 S3BUF = *puts;                //发一个字节
  292.                 while(B_TX3_Busy);        //等待发送完成
  293.         }
  294. }

  295. void PrintString4(u8 *puts)
  296. {
  297.     for (; *puts != 0;        puts++)
  298.         {
  299.                 B_TX4_Busy = 1;                //标志发送忙
  300.                 S4BUF = *puts;                //发一个字节
  301.                 while(B_TX4_Busy);        //等待发送完成
  302.         }
  303. }



  304. /********************* UART1中断函数************************/
  305. void UART1_int (void) interrupt UART1_VECTOR
  306. {
  307.         if(RI)
  308.         {
  309.                 RI = 0;
  310.                 RX1_Buffer[RX1_write] = SBUF;
  311.                 if(++RX1_write >= RX1_Length)        RX1_write = 0;
  312.         }

  313.         if(TI)
  314.         {
  315.                 TI = 0;
  316.                 B_TX1_Busy = 0;
  317.         }
  318. }

  319. /********************* UART2中断函数************************/
  320. void UART2_int (void) interrupt UART2_VECTOR
  321. {
  322.         if(RI2)
  323.         {
  324.                 CLR_RI2();
  325.                 RX2_Buffer[RX2_write] = S2BUF;
  326.                 if(++RX2_write >= RX2_Length)        RX2_write = 0;
  327.         }

  328.         if(TI2)
  329.         {
  330.                 CLR_TI2();
  331.                 B_TX2_Busy = 0;
  332.         }

  333. }

  334. /********************* UART3中断函数************************/
  335. void UART3_int (void) interrupt UART3_VECTOR
  336. {
  337.         if(RI3)
  338.         {
  339.                 CLR_RI3();
  340.                 RX3_Buffer[RX3_write] = S3BUF;
  341.                 if(++RX3_write >= RX3_Length)        RX3_write = 0;
  342.         }

  343.         if(TI3)
  344.         {
  345.                 CLR_TI3();
  346.                 B_TX3_Busy = 0;
  347.         }

  348. }

  349. /********************* UART4中断函数************************/
  350. void UART4_int (void) interrupt UART4_VECTOR
  351. {
  352.         if(RI4)
  353.         {
  354.                 CLR_RI4();
  355.                 RX4_Buffer[RX4_write] = S4BUF;
  356.                 if(++RX4_write >= RX4_Length)        RX4_write = 0;
  357.         }

  358.         if(TI4)
  359.         {
  360.                 CLR_TI4();
  361.                 B_TX4_Busy = 0;
  362.         }

  363. }
复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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