找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STC89C52 UART程序

[复制链接]
跳转到指定楼层
楼主
ID:162993 发表于 2017-1-26 22:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STC89C52UART程序:
1.UartMCUtoPC.zip (24.96 KB, 下载次数: 7)

  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU Limited ---------------------------------------------*/
  3. /* --- STC89-90xx Series MCU UART (8-bit/9-bit)Demo ----------------*/
  4. /* --- Mobile: (86)13922805190 -------------------------------------*/
  5. /* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
  6. /* --- Tel: 86-0513-55012928,55012929,55012966----------------------*/
  7. /* If you want to use the program or the program referenced in the  */
  8. /* article, please specify in which data and procedures from STC    */
  9. /*------------------------------------------------------------------*/

  10. #include "reg51.h"
  11. #include "intrins.h"

  12. typedef unsigned char BYTE;
  13. typedef unsigned int WORD;

  14. #define FOSC 11059200L      //System frequency
  15. #define BAUD 9600           //UART baudrate

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

  22. //在此更改模式!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  23. #define PARITYBIT NONE_PARITY   //Testing even parity

  24. sbit bit9 = P2^2;           //P2.2 show UART data bit9
  25. bit busy;

  26. void SendData(BYTE dat);
  27. void SendString(char *s);

  28. void main()
  29. {
  30. #if (PARITYBIT == NONE_PARITY)
  31.     SCON = 0x50;            //8-bit variable UART
  32. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  33.     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
  34. #elif (PARITYBIT == SPACE_PARITY)
  35.     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
  36. #endif

  37.     TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
  38.     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
  39.     TR1 = 1;                //Timer1 start run
  40.     ES = 1;                 //Enable UART interrupt
  41.     EA = 1;                 //Open master interrupt switch

  42.     SendString("Hello 51\n");
  43.     while(1);
  44. }

  45. /*----------------------------
  46. UART interrupt service routine
  47. ----------------------------*/
  48. void Uart_Isr() interrupt 4 using 1
  49. {
  50.     if (RI)
  51.     {
  52.         RI = 0;             //Clear receive interrupt flag
  53.         P0 = SBUF;          //P0 show UART data
  54.         bit9 = RB8;         //P2.2 show parity bit
  55.     }
  56.     if (TI)
  57.     {
  58.         TI = 0;             //Clear transmit interrupt flag
  59.         busy = 0;           //Clear transmit busy flag
  60.     }
  61. }

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

  90. /*----------------------------
  91. Send a string to UART
  92. Input: s (address of string)
  93. Output:None
  94. ----------------------------*/
  95. void SendString(char *s)
  96. {
  97.     while (*s)              //Check the end of the string
  98.     {
  99.         SendData(*s++);     //Send current char and increment string ptr
  100.     }
  101. }

复制代码


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

使用道具 举报

沙发
ID:1 发表于 2017-2-12 23:42 | 只看该作者
好资料,51黑有你更精彩!!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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