查看: 1100|回复: 4

天问STC8单片机图形化编程如何设置奇偶校验那?

[复制链接]

1

主题

3

帖子

83

积分

注册会员

Rank: 2

积分
83
发表于 2022-1-1 11:33:58 | 显示全部楼层 |阅读模式
关于STC8单片机串口通信的问题,再图形化编程里可以设置波特率,但是奇、偶检验的选项没有?
回复

使用道具 举报

8

主题

46

帖子

901

积分

版主

Rank: 7Rank: 7Rank: 7

积分
901
发表于 2022-1-2 14:25:54 | 显示全部楼层

回帖奖励 +10 金钱

得先从STC-ISP软件上的STC代码范例库中拷贝到串口通讯的奇偶校验设置与解析的源码,其实就是几句操作寄存器的代码,根据这个源码在天问Block里放置代码宽,插入相关代码即可。当然,会写扩展库的大佬,可以写成扩展库。
以上仅为思路。以下是拷贝于STC-ISP中的一个范例
  1. /*------------------------------------------------------------------*/
  2. /* --- STC MCU Limited ---------------------------------------------*/
  3. /* --- STC12C5Axx 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. /* --- Web: www.STCMCU.com -----------------------------------------*/
  8. /* --- Web: www.GXWMCU.com -----------------------------------------*/
  9. /* If you want to use the program or the program referenced in the  */
  10. /* article, please specify in which data and procedures from STC    */
  11. /*------------------------------------------------------------------*/

  12. #include "reg51.h"
  13. #include "intrins.h"

  14. typedef unsigned char BYTE;
  15. typedef unsigned int WORD;

  16. #define FOSC 11059200L      //System frequency
  17. #define BAUD 9600           //UART baudrate

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

  24. #define PARITYBIT NONE_PARITY   //Testing even parity

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

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

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

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

  43.     SendString("STC12C5A60S2\r\nUart Test !\r\n");
  44.     while(1);
  45. }

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

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

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

使用道具 举报

0

主题

2

帖子

126

积分

注册会员

Rank: 2

积分
126
发表于 2022-1-5 22:30:08 | 显示全部楼层
libai500 发表于 2022-1-2 14:25
得先从STC-ISP软件上的STC代码范例库中拷贝到串口通讯的奇偶校验设置与解析的源码,其实就是几句操作寄存器 ...

谢谢分享
回复

使用道具 举报

1

主题

3

帖子

83

积分

注册会员

Rank: 2

积分
83
 楼主| 发表于 2022-1-25 17:21:31 | 显示全部楼层
偶校验扩展库搞了几天没搞定,哪位高手帮忙写一个偶校验的扩展库学习一下那
回复

使用道具 举报

2

主题

4

帖子

138

积分

注册会员

Rank: 2

积分
138
发表于 2023-12-19 00:45:00 | 显示全部楼层
你的校验搞定了吗
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|小黑屋|好好搭搭在线 ( © 好好搭搭在线 浙ICP备19030393号-1 )

GMT+8, 2024-3-28 21:38 , Processed in 0.331786 second(s), 22 queries .

Powered by Discuz!

© 2001-2024 Comsenz Inc.

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