亚洲春色中文字幕久久久-三上亚,一吻二脱三床四吻胸,国产真实伦对白视频全集,在线毛片观看,精品成品入口黄网,国产毛aⅴ片久久久,亚洲AV色香蕉一区二区三区老师,萧皇后A级艳片,色情日本视频更新,99久久亚洲精品日本无码

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3766|回復: 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 單片機教程網

快速回復 返回頂部 返回列表