久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3436|回復: 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 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美一区二区三区在线观看 | 亚洲 欧美 另类 综合 偷拍 | www.成人久久 | 欧美二三区 | 日韩精品| 欧美一区二区三区日韩 | av一区在线 | 亚洲人成人一区二区在线观看 | 日韩在线国产精品 | 成人视屏在线观看 | 久久精品久久精品久久精品 | 欧美日韩成人网 | 日本免费一区二区三区 | 日本三级做a全过程在线观看 | 日韩视频二区 | 久久一区二区三区四区 | 毛片视频免费 | 在线观看中文字幕av | 欧美一区二区三区视频 | 亚欧午夜| 国产成人精品一区二区三区在线 | 国产精品久久久久免费 | 国产高清视频一区 | 久热国产精品视频 | 国产午夜视频 | 成人在线精品视频 | 国产在线精品一区二区三区 | 国产亚洲一区二区三区 | 在线观看成人免费视频 | 成人av一区二区在线观看 | 欧美高清性xxxxhd | 一级黄片一级毛片 | 国产91久久精品一区二区 | 国产一区二区三区四区在线观看 | 亚洲大片 | 欧美精品在线播放 | 久久久免费精品 | 国产精品日韩欧美一区二区 | 成人精品一区亚洲午夜久久久 | 99精品视频免费观看 | 精品视频网 |