找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2447|回复: 0
收起左侧

51单片机串口库文件com.h头文件,使用方便,添加即用

[复制链接]
ID:254411 发表于 2021-10-2 12:19 | 显示全部楼层 |阅读模式
c51串口库文件,使用方便,添加即用。
单片机源程序如下:
  1. #include "reg51.h"
  2. #include "intrins.h"

  3. typedef unsigned char BYTE;
  4. typedef unsigned int WORD;

  5. #define FOSC 11059200L      //System frequency
  6. #define BAUD 9600           //UART baudrate

  7. /*Define UART parity mode*/
  8. #define NONE_PARITY     0   //None parity
  9. #define ODD_PARITY      1   //Odd parity
  10. #define EVEN_PARITY     2   //Even parity
  11. #define MARK_PARITY     3   //Mark parity
  12. #define SPACE_PARITY    4   //Space parity

  13. #define PARITYBIT NONE_PARITY //EVEN_PARITY   //Testing even parity

  14. //yy sbit bit9 = P2^2;           //P2.2 show UART data bit9
  15. bit busy;

  16. void SendData(BYTE dat);
  17. void SendString(char *s);

  18. void InitCom()
  19. {
  20. #if (PARITYBIT ==NONE_PARITY )
  21.     SCON = 0x50;            //8-bit variable UART
  22. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  23.     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
  24. #elif (PARITYBIT == SPACE_PARITY)
  25.     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
  26. #endif

  27.     TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
  28.     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
  29.     TR1 = 1;                //Timer1 start run
  30.     ES = 1;                 //Enable UART interrupt
  31.     EA = 1;                 //Open master interrupt switch

  32.     SendString("yySTC12C5A60S2\r\nUart Test !\r\n");
  33.            SendData(0x31);
  34.     while(1);
  35. }

  36. /*----------------------------
  37. UART interrupt service routine
  38. ----------------------------*/
  39. void Uart_Isr() interrupt 4 using 1
  40. {
  41.     if (RI)
  42.     {
  43.         RI = 0;             //Clear receive interrupt flag
  44.         P0 = SBUF;          //P0 show UART data
  45. //yy        bit9 = RB8;         //P2.2 show parity bit
  46.     }
  47.     if (TI)
  48.     {
  49.         TI = 0;             //Clear transmit interrupt flag
  50.         busy = 0;           //Clear transmit busy flag
  51.     }
  52. }

  53. /*----------------------------
  54. Send a byte data to UART
  55. Input: dat (data to be sent)
  56. Output:None
  57. ----------------------------*/
  58. void SendData(BYTE dat)
  59. {
  60.     while (busy);           //Wait for the completion of the previous data is sent
  61.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  62.     if (P)                  //Set the parity bit according to P
  63.     {
  64. #if (PARITYBIT == ODD_PARITY)
  65.         TB8 = 0;            //Set parity bit to 0
  66. #elif (PARITYBIT == EVEN_PARITY)
  67.         TB8 = 1;            //Set parity bit to 1
  68. #endif
  69.     }
  70.     else
  71.     {
  72. #if (PARITYBIT == ODD_PARITY)
  73.         TB8 = 1;            //Set parity bit to 1
  74. #elif (PARITYBIT == EVEN_PARITY)
  75.         TB8 = 0;            //Set parity bit to 0
  76. #endif
  77.     }
  78.     busy = 1;
  79.     SBUF = ACC;             //Send data to UART buffer
  80. }

  81. /*----------------------------
  82. Send a string to UART
  83. Input: s (address of string)
  84. Output:None
  85. ----------------------------*/
  86. void SendString(char *s)
  87. {
  88.     while (*s)              //Check the end of the string
  89.     {
  90.         SendData(*s++);     //Send current char and increment string ptr
  91.     }
  92. }
复制代码

51hei.png

以上文件下载,.h格式文档下载(内容和本网页上的一模一样,方便保存)::
Com.rar (1.34 KB, 下载次数: 16)

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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