找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机定时器与串口同时使用模板程序

[复制链接]
跳转到指定楼层
楼主
ID:273434 发表于 2020-3-23 15:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
刚刚搞好的串口和定时器同时使用的程序模板,这个程序可以用来使用的。分享一下,参照一下,加油,中国

单片机源程序如下:
  1. #include "reg51.h"
  2. sfr T2CON  = 0xC8;          //timer2 control register
  3. sfr RCAP2L = 0xCA;
  4. sfr RCAP2H = 0xCB;
  5. sfr TL2    = 0xCC;
  6. sfr TH2    = 0xCD;

  7. typedef unsigned char BYTE;
  8. typedef unsigned int WORD;

  9. #define FOSC 11059200L      //System frequency
  10. #define BAUD 115200       //UART baudrate

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

  17. #define PARITYBIT EVEN_PARITY   //Testing even parity

  18. sbit bit9 = P2^2;           //P2.2 show UART data bit9
  19. bit busy;

  20. //-----------------------------------------------

  21. /* define constants */

  22. #define T1MS (65536-FOSC/12/1000)   //1ms timer calculation method in 12T mode

  23. /* define SFR */
  24. sbit TEST_LED = P1^0;               //work LED, flash once per second

  25. /* define variables */
  26. WORD count;                         //1000 times counter
  27. void SendData(BYTE dat);
  28. void SendString(char *s);
  29. //-----------------------------------------------

  30. /* Timer0 interrupt routine */
  31. void tm0_isr() interrupt 1 using 1
  32. {
  33.     TL0 = T1MS;                     //reload timer0 low byte
  34.     TH0 = T1MS >> 8;                //reload timer0 high byte
  35.     if (count-- == 0)               //1ms * 1000 -> 1s
  36.     {
  37.         count = 10000;               //reset counter
  38.         TEST_LED = ! TEST_LED;      //work LED flash
  39.     }
  40. }

  41. //-----------------------------------------------

  42. /* main program */
  43. void main()
  44. {
  45.     TMOD = 0x01;                    //set timer0 as mode1 (16-bit)
  46.     TL0 = T1MS;                     //initial timer0 low byte
  47.     TH0 = T1MS >> 8;                //initial timer0 high byte
  48.     TR0 = 1;                        //timer0 start running
  49.     ET0 = 1;                        //enable timer0 interrupt
  50.     EA = 1;                         //open global interrupt switch
  51.     count = 0;                      //initial counter
  52.        
  53.           #if (PARITYBIT == NONE_PARITY)
  54.     SCON = 0x50;            //8-bit variable UART
  55. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  56.     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
  57. #elif (PARITYBIT == SPACE_PARITY)
  58.     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
  59. #endif

  60.     TL2 = RCAP2L = (65536-(FOSC/32/BAUD)); //Set auto-reload vaule
  61.     TH2 = RCAP2H = (65536-(FOSC/32/BAUD)) >> 8;
  62.     T2CON = 0x34;           //Timer2 start run
  63.     ES = 1;                 //Enable UART interrupt
  64.     EA = 1;                 //Open master interrupt switch

  65.     SendString("STC89-90xx\r\nUart Test !\r\n");

  66.     while (1)
  67.                 {
  68.                         if(count==9999)
  69.                         {
  70.                                 SendString("STC89-90xx\r\nUart Test !\r\n");
  71.                         }
  72.                        
  73.                 }
  74. }
  75. /*----------------------------
  76. UART interrupt service routine
  77. ----------------------------*/
  78. void Uart_Isr() interrupt 4 using 1
  79. {
  80.     if (RI)
  81.     {
  82.         RI = 0;             //Clear receive interrupt flag
  83.         P0 = SBUF;          //P0 show UART data
  84.         bit9 = RB8;         //P2.2 show parity bit
  85.     }
  86.     if (TI)
  87.     {
  88.         TI = 0;             //Clear transmit interrupt flag
  89.         busy = 0;           //Clear transmit busy flag
  90.     }
  91. }

  92. /*----------------------------
  93. Send a byte data to UART
  94. Input: dat (data to be sent)
  95. Output:None
  96. ----------------------------*/
  97. void SendData(BYTE dat)
  98. {
  99.     while (busy);           //Wait for the completion of the previous data is sent
  100.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  101.     if (P)                  //Set the parity bit according to P
  102.     {
  103. #if (PARITYBIT == ODD_PARITY)
  104.         TB8 = 0;            //Set parity bit to 0
  105. #elif (PARITYBIT == EVEN_PARITY)
  106.         TB8 = 1;            //Set parity bit to 1
  107. #endif
  108.     }
  109.     else
  110.     {
  111. #if (PARITYBIT == ODD_PARITY)
  112.         TB8 = 1;            //Set parity bit to 1
  113. #elif (PARITYBIT == EVEN_PARITY)
  114.         TB8 = 0;            //Set parity bit to 0
  115. #endif
  116.     }
  117.     busy = 1;
  118.     SBUF = ACC;             //Send data to UART buffer
  119. }

  120. /*----------------------------
  121. Send a string to UART
  122. Input: s (address of string)
  123. Output:None
  124. ----------------------------*/
  125. void SendString(char *s)
  126. {
  127.     while (*s)              //Check the end of the string
  128.     {
  129.         SendData(*s++);     //Send current char and increment string ptr
  130.     }
  131. }
复制代码

所有资料51hei提供下载:
定时器串口test.zip (23.22 KB, 下载次数: 25)


评分

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

查看全部评分

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

使用道具 举报

沙发
ID:81918 发表于 2020-3-31 18:30 | 只看该作者
真是太好了,正好用的上,明天试下,学习楼主
回复

使用道具 举报

板凳
ID:398219 发表于 2021-9-17 19:32 | 只看该作者
最近一直在研究串口和定时器同时用,谢谢分享
回复

使用道具 举报

地板
ID:398219 发表于 2021-9-19 13:36 | 只看该作者
还是找到原因了,当串时不用开中断,关闭就好了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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